From d14c25baf8fd7d2a8475ef613b3601de7c435ef5 Mon Sep 17 00:00:00 2001 From: niemisami Date: Sun, 20 Dec 2020 19:17:43 +0200 Subject: [PATCH 1/5] Create bash scripts initialize new Ubuntu server for backend code --- digit_dev.config.js | 2 +- scripts/dev-entrypoint.sh | 20 ++++++++ .../applied_migrations.json | 0 scripts/server_setup_files/pm2.config.js | 21 ++++++++ scripts/server_setup_files/post-receive | 42 ++++++++++++++++ scripts/server_setup_files/setup-elmeri.sh | 48 +++++++++++++++++++ scripts/start-elmeri-setup.sh | 11 +++++ 7 files changed, 143 insertions(+), 1 deletion(-) create mode 100755 scripts/dev-entrypoint.sh create mode 100644 scripts/server_setup_files/applied_migrations.json create mode 100644 scripts/server_setup_files/pm2.config.js create mode 100755 scripts/server_setup_files/post-receive create mode 100755 scripts/server_setup_files/setup-elmeri.sh create mode 100755 scripts/start-elmeri-setup.sh diff --git a/digit_dev.config.js b/digit_dev.config.js index b79464c..2d9a387 100644 --- a/digit_dev.config.js +++ b/digit_dev.config.js @@ -21,4 +21,4 @@ module.exports = { node_args: '--inspect=0.0.0.0:9230' } ] -}; +} diff --git a/scripts/dev-entrypoint.sh b/scripts/dev-entrypoint.sh new file mode 100755 index 0000000..14b3cae --- /dev/null +++ b/scripts/dev-entrypoint.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -e + +HASH_TARGET=package.json +HASH_FILE=.hash-$HASH_TARGET +HASH="" + +if [[ -f $HASH_FILE ]]; then + HASH=$(head -n 1 $HASH_FILE) +fi + +if [[ $(md5sum package.json| cut -d ' ' -f 1) = $HASH ]]; then + echo "Skip npm install" +else + echo "Running npm install" + npm install --no-optional + sh docker/generate-checksum-file.sh package.json +fi + +node_modules/.bin/pm2-runtime keeper_dev.config.js \ No newline at end of file diff --git a/scripts/server_setup_files/applied_migrations.json b/scripts/server_setup_files/applied_migrations.json new file mode 100644 index 0000000..e69de29 diff --git a/scripts/server_setup_files/pm2.config.js b/scripts/server_setup_files/pm2.config.js new file mode 100644 index 0000000..69eb4e3 --- /dev/null +++ b/scripts/server_setup_files/pm2.config.js @@ -0,0 +1,21 @@ +module.exports = { + apps: [ + { + name: 'digit-api', + cwd: '/var/www/digit_api', + script: '/var/www/digit_api/bin/index.js', + exec_mode: 'fork', + env: { + NODE_ENV: 'production', + PORT: 3000, + PGDATABASE: 'digit', + PGUSER: 'digit', + PGHOST: 'ADD_ON_SERVER', + PGPASSWORD: 'ADD_ON_SERVER', + PGPORT: '5432', + SECRET_KEY: 'ADD_ON_SERVER', + FACEBOOK_ACCESS_TOKEN: 'ADD_ON_SERVER' + } + } + ] +} diff --git a/scripts/server_setup_files/post-receive b/scripts/server_setup_files/post-receive new file mode 100755 index 0000000..a0dd438 --- /dev/null +++ b/scripts/server_setup_files/post-receive @@ -0,0 +1,42 @@ +#!/bin/bash +GIT_DIR="/home/digit/digit_api.git" +MASTER_BRANCH="master" +# STAGING_BRANCH="staging" # Option for other branches + +while read oldrev newrev ref; do + # only checking out the master (or whatever branch you would like to deploy) + if [[ $ref = refs/heads/$MASTER_BRANCH ]]; then + BRANCH=$MASTER_BRANCH + TARGET="/var/www/digit_api" + APPNAME="digit-api" + # elif [[ $ref = refs/heads/$STAGING_BRANCH ]]; then + # BRANCH=$STAGING_BRANCH + # TARGET="/var/www/digit_api-staging" + # APPNAME="digit-api-staging" + else + echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server." + exit + fi +done + +echo "Deploying ${BRANCH} branch to server..." +git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH + +cd $TARGET + +CHECKSUM_FILE=./checksum +CACHED_FILE=./package-lock.json +CHECKSUM_NEW="$(cksum $CACHED_FILE)" + +if [[ ! -f "$CHECKSUM_FILE" || $(head $CHECKSUM_FILE) != $CHECKSUM_NEW ]]; then + echo "Install packages" + npm install + echo "Update cache" + echo $CHECKSUM_NEW >$CHECKSUM_FILE +else + echo "Retrieve cache" +fi + +pm2 startOrRestart ~/server_setup_files/pm2.config.js + +echo "Started $APPNAME" diff --git a/scripts/server_setup_files/setup-elmeri.sh b/scripts/server_setup_files/setup-elmeri.sh new file mode 100755 index 0000000..4bad2ae --- /dev/null +++ b/scripts/server_setup_files/setup-elmeri.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# Elmeri Ubuntu server setup + +echo "Start the initialization on the server ${PWD}" + +# Update the system: +sudo apt update +sudo apt install -y build-essential +sudo timedatectl set-timezone Europe/Helsinki + +# Install libraries +cd ~ +sudo apt install -y git +curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - +sudo apt install -y nodejs + +# User and permissions +sudo adduser digit --disabled-password +sudo usermod -aG www-data digit +sudo mkdir -p /var/www +sudo chown -R www-data:www-data /var/www +sudo chmod -R 775 /var/www/ + +# Authentication and ssh access +sudo mkdir -p /home/digit/.ssh +sudo chmod 700 /home/digit/.ssh +sudo cp ~/.ssh/authorized_keys /home/digit/.ssh/ +sudo chmod 600 /home/digit/.ssh/authorized_keys +sudo cp -r ~/server_setup_files /home/digit/ +sudo chown -R digit:digit /home/digit + +# Setup project +sudo npm install -g pm2 +sudo su - digit +mkdir -p /var/www/digit_api +ln -s /var/www/digit_api digit_api + +git init --bare ~/digit_api.git +cp ~/server_setup_files/post-receive ~/digit_api.git/hooks/post-receive + +# # CI +# git push --force ssh://digit@elmeri.digit.fi/~/digit_api.git ${BRANCH} + +# # Local development +# git remote add production digit@elmeri.digit.fi:~/digit_api.git +# git push production ${BRANCH} + +echo "Server setup complete" \ No newline at end of file diff --git a/scripts/start-elmeri-setup.sh b/scripts/start-elmeri-setup.sh new file mode 100755 index 0000000..5bb1e1b --- /dev/null +++ b/scripts/start-elmeri-setup.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +USERNAME=ubuntu +SERVER_URL=elmeri.digit.fi + +echo "Copy server_setup_files to the ${SERVER_URL}" +rsync -avz --delete server_setup_files/ ${USERNAME}@${SERVER_URL}:~/server_setup_files + +# Start the setup on the server +ssh ${USERNAME}@${SERVER_URL} 'bash -s' < ./server_setup_files/setup-elmeri.sh + From bc4141f0ce3555db20b81d4940db7729abce70fe Mon Sep 17 00:00:00 2001 From: niemisami Date: Sun, 20 Dec 2020 17:25:29 +0200 Subject: [PATCH 2/5] Add health check route /ping --- app/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/index.js b/app/index.js index 0e05b02..0896969 100644 --- a/app/index.js +++ b/app/index.js @@ -44,6 +44,7 @@ app.use(databaseMiddlewares) app.use(authMiddlewares) app.use('/api', routes) +app.use('/ping', (req, res) => res.send({ ok: 200 })) const staticUploadsPath = process.env.NODE_ENV === 'test' ? '/uploads_test' : '/uploads' // TODO: handle uplods using Nginx From eab7bf91c6d413275cbe00f91d1453697931bb99 Mon Sep 17 00:00:00 2001 From: niemisami Date: Sun, 20 Dec 2020 19:15:28 +0200 Subject: [PATCH 3/5] Add database RDS instance route --- scripts/server_setup_files/post-receive | 2 +- scripts/start-elmeri-setup.sh | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/server_setup_files/post-receive b/scripts/server_setup_files/post-receive index a0dd438..4c5e98c 100755 --- a/scripts/server_setup_files/post-receive +++ b/scripts/server_setup_files/post-receive @@ -37,6 +37,6 @@ else echo "Retrieve cache" fi -pm2 startOrRestart ~/server_setup_files/pm2.config.js +pm2 startOrRestart ~/server_setup_files/pm2.config.js --update-env echo "Started $APPNAME" diff --git a/scripts/start-elmeri-setup.sh b/scripts/start-elmeri-setup.sh index 5bb1e1b..129061a 100755 --- a/scripts/start-elmeri-setup.sh +++ b/scripts/start-elmeri-setup.sh @@ -1,11 +1,13 @@ #!/bin/sh +# Use identity from params or the default key file +IDENTITY_FILE=${1:-~/.ssh/id_rsa} USERNAME=ubuntu -SERVER_URL=elmeri.digit.fi +SERVER_URL=13.49.183.192 # EC2 private ip because elmeri.digit.fi doesn't work until ELB health check has succeeded echo "Copy server_setup_files to the ${SERVER_URL}" -rsync -avz --delete server_setup_files/ ${USERNAME}@${SERVER_URL}:~/server_setup_files +rsync -avz --delete server_setup_files/ -e "ssh -i ${IDENTITY_FILE}" ${USERNAME}@${SERVER_URL}:~/server_setup_files # Start the setup on the server -ssh ${USERNAME}@${SERVER_URL} 'bash -s' < ./server_setup_files/setup-elmeri.sh +ssh -i ${IDENTITY_FILE} ${USERNAME}@${SERVER_URL} 'bash -s' < ./server_setup_files/setup-elmeri.sh From 79a026e36a6e0b9b7c2c70a14fb50294c760e7ed Mon Sep 17 00:00:00 2001 From: niemisami Date: Sun, 20 Dec 2020 18:04:54 +0200 Subject: [PATCH 4/5] Update new elmeri EC2 ip and use useradd instead of interactive adduser --- scripts/server_setup_files/setup-elmeri.sh | 2 +- scripts/start-elmeri-setup.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/server_setup_files/setup-elmeri.sh b/scripts/server_setup_files/setup-elmeri.sh index 4bad2ae..a9b84eb 100755 --- a/scripts/server_setup_files/setup-elmeri.sh +++ b/scripts/server_setup_files/setup-elmeri.sh @@ -15,7 +15,7 @@ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt install -y nodejs # User and permissions -sudo adduser digit --disabled-password +sudo useradd --create-home --shell /bin/bash digit sudo usermod -aG www-data digit sudo mkdir -p /var/www sudo chown -R www-data:www-data /var/www diff --git a/scripts/start-elmeri-setup.sh b/scripts/start-elmeri-setup.sh index 129061a..9ece51f 100755 --- a/scripts/start-elmeri-setup.sh +++ b/scripts/start-elmeri-setup.sh @@ -3,7 +3,7 @@ # Use identity from params or the default key file IDENTITY_FILE=${1:-~/.ssh/id_rsa} USERNAME=ubuntu -SERVER_URL=13.49.183.192 # EC2 private ip because elmeri.digit.fi doesn't work until ELB health check has succeeded +SERVER_URL=13.49.127.199 # EC2 private ip because elmeri.digit.fi doesn't work until ELB health check has succeeded echo "Copy server_setup_files to the ${SERVER_URL}" rsync -avz --delete server_setup_files/ -e "ssh -i ${IDENTITY_FILE}" ${USERNAME}@${SERVER_URL}:~/server_setup_files From a8187cc5eb9050b25b69e1f6f5116d806aeff43b Mon Sep 17 00:00:00 2001 From: niemisami Date: Sun, 20 Dec 2020 18:59:58 +0200 Subject: [PATCH 5/5] Include initial migrations to initialization scripts --- scripts/server_setup_files/applied_migrations.json | 11 +++++++++++ scripts/server_setup_files/post-receive | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/server_setup_files/applied_migrations.json b/scripts/server_setup_files/applied_migrations.json index e69de29..c4f2e71 100644 --- a/scripts/server_setup_files/applied_migrations.json +++ b/scripts/server_setup_files/applied_migrations.json @@ -0,0 +1,11 @@ +[ + "0000-initial.js", + "0001-page-content.js", + "0002-navigation.js", + "0003-sponsor.js", + "0004-user-types.js", + "0005-event.js", + "0006-files.js", + "0007-update-nav-data.js", + "0008-event-enroll.js" +] \ No newline at end of file diff --git a/scripts/server_setup_files/post-receive b/scripts/server_setup_files/post-receive index 4c5e98c..1919a78 100755 --- a/scripts/server_setup_files/post-receive +++ b/scripts/server_setup_files/post-receive @@ -37,6 +37,16 @@ else echo "Retrieve cache" fi -pm2 startOrRestart ~/server_setup_files/pm2.config.js --update-env +# If app does not exists it still might have some applied migrations +APP_ID=$(pm2 id $APPNAME) +echo $APP_ID +if [[ $APP_ID != "[]" ]]; then + echo "App is already running" + pm2 restart $APPNAME --update-env +else + echo "Starting for the first time" + cp ~/server_setup_files/applied_migrations.json $TARGET/migrations/applied_digit.json + pm2 start ~/server_setup_files/pm2.config.js +fi echo "Started $APPNAME"