forked from mtusi2026/mtusi2026lab4
117 lines
4.5 KiB
Bash
Executable File
117 lines
4.5 KiB
Bash
Executable File
#!/bin/sh
|
|
# Build and test the committed revision from a separate clone with empty named
|
|
# volumes, then remove only that clone's containers, network, volumes and image.
|
|
#
|
|
# The main lab4 stand (if running) is stopped with data preserved for the
|
|
# duration so both stands fit the 2 vCPU / ~2 GiB Colima VM, and restarted at
|
|
# the end. The temporary clone lives under .local/highload-tasks/lab4-artifacts
|
|
# because Colima only shares the user's home into the VM.
|
|
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
|
|
|
|
artifact_root=${LAB4_ARTIFACTS:-$(cd "$root/../.." && pwd)/.local/highload-tasks/lab4-artifacts}
|
|
mkdir -p "$artifact_root"
|
|
tmp=$(mktemp -d "$artifact_root/clean-clone.XXXXXX")
|
|
project=mtusi-lab4-melnikov-clean
|
|
port=8085
|
|
image=$project:local
|
|
main_was_running=0
|
|
if docker ps --format '{{.Names}}' | grep -q '^mtusi-lab4-melnikov-'; then
|
|
main_was_running=1
|
|
fi
|
|
|
|
cleanup() {
|
|
status=$?
|
|
if [ -d "$tmp" ]; then
|
|
(cd "$tmp" && COMPOSE_PROJECT_NAME=$project LB_PORT=$port \
|
|
./scripts/compose.sh --env-file .env down -v --remove-orphans >/dev/null 2>&1 || true)
|
|
ids=$(docker ps -aq --filter label=com.docker.compose.project=$project || true)
|
|
if [ -n "$ids" ]; then docker rm -f $ids >/dev/null 2>&1 || true; fi
|
|
volumes=$(docker volume ls -q --filter label=com.docker.compose.project=$project || true)
|
|
if [ -n "$volumes" ]; then docker volume rm $volumes >/dev/null 2>&1 || true; fi
|
|
networks=$(docker network ls -q --filter label=com.docker.compose.project=$project || true)
|
|
if [ -n "$networks" ]; then docker network rm $networks >/dev/null 2>&1 || true; fi
|
|
docker image rm -f "$image" >/dev/null 2>&1 || true
|
|
case "$tmp" in
|
|
"$artifact_root"/clean-clone.*) rm -rf "$tmp" ;;
|
|
*) echo "clean-clone-check: refusing to remove unexpected path $tmp" >&2 ;;
|
|
esac
|
|
fi
|
|
if [ "$main_was_running" = "1" ]; then
|
|
(cd "$root" && make up >/dev/null 2>&1 && ./scripts/wait-ready.sh >/dev/null 2>&1 || true)
|
|
fi
|
|
exit "$status"
|
|
}
|
|
trap cleanup EXIT INT TERM
|
|
|
|
echo "== clean clone =="
|
|
echo "source revision: $revision"
|
|
echo "clone: $tmp"
|
|
echo "project: $project; LB port: $port; image: $image"
|
|
if [ "$main_was_running" = "1" ]; then
|
|
echo "stopping the main lab4 stand with data preserved for the check"
|
|
(cd "$root" && make stop >/dev/null)
|
|
fi
|
|
git clone --quiet "$root" "$tmp"
|
|
cd "$tmp"
|
|
test "$(git rev-parse HEAD)" = "$revision"
|
|
|
|
echo "== local configuration with fresh synthetic credentials =="
|
|
./scripts/init-env.sh
|
|
python3 - "$tmp/.env" "$port" "$project" <<'PY'
|
|
import sys
|
|
path, port, project = sys.argv[1], sys.argv[2], sys.argv[3]
|
|
lines = []
|
|
for line in open(path):
|
|
if line.startswith('LB_PORT='):
|
|
line = 'LB_PORT=%s\n' % port
|
|
elif line.startswith('COMPOSE_PROJECT_NAME='):
|
|
line = 'COMPOSE_PROJECT_NAME=%s\n' % project
|
|
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
|
|
|
|
echo "== offline contract and syntax checks =="
|
|
make check
|
|
|
|
echo "== build, migrate and bootstrap the bucket from empty volumes =="
|
|
GIT_REVISION=$revision API_IMAGE=$image 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 'attachments=' || count(*) FROM attachments;" \
|
|
-c "SELECT 'migrations=' || string_agg(version::text, ',' ORDER BY version) FROM schema_migrations;"
|
|
./scripts/compose.sh --env-file .env logs s3-init 2>&1 | grep -E '"created":(true|false)' || true
|
|
./scripts/compose.sh --env-file .env ps --format 'table {{.Name}}\t{{.Status}}'
|
|
|
|
echo "== public and own API 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 =="
|
|
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, bootstrapped and tested; temporary resources removed"
|