Skip to content
Merged
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
38 changes: 31 additions & 7 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ SITE_URL="${SITE_URL:-http://localhost/project2}"
BRANCH="${BRANCH:-main}"
KEEP_BACKUPS=5
KEEP_OLD_ASSETS_DAYS=14
STAGE=""
REVISION=""

WINE_COUNT_QUERY='{"query":"{ wines(first: 1) { totalCount } }"}'

Expand Down Expand Up @@ -84,6 +86,27 @@ build() {
(cd backend && npm ci && npm run typecheck)
}

# Copies what will be published to a folder on the VM's own disk, as you. The checkout is in
# your NTNU home directory, which is a network drive that root cannot read, so the sudo
# commands below cannot copy from it. They copy from this folder.
stage_files() {
step "Collect the files to publish"
STAGE="$(mktemp -d "${TMPDIR:-/var/tmp}/project2-deploy.XXXXXX")"
trap 'rm -rf "$STAGE"' EXIT
# .env exists only on the VM, and logs are not part of the code.
rsync -a --exclude .env --exclude logs backend/ "$STAGE/backend/"
rsync -a frontend/dist/ "$STAGE/frontend/"
cp scripts/project2-backend.service "$STAGE/"
cp "$(command -v node)" "$STAGE/node"
# Readable for root and for the checks, whatever the permissions were in the checkout.
chmod -R u+rwX,go+rX "$STAGE"
chmod 755 "$STAGE"
REVISION="$(git rev-parse HEAD)"
echo "Collected $(du -sh "$STAGE" | cut -f1) in $STAGE"
# Nothing below reads the checkout, so leave it: root cannot stand in it either.
cd /
}

back_up_database() {
step "Back up the users' data"
# Everything except the rows of community_rating: the 21 million X-Wines ratings are 2 GB
Expand All @@ -107,11 +130,11 @@ publish_backend() {
# A copy of the code that runs now, to go back to if the new code does not start.
sudo rsync -a --delete --exclude .env "$APP_DIR/backend/" "$APP_DIR/backend.previous/"
# --delete removes files that are gone from the repository. .env exists only on the VM.
sudo rsync -a --delete --no-owner --no-group --exclude .env --exclude logs backend/ "$APP_DIR/backend/"
sudo install -m 755 "$(command -v node)" "$APP_DIR/bin/node"
sudo rsync -a --delete --no-owner --no-group --exclude .env "$STAGE/backend/" "$APP_DIR/backend/"
sudo install -m 755 "$STAGE/node" "$APP_DIR/bin/node"

if ! sudo cmp -s scripts/project2-backend.service "/etc/systemd/system/$SERVICE.service"; then
sudo install -m 644 scripts/project2-backend.service "/etc/systemd/system/$SERVICE.service"
if ! sudo cmp -s "$STAGE/project2-backend.service" "/etc/systemd/system/$SERVICE.service"; then
sudo install -m 644 "$STAGE/project2-backend.service" "/etc/systemd/system/$SERVICE.service"
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE"
echo "Installed the systemd service $SERVICE"
Expand Down Expand Up @@ -172,11 +195,11 @@ wait_for_backend() {
publish_frontend() {
step "Publish the frontend"
sudo mkdir -p "$WEB_DIR"
sudo rsync -a --no-owner --no-group frontend/dist/ "$WEB_DIR/"
sudo rsync -a --no-owner --no-group "$STAGE/frontend/" "$WEB_DIR/"
# Files from earlier builds stay for a while, so a visitor with the old page open can still
# load the parts of it they have not opened yet. After that they are only using disk space.
sudo find "$WEB_DIR/assets" -type f -mtime "+$KEEP_OLD_ASSETS_DAYS" -delete
git rev-parse HEAD | sudo tee "$APP_DIR/REVISION" >/dev/null
echo "$REVISION" | sudo tee "$APP_DIR/REVISION" >/dev/null
}

check_site() {
Expand Down Expand Up @@ -207,11 +230,12 @@ main() {
check_before_start
update_checkout
build
stage_files
back_up_database
publish_backend
publish_frontend
check_site
printf '\n\033[32mDeployed %s to %s\033[0m\n' "$(git rev-parse --short HEAD)" "$SITE_URL/"
printf '\n\033[32mDeployed %s to %s\033[0m\n' "${REVISION:0:7}" "$SITE_URL/"
}

main "$@"