feat(deploy): reliability API routes, atomic releases, version single-source

This commit is contained in:
2026-08-22 15:20:39 -06:00
parent aba08e36c7
commit 1bb15de258
10 changed files with 586 additions and 40 deletions
+97 -36
View File
@@ -21,6 +21,11 @@ Options:
--no-portal-refresh Skip the best-effort portal refresh
-h, --help Show this help
Deploys to /mmc/mk8/releases/<ts>/ with an atomically repointed 'current'
symlink, mirrors the payload into the legacy /root/payloads location the
init scripts run from, verifies sha256 of the upload, and polls the local
API after start; on failure it rolls the symlink + legacy dir back.
If neither --password nor --ssh-key is supplied, ssh/scp prompt normally.
EOF
}
@@ -61,6 +66,17 @@ STAGE="$OUT_DIR/stage"
printf 'Payload directory not found: %s\n' "$PAYLOAD_DIR" >&2
exit 1
}
VERSION_FILE="$ROOT/VERSION"
[[ -f "$VERSION_FILE" ]] || {
printf 'VERSION file not found: %s\n' "$VERSION_FILE" >&2
exit 1
}
VERSION="$(tr -d '[:space:]' < "$VERSION_FILE")"
[[ -n "$VERSION" ]] || {
printf 'VERSION file is empty.\n' >&2
exit 1
}
printf 'Deploying Mark VIII version %s\n' "$VERSION"
mkdir -p "$OUT_DIR"
rm -rf "$STAGE"
@@ -68,6 +84,10 @@ mkdir -p "$STAGE/user/$PAYLOAD_CATEGORY"
cp -R "$PAYLOAD_DIR" "$STAGE/user/$PAYLOAD_CATEGORY/$PAYLOAD_KEY"
find "$STAGE" \( -type d -name __pycache__ -o -type f -name '*.pyc' \) -prune -exec rm -rf {} +
# Stamp build copies only (never the source tree): payload.sh header,
# staged server.py SERVER_VERSION constant; manifest is stamped below.
python3 "$ROOT/scripts/build_common.py" "$STAGE" "$VERSION"
B64_KEY="$(python3 -c 'import base64; print(base64.urlsafe_b64encode(b"pager-webui").decode().rstrip("="))')"
ZIP_NAME="payload-$B64_KEY.zip"
ZIP_PATH="$OUT_DIR/$ZIP_NAME"
@@ -94,7 +114,9 @@ with open(destination, 'w', encoding='ascii') as handle:
json.dump(manifest, handle, indent=2)
handle.write('\n')
PY
printf 'Built: %s\n' "$ZIP_PATH"
# Single-source version: stamp the generated manifest copy too.
python3 "$ROOT/scripts/build_common.py" "$MANIFEST_PATH" "$VERSION" >/dev/null
printf 'Built: %s (sha256 %s)\n' "$ZIP_PATH" "$HASH"
TARGET="$PAGER_USER@$PAGER_HOST"
SSH_OPTS=(-o StrictHostKeyChecking=accept-new)
@@ -166,49 +188,88 @@ echo PYTHON_OK'
install_python3
run_scp "$ZIP_PATH" "$MANIFEST_PATH" "$TARGET:/tmp/"
run_ssh "$TARGET" 'mkdir -p /tmp/mk8-stage && rm -rf /tmp/mk8-stage/*'
run_scp "$ZIP_PATH" "$MANIFEST_PATH" "$TARGET:/tmp/mk8-stage/"
REMOTE_PAYLOAD_DIR="user/$PAYLOAD_CATEGORY/$PAYLOAD_KEY"
LEGACY_PAYLOAD_DIR="user/general/$PAYLOAD_KEY"
RELEASE_TS="$(date +%Y%m%d-%H%M%S)"
REMOTE_COMMAND="set -e
cd /root/payloads
stage='.pager-webui.deploy.\$\$'
backup='.pager-webui.backup.\$\$'
trap 'rm -rf \"\$stage\" \"\$backup\"' EXIT
mkdir -p \"\$stage\"
cd \"\$stage\"
unzip -q '/tmp/$ZIP_NAME'
new=\"\$PWD/$REMOTE_PAYLOAD_DIR\"
[ -f \"\$new/server.py\" ] && [ -f \"\$new/payload.sh\" ] && [ -d \"\$new/www\" ]
cp /tmp/_hak5_manifest.json \"\$new/_hak5_manifest.json\"
chmod +x \"\$new/payload.sh\" \"\$new/pagerwebui.init\"
chmod -R 755 \"\$new/www\"
cd /root/payloads
if [ -d '$REMOTE_PAYLOAD_DIR' ]; then
mkdir -p \"\$(dirname \"\$backup\")\"
mv '$REMOTE_PAYLOAD_DIR' \"\$backup\"
fi
if mv \"\$new\" '$REMOTE_PAYLOAD_DIR'; then
rm -rf \"\$backup\" '$LEGACY_PAYLOAD_DIR'
else
[ ! -d \"\$backup\" ] || mv \"\$backup\" '$REMOTE_PAYLOAD_DIR'
STAGE_DIR='/tmp/mk8-stage'
ZIP='\$STAGE_DIR/$ZIP_NAME'
RELDIR='/mmc/mk8/releases/$RELEASE_TS'
PAYDIR='$REMOTE_PAYLOAD_DIR'
LIVE=\"/root/payloads/\$PAYDIR\"
BACKUP=\"/root/payloads/.pager-webui.backup.\$\$\"
CURRENT='/mmc/mk8/releases/current'
PREV=\$(readlink \$CURRENT 2>/dev/null || true)
cleanup() { rm -rf \"\$STAGE_DIR\"; }
trap cleanup EXIT
# Upload integrity gate: remote sha256 must match the local build hash.
GOT=\$(sha256sum \"\$ZIP\" | awk '{print \$1}')
[ \"\$GOT\" = '$HASH' ] || { echo 'sha256 mismatch on uploaded zip' >&2; exit 1; }
[ -f \"\$STAGE_DIR/_hak5_manifest.json\" ] || { echo 'manifest missing' >&2; exit 1; }
/etc/init.d/pagerwebui stop >/dev/null 2>&1 || true
mkdir -p \"\$RELDIR\"
unzip -q \"\$ZIP\" -d \"\$RELDIR\"
NEW=\"\$RELDIR/\$PAYDIR\"
[ -f \"\$NEW/server.py\" ] && [ -f \"\$NEW/payload.sh\" ] && [ -d \"\$NEW/www\" ] || {
echo 'release payload incomplete' >&2
rm -rf \"\$RELDIR\"
exit 1
}
cp \"\$STAGE_DIR/_hak5_manifest.json\" \"\$NEW/_hak5_manifest.json\"
chmod +x \"\$NEW/payload.sh\" \"\$NEW/pagerwebui.init\"
chmod -R 755 \"\$NEW/www\"
ln -sfn \"\$RELDIR\" \"\$CURRENT\"
# Mirror into the legacy /root/payloads path the init scripts execute.
rollback_install() {
rm -rf \"\$LIVE\"
[ ! -d \"\$BACKUP\" ] || mv \"\$BACKUP\" \"\$LIVE\"
[ -z \"\$PREV\" ] || ln -sfn \"\$PREV\" \"\$CURRENT\"
}
if [ -d \"\$LIVE\" ]; then mv \"\$LIVE\" \"\$BACKUP\"; fi
if ! mkdir -p \"\$LIVE\" || ! cp -a \"\$NEW/.\" \"\$LIVE/\"; then
rollback_install
exit 1
fi
rm -f '/tmp/$ZIP_NAME' /tmp/_hak5_manifest.json
cp '$REMOTE_PAYLOAD_DIR/pagerwebui.init' /etc/init.d/pagerwebui
cp -f \"\$LIVE/pagerwebui.init\" /etc/init.d/pagerwebui
chmod +x /etc/init.d/pagerwebui
/etc/init.d/pagerwebui enable
if /etc/init.d/pagerwebui running >/dev/null 2>&1; then
/etc/init.d/pagerwebui restart
else
/etc/init.d/pagerwebui start
fi
cp -f '$REMOTE_PAYLOAD_DIR/mk8-guard.init' /etc/init.d/mk8-guard
cp -f \"\$LIVE/mk8-guard.init\" /etc/init.d/mk8-guard
chmod 755 /etc/init.d/mk8-guard
/etc/init.d/mk8-guard enable
echo EXTRACT_OK"
run_ssh "$TARGET" "$REMOTE_COMMAND"
printf 'Installed to /root/payloads/%s/\n' "$REMOTE_PAYLOAD_DIR"
/etc/init.d/pagerwebui start
# Post-deploy verification: API must answer within 60s or we roll back.
i=0
HEALTH_OK=''
while [ \$i -lt 60 ]; do
if curl -fsS -m 3 http://127.0.0.1:8080/api/api_ping >/dev/null 2>&1; then
HEALTH_OK=1
break
fi
sleep 1
i=\$((i+1))
done
if [ -z \"\$HEALTH_OK\" ]; then
echo 'post-deploy health check failed; rolling back' >&2
/etc/init.d/pagerwebui stop >/dev/null 2>&1 || true
rollback_install
rm -rf \"\$RELDIR\"
/etc/init.d/pagerwebui start
exit 1
fi
rm -rf \"\$BACKUP\"
echo RELEASE_OK@\"\$RELDIR\""
if run_ssh "$TARGET" "$REMOTE_COMMAND"; then
printf 'Release active: /mmc/mk8/releases/%s\n' "$RELEASE_TS"
else
printf 'Deployment failed; previous release restored on the pager.\n' >&2
exit 1
fi
if $PORTAL_REFRESH && [[ -n "$PASSWORD" ]]; then
PASSWORD_B64="$(printf '%s' "$PASSWORD" | base64)"
@@ -235,4 +296,4 @@ elif $PORTAL_REFRESH; then
printf 'Skipping portal refresh without --password; payload installation is complete.\n'
fi
printf 'Deploy complete. Browse http://%s:8080/\n' "$PAGER_HOST"
printf 'Deploy complete (v%s). Browse http://%s:8080/\n' "$VERSION" "$PAGER_HOST"