Menu
Public repository / master
AkurAI-Drive / site/deploy.sh
Latest change d63e826eb482cb963477774b04fe8a84f23f0e45 - deploy: in-place swaps matching live units with health-checked rollback; no unit rewrites by AkurAI Agent
#!/usr/bin/env bash
# Publish the marketing site (Framework template frontend/ plus its locales and
# routes backend/) into the live layout. Called by the approved AkurAI Build
# production job, not ad hoc.
#
# Live layout (systemd unit akurai-drive, owned by the host, NOT managed here):
# WorkingDirectory=/opt/akurai-drive/app
# serves /opt/akurai-drive/app/frontend on 0.0.0.0:8095
# Backs up app/frontend and app/backend, extracts the new ones, restarts,
# health-checks 8095 and restores the backups on failure.
set -euo pipefail
SSH_HOST="${AKURAI_MAIL_SSH:-akurai-ec2}"
ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
SOURCE="${DRIVE_SITE_SOURCE:-$ROOT_DIR/site}"
RELEASE="${DRIVE_RELEASE:-${AKURAI_COMMIT_SHA:-local}}"
if [[ "$SOURCE" != /* ]]; then SOURCE="$ROOT_DIR/$SOURCE"; fi
if [[ ! -d "$SOURCE" && ! -f "$SOURCE" ]]; then
echo "site source directory or archive not found: $SOURCE" >&2
exit 1
fi
if [[ ! "$RELEASE" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "invalid release id" >&2
exit 2
fi
remote_archive="/tmp/akurai-drive-site-$RELEASE.tar.gz"
if [[ -f "$SOURCE" ]]; then
scp -q "$SOURCE" "$SSH_HOST:$remote_archive"
else
# Stream the archive; no local temp file.
tar -czf - -C "$SOURCE" frontend backend | ssh "$SSH_HOST" "cat > '$remote_archive'"
fi
ssh "$SSH_HOST" "sudo bash -s -- '$RELEASE'" <<'REMOTE'
set -euo pipefail
release="$1"
app=/opt/akurai-drive/app
archive="/tmp/akurai-drive-site-$release.tar.gz"
ts="$(date +%Y%m%d%H%M%S)"
owner="$(stat -c %U:%G "$app")"
service=akurai-drive
healthy() {
for _ in $(seq 1 20); do
curl -fsS -o /dev/null http://127.0.0.1:8095/ && return 0
sleep 1
done
return 1
}
test -d "$app"
staging="$(mktemp -d "$app/../.site-stage-XXXXXX")"
tar -xzf "$archive" -C "$staging"
rm -f "$archive"
test -d "$staging/frontend"
test -d "$staging/backend"
chown -R "$owner" "$staging"
for d in frontend backend; do
if [ -e "$app/$d" ]; then mv "$app/$d" "$app/$d.bak-$ts"; fi
mv "$staging/$d" "$app/$d"
done
rmdir "$staging"
printf '%s\n' "$release" > "$app/deployed-release.txt"
systemctl restart "$service"
if healthy; then
echo " [ok] $service healthy (release $release; backups *.bak-$ts)"
exit 0
fi
echo " [FAIL] health check failed; rolling back" >&2
for d in frontend backend; do
if [ -e "$app/$d.bak-$ts" ]; then
rm -rf "$app/$d"
mv "$app/$d.bak-$ts" "$app/$d"
fi
done
systemctl restart "$service"
if healthy; then echo " [rollback] previous site healthy" >&2; else echo " [rollback] STILL UNHEALTHY" >&2; fi
exit 1
REMOTE
echo "AkurAI Drive marketing site published: $RELEASE"