#!/bin/sh # igworker 0.1.2 installer for Linux and macOS. Generated at release time by # scripts/sign-release.sh; do not edit the generated file. # # curl -fsSL https://igcoord.app.vlazic.com/releases/0.1.2/install.sh | sh install or upgrade, start the service # curl -fsSL https://igcoord.app.vlazic.com/releases/0.1.2/install.sh | sh -s uninstall stop the service and remove everything # # The SHA-256 of every binary is written into this script, which is fetched over HTTPS: that is # the trust anchor for the first install (trust on first use). Every later update is verified by # the worker itself against the owner's signing key built into the binary. # # Nothing here needs root. Environment: # IGWORKER_NO_SERVICE=1 install or remove the files only, leave systemd/launchd alone set -eu version='0.1.2' base_url='https://igcoord.app.vlazic.com/releases/0.1.2/' die() { echo "igworker install: $*" >&2; exit 1; } say() { echo "igworker install: $*"; } artifact() { # platform -> "file sha256" case "$1" in darwin-amd64) echo 'igworker_0.1.2_darwin_amd64 0024cb3577e503c4513b2caebdc8292a4bbe1f5621a7f48053809907723864c6' ;; darwin-arm64) echo 'igworker_0.1.2_darwin_arm64 668c5110ab22d8e125b89f0f321e503237175fdb36607ccb7cff693782903921' ;; linux-amd64) echo 'igworker_0.1.2_linux_amd64 03e5f6d7f1ea76b9d07fc3b7197b9d4cb633ea668115f47dfcb0ef26fbaaf198' ;; linux-arm64) echo 'igworker_0.1.2_linux_arm64 bf7064bbbb173438bf12bf6b907558cd515b195cb4822402c3ed03b458c7ef96' ;; linux-armv6) echo 'igworker_0.1.2_linux_armv6 f262da3d8ad4b5de21527bf6aed5c0e30074f0561ad42e161e46fb0a32bf938a' ;; linux-armv7) echo 'igworker_0.1.2_linux_armv7 fb92de3ae73c412fdbb5977570f545c3ce97eded71f6f20d0d80543a7c7a40fd' ;; windows-amd64) echo 'igworker_0.1.2_windows_amd64.exe 3957efd812532f77af8f0211bfc0799debc356aa6d6a45e43d22dcd4ac1bfcc2' ;; *) return 1 ;; esac } platform() { os=$(uname -s) arch=$(uname -m) case "$os" in Linux) os=linux ;; Darwin) os=darwin ;; *) die "unsupported OS $os (use install.ps1 on Windows)" ;; esac case "$arch" in x86_64 | amd64) arch=amd64 # A Terminal running under Rosetta on Apple Silicon reports x86_64. if [ "$os" = darwin ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = 1 ]; then arch=arm64 fi ;; aarch64 | arm64) arch=arm64 ;; # static binary: also fine under a 64-bit kernel with 32-bit userland armv7l | armv8l) arch=armv7 ;; armv6l) arch=armv6 ;; *) die "unsupported CPU $arch" ;; esac echo "$os-$arch" } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 else shasum -a 256 "$1" | cut -d' ' -f1 fi } fetch() { # url file if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$2" "$1" else wget -q -O "$2" "$1" fi } plat=$(platform) case "$plat" in linux-*) home_dir="$HOME/.local/share/igworker" config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/igworker" units="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user" ;; darwin-*) home_dir="$HOME/Library/Application Support/igworker" config_dir="$home_dir" agent="$HOME/Library/LaunchAgents/com.vlazic.igworker.plist" logs="$HOME/Library/Logs/igworker" ;; esac bin="$home_dir/bin" stop_service() { [ -z "${IGWORKER_NO_SERVICE:-}" ] || return 0 case "$plat" in linux-*) systemctl --user disable --now igworker.service 2>/dev/null || true systemctl --user reset-failed igworker.service igworker-rollback.service 2>/dev/null || true ;; darwin-*) launchctl bootout "gui/$(id -u)/com.vlazic.igworker" 2>/dev/null || true ;; esac } uninstall() { stop_service case "$plat" in linux-*) rm -f "$units/igworker.service" "$units/igworker-rollback.service" [ -n "${IGWORKER_NO_SERVICE:-}" ] || systemctl --user daemon-reload || true ;; darwin-*) rm -rf "$agent" "$logs" ;; esac rm -rf "$home_dir" "$config_dir" say "removed igworker, its service, configuration and keys" # Lingering is left as is: the host may use it for other services. } install_service_linux() { mkdir -p "$units" cat >"$units/igworker.service" <<'IGWORKER_EOF' # systemd user unit for igworker: ~/.config/systemd/user/igworker.service # # systemctl --user daemon-reload && systemctl --user enable --now igworker.service # journalctl --user -u igworker # # A user manager only runs while its user is logged in. To keep the worker running on a headless # machine (a Raspberry Pi), the host enables lingering once, with their consent: # # loginctl enable-linger "$USER" # # network-online.target does not exist in a user manager; the worker retries the coordinator itself. [Unit] Description=ig-workers worker StartLimitIntervalSec=600 StartLimitBurst=10 # After repeated crashes during an update trial, put the previous binary back (see that unit). OnFailure=igworker-rollback.service [Service] Type=exec ExecStart=%h/.local/share/igworker/bin/igworker run # Every exit restarts, including 75 (EX_TEMPFAIL) after a self-update or a rollback. Restart=always RestartSec=10 # Stay out of the host's way. CPUQuota, MemoryMax and TasksMax only apply where the user manager # has those cgroup controllers delegated (cgroup v2 with a recent systemd); elsewhere they are # ignored. Nice=10 IOSchedulingClass=idle CPUQuota=50% MemoryMax=300M TasksMax=64 NoNewPrivileges=true # PrivateTmp and other sandboxing need unprivileged user namespaces in a user manager, which some # distributions restrict; they are left out so the unit starts everywhere. [Install] WantedBy=default.target IGWORKER_EOF cat >"$units/igworker-rollback.service" <<'IGWORKER_EOF' # Safety net for a new igworker that crashes before its own trial check can roll it back: # igworker.service starts this unit (OnFailure=) once it hits its start limit. The script restores # the previous binary only while an update is in trial, then restarts the worker; the restored # version notices the failed trial and reports it as rolled back. # # Install next to igworker.service in ~/.config/systemd/user/; it is never enabled by itself. [Unit] Description=ig-workers worker: roll back a failing update [Service] Type=oneshot ExecStart=/bin/sh %h/.local/share/igworker/igworker-rollback.sh IGWORKER_EOF cat >"$home_dir/igworker-rollback.sh" <<'IGWORKER_EOF' #!/bin/sh # Run by igworker-rollback.service when igworker.service has failed. Restores bin/igworker.prev # only while an update is in trial (igworker.update.json says "trial": true); otherwise the failure # has nothing to do with an update and the unit stays failed for a human to look at. # # igworker-rollback.sh the user unit of the install script (~/.local/share/igworker) # igworker-rollback.sh --system the system unit of the .deb and .rpm packages (/var/lib/igworker) set -u if [ "${1:-}" = --system ]; then bin=/var/lib/igworker/bin scope= else bin="$HOME/.local/share/igworker/bin" scope=--user fi if ! grep -q '"trial": true' "$bin/igworker.update.json" 2>/dev/null; then echo "igworker-rollback: no update in trial; leaving the service failed" exit 0 fi if [ ! -f "$bin/igworker.prev" ]; then echo "igworker-rollback: update in trial but no previous binary to restore" >&2 exit 1 fi mv -f "$bin/igworker.prev" "$bin/igworker" echo "igworker-rollback: restored the previous binary" if [ -z "${IGWORKER_NO_SERVICE:-}" ]; then # $scope is empty or one word: unquoted on purpose. systemctl $scope reset-failed igworker.service systemctl $scope start igworker.service fi IGWORKER_EOF [ -z "${IGWORKER_NO_SERVICE:-}" ] || return 0 systemctl --user daemon-reload systemctl --user enable --now igworker.service if [ "$(loginctl show-user "$(id -un)" -p Linger --value 2>/dev/null)" != yes ]; then say "the worker runs while you are logged in. To keep it running after logout and" say "across reboots (headless machines), run once: loginctl enable-linger $(id -un)" fi } install_service_darwin() { mkdir -p "$(dirname "$agent")" "$logs" cat >"$agent.tmp" <<'IGWORKER_EOF' Label com.vlazic.igworker ProgramArguments @HOME@/Library/Application Support/igworker/bin/igworker run RunAtLoad KeepAlive ThrottleInterval 10 ProcessType Background LowPriorityIO Nice 10 StandardOutPath @HOME@/Library/Logs/igworker/igworker.log StandardErrorPath @HOME@/Library/Logs/igworker/igworker.log IGWORKER_EOF # Escape the characters sed treats specially in a replacement. home_esc=$(printf '%s\n' "$HOME" | sed 's/[&|\\]/\\&/g') sed "s|@HOME@|$home_esc|g" "$agent.tmp" >"$agent" rm -f "$agent.tmp" [ -z "${IGWORKER_NO_SERVICE:-}" ] || return 0 # bootout in stop_service returns before launchd has removed the job; retry until it has. tries=0 until launchctl bootstrap "gui/$(id -u)" "$agent" 2>/dev/null; do tries=$((tries + 1)) [ "$tries" -lt 10 ] || die "launchctl bootstrap failed; run: launchctl bootstrap gui/$(id -u) \"$agent\"" sleep 1 done } # write_state starts the update state afresh for the installed version (a reinstall ends any # trial) and keeps the highest manifest sequence seen, at least this release's: a freshly # installed worker then refuses every older manifest, so it cannot be pointed at a version that a # later release revoked or put below its floor. write_state() { state="$bin/igworker.update.json" seq=4 old=$(sed -n 's/.*"max_sequence": *\([0-9][0-9]*\).*/\1/p' "$state" 2>/dev/null || true) if [ -n "$old" ] && [ "$old" -gt "$seq" ]; then seq=$old fi printf '{\n "current": "%s",\n "trial": false,\n "attempts": 0,\n "max_sequence": %s\n}\n' "$version" "$seq" >"$state" chmod 0600 "$state" } install() { art=$(artifact "$plat") || die "release $version has no binary for $plat" file=${art% *} want=${art#* } mkdir -p "$bin" tmp="$bin/igworker.download" trap 'rm -f "$tmp"' EXIT say "downloading $file" fetch "$base_url$file" "$tmp" || die "download of $base_url$file failed" got=$(sha256 "$tmp") [ "$got" = "$want" ] || die "SHA-256 mismatch for $file: got $got, want $want; nothing installed" chmod 0755 "$tmp" "$tmp" --selftest >/dev/null || die "the downloaded binary does not run on this machine ($plat)" stop_service mv -f "$tmp" "$bin/igworker" trap - EXIT write_state case "$plat" in linux-*) install_service_linux ;; darwin-*) install_service_darwin ;; esac say "installed igworker $version ($plat) in $bin" } case "${1:-install}" in install) install ;; uninstall) uninstall ;; *) die "usage: install.sh [install|uninstall]" ;; esac