#!/usr/bin/env bash set -Eeuo pipefail NODE_SOURCE="${1:?Node executable is required}" if ! command -v docker >/dev/null || ! docker compose version >/dev/null 2>&1; then apt-get update -qq DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io docker-compose-v2 systemctl enable --now docker fi if ! command -v ffmpeg >/dev/null; then apt-get update -qq DEBIAN_FRONTEND=noninteractive apt-get install -y ffmpeg fi umask 027 if [ ! -f /etc/prompt.env ]; then "$NODE_SOURCE" <<'NODE' > /etc/prompt.env const {randomBytes}=require('crypto'); const password=randomBytes(32).toString('hex'); console.log(`POSTGRES_PASSWORD=${password}\nPG_PORT=18132\nS3_PORT=18133\nDATABASE_URL=postgresql://prompt:${password}@127.0.0.1:18132/prompt\nS3_ENDPOINT=http://127.0.0.1:18133\nS3_ACCESS_KEY=${randomBytes(16).toString('hex')}\nS3_SECRET_KEY=${randomBytes(32).toString('hex')}\nS3_BUCKET=prompt\nFFMPEG_PATH=/usr/bin/ffmpeg\nFFPROBE_PATH=/usr/bin/ffprobe`); NODE fi chown root:prompt /etc/prompt.env chmod 0640 /etc/prompt.env install -d -m 0755 /opt/prompt/infra install -m 0644 compose.yml /opt/prompt/infra/compose.yml docker compose -p prompt --env-file /etc/prompt.env -f /opt/prompt/infra/compose.yml up -d for attempt in $(seq 1 60); do if docker compose -p prompt --env-file /etc/prompt.env -f /opt/prompt/infra/compose.yml exec -T postgres pg_isready -U prompt -d prompt >/dev/null 2>&1; then break; fi if [ "$attempt" -eq 60 ]; then echo 'PostgreSQL startup timed out'; exit 1; fi sleep 2 done # Additive migrations follow this backup. Keep the most recent 10 database dumps. install -d -o root -g root -m 0700 /var/lib/prompt/backups docker compose -p prompt --env-file /etc/prompt.env -f /opt/prompt/infra/compose.yml exec -T postgres pg_dump -U prompt -d prompt -Fc > "/var/lib/prompt/backups/$(date -u +%Y%m%dT%H%M%S)-${2:-deploy}.dump" find /var/lib/prompt/backups -name '*.dump' -printf '%T@ %p\n' | sort -rn | tail -n +11 | cut -d' ' -f2- | xargs -r rm --