#!/usr/bin/env bash # Operator actions of the isolated demonstration stand. # # The stand is a dedicated Compose project (DEMO_PROJECT, lincodex-demo by # default) of docker-compose.yml with docker-compose.demo.yml. Every action # requires DEMO_MODE=isolated and addresses only that project; the ordinary # project (APP_PROJECT) and its volumes are never touched. # # Usage: demo.sh seed|reset|down # seed start the demo project when needed, run the idempotent seed, and # print the inventory of its records # reset check that the demo database holds no account outside the # synthetic domain, discard the demo project with its volumes, # start it again, and seed # down stop the demo project and keep its volumes # # Environment (deploy/Makefile sets it): # DEMO_MODE must be "isolated" # DEMO_PROJECT Compose project of the demo stand; contains "demo" and # differs from APP_PROJECT # APP_PROJECT the ordinary project, only compared against # ENV_FILE root env file with the stand secrets # DEMO_ENV_FILE demo env file with DEMO_ACCOUNT_PASSWORD # DEMO_HTTP_PORT host port of the demo frontend # DEMO_PUBLIC_URL public origin of the demo stand # DEMO_SUBNET, DEMO_FRONTEND_IP pinned network of the demo project # DEMO_ACTOR operator: that the seed records # DOCKER docker CLI set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) compose_file="$root/deploy/docker/docker-compose.yml" demo_file="$root/deploy/docker/docker-compose.demo.yml" docker_cli="${DOCKER:-docker}" demo_label='org.lincodex.demo-mode' ready_attempts=120 fail() { printf 'demo: %s\n' "$1" >&2 exit 2 } if [[ "$#" -ne 1 ]]; then fail 'usage: demo.sh seed|reset|down' fi action=$1 case "$action" in seed | reset | down) ;; *) fail 'usage: demo.sh seed|reset|down' ;; esac # The guards run before any Docker command. if [[ "${DEMO_MODE:-}" != isolated ]]; then fail "refusing to $action: set DEMO_MODE=isolated; the demo targets act only on an isolated demo stand" fi project="${DEMO_PROJECT:-lincodex-demo}" ordinary_project="${APP_PROJECT:-app}" if [[ ! "$project" =~ ^[a-z0-9][a-z0-9_-]*$ || "$project" != *demo* ]]; then fail "refusing to $action: DEMO_PROJECT must be a Compose project name that contains 'demo'" fi if [[ "$project" == "$ordinary_project" ]]; then fail "refusing to $action: DEMO_PROJECT must differ from the ordinary project APP_PROJECT=$ordinary_project" fi env_file="${ENV_FILE:-$root/.env}" demo_env_file="${DEMO_ENV_FILE:-$root/deploy/demo/demo.env.example}" [[ -f "$env_file" ]] || fail "missing $env_file; copy .env.example to .env and replace the placeholder passwords" [[ -f "$demo_env_file" ]] || fail "missing demo env file $demo_env_file" actor="${DEMO_ACTOR:-operator:demo-seed}" [[ "$actor" =~ ^operator:[a-z0-9][a-z0-9._-]*$ ]] || fail 'DEMO_ACTOR must be operator:' port="${DEMO_HTTP_PORT:-8090}" [[ "$port" =~ ^[1-9][0-9]{0,4}$ ]] || fail 'DEMO_HTTP_PORT must be a port number' public_url="${DEMO_PUBLIC_URL:-http://localhost:$port}" subnet="${DEMO_SUBNET:-172.29.84.0/24}" frontend_ip="${DEMO_FRONTEND_IP:-172.29.84.10}" # The shell values override the env files, so the root .env cannot point the # demo project at the ordinary project's names, network, or port. compose() { APP_PROJECT="$project" APP_NETWORK="${project}_default" APP_STORAGE_NETWORK="${project}_storage" \ APP_HTTP_PORT="$port" APP_SUBNET="$subnet" APP_FRONTEND_IP="$frontend_ip" APP_PUBLIC_URL="$public_url" \ DEMO_MODE=isolated "$docker_cli" compose \ --env-file "$env_file" --env-file "$demo_env_file" \ -p "$project" -f "$compose_file" -f "$demo_file" "$@" } volume_exists() { "$docker_cli" volume inspect "$1" >/dev/null 2>&1 } # A volume carries the demo label only when the demo overlay created it, so a # volume of an ordinary stand under this name is never adopted or discarded. check_volumes() { local volume label for volume in "${project}_postgres_data" "${project}_seaweedfs_data" "${project}_kafka_data"; do if volume_exists "$volume"; then label=$("$docker_cli" volume inspect --format "{{ index .Labels \"$demo_label\" }}" "$volume") if [[ "$label" != isolated ]]; then fail "refusing to $action: volume $volume was not created by the demo overlay" fi fi done } start_stand() { compose up -d --build --wait --wait-timeout 300 local attempt response for ((attempt = 1; attempt <= ready_attempts; attempt++)); do response=$(compose exec -T frontend wget -q -O - http://backend:8080/readyz 2>/dev/null || true) if [[ "$response" == '{"status":"ready"}' ]]; then return 0 fi sleep 1 done fail "the backend of $project did not become ready" } run_command() { compose --profile demo-seed run --rm --build demo-seed "$@" } # The inventory after the seed lets two runs be compared line by line. seed() { start_stand run_command seed --actor "$actor" run_command inventory printf 'demo: the stand %s serves %s; the password is DEMO_ACCOUNT_PASSWORD of %s\n' \ "$project" "$public_url" "$demo_env_file" } check_volumes case "$action" in seed) seed ;; reset) if volume_exists "${project}_postgres_data"; then start_stand if ! run_command check-reset; then fail "reset refused for $project; nothing was deleted" fi fi compose down --volumes --remove-orphans seed ;; down) compose down ;; esac