#!/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 local demo env file; the example supplies defaults # APP_HTTP_BIND comes from the demo env file so the core bind is ignored # 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 # DEMO_RELEASE_MANIFEST optional repository-relative release manifest; seed # and reset then start and seed the stand from the exact # image IDs and digests it records, without building or # pulling, and check the running images against it # 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" release_script="$root/deploy/scripts/release_manifest.py" docker_cli="${DOCKER:-docker}" demo_label='org.lincodex.demo-mode' ready_attempts=120 fail() { printf 'demo: %s\n' "$1" >&2 exit 2 } read_env_value() { local file=$1 key=$2 line value='' while IFS= read -r line || [[ -n "$line" ]]; do if [[ "$line" == "$key="* ]]; then value=${line#*=} fi done < "$file" printf '%s' "$value" } 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 ordinary_project="${APP_PROJECT:-app}" env_file="${ENV_FILE:-$root/.env}" demo_example_file="$root/deploy/demo/demo.env.example" demo_env_file="${DEMO_ENV_FILE:-$root/deploy/demo/demo.env}" [[ -f "$env_file" ]] || fail "missing $env_file; copy .env.example to .env and replace the placeholder passwords" [[ -f "$demo_example_file" ]] || fail "missing demo env example $demo_example_file" [[ -f "$demo_env_file" ]] || fail "missing demo env file $demo_env_file" configured_project=$(read_env_value "$demo_env_file" DEMO_PROJECT) if [[ -z "$configured_project" && "$demo_env_file" != "$demo_example_file" ]]; then configured_project=$(read_env_value "$demo_example_file" DEMO_PROJECT) fi project="${DEMO_PROJECT:-${configured_project:-lincodex-demo}}" configured_ordinary_project=$(read_env_value "$env_file" APP_PROJECT) 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" || ( -n "$configured_ordinary_project" && "$project" == "$configured_ordinary_project" ) ]]; then fail "refusing to $action: DEMO_PROJECT must differ from the ordinary project APP_PROJECT" fi 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}" release_manifest="${DEMO_RELEASE_MANIFEST:-}" env_files=(--env-file "$env_file" --env-file "$demo_example_file") if [[ "$demo_env_file" != "$demo_example_file" ]]; then env_files+=(--env-file "$demo_env_file") fi compose_files=(-f "$compose_file" -f "$demo_file") # From the current sources by default; pinned by the release override below. up_args=(--build) run_args=(--build) # 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. The demo # env file is loaded last to override core-only settings such as APP_HTTP_BIND. 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_files[@]}" \ -p "$project" "${compose_files[@]}" "$@" } 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 } # The release script pins every service of the stand, the seed included, to # the image IDs and digests of the manifest after checking that the Compose # files and data artifacts still match it. The override lives in a private # temporary directory for this run only. pin_release_images() { override_dir=$(mktemp -d) trap 'rm -rf "$override_dir"' EXIT DOCKER="$docker_cli" PYTHONDONTWRITEBYTECODE=1 python3 "$release_script" demo-override \ --manifest "$release_manifest" --output-dir "$override_dir" || fail "refusing to $action: the release manifest $release_manifest cannot be restored on the demo stand" compose_files+=(-f "$override_dir/release-restore.yml") up_args=(--no-build --pull never) run_args=() } start_stand() { compose up -d "${up_args[@]}" --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 if [[ -n "$release_manifest" ]]; then DOCKER="$docker_cli" PYTHONDONTWRITEBYTECODE=1 python3 "$release_script" verify \ --project "$project" --manifest "$release_manifest" || fail "the running images of $project differ from $release_manifest" fi return 0 fi sleep 1 done fail "the backend of $project did not become ready" } run_command() { compose --profile demo-seed run --rm ${run_args[@]+"${run_args[@]}"} 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 if [[ -n "$release_manifest" && "$action" != down ]]; then pin_release_images fi 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