Files
Jyotisha/deploy/prune-production-release.sh
ClaudeandClaude Opus 5.5 0054781bc5
Independent Staging Quality Gate / validate-frontend (push) Successful in 6m34s
Independent Staging Quality Gate / validate-python (push) Successful in 8m34s
Independent Staging Quality Gate / publish (push) Successful in 3m57s
ops: container memory limits, api init, 65% disk alert, release cleanup (BUG-2428)
- postgres 1g + oom_score_adj -500; api 1536m + init; web 768m; caddy 128m
  (BUG-2416 hardening kept; web keeps its own SIGTERM handling)
- disk alert 80% -> 65%, below the 70% backup stop line
- ops-check: container_memory_high at 85% of the container's own limit
- after a verified production deploy: keep newest 5 pre-release recovery
  points (+ baseline) and current/previous app images; dry-run supported

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-26 23:18:02 +08:00

125 lines
4.7 KiB
Bash

#!/usr/bin/env bash
# BUG-2428: release cleanup that runs after a verified production deploy.
# - keep only the newest pre-release recovery points (production-pre-migration-*)
# in the local backups directory; the one named by .state/recovery-baseline.env
# is always kept. Daily backups and the OSS copies are never touched here.
# - remove old application images of the release repository, keeping the
# images that are running now and the previous release's images for rollback.
# Every removal prints one "release_cleanup ..." line. RELEASE_CLEANUP_DRY_RUN=1
# prints "would_remove" lines and deletes nothing.
set -euo pipefail
release_cleanup_log() {
printf 'release_cleanup %s\n' "$*"
}
release_cleanup_dry_run() {
[ "${RELEASE_CLEANUP_DRY_RUN:-0}" = "1" ]
}
# prune_recovery_points BACKUP_DIRECTORY KEEP [BASELINE_FILE]
prune_recovery_points() {
local directory="$1"
local keep="$2"
local baseline="${3:-}"
local protected="" reference name base verify index=0
local -a points=()
[ -n "$directory" ] && [ -d "$directory" ] && [ ! -L "$directory" ] || {
echo "backup directory is unsafe" >&2
return 1
}
[[ "$keep" =~ ^[1-9][0-9]?$ ]] || {
echo "recovery point count must be a positive integer" >&2
return 1
}
if [ -n "$baseline" ] && [ -f "$baseline" ] && [ ! -L "$baseline" ]; then
reference="$(awk -F= '$1 == "RECOVERY_REFERENCE" { print $2; exit }' "$baseline")"
protected="${reference##*/}"
fi
# Names carry a UTC timestamp, so a reverse name sort is newest first.
mapfile -t points < <(
find "$directory" -maxdepth 1 -type f -name 'production-pre-migration-*.dump.enc' -printf '%f\n' 2>/dev/null |
grep -E '^production-pre-migration-[0-9]{8}T[0-9]{6}Z\.dump\.enc$' | LC_ALL=C sort -r || true
)
for name in "${points[@]}"; do
index=$((index + 1))
if (( index <= keep )) || [ "$name" = "$protected" ]; then
continue
fi
base="${name%.dump.enc}"
verify="$directory/${base}-restore-verify.json"
if release_cleanup_dry_run; then
release_cleanup_log "would_remove recovery_point=$name"
continue
fi
rm -f -- "$directory/$name"
if [ -f "$verify" ] && [ ! -L "$verify" ]; then
rm -f -- "$verify"
fi
release_cleanup_log "removed recovery_point=$name"
done
release_cleanup_log "recovery_points_kept=$(( ${#points[@]} < keep ? ${#points[@]} : keep ))${protected:+ baseline=$protected}"
}
release_cleanup_docker() {
if declare -p docker_command >/dev/null 2>&1; then
"${docker_command[@]}" "$@"
else
docker "$@"
fi
}
# prune_release_images REPOSITORY KEEP_REF...
# KEEP_REF is an image id (sha256:...) or a digest reference. Images used by any
# container, running or stopped, are kept as well. Removal is by id without
# force, so Docker itself refuses anything still in use.
prune_release_images() {
local repository="$1"
shift
local ref id refs line container_image short
declare -A keep_ids=()
local -a ids=()
[[ "$repository" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?(:[1-9][0-9]{0,4})?(/[a-z0-9]+([._-][a-z0-9]+)*)+$ ]] || {
echo "release image repository is unsafe" >&2
return 1
}
for ref in "$@"; do
[ -n "$ref" ] || continue
id="$(release_cleanup_docker image inspect --format '{{.Id}}' "$ref" 2>/dev/null || true)"
[[ "$id" =~ ^sha256:[0-9a-f]{64}$ ]] || {
echo "release image to keep is not on the host; skipping image cleanup" >&2
return 0
}
keep_ids["$id"]=1
done
while IFS= read -r line; do
[ -n "$line" ] || continue
container_image="$(release_cleanup_docker inspect --format '{{.Image}}' "$line" 2>/dev/null || true)"
[[ "$container_image" =~ ^sha256:[0-9a-f]{64}$ ]] && keep_ids["$container_image"]=1
done < <(release_cleanup_docker ps -aq --no-trunc 2>/dev/null || true)
mapfile -t ids < <(release_cleanup_docker image ls -q --no-trunc 2>/dev/null | LC_ALL=C sort -u || true)
for id in "${ids[@]}"; do
[[ "$id" =~ ^sha256:[0-9a-f]{64}$ ]] || continue
[ -z "${keep_ids[$id]:-}" ] || continue
refs="$(release_cleanup_docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}{{range .RepoTags}}{{println .}}{{end}}' "$id" 2>/dev/null || true)"
# Only images of the release repository; postgres, caddy and anything else stay.
grep -Eq "^${repository//./\\.}[@:]" <<<"$refs" || continue
short="${id#sha256:}"
short="${short:0:12}"
if release_cleanup_dry_run; then
release_cleanup_log "would_remove image=$short"
continue
fi
if release_cleanup_docker image rm "$id" >/dev/null 2>&1; then
release_cleanup_log "removed image=$short"
else
release_cleanup_log "image_remove_failed image=$short"
fi
done
release_cleanup_log "images_kept=${#keep_ids[@]}"
}