steid

@jamesgill /

steid/install.sh
Bourne Again Shell (bash)25.6 KBCode·Blame·History·Raw
1#!/bin/sh
2#
3# Steid installer for a fresh Debian/Ubuntu server.
4#
5# curl -fsSL https://.../install.sh | sh -s -- --domain git.example.com
6#
7# It installs Steid to /opt/steid, keeps state in /var/lib/steid, runs it as an
8# unprivileged system user under systemd, and puts Caddy in front of it with an
9# automatic HTTPS certificate for the domain you give.
10#
11# Re-running it is an upgrade: it downloads the requested version, replaces the
12# binary and assets, rewrites the unit and the Caddyfile, and restarts. It never
13# touches /var/lib/steid, and it never overwrites /etc/steid/steid.env once that
14# exists, so anything you have edited there survives.
15#
16# ON `curl | sh`: you are being asked to run a script you have not read, as root,
17# from a URL. That is a real trust decision and "it's convenient" is not an
18# answer to it. Two honest alternatives: download it first and read it
19# (`curl -fsSL … -o install.sh; less install.sh; sh install.sh --domain …`), or
20# follow the manual path in README.md, which is the same dozen commands written
21# out. Nothing here is magic; the script exists to save typing, not to be trusted
22# blindly.
23
24set -eu
25
26# --- WHERE RELEASES COME FROM -----------------------------------------------
27#
28# Live since 0.4.0: jpgill.dev serves the artefacts and this script itself, as
29# static files ahead of the application (see deploy/Caddyfile). `--tarball` is
30# still the bootstrap for the very first instance of a *new* distribution host,
31# and the offline path.
32#
33# The layout expected here, and produced by release.sh, is:
34#
35# ${RELEASE_BASE_URL}/v${VERSION}/steid-${VERSION}-${TARGET}.tar.gz
36# ${RELEASE_BASE_URL}/v${VERSION}/steid-${VERSION}-${TARGET}.tar.gz.sha256
37#
38# Steid is distributed from a Steid instance rather than from a code-hosting
39# service, which is the point of the project rather than a flourish. The cost is
40# discovery: nobody stumbles across it. That is a marketing problem, not a
41# technical dependency, and a mirror can solve it later without this URL moving.
42# The *project's* distribution host — a constant, and NOT the `--domain` the
43# person running this installs onto. Everyone downloads Steid from here; each
44# installer then runs their own instance on their own hostname.
45#
46# Scoped under the repository rather than a root-level /releases: Steid serves
47# profiles at /{handle}, so a root path would squat its own namespace. This is
48# also exactly where a real release feature would put these files, so published
49# links survive that feature landing.
50RELEASE_BASE_URL="${STEID_RELEASE_BASE_URL:-https://jpgill.dev/jamesgill/repos/steid/releases}"
51
52# The version to install. Pinned rather than "latest" because there is no
53# redirect to resolve "latest" against, and a pinned default makes the upgrade
54# path explicit: `--version 0.5.0`.
55VERSION="${STEID_VERSION:-0.5.0}"
56# ----------------------------------------------------------------------------
57
58# The build target to fetch. musl is preferred — one static binary that does not
59# care which glibc the host has — but whether Steid builds against musl at all is
60# still being established, so the choice is a variable rather than a fact.
61# Override with --flavour gnu if the published artefacts are glibc.
62# gnu, matching what release.sh builds. musl was tried and rejected: it fails on
63# `ring` with Debian's musl-gcc wrapper, and — the decisive part — musl buys a
64# binary with no runtime dependencies while Steid hard-requires `git` on PATH, so
65# the portability cannot be used. THIS MUST AGREE WITH release.sh: they were
66# briefly out of step and the symptom was a confusing "checksum mismatch",
67# because the installer was looking for a musl tarball that was never built.
68FLAVOUR="gnu"
69
70DOMAIN=""
71WWW=0
72FORCE_CADDYFILE=0
73# A local artefact to install instead of downloading one. This exists because of
74# a bootstrap: the very first instance is what will *serve* the releases, so at
75# that moment there is nowhere to download from. It doubles as the offline and
76# air-gapped path.
77TARBALL=""
78PORT="3000"
79# Scheduled backups are OFF unless asked for. A timer that appears without being
80# asked for is a surprise, and this one writes a full copy of the instance to
81# disk every night — which is exactly what somebody with a small volume needs to
82# have agreed to. Omitting the flag on a later run leaves whatever is already
83# installed alone, the way the Caddyfile and the env file are left alone;
84# `--backups off` is how you actually remove it.
85BACKUPS=""
86INSTALL_DIR="/opt/steid"
87STATE_DIR="/var/lib/steid"
88CONF_DIR="/etc/steid"
89STEID_USER="steid"
90INSTALL_CADDY=1
91
92die() { echo "install.sh: $*" >&2; exit 1; }
93say() { echo "==> $*"; }
94
95usage() {
96 cat >&2 <<'USAGE'
97Usage: install.sh --domain <hostname> [options]
98
99 --tarball <path> install from a local tarball instead of downloading.
100 Needed for the first install, which has nowhere to
101 download from yet, and for offline installs.
102 --www also serve www.<hostname>, redirecting it to the bare
103 hostname. Requires a www DNS record pointing here —
104 without one Caddy retries a certificate it cannot get.
105 --force-caddyfile overwrite an existing /etc/caddy/Caddyfile. Off by
106 default so an upgrade cannot silently discard your edits.
107 --domain <hostname> the public hostname, e.g. git.example.com. Its DNS must
108 already point at this machine or the certificate cannot
109 be issued. Required.
110 --version <v> release to install (default: the pinned one above)
111 --flavour <musl|gnu> which build to fetch (default: gnu — see below; musl
112 is not built, and asking for it gets a 404)
113 --backups daily install a systemd timer that runs `steid backup` into
114 /var/lib/steid/backups every night, keeps the most
115 recent 14, and tells the settings page when it last
116 ran. Off unless you ask. `--backups off` removes it.
117 These stay on the same machine — see the note at the
118 end for copying them somewhere else.
119 --port <n> loopback port Steid listens on (default: 3000)
120 --no-caddy install and run Steid but do not touch Caddy. Only for
121 putting your own TLS-terminating proxy in front. Steid
122 has no TLS of its own; without a proxy, access tokens
123 cross the network in cleartext.
124 -h, --help this
125USAGE
126 exit "${1:-0}"
127}
128
129# --- arguments --------------------------------------------------------------
130
131while [ $# -gt 0 ]; do
132 case "$1" in
133 --domain) DOMAIN="${2:-}"; [ -n "$DOMAIN" ] || die "--domain needs a hostname"; shift 2 ;;
134 --tarball) TARBALL="${2:-}"; [ -n "$TARBALL" ] || die "--tarball needs a path"; shift 2 ;;
135 --www) WWW=1; shift ;;
136 --force-caddyfile) FORCE_CADDYFILE=1; shift ;;
137 --version) VERSION="${2:-}"; [ -n "$VERSION" ] || die "--version needs a value"; shift 2 ;;
138 --flavour) FLAVOUR="${2:-}"; [ -n "$FLAVOUR" ] || die "--flavour needs a value"; shift 2 ;;
139 --port) PORT="${2:-}"; [ -n "$PORT" ] || die "--port needs a number"; shift 2 ;;
140 --backups) BACKUPS="${2:-}"; [ -n "$BACKUPS" ] || die "--backups needs daily or off"; shift 2 ;;
141 --no-caddy) INSTALL_CADDY=0; shift ;;
142 -h|--help) usage 0 ;;
143 *) echo "install.sh: unknown argument: $1" >&2; usage 1 ;;
144 esac
145done
146
147# Fail early and loudly rather than half-installing. Everything below this point
148# assumes these hold.
149[ "$(id -u)" = "0" ] || die "must run as root (try: sudo sh install.sh --domain …)"
150
151if [ "$INSTALL_CADDY" = 1 ]; then
152 [ -n "$DOMAIN" ] || die "--domain is required. Caddy needs a real hostname to
153 obtain a certificate for, and Steid has no TLS of its own. If you are putting
154 your own proxy in front, pass --no-caddy."
155fi
156
157if [ -n "$DOMAIN" ]; then
158 case "$DOMAIN" in
159 *[!A-Za-z0-9.-]*|-*|.*|*.) die "'$DOMAIN' does not look like a hostname" ;;
160 esac
161fi
162
163case "$PORT" in
164 ''|*[!0-9]*) die "--port must be a number" ;;
165esac
166
167case "$FLAVOUR" in
168 musl|gnu) ;;
169 *) die "--flavour must be 'musl' or 'gnu'" ;;
170esac
171
172case "$BACKUPS" in
173 ''|daily|off) ;;
174 *) die "--backups must be 'daily' or 'off'" ;;
175esac
176
177# Where the timer writes, how many it keeps, and the file it touches afterwards.
178# Steid reads only the marker's modification time, so this and the settings page
179# cannot drift into disagreeing about a format.
180BACKUP_DIR="${STATE_DIR}/backups"
181BACKUP_KEEP=14
182BACKUP_MARKER="${BACKUP_DIR}/.last-backup"
183
184case "${TARBALL:+local}${RELEASE_BASE_URL}" in
185 local*) : ;; # installing from a file; the download URL is irrelevant
186 *REPLACE-ME*) die "RELEASE_BASE_URL is still the placeholder. Edit the top of
187 this script (or set STEID_RELEASE_BASE_URL) to point at real release artefacts,
188 or pass --tarball to install from a local file." ;;
189esac
190
191command -v systemctl >/dev/null 2>&1 || die "no systemd here; follow the manual path in README.md"
192command -v apt-get >/dev/null 2>&1 || die "this installer only knows apt (Debian/Ubuntu).
193 The manual path in README.md works on anything with systemd."
194
195case "$(uname -m)" in
196 x86_64|amd64) ARCH="x86_64" ;;
197 aarch64|arm64) ARCH="aarch64" ;;
198 *) die "unsupported architecture: $(uname -m). Only x86_64 and aarch64 are built." ;;
199esac
200
201TARGET="${ARCH}-unknown-linux-${FLAVOUR}"
202NAME="steid-${VERSION}-${TARGET}"
203URL="${RELEASE_BASE_URL}/v${VERSION}/${NAME}.tar.gz"
204
205say "installing Steid ${VERSION} (${TARGET}) for ${DOMAIN:-<no domain>}"
206
207# --- prerequisites ----------------------------------------------------------
208
209# git is not optional and not a runtime nicety: Steid shells out to `git init
210# --bare` to create a repository and to `git http-backend` to serve every clone
211# and push. Without it the install succeeds and the first repository fails.
212say "installing prerequisites (git, curl, ca-certificates)"
213export DEBIAN_FRONTEND=noninteractive
214apt-get update -qq
215apt-get install -y -qq --no-install-recommends git curl ca-certificates
216
217# --- download ---------------------------------------------------------------
218
219TMP="$(mktemp -d)"
220# shellcheck disable=SC2064 # $TMP is expanded now on purpose: it never changes.
221trap "rm -rf '$TMP'" EXIT INT TERM
222
223if [ -n "$TARBALL" ]; then
224 [ -f "$TARBALL" ] || die "--tarball: no such file: ${TARBALL}"
225 say "installing from ${TARBALL}"
226 cp "$TARBALL" "${TMP}/${NAME}.tar.gz"
227
228 # A checksum beside a local file is verified when present, but not demanded:
229 # whoever passes --tarball already chose the bytes, so refusing to proceed
230 # without a .sha256 would block the bootstrap this option exists for.
231 if [ -f "${TARBALL}.sha256" ]; then
232 say "verifying checksum"
233 # Compared by value, not with `sha256sum -c`: that matches on the filename
234 # recorded inside the .sha256, which need not be what the file is called
235 # by the time someone passes it here.
236 EXPECTED="$(cut -d" " -f1 < "${TARBALL}.sha256")"
237 ACTUAL="$(sha256sum < "${TMP}/${NAME}.tar.gz" | cut -d" " -f1)"
238 [ "$EXPECTED" = "$ACTUAL" ] \
239 || die "checksum mismatch — ${TARBALL} does not match its .sha256"
240 else
241 say "no ${TARBALL}.sha256 beside it; installing unverified"
242 fi
243else
244 say "downloading ${URL}"
245 curl -fsSL "$URL" -o "${TMP}/${NAME}.tar.gz" \
246 || die "download failed. Is version ${VERSION} published for ${TARGET}?"
247 curl -fsSL "${URL}.sha256" -o "${TMP}/${NAME}.tar.gz.sha256" \
248 || die "checksum file missing next to the tarball; refusing to install unverified"
249
250 say "verifying checksum"
251 ( cd "$TMP" && sha256sum -c "${NAME}.tar.gz.sha256" >/dev/null ) \
252 || die "checksum mismatch — the download is corrupt or tampered with"
253fi
254
255tar -xzf "${TMP}/${NAME}.tar.gz" -C "$TMP"
256[ -x "${TMP}/${NAME}/steid" ] || die "tarball has no steid binary at ${NAME}/steid"
257# The bundle must ship and must land beside the binary: AssetBundle::load() walks
258# up from the executable looking for assets/manifest.toml and the process exits
259# at startup without it.
260[ -f "${TMP}/${NAME}/assets/manifest.toml" ] || die "tarball has no assets/manifest.toml"
261
262# --- user and directories ---------------------------------------------------
263
264if ! id "$STEID_USER" >/dev/null 2>&1; then
265 say "creating system user ${STEID_USER}"
266 # --home is the state directory: git wants a HOME, and giving it the one
267 # directory the service can write keeps that from being a surprise later.
268 useradd --system --home-dir "$STATE_DIR" --shell /usr/sbin/nologin "$STEID_USER"
269fi
270
271mkdir -p "$INSTALL_DIR" "$STATE_DIR" "$CONF_DIR"
272# State is exactly two things: the SQLite database file and the repository
273# directory. Both live here, and together they are the entire backup surface.
274mkdir -p "${STATE_DIR}/repos"
275chown -R "${STEID_USER}:${STEID_USER}" "$STATE_DIR"
276chmod 750 "$STATE_DIR"
277
278# --- install files ----------------------------------------------------------
279
280# Stop before replacing the binary: overwriting a running executable in place
281# fails with ETXTBSY, and a half-swapped install/assets pair would serve stale
282# hashed CSS until the next restart anyway.
283if systemctl is-active --quiet steid 2>/dev/null; then
284 say "stopping steid for the upgrade"
285 systemctl stop steid
286fi
287
288say "installing to ${INSTALL_DIR}"
289install -m 0755 "${TMP}/${NAME}/steid" "${INSTALL_DIR}/steid"
290rm -rf "${INSTALL_DIR}/assets"
291cp -R "${TMP}/${NAME}/assets" "${INSTALL_DIR}/assets"
292if [ -f "${TMP}/${NAME}/README.md" ]; then
293 cp "${TMP}/${NAME}/README.md" "${INSTALL_DIR}/README.md"
294fi
295chown -R root:root "$INSTALL_DIR"
296# Read-only to the service user on purpose: Steid never writes here.
297chmod -R a+rX "$INSTALL_DIR"
298
299# --- configuration ----------------------------------------------------------
300
301# Written once and then left alone, so an upgrade cannot silently revert a
302# setting someone deliberately changed.
303if [ ! -f "${CONF_DIR}/steid.env" ]; then
304 say "writing ${CONF_DIR}/steid.env"
305 cat > "${CONF_DIR}/steid.env" <<EOF
306# Steid configuration. Restart after editing: systemctl restart steid
307
308# All state lives under ${STATE_DIR}. SQLite writes -wal and -shm siblings, so
309# the directory must be writable, not just the file.
310STEID_DATABASE_URL=sqlite:${STATE_DIR}/steid.db?mode=rwc
311STEID_DATA_DIR=${STATE_DIR}/repos
312
313# HOST and PORT are read by the web framework itself and are deliberately not
314# STEID_-prefixed. Loopback only: Caddy is the way in, and binding 0.0.0.0 would
315# expose plain HTTP — and therefore access tokens in cleartext — to the internet.
316HOST=127.0.0.1
317PORT=${PORT}
318
319# Caddy sits in front and appends the address it accepted the connection from to
320# X-Forwarded-For, so the rate limiters may trust that header to tell one caller
321# from another. Turn this OFF if you ever put Steid on a public port directly:
322# with nothing appending the header a caller can invent one per request and buy
323# itself a fresh rate-limit budget each time.
324STEID_TRUSTED_PROXY=true
325
326# The address this instance answers on, used by \`steid reset-password\` to print a
327# whole link. Every link built while serving is derived from the request instead,
328# so this is read by the command line and nothing else.
329STEID_PUBLIC_URL=https://${DOMAIN}
330
331# Outgoing mail, for password reset. Both are needed or neither does anything:
332# without them the forgot-password page says so and points at
333# \`steid reset-password <handle>\`, which prints a one-time link on this machine.
334# The URL carries the submission password, which is why this file is mode 640. Quote
335# any value containing spaces or angle brackets.
336# STEID_SMTP_URL=smtps://user:password@smtp.example.com:465
337# STEID_MAIL_FROM="Steid <steid@${DOMAIN}>"
338
339# Deliberately absent: STEID_INSECURE_COOKIES. It strips Secure from the session
340# cookie and exists only for plain-HTTP local development. Setting it here would
341# hand out a session cookie that any network hop can read.
342EOF
343 chmod 640 "${CONF_DIR}/steid.env"
344 chown "root:${STEID_USER}" "${CONF_DIR}/steid.env"
345else
346 say "keeping existing ${CONF_DIR}/steid.env"
347fi
348
349say "writing /etc/systemd/system/steid.service"
350cat > /etc/systemd/system/steid.service <<EOF
351# Managed by install.sh. Re-running the installer rewrites this file.
352[Unit]
353Description=Steid
354After=network-online.target
355Wants=network-online.target
356
357[Service]
358Type=simple
359User=${STEID_USER}
360Group=${STEID_USER}
361WorkingDirectory=${INSTALL_DIR}
362ExecStart=${INSTALL_DIR}/steid
363EnvironmentFile=${CONF_DIR}/steid.env
364Environment=HOME=${STATE_DIR}
365Restart=on-failure
366RestartSec=2s
367
368NoNewPrivileges=true
369PrivateTmp=true
370PrivateDevices=true
371ProtectSystem=strict
372ProtectHome=true
373ProtectKernelTunables=true
374ProtectKernelModules=true
375ProtectControlGroups=true
376RestrictSUIDSGID=true
377RestrictNamespaces=true
378LockPersonality=true
379RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
380ReadWritePaths=${STATE_DIR}
381
382[Install]
383WantedBy=multi-user.target
384EOF
385
386systemctl daemon-reload
387systemctl enable --quiet steid
388say "starting steid"
389systemctl restart steid
390
391# --- scheduled backups ------------------------------------------------------
392
393if [ "$BACKUPS" = "daily" ]; then
394 say "installing the nightly backup timer"
395
396 mkdir -p "$BACKUP_DIR"
397 chown "${STEID_USER}:${STEID_USER}" "$BACKUP_DIR"
398 # 700: an archive here is every row and every private repository in one file.
399 chmod 700 "$BACKUP_DIR"
400
401 # The env file is never rewritten, so the marker is appended rather than
402 # written with the rest — and only once, so re-running is idempotent.
403 if ! grep -q '^STEID_BACKUP_MARKER=' "${CONF_DIR}/steid.env"; then
404 cat >> "${CONF_DIR}/steid.env" <<EOF
405
406# Added by install.sh --backups daily. Steid reads this file's modification
407# time to show the last automatic backup on the settings page, and nothing else
408# about it. Removing the line makes the page say "none configured".
409STEID_BACKUP_MARKER=${BACKUP_MARKER}
410EOF
411 fi
412
413 cat > /etc/systemd/system/steid-backup.service <<EOF
414# Managed by install.sh --backups daily.
415[Unit]
416Description=Steid backup
417# The backup is taken through SQLite's own VACUUM INTO, so it is consistent with
418# the service running — this deliberately does not stop Steid.
419After=steid.service
420
421[Service]
422Type=oneshot
423User=${STEID_USER}
424Group=${STEID_USER}
425WorkingDirectory=${INSTALL_DIR}
426EnvironmentFile=${CONF_DIR}/steid.env
427Environment=HOME=${STATE_DIR}
428ExecStart=${INSTALL_DIR}/steid backup ${BACKUP_DIR}
429# Pruning and the marker run only if the backup succeeded, which is the point of
430# ExecStartPost: a failed run must not look like a successful one on the page.
431# The \`;\` before touch is deliberate — an empty glob must not fail the unit.
432ExecStartPost=/bin/sh -c 'cd ${BACKUP_DIR} && ls -1t steid-*.tar.gz 2>/dev/null | tail -n +$((BACKUP_KEEP + 1)) | xargs -r rm -f; touch ${BACKUP_MARKER}'
433
434NoNewPrivileges=true
435PrivateTmp=true
436PrivateDevices=true
437ProtectSystem=strict
438ProtectHome=true
439ProtectKernelTunables=true
440ProtectKernelModules=true
441ProtectControlGroups=true
442RestrictSUIDSGID=true
443RestrictNamespaces=true
444LockPersonality=true
445RestrictAddressFamilies=AF_UNIX
446ReadWritePaths=${STATE_DIR}
447EOF
448
449 cat > /etc/systemd/system/steid-backup.timer <<'EOF'
450# Managed by install.sh --backups daily.
451[Unit]
452Description=Nightly Steid backup
453
454[Timer]
455OnCalendar=daily
456# Spread across the hour so a fleet of instances does not all gzip themselves at
457# once, and catch up after a reboot rather than skipping the night.
458RandomizedDelaySec=1h
459Persistent=true
460
461[Install]
462WantedBy=timers.target
463EOF
464
465 systemctl daemon-reload
466 systemctl enable --quiet --now steid-backup.timer
467elif [ "$BACKUPS" = "off" ]; then
468 if [ -f /etc/systemd/system/steid-backup.timer ]; then
469 say "removing the backup timer (archives in ${BACKUP_DIR} are left alone)"
470 systemctl disable --quiet --now steid-backup.timer 2>/dev/null || true
471 rm -f /etc/systemd/system/steid-backup.timer /etc/systemd/system/steid-backup.service
472 systemctl daemon-reload
473 fi
474fi
475
476# --- caddy ------------------------------------------------------------------
477
478if [ "$INSTALL_CADDY" = 1 ]; then
479 # Whether Caddy was here *before* this run decides whether /etc/caddy/Caddyfile
480 # is somebody's configuration or the one the .deb ships. It has to be sampled
481 # here, because thirty lines later the package has been installed either way.
482 CADDY_WAS_INSTALLED=1
483 if ! command -v caddy >/dev/null 2>&1; then
484 CADDY_WAS_INSTALLED=0
485 say "installing Caddy from its official apt repository"
486 apt-get install -y -qq --no-install-recommends debian-keyring debian-archive-keyring apt-transport-https gnupg
487 curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
488 | gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
489 curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
490 > /etc/apt/sources.list.d/caddy-stable.list
491 apt-get update -qq
492 apt-get install -y -qq caddy
493 else
494 say "Caddy already installed"
495 fi
496
497 # NOT rewritten on upgrade. This used to be overwritten every run, which
498 # silently discarded operator edits — a www redirect added by hand vanished on
499 # the next install and took the site's www hostname with it, with nothing in the
500 # output to say so. The env file was already treated this way; the two now agree.
501 #
502 # CADDY_WAS_INSTALLED is the whole point of the second condition. The Caddy
503 # package ships its own /etc/caddy/Caddyfile — a placeholder site on :80 — so
504 # on a first install the file always exists by the time we look, and "keep
505 # what is there" quietly meant "keep the placeholder". The stranger's install
506 # then succeeded, Steid ran, and the domain served Caddy's welcome page with
507 # nothing anywhere saying why. Found in a container running the published
508 # one-liner, which is the only way it shows up: an upgrade never hits it.
509 if [ -f /etc/caddy/Caddyfile ] && [ "$FORCE_CADDYFILE" = 0 ] && [ "$CADDY_WAS_INSTALLED" = 1 ]; then
510 say "keeping existing /etc/caddy/Caddyfile (--force-caddyfile to replace)"
511 # And say so when that file cannot possibly be serving Steid, rather than
512 # leaving the operator to find out from a browser.
513 if ! grep -q "127.0.0.1:${PORT}" /etc/caddy/Caddyfile; then
514 echo "install.sh: WARNING — /etc/caddy/Caddyfile does not proxy 127.0.0.1:${PORT}," >&2
515 echo " so ${DOMAIN} will not reach Steid. Add a reverse_proxy to it (see" >&2
516 echo " deploy/Caddyfile in the source tree), or re-run with --force-caddyfile" >&2
517 echo " to have this installer write a fresh one." >&2
518 fi
519 else
520 say "writing /etc/caddy/Caddyfile for ${DOMAIN}"
521 mkdir -p /etc/caddy
522
523 WWW_BLOCK=""
524 if [ "$WWW" = 1 ]; then
525 WWW_BLOCK="
526# www redirects to the bare hostname so there is one canonical URL.
527www.${DOMAIN} {
528 redir https://${DOMAIN}{uri} permanent
529}"
530 fi
531
532 cat > /etc/caddy/Caddyfile <<EOF
533# Written by install.sh on first install only. Later runs leave it alone, so edits
534# here survive an upgrade. Use --force-caddyfile to have the installer replace it.
535#
536# Steid has no TLS of its own (Topcoat 0.5 ships none), and it authenticates git
537# over HTTP Basic — so without this proxy every push would send a personal access
538# token in cleartext. Caddy obtains and renews the certificate automatically,
539# provided ${DOMAIN} resolves here and ports 80 and 443 are open.
540${DOMAIN} {
541 reverse_proxy 127.0.0.1:${PORT} {
542 # Git's smart HTTP is a streaming protocol in both directions; buffering
543 # it turns a clone into a long silence and can stall negotiation.
544 flush_interval -1
545 }
546}
547${WWW_BLOCK}
548EOF
549 fi
550
551 systemctl enable --quiet caddy
552 systemctl reload caddy 2>/dev/null || systemctl restart caddy
553fi
554
555# --- report -----------------------------------------------------------------
556
557# A moment for the service to either come up or fall over, so the message below
558# reflects reality rather than optimism.
559sleep 2
560if ! systemctl is-active --quiet steid; then
561 echo >&2
562 echo "install.sh: steid is installed but not running. Look at:" >&2
563 echo " journalctl -u steid -n 50 --no-pager" >&2
564 exit 1
565fi
566
567if [ "$INSTALL_CADDY" = 1 ]; then
568 BASE_URL="https://${DOMAIN}"
569else
570 BASE_URL="http://127.0.0.1:${PORT} (put your own TLS proxy in front of this)"
571fi
572
573# Said only when a timer was installed, and said with the offsite caveat
574# attached: a nightly archive that never leaves the machine survives a mistake
575# and does not survive the machine.
576if [ -f /etc/systemd/system/steid-backup.timer ]; then
577 BACKUP_REPORT=" Backups nightly into ${BACKUP_DIR}, keeping ${BACKUP_KEEP}
578 systemctl list-timers steid-backup.timer
579 Take one now: ${INSTALL_DIR}/steid backup ${BACKUP_DIR}
580
581 !! These sit on this machine's disk. Copy them somewhere else — any
582 !! rclone remote will do, as a second timer or from your laptop:
583 !! rclone sync ${BACKUP_DIR} remote:steid-backups
584"
585else
586 BACKUP_REPORT=" Backups none scheduled. Re-run with --backups daily, or take one by
587 hand: ${INSTALL_DIR}/steid backup /var/backups
588"
589fi
590
591cat <<EOF
592
593Steid ${VERSION} is running.
594
595 Service systemctl status steid
596 Logs journalctl -u steid -f
597 Config ${CONF_DIR}/steid.env
598 State ${STATE_DIR} (the database and the repos — back up this directory)
599${BACKUP_REPORT}
600Next: claim the instance.
601
602 The setup token is printed to the log at startup, and ONLY while the instance
603 is unclaimed. It is held in memory, so every restart mints a new one:
604
605 journalctl -u steid --no-pager | tail -n 30
606
607 Then open ${BASE_URL}/auth/setup and paste it in.
608
609To upgrade later, re-run this script with a newer --version. It replaces the
610binary and assets and leaves ${STATE_DIR} untouched.
611EOF