From 62a9f3c886614744f0f3bfd6d5c104a17f6d07fc Mon Sep 17 00:00:00 2001 From: jg Date: Thu, 26 Oct 2023 15:38:40 -0400 Subject: [PATCH] Make deployable on GCP Based on https://cmu-313.github.io/projects/P3/deployment/#create-config-script --- Dockerfile | 8 ++++++-- config_template.json | 12 ++++++++++++ create_config.sh | 40 ++++++++++++++++++++++++++++++++++++++++ install/package.json | 3 +++ webpack.common.js | 9 +++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 config_template.json create mode 100644 create_config.sh diff --git a/Dockerfile b/Dockerfile index 8a5b7ae..779a648 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,8 @@ RUN mkdir -p /usr/src/app && \ chown -R node:node /usr/src/app WORKDIR /usr/src/app +RUN apt-get update && apt-get install -y jq + ARG NODE_ENV ENV NODE_ENV $NODE_ENV @@ -11,7 +13,7 @@ COPY --chown=node:node install/package.json /usr/src/app/package.json USER node -RUN npm install --only=prod && \ +RUN npm install && \ npm cache clean --force COPY --chown=node:node . /usr/src/app @@ -22,4 +24,6 @@ ENV NODE_ENV=production \ EXPOSE 4567 -CMD test -n "${SETUP}" && ./nodebb setup || node ./nodebb build; node ./nodebb start +RUN chmod +x create_config.sh + +CMD ./create_config.sh -n "${SETUP}" && ./nodebb setup || node ./nodebb build; node ./nodebb start diff --git a/config_template.json b/config_template.json new file mode 100644 index 0000000..82b4876 --- /dev/null +++ b/config_template.json @@ -0,0 +1,12 @@ +{ + "url": "", + "secret": "c5502d62-84a5-41f1-87eb-ee33a76fb7bc", + "database": "redis", + "redis": { + "host": "", + "port": "", + "password": "", + "database": "0" + }, + "port": "4567" +} diff --git a/create_config.sh b/create_config.sh new file mode 100644 index 0000000..259acf6 --- /dev/null +++ b/create_config.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Check that environment variables have been defined +if [[ -z "${REDIS_HOST+x}" ]]; then + # var is not defined + echo "Error: REDIS_HOST is not defined!" + exit 1 +fi + +if [[ -z "${REDIS_PORT+x}" ]]; then + # var is not defined + echo "Error: REDIS_PORT is not defined!" + exit 1 +fi + +if [[ -z "${REDIS_PASSWORD+x}" ]]; then + # var is not defined + echo "Error: REDIS_PASSWORD is not defined!" + exit 1 +fi + +if [[ -z "${DEPLOYMENT_URL+x}" ]]; then + # var is not defined + echo "Error: DEPLOYMENT_URL is not defined!" + exit 1 +fi + +# Read the JSON file +json_data=$(cat "/usr/src/app/config_template.json") + +# Update the JSON file with the environment variables +json_data=$(jq --arg deployment_url "$DEPLOYMENT_URL" '.url = $deployment_url' <<< "$json_data") +json_data=$(jq --arg host "$REDIS_HOST" '.redis.host = $host' <<< "$json_data") +json_data=$(jq --arg port "$REDIS_PORT" '.redis.port = $port' <<< "$json_data") +json_data=$(jq --arg password "$REDIS_PASSWORD" '.redis.password = $password' <<< "$json_data") + +# Write the updated JSON file to config.json +echo "$json_data" > "/usr/src/app/config.json" + +cat /usr/src/app/config.json diff --git a/install/package.json b/install/package.json index 919c51a..8e1c497 100644 --- a/install/package.json +++ b/install/package.json @@ -159,6 +159,9 @@ "@typescript-eslint/parser": "^5.48.0", "coveralls": "3.1.1", "eslint": "^8.31.0", + "@babel/core": "7.23.2", + "babel-loader": "9.1.3", + "@babel/preset-env": "7.23.2", "eslint-config-nodebb": "0.2.1", "eslint-plugin-import": "2.26.0", "grunt": "1.5.3", diff --git a/webpack.common.js b/webpack.common.js index 00ffffb..b45c8b4 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -67,6 +67,15 @@ module.exports = { exclude: /node_modules/, loader: 'ignore-loader', }, + { + test: /\.js$/, + use: { + loader: 'babel-loader', + options: { + presets: ['@babel/preset-env'] + } + } + }, ], }, };