diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 0cf0546..5278fee 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -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 } }"}' @@ -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 @@ -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" @@ -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() { @@ -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 "$@"