#!/usr/bin/env bash set -Eeuo pipefail commit=${1:?commit is required} archive=${2:?archive path is required} expected_sha=${3:?archive sha256 is required} stamp=$(date -u +%Y%m%dT%H%M%SZ) backup=/var/backups/lisglosips-s57/$stamp old=$(readlink -f /opt/lisglosips/current) new=/opt/lisglosips/releases/s57-ui-number-library-$stamp test "$(sha256sum "$archive" | awk '{print $1}')" = "$expected_sha" test -d "$old" test ! -e "$new" echo "phase=data-backup" systemctl start lisglosips-backup.service if systemctl --quiet is-failed lisglosips-backup.service; then systemctl status --no-pager lisglosips-backup.service exit 1 fi echo "phase=config-backup" install -d -m 0700 "$backup" cp -a /etc/lisglosips/api.env "$backup/api.env" printf '%s\n' "$old" > "$backup/previous-release" changed=0 rollback() { rc=$? if [ "$changed" -eq 1 ]; then echo "phase=rollback rc=$rc" cp -a "$backup/api.env" /etc/lisglosips/api.env ln -sfn "$old" /opt/lisglosips/current systemctl restart lisglosips@api || true fi exit "$rc" } trap rollback ERR echo "phase=stage-release" cp -a --reflink=auto "$old" "$new" tar -xzf "$archive" -C "$new" printf '%s\n' "$commit" > "$new/.deployed-commit" printf '%s\n' "$old" > "$new/.delta-base-release" chown -R root:lisglosips "$new/apps/api/dist" "$new/public" "$new/scripts" "$new/infra/server-b/s57" chmod -R u=rwX,g=rX,o= "$new/apps/api/dist" "$new/public" "$new/scripts" "$new/infra/server-b/s57" chmod 0755 "$new/infra/server-b/s57/deploy-ui-number-library.sh" tmp_env=$(mktemp) grep -Ev '^(ACTIVE_CALLS_MODE|ACTIVE_CALLS_HTTP_URL|ACTIVE_CALLS_TIMEOUT_MS)=' /etc/lisglosips/api.env > "$tmp_env" printf '%s\n' 'ACTIVE_CALLS_MODE=http' 'ACTIVE_CALLS_HTTP_URL=http://127.0.0.1:8888/mi' 'ACTIVE_CALLS_TIMEOUT_MS=3000' >> "$tmp_env" install -o root -g lisglosips -m 0640 "$tmp_env" /etc/lisglosips/api.env rm -f "$tmp_env" changed=1 ln -sfn "$new" /opt/lisglosips/current systemctl restart lisglosips@api sleep 4 echo "phase=verify" test "$(readlink -f /opt/lisglosips/current)" = "$new" test "$(cat /opt/lisglosips/current/.deployed-commit)" = "$commit" test "$(systemctl is-active lisglosips@api)" = active curl --fail --silent --show-error http://127.0.0.1:3000/api/v2/health/ready >/dev/null curl --fail --silent --show-error -H 'content-type: application/json' \ --data '{"jsonrpc":"2.0","id":1,"method":"dlg_list","params":[]}' \ http://127.0.0.1:8888/mi >/dev/null /opt/lisglosips/current/infra/server-b/s30/lisglosips-release-preflight.sh printf 'BACKUP=%s\nRELEASE=%s\nCOMMIT=%s\n' "$backup" "$new" "$commit" changed=0 trap - ERR