From 46574341bb683957828dcfa939090885758d8c45 Mon Sep 17 00:00:00 2001 From: nabarun Date: Tue, 18 Apr 2023 18:01:36 +0530 Subject: [PATCH] Use published react-peer/test-app from gitea --- .../compose/docker-compose-mobymask-app.yml | 2 +- .../compose/docker-compose-peer-test-app.yml | 9 ++-- .../watcher-mobymask-v2/test-app-start.sh | 10 ++--- .../apply-webapp-config.sh | 7 ++-- .../start-serving-app.sh | 2 +- .../cerc-react-peer/Dockerfile | 30 +++++++++---- .../cerc-react-peer/apply-webapp-config.sh | 42 +++++++++++++++++++ .../container-build/cerc-react-peer/build.sh | 6 ++- .../cerc-react-peer/start-serving-app.sh | 9 ++++ app/data/stacks/mobymask-v2/README.md | 4 +- app/data/stacks/mobymask-v2/web-apps.md | 4 +- 11 files changed, 95 insertions(+), 30 deletions(-) create mode 100755 app/data/container-build/cerc-react-peer/apply-webapp-config.sh create mode 100755 app/data/container-build/cerc-react-peer/start-serving-app.sh diff --git a/app/data/compose/docker-compose-mobymask-app.yml b/app/data/compose/docker-compose-mobymask-app.yml index ad97056a..6df48130 100644 --- a/app/data/compose/docker-compose-mobymask-app.yml +++ b/app/data/compose/docker-compose-mobymask-app.yml @@ -28,7 +28,7 @@ services: ports: - "0.0.0.0:3002:80" healthcheck: - test: ["CMD", "nc", "-v", "localhost", "3000"] + test: ["CMD", "nc", "-v", "localhost", "80"] interval: 20s timeout: 5s retries: 15 diff --git a/app/data/compose/docker-compose-peer-test-app.yml b/app/data/compose/docker-compose-peer-test-app.yml index 7d1b89d6..0a8d1c65 100644 --- a/app/data/compose/docker-compose-peer-test-app.yml +++ b/app/data/compose/docker-compose-peer-test-app.yml @@ -4,7 +4,7 @@ services: peer-test-app: # Builds and serves the peer-test react-app image: cerc/react-peer:local - working_dir: /app/packages/test-app + working_dir: /scripts env_file: - ../config/watcher-mobymask-v2/mobymask-params.env environment: @@ -12,13 +12,12 @@ services: CERC_RELAY_NODES: ${CERC_RELAY_NODES} command: ["sh", "./test-app-start.sh"] volumes: - - ../config/watcher-mobymask-v2/test-app-config.json:/app/packages/test-app/src/test-app-config.json - - ../config/watcher-mobymask-v2/test-app-start.sh:/app/packages/test-app/test-app-start.sh + - ../config/watcher-mobymask-v2/test-app-start.sh:/scripts/test-app-start.sh - peers_ids:/peers ports: - - "0.0.0.0:3003:3000" + - "0.0.0.0:3003:80" healthcheck: - test: ["CMD", "nc", "-v", "localhost", "3000"] + test: ["CMD", "nc", "-v", "localhost", "80"] interval: 20s timeout: 5s retries: 15 diff --git a/app/data/config/watcher-mobymask-v2/test-app-start.sh b/app/data/config/watcher-mobymask-v2/test-app-start.sh index ef7aa539..42e4397d 100755 --- a/app/data/config/watcher-mobymask-v2/test-app-start.sh +++ b/app/data/config/watcher-mobymask-v2/test-app-start.sh @@ -14,11 +14,7 @@ fi echo "Using CERC_RELAY_NODES $CERC_RELAY_NODES" -# Set relay nodes in config using CERC_RELAY_NODES -jq --argjson relayNodes "$CERC_RELAY_NODES" \ - '.relayNodes = $relayNodes' \ - ./src/test-app-config.json > ./src/config.json +# Use yq to create config.yml with environment variables +yq -n ".relayNodes = strenv(CERC_RELAY_NODES)" > /config/config.yml -yarn build - -serve -s build +/scripts/start-serving-app.sh diff --git a/app/data/container-build/cerc-laconic-console-host/apply-webapp-config.sh b/app/data/container-build/cerc-laconic-console-host/apply-webapp-config.sh index 7684eac3..bf041708 100755 --- a/app/data/container-build/cerc-laconic-console-host/apply-webapp-config.sh +++ b/app/data/container-build/cerc-laconic-console-host/apply-webapp-config.sh @@ -2,13 +2,12 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then set -x fi -if [[ $# -ne 3 ]]; then +if [[ $# -ne 2 ]]; then echo "Illegal number of parameters" >&2 exit 1 fi config_file_name=$1 webapp_files_dir=$2 -config_prefix=$3 if ! [[ -f ${config_file_name} ]]; then echo "Config file ${config_file_name} does not exist" >&2 exit 1 @@ -18,8 +17,8 @@ if ! [[ -d ${webapp_files_dir} ]]; then exit 1 fi # First some magic using yq to translate our yaml config file into an array of key value pairs like: -# ${config_prefix}= -readarray -t config_kv_pair_array < <( yq '.. | select(length > 2) | ([path | join("_"), .] | join("=") )' ${config_file_name} | sed "s/^/${config_prefix}_/" ) +# LACONIC_HOSTED_CONFIG_= +readarray -t config_kv_pair_array < <( yq '.. | select(length > 2) | ([path | join("_"), .] | join("=") )' ${config_file_name} | sed 's/^/LACONIC_HOSTED_CONFIG_/' ) declare -p config_kv_pair_array # Then iterate over that kv array making the template substitution in our web app files for kv_pair_string in "${config_kv_pair_array[@]}" diff --git a/app/data/container-build/cerc-laconic-console-host/start-serving-app.sh b/app/data/container-build/cerc-laconic-console-host/start-serving-app.sh index 6897c88a..a322e5fb 100755 --- a/app/data/container-build/cerc-laconic-console-host/start-serving-app.sh +++ b/app/data/container-build/cerc-laconic-console-host/start-serving-app.sh @@ -4,5 +4,5 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then fi # TODO: Don't hard wire this: webapp_files_dir=/usr/local/share/.config/yarn/global/node_modules/@cerc-io/console-app/dist/production -/scripts/apply-webapp-config.sh /config/config.yml ${webapp_files_dir} LACONIC_HOSTED_CONFIG +/scripts/apply-webapp-config.sh /config/config.yml ${webapp_files_dir} http-server -p 80 ${webapp_files_dir} diff --git a/app/data/container-build/cerc-react-peer/Dockerfile b/app/data/container-build/cerc-react-peer/Dockerfile index fcc198f3..f60ba504 100644 --- a/app/data/container-build/cerc-react-peer/Dockerfile +++ b/app/data/container-build/cerc-react-peer/Dockerfile @@ -1,14 +1,30 @@ FROM node:18.15.0-alpine3.16 -RUN apk --update --no-cache add make git python3 jq +# This container pulls npm package from a registry configured via env var +ARG CERC_NPM_REGISTRY_URL -WORKDIR /app +RUN apk --update --no-cache add make git python3 jq bash -COPY . . +# We do this to get a yq binary from the published container, for the correct architecture we're building here +COPY --from=docker.io/mikefarah/yq:latest /usr/bin/yq /usr/local/bin/yq -RUN yarn global add serve +RUN mkdir -p /scripts +COPY ./apply-webapp-config.sh /scripts +COPY ./start-serving-app.sh /scripts -RUN echo "Building react-peer" && \ - yarn install --ignore-scripts && yarn build --ignore @cerc-io/test-app +# Configure the local npm registry +RUN npm config set @cerc-io:registry ${CERC_NPM_REGISTRY_URL} -WORKDIR /app +RUN mkdir -p /config + +# Install simple web server for now (use nginx perhaps later) +RUN yarn global add http-server + +# Globally install the payload web app package +RUN yarn global add @cerc-io/test-app + + +# Expose port for http +EXPOSE 80 + +CMD ["/scripts/start-serving-app.sh"] diff --git a/app/data/container-build/cerc-react-peer/apply-webapp-config.sh b/app/data/container-build/cerc-react-peer/apply-webapp-config.sh new file mode 100755 index 00000000..a7f0a28e --- /dev/null +++ b/app/data/container-build/cerc-react-peer/apply-webapp-config.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi +if [[ $# -ne 3 ]]; then + echo "Illegal number of parameters" >&2 + exit 1 +fi +config_file_name=$1 +webapp_files_dir=$2 +config_prefix=$3 +if ! [[ -f ${config_file_name} ]]; then + echo "Config file ${config_file_name} does not exist" >&2 + exit 1 +fi +if ! [[ -d ${webapp_files_dir} ]]; then + echo "Webapp directory ${webapp_files_dir} does not exist" >&2 + exit 1 +fi +# First some magic using sed to translate our yaml config file into an array of key value pairs like: +# ${config_prefix}= +# sed "s/'//g" is used to remove single quote for relayNodes value +readarray -t config_kv_pair_array < <( sed -E 's/([^:]+):\s*(.*)/\1=\2/g' ${config_file_name} | sed "s/'//g" | sed "s/^/${config_prefix}_/" ) +declare -p config_kv_pair_array +# Then iterate over that kv array making the template substitution in our web app files +for kv_pair_string in "${config_kv_pair_array[@]}" +do + kv_pair=(${kv_pair_string//=/ }) + template_string_to_replace=${kv_pair[0]} + template_value_to_substitute=${kv_pair[1]} + # Run find and sed to do the substitution of one variable over all files + # See: https://stackoverflow.com/a/21479607/1701505 + echo "Substituting: ${template_string_to_replace} = ${template_value_to_substitute}" + + # TODO: Pass keys to be replaced without double quotes + if [[ "$template_string_to_replace" == "${config_prefix}_relayNodes" ]]; then + find ${webapp_files_dir} -type f -exec sed -i 's#"'"${template_string_to_replace}"'"#'"${template_value_to_substitute}"'#g' {} + + else + # Note: we do not escape our strings, on the expectation they do not container the '#' char. + find ${webapp_files_dir} -type f -exec sed -i 's#'${template_string_to_replace}'#'${template_value_to_substitute}'#g' {} + + fi +done diff --git a/app/data/container-build/cerc-react-peer/build.sh b/app/data/container-build/cerc-react-peer/build.sh index 48edc4b3..34be5c7c 100755 --- a/app/data/container-build/cerc-react-peer/build.sh +++ b/app/data/container-build/cerc-react-peer/build.sh @@ -6,4 +6,8 @@ source ${CERC_CONTAINER_BASE_DIR}/build-base.sh # See: https://stackoverflow.com/a/246128/1701505 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) -docker build -t cerc/react-peer:local -f ${SCRIPT_DIR}/Dockerfile ${build_command_args} ${CERC_REPO_BASE_DIR}/react-peer +# TODO: Publish package to local SO gitea similar to laconic-console +CERC_NPM_REGISTRY_URL="https://git.vdb.to/api/packages/cerc-io/npm/" + +docker build -t cerc/react-peer:local ${build_command_args} -f ${SCRIPT_DIR}/Dockerfile \ + --build-arg CERC_NPM_REGISTRY_URL ${SCRIPT_DIR} diff --git a/app/data/container-build/cerc-react-peer/start-serving-app.sh b/app/data/container-build/cerc-react-peer/start-serving-app.sh new file mode 100755 index 00000000..e01b91c3 --- /dev/null +++ b/app/data/container-build/cerc-react-peer/start-serving-app.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi + +# TODO: Don't hard wire this: +webapp_files_dir=/usr/local/share/.config/yarn/global/node_modules/@cerc-io/test-app/build +/scripts/apply-webapp-config.sh /config/config.yml ${webapp_files_dir} MOBYMASK_HOSTED_CONFIG +http-server -p 80 ${webapp_files_dir} diff --git a/app/data/stacks/mobymask-v2/README.md b/app/data/stacks/mobymask-v2/README.md index 9dcca6c0..aaa4ac7f 100644 --- a/app/data/stacks/mobymask-v2/README.md +++ b/app/data/stacks/mobymask-v2/README.md @@ -27,11 +27,11 @@ git checkout v0.2.35 # react-peer cd ~/cerc/react-peer -git checkout v0.2.31 +git checkout v0.2.32 # mobymask-ui cd ~/cerc/mobymask-ui -git checkout laconic +git checkout v0.1.2 # MobyMask cd ~/cerc/MobyMask diff --git a/app/data/stacks/mobymask-v2/web-apps.md b/app/data/stacks/mobymask-v2/web-apps.md index 2a98370a..866f22f0 100644 --- a/app/data/stacks/mobymask-v2/web-apps.md +++ b/app/data/stacks/mobymask-v2/web-apps.md @@ -19,11 +19,11 @@ Checkout to the required versions and branches in repos: ```bash # react-peer cd ~/cerc/react-peer -git checkout v0.2.31 +git checkout v0.2.32 # mobymask-ui cd ~/cerc/mobymask-ui -git checkout v0.1.1 +git checkout v0.1.2 ``` Build the container images: