steid

@jamesgill /

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