Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · packaging/install.sh

Line attribution on main · 481 line(s)

16a8be36
Keenan Nemetz
13d ago
1 #!/bin/sh
a4a6d1e9
Keenan Nemetz
12d ago
2 # Install Vole.
16a8be36
Keenan Nemetz
13d ago
3 #
4 # The release publishes this file as install.sh and serves it at
a4a6d1e9
Keenan Nemetz
12d ago
5 # /<repository>/channels/<channel>/install.sh. On Linux it detects the
6 # distribution, installs the release key and a package source, and then hands the
7 # install to apt, dnf, or pacman, so package and repository signature checks stay
8 # with the package manager. On macOS there is no package manager to hand off to,
9 # so it installs the release binary itself after checking it against the
10 # release's SHA256SUMS.
16a8be36
Keenan Nemetz
13d ago
11 #
12 # The key is pinned twice: its SHA-256 and its fingerprint. Both belong to
13 # packaging/keys/vole-release.asc, which sign-release.py publishes verbatim, so
14 # a host that serves a different key is refused before any file is written.
15 # Rotating the release key means updating the two constants below in the same
16 # change; scripts/check-install.sh fails when they disagree with the committed
17 # key.
18 #
19 # Run with --help for the options. This file is POSIX sh and is tested by
20 # scripts/check-install.sh in every push.
21 set -eu
22
23 VOLE_KEY_SHA256=f484ed987d8be0c3ef4aa3f16e5e0f14e12310cc65a3f8899aa70d72f86004ca
24 VOLE_KEY_FPR=01A6F610DBD731DD3C2321C52D74BC712CE88523
25
26 BASE=https://rodent.software/vole
27 CHANNEL=stable
28 PIN=
29 REPO_ONLY=0
30 DRY_RUN=0
31 ROOT=/
32 KEY_FILE=
33
34 PROG=vole-install
35
36 usage() {
37 cat <<'EOF'
a4a6d1e9
Keenan Nemetz
12d ago
38 Install Vole.
16a8be36
Keenan Nemetz
13d ago
39
40 Usage: install.sh [options]
41
42 Options:
43 --channel <name> follow this release channel (default: stable)
44 --version <tag> install a pinned release, for example v0.2.0
a4a6d1e9
Keenan Nemetz
12d ago
45 --repo-only write the key and repository, do not install (Linux)
16a8be36
Keenan Nemetz
13d ago
46 --dry-run print the plan, change nothing
47 --yes accepted for non-interactive callers
a4a6d1e9
Keenan Nemetz
12d ago
48 --root <dir> write configuration under <dir> (Linux), or install the
49 binary under <dir>/usr/local (macOS); does not use sudo
50 --key <file> use a local release key instead of downloading it (Linux)
16a8be36
Keenan Nemetz
13d ago
51 -h, --help show this help
52
a4a6d1e9
Keenan Nemetz
12d ago
53 On Linux the script supports Debian and Ubuntu, Fedora, RHEL, Rocky, and
54 AlmaLinux, and Arch Linux, and hands the install to the package manager. On any
55 other distribution it stops and points at the manual steps. On macOS it installs
56 the release binary into /usr/local/bin when run as root, and into ~/.local/bin
57 otherwise, after checking it against the release's SHA256SUMS.
16a8be36
Keenan Nemetz
13d ago
58 EOF
59 }
60
61 log() {
62 printf '%s: %s\n' "$PROG" "$*"
63 }
64
65 die() {
66 printf '%s: %s\n' "$PROG" "$*" >&2
67 exit 1
68 }
69
70 fetch() {
71 if command -v curl >/dev/null 2>&1; then
72 curl -fsSL --retry 3 --retry-delay 2 -o "$2" "$1"
73 elif command -v wget >/dev/null 2>&1; then
74 wget -q -O "$2" "$1"
75 else
76 die "curl or wget is required to download $1"
77 fi
78 }
79
80 sha256() {
81 if command -v sha256sum >/dev/null 2>&1; then
82 sha256sum "$1" | awk '{print $1}'
83 elif command -v shasum >/dev/null 2>&1; then
84 shasum -a 256 "$1" | awk '{print $1}'
85 elif command -v openssl >/dev/null 2>&1; then
86 openssl dgst -sha256 -r "$1" | awk '{print $1}'
87 else
88 die "sha256sum, shasum, or openssl is required to verify the release key"
89 fi
90 }
91
92 while [ "$#" -gt 0 ]; do
93 case "$1" in
94 --channel)
95 [ "$#" -ge 2 ] || die "--channel needs a value"
96 [ -n "$2" ] || die "--channel needs a value"
97 CHANNEL=$2
98 shift 2
99 ;;
100 --channel=*)
101 CHANNEL=${1#*=}
102 [ -n "$CHANNEL" ] || die "--channel needs a value"
103 shift
104 ;;
105 --version)
106 [ "$#" -ge 2 ] || die "--version needs a value"
107 [ -n "$2" ] || die "--version needs a value"
108 PIN=$2
109 shift 2
110 ;;
111 --version=*)
112 PIN=${1#*=}
113 [ -n "$PIN" ] || die "--version needs a value"
114 shift
115 ;;
116 --repo-only)
117 REPO_ONLY=1
118 shift
119 ;;
120 --dry-run)
121 DRY_RUN=1
122 shift
123 ;;
124 --yes | -y)
125 shift
126 ;;
127 --root)
128 [ "$#" -ge 2 ] || die "--root needs a value"
129 [ -n "$2" ] || die "--root needs a value"
130 ROOT=$2
131 shift 2
132 ;;
133 --root=*)
134 ROOT=${1#*=}
135 [ -n "$ROOT" ] || die "--root needs a value"
136 shift
137 ;;
138 --key)
139 [ "$#" -ge 2 ] || die "--key needs a value"
140 [ -n "$2" ] || die "--key needs a value"
141 KEY_FILE=$2
142 shift 2
143 ;;
144 --key=*)
145 KEY_FILE=${1#*=}
146 [ -n "$KEY_FILE" ] || die "--key needs a value"
147 shift
148 ;;
149 -h | --help)
150 usage
151 exit 0
152 ;;
153 *)
154 die "unknown option: $1 (try --help)"
155 ;;
156 esac
157 done
158
159 case "$CHANNEL" in
160 '' | *[!A-Za-z0-9._-]*)
161 die "invalid channel name: $CHANNEL"
162 ;;
163 esac
164
165 if [ -n "$PIN" ]; then
166 case "$PIN" in
167 v*) ;;
168 *) PIN="v$PIN" ;;
169 esac
170 case "$PIN" in
171 *[!A-Za-z0-9._-]*)
172 die "invalid release version: $PIN"
173 ;;
174 esac
175 BASE_URL="$BASE/releases/$PIN/artifacts"
176 else
177 BASE_URL="$BASE/channels/$CHANNEL"
178 fi
179
180 case "$ROOT" in
181 /) ;;
182 /*) ROOT=${ROOT%/} ;;
183 *) die "--root must be an absolute path" ;;
184 esac
185 [ -n "$ROOT" ] || ROOT=/
186 if [ "$ROOT" = / ]; then
187 ETC=/etc
188 else
189 ETC="$ROOT/etc"
190 fi
191 OS_RELEASE="$ETC/os-release"
192
193 if [ -n "$KEY_FILE" ]; then
194 KEY_SOURCE=$KEY_FILE
195 else
196 KEY_SOURCE="$BASE_URL/vole-release-key.asc"
197 fi
198
199 detect() {
a4a6d1e9
Keenan Nemetz
12d ago
200 case "$(uname -s)" in
201 Darwin)
202 FAMILY=darwin
203 DISTRO=macOS
204 ARCH=$(uname -m)
205 # The release names its macOS binaries after Go's GOARCH, not after
206 # uname's spelling of the same architecture.
207 case "$ARCH" in
208 arm64) ;;
209 x86_64 | amd64) ARCH=amd64 ;;
210 *) die "unsupported architecture: $ARCH" ;;
211 esac
212 return
213 ;;
214 Linux) ;;
215 *)
216 die "unsupported operating system '$(uname -s)'; this script installs Vole on Linux and macOS"
217 ;;
218 esac
16a8be36
Keenan Nemetz
13d ago
219 if [ ! -r "$OS_RELEASE" ]; then
220 die "cannot read $OS_RELEASE; this script needs a Linux distribution that provides it"
221 fi
222 # shellcheck disable=SC1090,SC1091
223 . "$OS_RELEASE"
224 DISTRO=${ID:-unknown}
225 ids=" ${ID:-} ${ID_LIKE:-} "
226 case "$ids" in
227 *" debian "* | *" ubuntu "*)
228 FAMILY=apt
229 ;;
230 *" fedora "* | *" rhel "* | *" centos "* | *" rocky "* | *" almalinux "*)
231 FAMILY=dnf
232 ;;
233 *" arch "*)
234 FAMILY=pacman
235 ;;
236 *)
237 die "unsupported distribution '$DISTRO' (ID_LIKE='${ID_LIKE:-}'); add the repository by hand: https://vole.rodent.software/install/#add-the-repository-by-hand"
238 ;;
239 esac
240 ARCH=$(uname -m)
241 case "$ARCH" in
242 x86_64 | amd64 | aarch64 | arm64) ;;
243 *) die "unsupported architecture: $ARCH" ;;
244 esac
245 }
246
247 hint() {
248 case "$FAMILY" in
249 apt) echo "apt-get update && apt-get install vole" ;;
250 dnf) echo "dnf install vole" ;;
251 pacman) echo "pacman -Syu vole" ;;
252 esac
253 }
254
a4a6d1e9
Keenan Nemetz
12d ago
255 # macOS has no package manager to hand the work to, so the script installs the
256 # binary itself. Running as root installs for the machine; otherwise it installs
257 # for the account. --root puts it under a directory instead, which is what an
258 # image build or a test wants.
259 prepare_macos() {
260 [ "$FAMILY" = darwin ] || return 0
261 ASSET="vole-darwin-$ARCH"
262 if [ "$ROOT" != / ]; then
263 DEST="$ROOT/usr/local/bin"
264 elif [ "$(id -u)" -eq 0 ]; then
265 DEST=/usr/local/bin
266 else
267 [ -n "${HOME:-}" ] || die "HOME is not set; run as root to install into /usr/local/bin"
268 DEST="$HOME/.local/bin"
269 fi
270 }
271
272 # A Homebrew installation owns its own copy of the binary, and two copies shadow
273 # each other in PATH order, so this defers to it instead of writing a second one.
274 # A --root install is building an image or running a test, not this machine, so it
275 # is left alone.
276 homebrew_managed() {
277 [ "$FAMILY" = darwin ] || return 1
278 [ "$ROOT" = / ] || return 1
279 command -v brew >/dev/null 2>&1 || return 1
280 brew list --formula vole >/dev/null 2>&1
281 }
282
16a8be36
Keenan Nemetz
13d ago
283 plan() {
284 if [ -n "$PIN" ]; then
285 log "would install release $PIN from $BASE_URL"
286 else
287 log "would follow channel $CHANNEL at $BASE_URL"
288 fi
289 log "detected $DISTRO ($FAMILY) on $ARCH"
a4a6d1e9
Keenan Nemetz
12d ago
290 case "$FAMILY" in
291 darwin)
292 if [ "$REPO_ONLY" = 1 ]; then
293 log "would refuse --repo-only: macOS installs the binary, not a repository"
294 return 0
295 fi
296 if homebrew_managed; then
297 log "would leave the Homebrew installation alone; upgrade with: brew upgrade vole"
298 return 0
299 fi
300 log "would install $ASSET into $DEST"
301 log "would check it against $BASE_URL/SHA256SUMS"
302 return 0
303 ;;
304 esac
16a8be36
Keenan Nemetz
13d ago
305 log "release key: $KEY_SOURCE (sha256 $VOLE_KEY_SHA256, fingerprint $VOLE_KEY_FPR)"
306 case "$FAMILY" in
307 apt)
308 log "would write $ETC/apt/keyrings/vole.asc and $ETC/apt/sources.list.d/vole.list"
309 ;;
310 dnf)
311 log "would write $ETC/pki/rpm-gpg/RPM-GPG-KEY-vole and $ETC/yum.repos.d/vole.repo"
312 ;;
313 pacman)
314 log "would add [vole] to $ETC/pacman.conf and import the key into the pacman keyring"
315 ;;
316 esac
317 if [ "$REPO_ONLY" = 1 ] || [ "$ROOT" != / ]; then
318 log "would stop after the repository; install with: $(hint)"
319 else
320 log "would run: $(hint)"
321 fi
322 }
323
324 verify_key() {
325 actual=$(sha256 "$1")
326 if [ "$actual" != "$VOLE_KEY_SHA256" ]; then
327 die "release key checksum mismatch: expected $VOLE_KEY_SHA256, got $actual"
328 fi
329 if command -v gpg >/dev/null 2>&1; then
330 fingerprint=$(gpg --batch --with-colons --import-options show-only --import "$1" 2>/dev/null |
331 awk -F: '$1 == "fpr" {print $10; exit}')
332 if [ "$fingerprint" != "$VOLE_KEY_FPR" ]; then
333 die "release key fingerprint mismatch: expected $VOLE_KEY_FPR, got ${fingerprint:-none}"
334 fi
335 fi
336 }
337
338 configure_apt() {
339 install -d -m 0755 "$ETC/apt/keyrings" "$ETC/apt/sources.list.d"
340 install -m 0644 "$tmp/key.asc" "$ETC/apt/keyrings/vole.asc"
341 printf 'deb [signed-by=/etc/apt/keyrings/vole.asc] %s/apt ./\n' "$BASE_URL" >"$tmp/vole.list"
342 install -m 0644 "$tmp/vole.list" "$ETC/apt/sources.list.d/vole.list"
343 }
344
345 configure_dnf() {
346 install -d -m 0755 "$ETC/pki/rpm-gpg" "$ETC/yum.repos.d"
347 install -m 0644 "$tmp/key.asc" "$ETC/pki/rpm-gpg/RPM-GPG-KEY-vole"
348 {
349 printf '[vole]\n'
350 printf 'name=Vole\n'
351 printf 'baseurl=%s/yum/\n' "$BASE_URL"
352 printf 'enabled=1\n'
353 printf 'gpgcheck=1\n'
354 printf 'repo_gpgcheck=1\n'
355 printf 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-vole\n'
356 } >"$tmp/vole.repo"
357 install -m 0644 "$tmp/vole.repo" "$ETC/yum.repos.d/vole.repo"
358 if [ "$ROOT" = / ]; then
359 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-vole
360 fi
361 }
362
363 configure_pacman() {
364 install -d -m 0755 "$ETC"
365 if [ ! -e "$ETC/pacman.conf" ]; then
366 : >"$ETC/pacman.conf"
367 fi
368 if ! grep -q '^\[vole\]$' "$ETC/pacman.conf"; then
369 {
370 printf '\n[vole]\n'
371 printf 'Server = %s/pacman/$arch\n' "$BASE_URL"
372 printf 'SigLevel = Required DatabaseRequired\n'
373 } >>"$ETC/pacman.conf"
374 fi
375 if [ "$ROOT" = / ]; then
376 [ -d /etc/pacman.d/gnupg ] || pacman-key --init
377 pacman-key --add "$tmp/key.asc"
378 pacman-key --lsign-key "$VOLE_KEY_FPR"
379 fi
380 }
381
382 install_package() {
383 case "$FAMILY" in
384 apt)
385 apt-get update
386 DEBIAN_FRONTEND=noninteractive apt-get install -y vole
387 ;;
388 dnf)
389 dnf install -y vole
390 ;;
391 pacman)
392 pacman -Syu --needed --noconfirm vole
393 ;;
394 esac
395 }
396
a4a6d1e9
Keenan Nemetz
12d ago
397 # macOS: no package manager is left to verify anything, so the binary is checked
398 # against the release's own SHA256SUMS instead. That list is fetched from the host
399 # that served the binary, so it authenticates the transfer but not the release;
d1f44d83
Keenan Nemetz
21h ago
400 # the detached signature over it is what docs/install.md#verify-a-direct-download documents, and checking that
a4a6d1e9
Keenan Nemetz
12d ago
401 # needs gpg, which a stock macOS does not ship.
402 install_binary() {
403 if ! fetch "$BASE_URL/$ASSET" "$tmp/vole"; then
404 die "cannot download $ASSET from $BASE_URL; this release may not publish macOS binaries"
405 fi
406 if ! fetch "$BASE_URL/SHA256SUMS" "$tmp/SHA256SUMS"; then
407 die "cannot download SHA256SUMS from $BASE_URL, so the download cannot be checked"
408 fi
409 want=$(awk -v asset="$ASSET" '$2 == asset { print $1; exit }' "$tmp/SHA256SUMS")
410 [ -n "$want" ] || die "SHA256SUMS does not name $ASSET"
411 actual=$(sha256 "$tmp/vole")
412 [ "$actual" = "$want" ] || die "$ASSET checksum mismatch: expected $want, got $actual"
413 install -d -m 0755 "$DEST"
414 install -m 0755 "$tmp/vole" "$DEST/vole"
415 log "installed $DEST/vole"
416 case ":$PATH:" in
417 *":$DEST:"*) ;;
418 *) log "add $DEST to PATH to run vole" ;;
419 esac
420 log "checked against $BASE_URL/SHA256SUMS; for a signature check see https://vole.rodent.software/packaging/#verify-a-direct-download"
421 }
422
16a8be36
Keenan Nemetz
13d ago
423 detect
a4a6d1e9
Keenan Nemetz
12d ago
424 prepare_macos
16a8be36
Keenan Nemetz
13d ago
425
426 if [ "$DRY_RUN" = 1 ]; then
427 plan
428 exit 0
429 fi
430
a4a6d1e9
Keenan Nemetz
12d ago
431 if [ "$FAMILY" = darwin ]; then
432 if [ "$REPO_ONLY" = 1 ]; then
433 die "--repo-only configures a package manager; macOS installs the binary"
434 fi
435 if homebrew_managed; then
436 die "vole is installed by Homebrew; upgrade it with: brew upgrade vole"
437 fi
438 elif [ "$ROOT" = / ] && [ "$(id -u)" -ne 0 ]; then
16a8be36
Keenan Nemetz
13d ago
439 die "run as root, for example: curl -fsSL $BASE_URL/install.sh | sudo sh"
440 fi
441 if [ -n "$KEY_FILE" ] && [ ! -r "$KEY_FILE" ]; then
442 die "cannot read key file: $KEY_FILE"
443 fi
444
445 tmp=$(mktemp -d)
446 trap 'rm -rf "$tmp"' 0
447
a4a6d1e9
Keenan Nemetz
12d ago
448 if [ "$FAMILY" = darwin ]; then
449 install_binary
450 exit 0
451 fi
452
16a8be36
Keenan Nemetz
13d ago
453 if [ -n "$KEY_FILE" ]; then
454 cp "$KEY_FILE" "$tmp/key.asc"
455 else
456 fetch "$KEY_SOURCE" "$tmp/key.asc"
457 fi
458 verify_key "$tmp/key.asc"
459
460 case "$FAMILY" in
461 apt) configure_apt ;;
462 dnf) configure_dnf ;;
463 pacman) configure_pacman ;;
464 esac
465
466 if [ "$REPO_ONLY" = 1 ] || [ "$ROOT" != / ]; then
467 log "repository configured from $BASE_URL"
468 if [ "$ROOT" != / ]; then
469 log "wrote files under $ROOT; --root does not install packages"
470 else
471 log "install with: $(hint)"
472 fi
473 exit 0
474 fi
475
476 install_package
477 if command -v vole >/dev/null 2>&1; then
478 log "installed vole $(vole version 2>/dev/null || true)"
479 else
480 log "installed vole"
481 fi