Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uuden Elmeri EC2 palvelimen pystytys #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion digit_dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ module.exports = {
node_args: '--inspect=0.0.0.0:9230'
}
]
};
}
20 changes: 20 additions & 0 deletions scripts/dev-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions scripts/server_setup_files/applied_migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pakko antaa migraatiot omana tiedostona, jos ajetaan jo alustettua kantaa vastaan

"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"
]
21 changes: 21 additions & 0 deletions scripts/server_setup_files/pm2.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tässä kerrotaan kuinka pm2:n pitää käynnistää palvelin sekä mistä tietokanta löytyy. Nämä arvot on korjattava käsin /home/digit/server_setup_files/pm2.config.js -tiedostosta ennen kun lähdetään ajamaan

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'
}
}
]
}
52 changes: 52 additions & 0 deletions scripts/server_setup_files/post-receive
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gitin post-receive hook, joka hoitelee master branchin koodit palvelimelle, ajaa tarvittasessa npm installin ja käynnistää pm2:lla koodit uudestaan. Hakee myös mahdolliset migraatiot ja asettaa ne oikeaan paikkaan

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

# 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"
48 changes: 48 additions & 0 deletions scripts/server_setup_files/setup-elmeri.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tämä scribula hoitaa palvelimen sekä projektin alustukset. Luo digit -käyttäjän ja ja no siinä ne lukee

# 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 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
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"
13 changes: 13 additions & 0 deletions scripts/start-elmeri-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omalla koneella sh start-elmeri-setup niin on palvelin pystyssä. Lisäsin ton IDENTITY_FILEN tohon, koska omalla toi elmerin ip muuttuu ja en jaksa konffia kaikkea .ssh/configgiin


# Use identity from params or the default key file
IDENTITY_FILE=${1:-~/.ssh/id_rsa}
USERNAME=ubuntu
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

# Start the setup on the server
ssh -i ${IDENTITY_FILE} ${USERNAME}@${SERVER_URL} 'bash -s' < ./server_setup_files/setup-elmeri.sh