Skip to content

Commit 90690ca

Browse files
Make the setup wizard resumable after a crash
If the wizard fails part-way (a package install, a transient error), re-running it previously meant re-answering every question and regenerating secrets — which would also break a partially written .env. - The wizard now saves all collected answers and generated secrets to a 0600 .wizard-state file once Step 1 completes (and again after the database step), and on re-run offers to resume with that exact configuration, skipping the questions. Every subsequent step is already idempotent, so it picks up where it stopped. - The state file is removed automatically on a successful install (it holds secrets in cleartext) and is gitignored. - Documented resume + the root/app-user behaviour in DEPLOYMENT. Verified: the answer/secret round-trip survives hostile values (backticks, $, quotes).
1 parent 19298bd commit 90690ca

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ storage/
2525
quarantine/
2626
# Project-local PostgreSQL cluster (created by scripts/postgres-local.sh)
2727
/.postgres/
28+
# Setup-wizard resume state (contains secrets; removed on successful install)
29+
/.wizard-state
2830

2931
# Prisma
3032
apps/api/prisma/*.db

‎docs/DEPLOYMENT.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,17 @@ bash scripts/setup-wizard.sh
126126
It asks for your domain(s), admin account, storage path and limits, then offers to
127127
install Node/pnpm/PM2/PostgreSQL/nginx/certbot as needed. Secrets (`MASTER_KEY`,
128128
`SESSION_SECRET`, DB password) are generated for you and written to a `0600` `.env`.
129-
Re-run it any time; it backs up an existing `.env` first. The sections below document
130-
the same steps manually.
129+
Re-run it any time; it backs up an existing `.env` first.
130+
131+
**If it crashes part-way** (e.g. a package install fails), just run it again: it saved
132+
your answers and generated secrets to a `0600` `.wizard-state` file and will offer to
133+
**resume with the same configuration** — every step is idempotent. The state file is
134+
removed automatically once the install completes.
135+
136+
**Running as root:** PostgreSQL refuses to run as root. When you launch the wizard as root
137+
and choose the project-local database, it creates (or reuses) an unprivileged system user
138+
to own and run the cluster, and PM2 drops to that user for the `postgres` process — so the
139+
whole flow works from a root shell.
131140

132141
## Bare-metal with PM2 (dedicated server)
133142

‎scripts/setup-wizard.sh‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,38 @@ say "${BOLD}OpenCoperLock setup wizard${RST} — dedicated-server install (PM2 +
7878
hr
7979
[[ "$PKG" == apt ]] || warn "Non-Debian system detected ($ID_LIKE). Package installs are skipped; install prerequisites yourself."
8080

81+
# ── Resume support ───────────────────────────────────────────────────────────
82+
# All later steps are idempotent, so if the wizard crashes part-way (e.g. a package
83+
# install failed) you can simply re-run it and resume with the exact same configuration
84+
# instead of re-answering everything. Answers + generated secrets are saved (0600) to a
85+
# state file after Step 1 and removed on success.
86+
STATE_FILE="$ROOT_DIR/.wizard-state"
87+
RESUME=false
88+
STATE_VARS=(TOPO WEB_DOMAIN API_DOMAIN APP_URL NEXT_PUBLIC_API_URL SETUP_NGINX SETUP_TLS
89+
WEB_PORT API_PORT ADMIN_EMAIL ADMIN_PASSWORD DB_CHOICE DB_PROVISION DB_NAME DB_USER
90+
DB_PASS APP_USER DATABASE_URL STORAGE_BASE STORAGE_PATH QUARANTINE_PATH QUOTA_GB CAP_GB
91+
DEFAULT_USER_QUOTA_BYTES GLOBAL_STORAGE_CAP_BYTES CLAMAV_ENABLED VIRUSTOTAL_API_KEY
92+
CERTBOT_EMAIL MASTER_KEY SESSION_SECRET)
93+
save_state() {
94+
umask 077
95+
: > "$STATE_FILE"
96+
local v
97+
for v in "${STATE_VARS[@]}"; do printf '%s=%q\n' "$v" "${!v-}" >> "$STATE_FILE"; done
98+
}
99+
if [[ -f "$STATE_FILE" ]]; then
100+
if ask_yn 'An unfinished setup was found. Resume with the saved configuration?' 'Y'; then
101+
# shellcheck disable=SC1090
102+
source "$STATE_FILE"; RESUME=true
103+
ok 'Resuming with the saved configuration (every step is idempotent).'
104+
else
105+
rm -f "$STATE_FILE"; warn 'Starting fresh; previous saved configuration discarded.'
106+
fi
107+
fi
108+
81109
# ─────────────────────────────────────────────────────────────────────────────
82110
# 1. COLLECT ANSWERS (everything up front; the API URL must be known before build)
83111
# ─────────────────────────────────────────────────────────────────────────────
112+
if ! $RESUME; then
84113
info "Step 1 / 8 — Configuration questions"
85114
say "${DIM}Press Enter to accept the [default].${RST}"
86115
say ""
@@ -199,6 +228,10 @@ fi
199228
MASTER_KEY="$(openssl rand -base64 32)"
200229
SESSION_SECRET="$(openssl rand -base64 32)"
201230

231+
# Persist everything so a crash in a later step can be resumed without re-asking.
232+
save_state
233+
fi # end "if ! $RESUME" — questions are skipped when resuming
234+
202235
# ── Summary & confirm ────────────────────────────────────────────────────────
203236
say ""; hr; info "Review"
204237
say " Topology : $([[ $TOPO == 1 ]] && echo 'two subdomains' || { [[ $TOPO == 2 ]] && echo 'single domain + /api' || echo 'local only'; })"
@@ -212,7 +245,9 @@ say " Per-user quota : ${QUOTA_GB} GiB Global cap: ${CAP_GB} GiB"
212245
say " ClamAV / VirusTotal : $([[ $CLAMAV_ENABLED == true ]] && echo on || echo off) / $([[ -n $VIRUSTOTAL_API_KEY ]] && echo set || echo off)"
213246
say " nginx / TLS : $([[ $SETUP_NGINX == true ]] && echo yes || echo no) / $([[ $SETUP_TLS == true ]] && echo yes || echo no)"
214247
hr
215-
ask_yn 'Proceed with these settings?' 'Y' || die 'Aborted by user.'
248+
if ! $RESUME; then
249+
ask_yn 'Proceed with these settings?' 'Y' || die 'Aborted by user.'
250+
fi
216251

217252
# ─────────────────────────────────────────────────────────────────────────────
218253
# 2. PREREQUISITES
@@ -319,6 +354,8 @@ SQL
319354
say ""; info "Step 3 / 8 — Using existing database (skipped provisioning)"
320355
;;
321356
esac
357+
# Capture the (possibly newly chosen) DATABASE_URL so a later crash resumes with it.
358+
save_state
322359

323360
# ─────────────────────────────────────────────────────────────────────────────
324361
# 4. STORAGE DIRECTORIES
@@ -481,6 +518,9 @@ pm2 start ecosystem.config.cjs
481518
pm2 save
482519
warn "To restart OpenCoperLock automatically on reboot, run the command pm2 printed above (pm2 startup)."
483520

521+
# Install succeeded — drop the resume state (it holds secrets in cleartext).
522+
rm -f "$STATE_FILE"
523+
484524
# ── Done ─────────────────────────────────────────────────────────────────────
485525
say ""; hr
486526
ok "OpenCoperLock is installed and running."

0 commit comments

Comments
 (0)