#!/bin/sh # nylon installer # Installs the latest nylon release to /usr/local/bin as well as a systemd # unit to enable nylon as a service. opt out with --no-service # Re-running upgrades nylon and restarts the service. Tested on Ubuntu 24.04. set -eu REPO="encodeous/nylon" BIN="/usr/local/bin/nylon" CONFIG_DIR="/etc/nylon" UNIT="/etc/systemd/system/nylon.service" usage() { cat <] [--no-service] [--uninstall] --version Install a specific release, e.g. v0.4.6 (default: latest) --no-service Don't install the systemd service that runs nylon with the configs in $CONFIG_DIR (installed by default) --uninstall Remove the service and binary (keeps $CONFIG_DIR) EOF } info() { echo "==> $*" >&2; } die() { echo "error: $*" >&2 exit 1 } as_root() { if [ "$(id -u)" = 0 ]; then "$@" elif command -v sudo >/dev/null 2>&1; then sudo "$@" else die "need root to run: $*" fi } detect_arch() { case "$(uname -m)" in x86_64 | amd64) ARCH=amd64 ;; aarch64 | arm64) ARCH=arm64 ;; i386 | i686) ARCH=386 ;; *) die "unsupported architecture: $(uname -m)" ;; esac } # resolves latest to a tag resolve_version() { case "$VERSION" in latest) VERSION="$(curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest" | sed -n 's#.*/tag/##p')" [ -n "$VERSION" ] || die "could not determine the latest release" ;; v*) ;; *) VERSION="v$VERSION" ;; esac } installed_version() { if [ -x "$BIN" ]; then "$BIN" version 2>/dev/null | sed -n 's/^Version: //p' fi } download_and_install() { asset="nylon-linux-$ARCH.tar.gz" base="https://github.com/$REPO/releases/download/$VERSION" info "downloading $asset ($VERSION)" curl -fsSL -o "$TMP/$asset" "$base/$asset" || die "failed to download $base/$asset" # check md5 for releases predating this pr (they didn't publish sha256) if curl -fsSL -o "$TMP/sum" "$base/$asset.sha256" 2>/dev/null; then actual="$(sha256sum "$TMP/$asset" | cut -d' ' -f1)" elif curl -fsSL -o "$TMP/sum" "$base/$asset.md5" 2>/dev/null; then actual="$(md5sum "$TMP/$asset" | cut -d' ' -f1)" else die "no checksum published for $asset; refusing to install" fi [ "$(cut -d' ' -f1 <"$TMP/sum")" = "$actual" ] || die "checksum mismatch for $asset" tar -xzf "$TMP/$asset" -C "$TMP" nylon "$TMP/nylon" version >/dev/null 2>&1 || die "the downloaded binary does not run on this system (if $(dirname "$TMP") is mounted noexec, set TMPDIR to another directory)" # atomic replace as_root install -m 0755 "$TMP/nylon" "$BIN.new" as_root mv -f "$BIN.new" "$BIN" info "installed $BIN ($VERSION)" } MARKER="# Managed by nylon's install.sh" service_running() { systemctl is-active --quiet nylon } # unit is only managed if it was installed by this script managed_unit() { grep -qF "$MARKER" "$UNIT" 2>/dev/null } install_service() { cat >"$TMP/unit" <