Newer
Older
status / install.sh
#!/usr/bin/env bash
# Installer for status-status
# Usage:
#   curl -fsSL https://raw.githubusercontent.com/dimonyga/status-status/main/install.sh | bash
#   curl -fsSL .../install.sh | bash -s -- --version v1.2.3 --prefix /usr/local/bin
#
# On systems with systemd, also installs unit files to /lib/systemd/system and
# sysconfig templates to /etc/sysconfig (without overwriting existing ones).
# Disable with --no-systemd or STATUS_NO_SYSTEMD=1.
set -euo pipefail

REPO="${STATUS_REPO:-TestSender/status}"
VERSION="${STATUS_VERSION:-latest}"
PREFIX="${STATUS_PREFIX:-/usr/local/bin}"
NO_SYSTEMD="${STATUS_NO_SYSTEMD:-0}"
BIN_NAME="status"

UNITS=(status-server.service status-agent.service status-exporter.service)
SYSCONFIGS=(sysconfig-server sysconfig-agent sysconfig-exporter)

while [ $# -gt 0 ]; do
  case "$1" in
    --version)     VERSION="$2"; shift 2 ;;
    --prefix)      PREFIX="$2";  shift 2 ;;
    --repo)        REPO="$2";    shift 2 ;;
    --no-systemd)  NO_SYSTEMD=1; shift ;;
    -h|--help)
      sed -n '2,10p' "$0"; exit 0 ;;
    *) echo "unknown option: $1" >&2; exit 1 ;;
  esac
done

err() { echo "error: $*" >&2; exit 1; }

if command -v curl >/dev/null 2>&1; then
  DL="curl -fsSL"
elif command -v wget >/dev/null 2>&1; then
  DL="wget -qO-"
else
  err "need curl or wget"
fi

os="$(uname -s | tr '[:upper:]' '[:lower:]')"
[ "$os" = "linux" ] || err "unsupported OS: $os (linux only)"

raw_arch="$(uname -m)"
case "$raw_arch" in
  x86_64|amd64)        suffix="linux-x86_64" ;;
  i386|i686|x86)       suffix="linux-x86" ;;
  aarch64|arm64)       suffix="linux-aarch64" ;;
  armv7l|armv6l|arm)   suffix="linux-arm" ;;
  mips)                suffix="linux-mips" ;;
  *) err "unsupported arch: $raw_arch" ;;
esac

if [ "$VERSION" = "latest" ]; then
  tag="$($DL "https://api.github.com/repos/$REPO/releases/latest" \
    | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -n1)"
  [ -n "$tag" ] || err "failed to resolve latest release"
else
  tag="$VERSION"
fi

asset="${BIN_NAME}-${suffix}"
bin_url="https://github.com/${REPO}/releases/download/${tag}/${asset}"
# systemd units and sysconfig templates are taken from the repo at the same tag
raw_base="https://raw.githubusercontent.com/${REPO}/${tag}"

echo "Installing ${BIN_NAME} ${tag} (${suffix}) -> ${PREFIX}/${BIN_NAME}"

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

$DL "$bin_url" > "$tmp/$BIN_NAME" || err "download failed: $bin_url"
[ -s "$tmp/$BIN_NAME" ] || err "empty download from $bin_url"
chmod +x "$tmp/$BIN_NAME"

SUDO=""
maybe_sudo() {
  # pick SUDO once for any privileged write below
  if [ -z "${SUDO+x}" ] || [ "$SUDO" = "" ]; then
    if [ "$(id -u)" -ne 0 ]; then
      if command -v sudo >/dev/null 2>&1; then SUDO="sudo"
      else err "root privileges required (install sudo or run as root)"
      fi
    fi
  fi
}

if [ ! -w "$PREFIX" ]; then maybe_sudo; fi
$SUDO mkdir -p "$PREFIX"
$SUDO install -m 0755 "$tmp/$BIN_NAME" "$PREFIX/$BIN_NAME"
echo "Installed binary: $PREFIX/$BIN_NAME"

# --- systemd units + sysconfig -------------------------------------------
install_systemd() {
  [ "$NO_SYSTEMD" = "1" ] && { echo "systemd install skipped (--no-systemd)"; return; }
  command -v systemctl >/dev/null 2>&1 || { echo "systemd not detected, skipping unit install"; return; }

  maybe_sudo

  unit_dir="/lib/systemd/system"
  [ -d "$unit_dir" ] || unit_dir="/etc/systemd/system"
  sysconf_dir="/etc/sysconfig"

  $SUDO mkdir -p "$unit_dir" "$sysconf_dir"

  for u in "${UNITS[@]}"; do
    $DL "$raw_base/$u" > "$tmp/$u" || err "download failed: $raw_base/$u"
    [ -s "$tmp/$u" ] || err "empty unit: $u"
    # point ExecStart at the prefix we actually installed to
    sed -i "s|/usr/bin/${BIN_NAME}|${PREFIX}/${BIN_NAME}|g" "$tmp/$u"
    $SUDO install -m 0644 "$tmp/$u" "$unit_dir/$u"
    echo "Installed unit:   $unit_dir/$u"
  done

  for s in "${SYSCONFIGS[@]}"; do
    dest="$sysconf_dir/status-${s#sysconfig-}"
    if $SUDO test -e "$dest"; then
      echo "Kept existing:    $dest"
      continue
    fi
    $DL "$raw_base/$s" > "$tmp/$s" || err "download failed: $raw_base/$s"
    [ -s "$tmp/$s" ] || err "empty sysconfig: $s"
    $SUDO install -m 0640 "$tmp/$s" "$dest"
    echo "Installed config: $dest"
  done

  # create 'status' system user referenced by the units, if missing
  if ! getent passwd status >/dev/null 2>&1; then
    $SUDO useradd -r -s /sbin/nologin -d /dev/null -c "status-status service" status \
      && echo "Created user:     status"
  fi

  $SUDO systemctl daemon-reload || true

  cat <<EOF

Next steps:
  sudo \$EDITOR /etc/sysconfig/status-server      # or status-agent / status-exporter
  sudo systemctl enable --now status-server       # pick the role(s) you want
EOF
}

install_systemd