#!/bin/sh # trunkjet quickstart control script (D-1854). Lives next to compose.yaml; docs/manual/quickstart.md. # # ./trunkjet up start (first run: secrets, FoundationDB, Garage; prints the setup link) # ./trunkjet url the address and, while setup is open, the one-time setup link # ./trunkjet inbox the newest mails of the local inbox (sign-in codes) # ./trunkjet status | logs [SERVICE] | stop | down # ./trunkjet backup [FILE] consistent backup of every volume (stops the stack for seconds) # ./trunkjet restore FILE replace all data with a backup (asks first; --yes to skip) # ./trunkjet upgrade [IMAGE] backup, switch to IMAGE (default: the newest of the channel), migrate # ./trunkjet doctor the containers, the server's readiness and the installation checks # ./trunkjet uninstall remove containers AND all data volumes (asks first) # # Settings live in ./.env (created on the first run): TRUNKJET_IMAGE, TRUNKJET_PORT, # TRUNKJET_BIND, TRUNKJET_ORIGIN, TJET_SMTP_RELAY, ... . set -eu HERE=$(cd "$(dirname "$0")" && pwd) cd "$HERE" PROJECT=${TRUNKJET_PROJECT:-trunkjet} say() { printf '%s\n' "$*"; } die() { printf 'trunkjet: %s\n' "$*" >&2; exit 1; } find_compose() { if [ -n "${TRUNKJET_COMPOSE:-}" ]; then COMPOSE=$TRUNKJET_COMPOSE; return; fi if docker compose version >/dev/null 2>&1; then COMPOSE="docker compose" elif command -v podman >/dev/null 2>&1 && podman compose version >/dev/null 2>&1; then COMPOSE="podman compose" else die "needs Docker with the compose plugin (Docker Desktop on macOS/Windows) or Podman with podman-compose" fi } dc() { $COMPOSE -p "$PROJECT" -f "$HERE/compose.yaml" --env-file "$HERE/.env" "$@"; } ensure_env() { [ -f .env ] && return port=${TRUNKJET_PORT:-8640} cat > .env <> trunkjet.log if ! dc up "$@" >> trunkjet.log 2>&1; then tail -n 30 trunkjet.log >&2 die "docker compose up failed (the whole log: ./trunkjet.log)" fi } # tjet-admin inside the tjetd container, with the S3 key it needs (storage checks). admin() { dc exec -T tjetd /bin/sh -c 'set -a; . /var/lib/trunkjet-secrets/s3.env; set +a; exec tjet-admin "$@"' tjet-admin "$@"; } init_fdb() { i=0 until dc exec -T fdb fdbcli --timeout 5 --exec 'status minimal' 2>/dev/null | grep -q 'is available'; do if [ $i -eq 0 ]; then # A never-configured database: create it (single redundancy, ssd engine). Harmless when # it already exists. dc exec -T fdb fdbcli --timeout 20 --exec 'configure new single ssd' >/dev/null 2>&1 || true fi i=$((i + 1)) [ $i -le 90 ] || die "FoundationDB did not become available (./trunkjet logs fdb)" sleep 1 done } init_garage() { i=0 until garage status >/dev/null 2>&1; do i=$((i + 1)); [ $i -le 60 ] || die "Garage did not start (./trunkjet logs garage)"; sleep 1 done node=$(garage status 2>/dev/null | awk '/127.0.0.1:3901/ {print $1; exit}') [ -n "$node" ] || die "Garage reports no node" if ! garage layout show 2>/dev/null | grep -q "$node.*dc1"; then garage layout assign -z dc1 -c "${TRUNKJET_GARAGE_CAPACITY:-100G}" "$node" >/dev/null ver=$(garage layout show 2>/dev/null | awk '/apply --version/ {print $NF; exit}') garage layout apply --version "${ver:-1}" >/dev/null fi garage bucket info trunkjet >/dev/null 2>&1 || garage bucket create trunkjet >/dev/null if ! dc run --rm -T --no-deps init has-s3-key >/dev/null 2>&1; then # The key's secret goes through this pipe only (never an argument or a file on the host). garage key create trunkjet-server | dc run --rm -T --no-deps init store-s3-key >/dev/null fi id=$(dc run --rm -T --no-deps init s3-key-id) garage bucket allow --read --write --owner trunkjet --key "$id" >/dev/null } wait_ready() { i=0 while :; do h=$(docker inspect -f '{{.State.Health.Status}}' "$(dc ps -q tjetd)" 2>/dev/null || echo starting) [ "$h" = healthy ] && return 0 i=$((i + 1)) [ $i -le 150 ] || die "tjetd did not become ready (./trunkjet logs tjetd)" sleep 2 done } setup_open() { origin=$(envval TRUNKJET_ORIGIN) # Through the published port with the canonical Host (127.0.0.1 is an alias that redirects). curl -fsS -X POST -H 'content-type: application/json' -H "origin: $origin" -d '{}' \ -H "host: ${origin#*://}" "http://127.0.0.1:$(envval TRUNKJET_PORT)/v1/auth/web/setup/status" 2>/dev/null | grep -q '"open":true' } print_url() { origin=$(envval TRUNKJET_ORIGIN) say "" say " trunkjet is running: $origin" if setup_open; then say " Create the admin account (this link works once):" say "" say " $(dc exec -T tjetd /usr/bin/tjet-quickstart-init setup-url "$origin")" say "" fi say " Sign-in codes go to the local inbox: ./trunkjet inbox" say " Docs: $origin/docs/quickstart Stop: ./trunkjet stop Backup: ./trunkjet backup" say "" } cmd_up() { ensure_env img=$(envval TRUNKJET_IMAGE) if ! docker image inspect "$img" >/dev/null 2>&1; then say "Pulling $img ..." docker pull "$img" >/dev/null || die "image $img is not available (docs/manual/quickstart.md: build it with Bazel or load it)" fi say "Starting FoundationDB, the object store, the inbox and the proxy..." up -d pod fdb init garage inbox proxy init_fdb init_garage say "Starting the server..." up -d tjetd wait_ready print_url } # The newest mails of the local inbox (Mailpit inside the stack; not published on any port, so # no browser page or other local user can read the codes). cmd_inbox() { n=${1:-3} ids=$(dc exec -T inbox wget -qO- "http://127.0.0.1:8025/api/v1/messages?limit=$n" | tr ',' '\n' | sed -n 's/^.*"ID":"\([A-Za-z0-9]*\)".*$/\1/p') [ -n "$ids" ] || { say "(the inbox is empty)"; return 0; } for id in $ids; do say "----------------------------------------------------------------------" dc exec -T inbox wget -qO- "http://127.0.0.1:8025/view/$id.txt" || true say "" done } backup_name() { printf 'trunkjet-backup-%s.tar.gz' "$(date -u +%Y%m%dT%H%M%SZ)"; } VOLUMES="secrets tjetd-secrets fdb-data garage-meta garage-data state" cmd_backup() { ensure_env out=${1:-backups/$(backup_name)} (umask 077; mkdir -p "$(dirname "$out")") outdir=$(cd "$(dirname "$out")" && pwd) outfile=$(basename "$out") img=$(envval TRUNKJET_IMAGE) running=$(dc ps -q tjetd 2>/dev/null || true) # Stop everything that writes (tjetd, FoundationDB, Garage): a cold, consistent copy. dc stop tjetd garage fdb >/dev/null 2>&1 || true set -- for v in $VOLUMES; do set -- "$@" -v "${PROJECT}_$v:/backup/$v:ro"; done rc=0 # The archive holds the secrets: created 0600 and handed to the calling user (review M1); the # file name is an argument, never part of the shell text (review L1). docker run --rm --network none --user 0:0 "$@" -v "$outdir:/out" --entrypoint /bin/sh "$img" -c \ 'umask 077; cd /backup && tar -czf "/out/.$1.part" $2 && chown "$3" "/out/.$1.part" && mv "/out/.$1.part" "/out/$1"' \ sh "$outfile" "$VOLUMES" "$(id -u):$(id -g)" || rc=$? [ -n "$running" ] && { dc start fdb garage >/dev/null; dc start tjetd >/dev/null; } [ $rc -eq 0 ] || die "backup failed" chmod 600 "$outdir/$outfile" say "backup written: $outdir/$outfile (contains the secrets: keep it private)" } cmd_restore() { [ $# -ge 1 ] || die "usage: ./trunkjet restore FILE [--yes]" file=$1 [ -f "$file" ] || die "no such file: $file" [ "${2:-}" = "--yes" ] || { printf 'This replaces ALL data of this trunkjet with %s. Type yes: ' "$file" read -r a; [ "$a" = yes ] || die "aborted" } ensure_env find_compose indir=$(cd "$(dirname "$file")" && pwd) img=$(envval TRUNKJET_IMAGE) dc down >/dev/null 2>&1 || true set -- for v in $VOLUMES; do docker volume rm "${PROJECT}_$v" >/dev/null 2>&1 || true docker volume create --label com.docker.compose.project="$PROJECT" --label com.docker.compose.volume="$v" "${PROJECT}_$v" >/dev/null set -- "$@" -v "${PROJECT}_$v:/restore/$v" done docker run --rm --network none --user 0:0 "$@" -v "$indir:/in:ro" --entrypoint /bin/sh "$img" -c \ 'cd /restore && tar -xzpf "/in/$1" --same-owner' sh "$(basename "$file")" || die "restore failed" say "restored from $file" cmd_up } cmd_upgrade() { ensure_env new=${1:-$(envval TRUNKJET_IMAGE)} cmd_backup "backups/pre-upgrade-$(backup_name)" if [ -z "${1:-}" ]; then say "Pulling $new ..." docker pull "$new" >/dev/null 2>&1 || say "(no registry copy of $new; using the local image)" fi docker image inspect "$new" >/dev/null 2>&1 || die "image $new is not available" tmp=$(mktemp .env.XXXXXX) grep -v '^TRUNKJET_IMAGE=' .env > "$tmp"; printf 'TRUNKJET_IMAGE=%s\n' "$new" >> "$tmp"; mv "$tmp" .env # Not a rolling upgrade (install.md: stop, upgrade, start), then the schema steps once. dc stop tjetd >/dev/null 2>&1 || true up -d --remove-orphans pod fdb init garage inbox proxy init_fdb up -d tjetd wait_ready admin schema migrate say "upgraded to $new" print_url } find_compose case "${1:-up}" in up|start) cmd_up ;; url) ensure_env; print_url ;; inbox) shift; cmd_inbox "$@" ;; status) dc ps ;; logs) shift; dc logs --tail=200 "$@" ;; stop) dc stop ;; down) dc down ;; backup) shift; cmd_backup "$@" ;; restore) shift; cmd_restore "$@" ;; upgrade) shift; cmd_upgrade "$@" ;; # The readiness checks (FoundationDB, cell link, storage round trip), then the installation # doctor as the operator (no account needed; exit 1 when a check fails). doctor) dc ps; dc exec -T tjetd curl -sS http://127.0.0.1:9640/readyz; echo; admin doctor ;; uninstall) [ "${2:-}" = "--yes" ] || { printf 'Remove trunkjet and ALL its data? Type yes: '; read -r a; [ "$a" = yes ] || die "aborted"; } dc down -v ;; *) sed -n '2,13p' "$0"; exit 2 ;; esac