steid

@jamesgill /

steid/deploy.sh
Bourne Again Shell (bash)9.2 KBCode·Blame·History·Raw
1#!/usr/bin/env bash
2# Deploy a build of Steid to a running instance.
3#
4# ./deploy.sh # build, upload, upgrade, verify
5# ./deploy.sh --skip-build # use whatever is already in dist/
6# ./deploy.sh --host steid --domain jpgill.dev
7# ./deploy.sh --publish # also publish dist/ for strangers to download
8#
9# This is the operator's script, not the stranger's — `install.sh` is what someone
10# else runs to install Steid for the first time. This one assumes an instance
11# already exists and you want it running a newer build.
12#
13# WHY IT DOES MORE THAN scp-AND-RUN. An upgrade replaces the binary of a live
14# service. Three things can go wrong and each is silent: the new build might not
15# boot, the state might not survive, and a mistake leaves nothing to go back to.
16# So it snapshots the database and the current binary first, upgrades, then
17# *checks the site actually works* — and restores if it does not.
18#
19# MIGRATIONS RUN THEMSELVES, ON BOOT, AND THIS SCRIPT CANNOT UNDO THEM. The
20# rollback below restores the binary, never the schema — so a migration that the
21# previous release cannot read leaves a rolled-back instance broken. Expand,
22# deploy, contract: see "Migrations" in plans/architecture.md.
23#
24# WHY --publish IS OFF BY DEFAULT. This box has two jobs: it runs jpgill.dev, and
25# it is the host every `install.sh` downloads from. Those are not the same event.
26# A deploy happens whenever a build is worth running; a publish changes what
27# strangers get, permanently and for a version number that can never be re-cut.
28# Publishing on every deploy would put half-finished builds behind a version
29# other people install, and would publish only the one architecture a deploy
30# happens to have built. So it is opt-in — and, so it stops being forgotten, it
31# is one flag on the command that was going to be run anyway.
32
33set -euo pipefail
34
35HOST="steid" # an ssh alias or user@address
36DOMAIN="jpgill.dev"
37TARGET="x86_64-unknown-linux-gnu"
38SKIP_BUILD=0
39PUBLISH=0
40VERSION=""
41
42# Where the artefacts are served from on the box. NOT under /var/lib/steid: that
43# directory is the instance's state and its whole backup surface — the snapshot
44# below tars it on every deploy, so release tarballs living there would be copied
45# into every backup forever. It is also mode 750 steid:steid, and Caddy runs as
46# `caddy`, so serving from inside it would mean loosening the directory that
47# protects steid.db. Caddy's `handle_path` blocks point here; see deploy/Caddyfile.
48DIST_DIR="/srv/steid-dist"
49
50die() { echo "deploy.sh: $*" >&2; exit 1; }
51say() { echo "==> $*"; }
52
53while [ $# -gt 0 ]; do
54 case "$1" in
55 --host) HOST="${2:?--host needs a value}"; shift 2 ;;
56 --domain) DOMAIN="${2:?--domain needs a value}"; shift 2 ;;
57 --target) TARGET="${2:?--target needs a triple}"; shift 2 ;;
58 --version) VERSION="${2:?--version needs a value}"; shift 2 ;;
59 --skip-build) SKIP_BUILD=1; shift ;;
60 --publish) PUBLISH=1; shift ;;
61 -h|--help) sed -n '2,18p' "$0" | sed 's/^#\{1,2\} \{0,1\}//'; exit 0 ;;
62 *) die "unknown argument: $1" ;;
63 esac
64done
65
66cd "$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)"
67
68[ -n "$VERSION" ] || VERSION="$(grep -m1 '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')"
69NAME="steid-${VERSION}-${TARGET}"
70TARBALL="dist/${NAME}.tar.gz"
71
72# Refuse to deploy a working tree that is not what is committed. The instance
73# should always be running a commit you can point at, or debugging it later means
74# guessing what was on the machine that built it.
75if [ -n "$(git status --porcelain)" ]; then
76 die "working tree is dirty — commit first, so the deployed build has a commit to name"
77fi
78COMMIT="$(git rev-parse --short HEAD)"
79
80if [ "$SKIP_BUILD" = 0 ]; then
81 say "building ${NAME} from ${COMMIT}"
82 ./release.sh --target "$TARGET" --version "$VERSION" >/dev/null
83else
84 say "skipping build (--skip-build)"
85fi
86[ -f "$TARBALL" ] || die "no ${TARBALL} — build it, or drop --skip-build"
87
88say "checking the instance is reachable"
89ssh -o BatchMode=yes "$HOST" true || die "cannot ssh to ${HOST}"
90
91# --- snapshot, so there is something to go back to --------------------------
92
93STAMP="$(date +%Y%m%d-%H%M%S)"
94say "snapshotting state and the current binary (${STAMP})"
95ssh -o BatchMode=yes "$HOST" "
96 set -e
97 sudo mkdir -p /var/backups/steid
98 sudo tar czf /var/backups/steid/state-${STAMP}.tar.gz -C /var/lib steid
99 sudo tar czf /var/backups/steid/binary-${STAMP}.tar.gz -C /opt steid
100 sudo find /var/backups/steid -name '*.tar.gz' -mtime +14 -delete
101"
102
103# --- upgrade ----------------------------------------------------------------
104
105say "uploading"
106scp -q -o BatchMode=yes "$TARBALL" "${TARBALL}.sha256" install.sh "${HOST}:"
107
108say "upgrading"
109ssh -o BatchMode=yes "$HOST" "sudo sh ./install.sh --domain '${DOMAIN}' --tarball './${NAME}.tar.gz'" \
110 | grep -E '^==>|is running' || true
111
112# --- verify, and roll back if it is not actually serving --------------------
113
114say "verifying https://${DOMAIN}"
115OK=0
116for _ in $(seq 1 15); do
117 sleep 2
118 if [ "$(curl -s -o /dev/null -m 10 -w '%{http_code}' "https://${DOMAIN}/healthz")" = "200" ]; then
119 OK=1; break
120 fi
121done
122
123if [ "$OK" = 1 ]; then
124 say "healthy — ${VERSION} (${COMMIT}) is live at https://${DOMAIN}"
125 ssh -o BatchMode=yes "$HOST" "systemctl is-active steid caddy | paste -sd' ' -" | sed 's/^/ steid caddy: /'
126else
127 echo "deploy.sh: the new build is NOT serving. Rolling back." >&2
128 ssh -o BatchMode=yes "$HOST" "
129 set -e
130 sudo systemctl stop steid
131 sudo rm -rf /opt/steid
132 sudo tar xzf /var/backups/steid/binary-${STAMP}.tar.gz -C /opt
133 sudo systemctl start steid
134 "
135 sleep 4
136 ROLLED="$(curl -s -o /dev/null -m 10 -w '%{http_code}' "https://${DOMAIN}/healthz")"
137 echo "deploy.sh: rolled back to the previous binary (healthz ${ROLLED})." >&2
138 echo "deploy.sh: state was NOT restored — the database is untouched by an upgrade," >&2
139 echo " and /var/backups/steid/state-${STAMP}.tar.gz holds it if you need it." >&2
140 exit 1
141fi
142
143# --- publish, so the release the box is running is the one strangers get -----
144
145if [ "$PUBLISH" = 1 ]; then
146 say "publishing v${VERSION} to ${HOST}:${DIST_DIR}"
147
148 # Every target install.sh can ask for, which is what `release.sh --all`
149 # builds. A missing one is not fatal — publishing the architecture you have
150 # beats publishing nothing — but it is called out, because the failure lands
151 # on a stranger as a 404 halfway through an install that looked fine, and it
152 # reads as a broken host rather than a missing file.
153 PUBLISH_TARGETS="x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu"
154 FILES=""
155 for t in $PUBLISH_TARGETS; do
156 f="dist/steid-${VERSION}-${t}.tar.gz"
157 if [ -f "$f" ] && [ -f "${f}.sha256" ]; then
158 FILES="${FILES} ${f} ${f}.sha256"
159 else
160 echo "deploy.sh: no ${f} — anyone whose 'uname -m' picks ${t} gets a 404." >&2
161 echo " Build it first: ./release.sh --target ${t} --version ${VERSION}" >&2
162 fi
163 done
164 [ -n "$FILES" ] || die "nothing to publish for ${VERSION}"
165
166 # Upload to the login user's home, then `sudo install` into place: the served
167 # directory is root-owned and scp cannot elevate. `install` rather than `mv`
168 # so the mode is stated outright instead of inherited from whatever umask the
169 # upload happened to run under — these files are world-readable on purpose.
170 # shellcheck disable=SC2086 # FILES is a deliberate word-split list.
171 scp -q -o BatchMode=yes $FILES install.sh "${HOST}:"
172 REMOTE="set -e; sudo install -d -m 755 -o root -g root ${DIST_DIR}/v${VERSION};"
173 for f in $FILES; do
174 REMOTE="${REMOTE} sudo install -m 644 -o root -g root ./$(basename "$f") ${DIST_DIR}/v${VERSION}/;"
175 done
176 # install.sh sits at the root of the served directory: the URL a stranger is
177 # handed is https://<domain>/install.sh and there is nowhere else for it.
178 REMOTE="${REMOTE} sudo install -m 644 -o root -g root ./install.sh ${DIST_DIR}/install.sh"
179 ssh -o BatchMode=yes "$HOST" "$REMOTE"
180
181 # Fetched back over HTTPS rather than trusted from the filesystem. What
182 # matters is not that the bytes are on the box but that Caddy serves them,
183 # and the two distribution blocks in /etc/caddy/Caddyfile are easy to not
184 # have enabled — a silent 404 there is the failure this flag exists to stop.
185 say "verifying the published URLs"
186 for f in $FILES; do
187 URL="https://${DOMAIN}/jamesgill/repos/steid/releases/v${VERSION}/$(basename "$f")"
188 CODE="$(curl -s -o /dev/null -m 60 -w '%{http_code}' "$URL")"
189 [ "$CODE" = "200" ] || die "published but not served: ${URL} (HTTP ${CODE}).
190 Are the distribution handle_path blocks in /etc/caddy/Caddyfile? See deploy/Caddyfile."
191 echo " ${CODE} ${URL}"
192 done
193 CODE="$(curl -s -o /dev/null -m 30 -w '%{http_code}' "https://${DOMAIN}/install.sh")"
194 [ "$CODE" = "200" ] || die "published but not served: https://${DOMAIN}/install.sh (HTTP ${CODE})"
195 echo " ${CODE} https://${DOMAIN}/install.sh"
196
197 say "published. A stranger now installs this version with:"
198 echo " curl -fsSL https://${DOMAIN}/install.sh | sh -s -- --domain <their-host> --version ${VERSION}"
199fi