feat: add stack reset helper and setup runbook

This commit is contained in:
Md Bayazid Bostame
2026-03-31 15:11:16 +02:00
parent 80cb7a409d
commit 541736a9a2
4 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DEPLOY_HOST="${DEPLOY_HOST:-root@192.168.2.55}"
DEPLOY_PATH="${DEPLOY_PATH:-/opt/workdock}"
HEALTH_URL="${HEALTH_URL:-http://192.168.2.55:8088/healthz/}"
REMOTE_ENV_FILE="${REMOTE_ENV_FILE:-.env.test}"
COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.prod.yml}"
RUN_DJANGO_CHECK="${RUN_DJANGO_CHECK:-0}"
SSH_CMD="${SSH_CMD:-ssh -4}"
RSYNC_SSH="${RSYNC_SSH:-ssh -4}"
EXPECTED_BRANCH="${EXPECTED_BRANCH:-}"
RESET_CONFIRM="${RESET_CONFIRM:-}"
cd "$REPO_ROOT"
current_branch="$(git branch --show-current)"
if [[ -n "$EXPECTED_BRANCH" && "$current_branch" != "$EXPECTED_BRANCH" ]]; then
echo "Expected branch '$EXPECTED_BRANCH' for this reset, got '$current_branch'." >&2
exit 1
fi
if [[ "$RESET_CONFIRM" != "RESET" ]]; then
cat >&2 <<EOF
Refusing destructive reset.
This command wipes the stack state for:
$DEPLOY_HOST:$DEPLOY_PATH
It will remove:
- database volume/data
- generated media
- staticfiles volume
- backup volume
Run again with:
RESET_CONFIRM=RESET ./scripts/reset_stack_from_mac.sh
EOF
exit 1
fi
echo "Current branch: $current_branch"
echo "Updating local branch from origin/$current_branch ..."
git pull --ff-only origin "$current_branch"
echo "Checking remote env file..."
$SSH_CMD "$DEPLOY_HOST" "test -f '$DEPLOY_PATH/$REMOTE_ENV_FILE'" || {
echo "Missing remote env file: $DEPLOY_PATH/$REMOTE_ENV_FILE" >&2
echo "Create or restore the server env file before resetting." >&2
exit 1
}
echo "Syncing repository to ${DEPLOY_HOST}:${DEPLOY_PATH} ..."
rsync -az --delete \
--filter 'P .env.test' \
--filter 'P .env.prod' \
--exclude '.git' \
--exclude '.github' \
--exclude '.venv' \
--exclude '__pycache__' \
--exclude 'node_modules' \
--exclude 'backend/media' \
--exclude 'backend/staticfiles' \
-e "$RSYNC_SSH" \
"$REPO_ROOT"/ \
"${DEPLOY_HOST}:${DEPLOY_PATH}/"
echo "Resetting remote stack state..."
$SSH_CMD "$DEPLOY_HOST" "cd '$DEPLOY_PATH' && export APP_ENV_FILE='$REMOTE_ENV_FILE' && docker compose --env-file '$REMOTE_ENV_FILE' -f '$COMPOSE_FILE' down -v"
echo "Rebuilding and bootstrapping fresh stack..."
$SSH_CMD "$DEPLOY_HOST" \
"cd '$DEPLOY_PATH' && RUN_DJANGO_CHECK='$RUN_DJANGO_CHECK' DEPLOY_HEALTH_URL='$HEALTH_URL' ./scripts/deploy_stack.sh '$REMOTE_ENV_FILE' '$COMPOSE_FILE'"
echo "Verifying health endpoint..."
curl --fail --silent --show-error --max-time 10 "$HEALTH_URL" >/dev/null
commit_sha="$(git rev-parse --short HEAD)"
echo "Reset deployment healthy: $HEALTH_URL"
echo "Deployed commit: $commit_sha"
echo "Branch: $current_branch"