выполнение лабы на 5
This commit is contained in:
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
SECRETS_DIR="$ROOT/secrets"
|
||||
PASSWORD_FILE="$SECRETS_DIR/postgres_password"
|
||||
DATABASE_URL_FILE="$SECRETS_DIR/database_url"
|
||||
|
||||
if [ -e "$PASSWORD_FILE" ] || [ -e "$DATABASE_URL_FILE" ]; then
|
||||
printf '%s\n' 'Secret files already exist; refusing to overwrite them.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
command -v openssl >/dev/null 2>&1 || {
|
||||
printf '%s\n' 'openssl is required to generate local credentials.' >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
mkdir -p "$SECRETS_DIR"
|
||||
chmod 700 "$SECRETS_DIR"
|
||||
umask 077
|
||||
password=$(openssl rand -hex 32)
|
||||
printf '%s' "$password" > "$PASSWORD_FILE"
|
||||
printf 'postgresql://messenger:%s@postgres:5432/messenger' "$password" > "$DATABASE_URL_FILE"
|
||||
chmod 644 "$PASSWORD_FILE" "$DATABASE_URL_FILE"
|
||||
unset password
|
||||
printf '%s\n' 'Local PostgreSQL secret files created.'
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
|
||||
docker compose --env-file .env restart postgres
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 postgres
|
||||
docker compose --env-file .env restart api load_balancer
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
|
||||
docker compose --env-file .env up -d --scale api=2 --wait --wait-timeout 60
|
||||
containers=$(docker compose --env-file .env ps -q api)
|
||||
if [ -z "$containers" ]; then
|
||||
printf '%s\n' 'No running API containers found.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for container in $containers; do
|
||||
docker stop --time 15 "$container" >/dev/null
|
||||
docker start "$container" >/dev/null
|
||||
attempts=0
|
||||
while [ "$attempts" -lt 40 ]; do
|
||||
state=$(docker inspect --format '{{.State.Health.Status}}' "$container")
|
||||
if [ "$state" = healthy ]; then
|
||||
printf 'PASS: API container %s restarted and is healthy\n' "$container"
|
||||
break
|
||||
fi
|
||||
if [ "$state" = unhealthy ] || [ "$state" = none ]; then
|
||||
printf 'API container %s failed health check (state: %s)\n' "$container" "$state" >&2
|
||||
exit 1
|
||||
fi
|
||||
attempts=$((attempts + 1))
|
||||
sleep 1
|
||||
done
|
||||
if [ "$attempts" -eq 40 ]; then
|
||||
printf 'API container %s did not become healthy in 40 seconds\n' "$container" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
|
||||
target_container=
|
||||
for container in $(docker compose --env-file .env ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
target_container=$container
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$target_container" ]; then
|
||||
printf '%s\n' 'No running API container matches TARGET_INSTANCE_ID.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -n "${FAILOVER_EVIDENCE_FILE:-}" ]; then
|
||||
case "$FAILOVER_EVIDENCE_FILE" in
|
||||
/*) evidence_file=$FAILOVER_EVIDENCE_FILE ;;
|
||||
*) evidence_file="$ROOT/$FAILOVER_EVIDENCE_FILE" ;;
|
||||
esac
|
||||
printf '%s target_instance=%s container=%s\n' \
|
||||
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$TARGET_INSTANCE_ID" "$target_container" \
|
||||
>> "$evidence_file"
|
||||
fi
|
||||
|
||||
docker stop --time 2 "$target_container" >/dev/null
|
||||
Reference in New Issue
Block a user