#!/bin/sh # Installs the SN46 validator (sn46-signer, which also runs the watchdog, and sn46-audit) from a # release of Subnet46/v2-sn46-validator. # # curl -fsSL https://raw.githubusercontent.com/Subnet46/v2-sn46-validator/dev/install.sh | sh # curl -fsSL .../install.sh | sh -s -- --system # /usr/local/bin, systemd units, /etc/sn46 # # --system also creates the `sn46` system user the units run as, makes /etc/sn46 (root:sn46, # 0750), puts the systemd units in /etc/systemd/system and the example configs in # /etc/sn46/*.example.toml; it never enables or starts a unit and never touches a real config. # SN46_VERSION=v0.1.0 picks a release (default: the latest). SN46_BIN_DIR overrides where the # binary goes. SN46_RELEASES points at another copy of the releases page (file:// for tests). # Safe to run again: it replaces what changed and says what it did. set -eu repo=Subnet46/v2-sn46-validator releases=${SN46_RELEASES:-https://github.com/$repo/releases} version=${SN46_VERSION:-latest} say() { printf 'sn46: %s\n' "$*"; } die() { printf 'sn46: error: %s\n' "$*" >&2; exit 1; } has() { command -v "$1" >/dev/null 2>&1; } usage() { cat <<'EOF' usage: install.sh [--system] installs sn46-signer and sn46-audit into ~/.local/bin, the units and example configs into ~/.local/share/sn46 (--system: /usr/local/bin, /etc/systemd/system, /etc/sn46 and the sn46 system user, with sudo) SN46_VERSION=vX.Y.Z a given release instead of the latest SN46_BIN_DIR= another install directory EOF } system=0 for arg; do case $arg in --system) system=1 ;; -h | --help) usage; exit 0 ;; *) die "unknown option: $arg (try --help)" ;; esac done [ "$(uname -s)" = Linux ] || die "only Linux is supported, not $(uname -s)" case $(uname -m) in x86_64 | amd64) target=x86_64-unknown-linux-gnu ;; aarch64 | arm64) target=aarch64-unknown-linux-gnu ;; *) die "no release for $(uname -m): build from source (README)" ;; esac sudo= if [ "$system" = 1 ]; then bin_dir=${SN46_BIN_DIR:-/usr/local/bin} share=/usr/local/share/sn46 if [ "$(id -u)" != 0 ]; then has sudo || die "--system needs root or sudo" sudo=sudo fi else bin_dir=${SN46_BIN_DIR:-$HOME/.local/bin} share=${XDG_DATA_HOME:-$HOME/.local/share}/sn46 fi fetch() { if has curl; then curl -fsSL --proto '=https,file' --proto-redir '=https' -o "$2" "$1" elif has wget; then wget -q -O "$2" "$1" else die "curl or wget is required" fi } sha256() { if has sha256sum; then sha256sum "$1" | cut -d ' ' -f 1 elif has shasum; then shasum -a 256 "$1" | cut -d ' ' -f 1 else die "sha256sum or shasum is required" fi } # put : installs the file unless an identical one is there. changed=0 put() { if [ -f "$3" ] && cmp -s "$2" "$3"; then say "unchanged $3" return fi changed=1 $sudo mkdir -p "$(dirname "$3")" $sudo cp "$2" "$3.sn46-new" $sudo chmod "$1" "$3.sn46-new" $sudo mv -f "$3.sn46-new" "$3" say "installed $3" } tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT trap 'exit 1' INT TERM if [ "$version" = latest ]; then base=$releases/latest/download; else base=$releases/download/$version; fi pkg=sn46-validator-$target asset=$pkg.tar.gz say "downloading $asset ($version) from $base" fetch "$base/$asset" "$tmp/$asset" || die "no $asset in release $version" fetch "$base/SHA256SUMS" "$tmp/SHA256SUMS" || die "no SHA256SUMS in release $version" want=$(awk -v f="$asset" '$2 == f || $2 == "*" f { print $1 }' "$tmp/SHA256SUMS") [ -n "$want" ] || die "$asset is not listed in SHA256SUMS" got=$(sha256 "$tmp/$asset") [ "$got" = "$want" ] || die "sha256 mismatch for $asset: expected $want, got $got" say "sha256 ok $got" tar -xzf "$tmp/$asset" -C "$tmp" for b in sn46-signer sn46-audit; do [ -f "$tmp/$pkg/$b" ] || die "$asset does not hold $b" done installed=$(cat "$tmp/$pkg/VERSION" 2>/dev/null || echo "$version") bins_changed=0 for b in "$tmp/$pkg"/sn46-*; do changed=0 put 755 "$b" "$bin_dir/$(basename "$b")" [ "$changed" = 0 ] || bins_changed=1 done # The user the units run as, and the config directory only it (and root) can read. if [ "$system" = 1 ]; then if id sn46 >/dev/null 2>&1; then say "unchanged user sn46" else has useradd || die "useradd is required to create the sn46 user" nologin=$(command -v nologin || echo /usr/sbin/nologin) $sudo useradd --system --no-create-home --home-dir /var/lib/sn46 --shell "$nologin" sn46 say "created user sn46" fi $sudo mkdir -p /etc/sn46 $sudo chown root:sn46 /etc/sn46 $sudo chmod 750 /etc/sn46 fi # Units name the binaries where this run put them. units_changed=0 for unit in "$tmp/$pkg"/deploy/systemd/*; do [ -f "$unit" ] || continue sed "s|@BINDIR@|$bin_dir|g" "$unit" >"$tmp/unit" if [ "$system" = 1 ]; then dest=/etc/systemd/system; else dest=$share/systemd; fi changed=0 put 644 "$tmp/unit" "$dest/$(basename "$unit")" [ "$changed" = 0 ] || units_changed=1 done # Examples only: signer.toml, audit.toml and the key files are the operator's and never written. for ex in "$tmp/$pkg"/deploy/*.example.toml; do [ -f "$ex" ] || continue if [ "$system" = 1 ]; then dest=/etc/sn46; else dest=$share; fi put 644 "$ex" "$dest/$(basename "$ex")" done say "sn46-validator $installed is in $bin_dir" case ":$PATH:" in *":$bin_dir:"*) ;; *) say "note: $bin_dir is not on PATH; add it: export PATH=\"$bin_dir:\$PATH\"" ;; esac if [ "$system" = 1 ]; then if [ "$units_changed" = 1 ] && has systemctl && [ -d /run/systemd/system ]; then $sudo systemctl daemon-reload say "systemd reloaded; no unit was enabled or started" fi if [ "$bins_changed" = 1 ] && has systemctl; then for u in sn46-signer sn46-watchdog; do if systemctl is-active --quiet "$u" 2>/dev/null; then say "$u is running the old binary: sudo systemctl restart $u" fi done fi say "next: the keys and /etc/sn46/signer.toml and audit.toml from the examples, then" say " sudo systemctl enable --now sn46-signer (docs/OPERATOR.md)" else say "units and example configs are in $share; for a service install: --system" fi