Files
mtusi2026lab3/scripts/clean-clone-check.sh

91 lines
3.2 KiB
Bash
Executable File

#!/bin/sh
# Build and test the committed revision from a separate clone with empty
# volumes, then remove only that clone's containers, volumes, network and image.
# The main stand is stopped (data kept) for the duration to avoid resource
# contention, and restarted afterwards by the operator.
set -eu
cd "$(dirname "$0")/.."
root=$(pwd)
revision=$(git rev-parse HEAD)
if [ -n "$(git status --porcelain)" ]; then
echo "clean-clone-check: working tree is dirty; commit first" >&2
exit 2
fi
# Colima only bind-mounts its configured shared paths (on this machine the
# user's home). A clone under /tmp would expose an empty haproxy.cfg inside the
# VM, so keep the temporary clone under the shared lab-artifacts directory.
artifact_root=${LAB3_ARTIFACTS:-$(cd "$root/../.." && pwd)/.local/highload-tasks/lab3-artifacts}
mkdir -p "$artifact_root"
tmp=$(mktemp -d "$artifact_root/clean-clone.XXXXXX")
project=mtusi-lab3-melnikov-clean
image=$project:local
port=8084
cleanup() {
if [ -d "$tmp" ]; then
(cd "$tmp" && COMPOSE_PROJECT_NAME=$project LB_PORT=$port IMAGE=$image \
./scripts/compose.sh --env-file .env down -v --remove-orphans >/dev/null 2>&1 || true)
docker image rm -f "$image" >/dev/null 2>&1 || true
rm -rf "$tmp"
fi
}
trap cleanup EXIT INT TERM
echo "== clean clone =="
echo "source revision: $revision"
echo "clone: $tmp"
echo "project: $project; LB port: $port"
git clone --quiet "$root" "$tmp"
cd "$tmp"
test "$(git rev-parse HEAD)" = "$revision"
echo "== environment and validation =="
./scripts/init-env.sh
python3 - "$tmp/.env" "$port" <<'PY'
import sys
path, port = sys.argv[1], sys.argv[2]
lines = []
for line in open(path):
if line.startswith('LB_PORT='):
line = 'LB_PORT=%s\n' % port
lines.append(line)
open(path, 'w').writelines(lines)
PY
export COMPOSE_PROJECT_NAME=$project
export LB_PORT=$port
export IMAGE=$image
export BASE_URL=http://127.0.0.1:$port
export PYTHON=/usr/bin/python3
make check
/usr/bin/python3 -m venv .venv
.venv/bin/python -m pip install -q -r requirements-dev.txt
make validate PYTHON=.venv/bin/python
echo "== build and start from empty named volumes =="
make up
postgres=$(./scripts/compose.sh --env-file .env ps -q postgres)
set -a; . ./.env; set +a
docker exec -i "$postgres" psql -X -A -t -U "$POSTGRES_USER" -d "$POSTGRES_DB" \
-c "SELECT 'users=' || count(*) FROM users;" \
-c "SELECT 'migrations=' || string_agg(version::text, ',' ORDER BY version) FROM schema_migrations;"
./scripts/compose.sh --env-file .env ps --format 'table {{.Name}}\t{{.Status}}'
echo "== public and own tests on the clean stand =="
make test
make test-student
make test-persistence ACTION_SCRIPT=scripts/restart.sh
echo "== intentional cleanup of this temporary clone's resources =="
./scripts/compose.sh --env-file .env ps -q | wc -l
make purge
docker image rm -f "$image" >/dev/null 2>&1 || true
if docker ps -aq --filter label=com.docker.compose.project=$project | grep -q .; then
echo "clean-clone-check: containers remain" >&2
exit 1
fi
if docker volume ls -q | grep -q "^${project}-"; then
echo "clean-clone-check: volumes remain" >&2
exit 1
fi
echo "PASS: clean clone built, migrated, tested and removed; main stand untouched"