#!/bin/sh # Install the built binary and a systemd unit on this host, then (re)start the service. # Runs as the user who should own the terminals (their login shell, their sudo rights); # needs sudo only for the copy into /usr/local/bin and the unit install. # # HAMLAB_USER user the daemon runs as (default: the invoking user) # PREFIX binary directory (default: /usr/local/bin) set -eu cd "$(dirname "$0")/.." USER_NAME="${HAMLAB_USER:-${SUDO_USER:-$(id -un)}}" GROUP_NAME="$(id -gn "$USER_NAME")" HOME_DIR="$(getent passwd "$USER_NAME" | cut -d: -f6)" PREFIX="${PREFIX:-/usr/local/bin}" [ -n "$HOME_DIR" ] || { echo "no home dir for $USER_NAME" >&2; exit 1; } unit=$(mktemp) sed -e "s|@USER@|$USER_NAME|g" -e "s|@GROUP@|$GROUP_NAME|g" -e "s|@HOME@|$HOME_DIR|g" -e "s|@PREFIX@|$PREFIX|g" \ deploy/hamlab.service > "$unit" sudo install -m 0755 bin/hamlab "$PREFIX/hamlab" # other-architecture builds sit beside the binary so the hub can push them to matching machines for alt in bin/hamlab-linux-*; do [ -f "$alt" ] && sudo install -m 0755 "$alt" "$PREFIX/hamlab-${alt##*-linux-}" done sudo install -m 0644 "$unit" /etc/systemd/system/hamlab.service rm -f "$unit" # Keep the daemon's read-only sudo polling (SMART, UPS, journal) out of the journal: with # 250 ms refresh rates the sudo session lines would otherwise dominate the log. quiet=$(mktemp) cat > "$quiet" <<'SUDOERS' # Ham Lab: don't log the daemon's read-only polling commands (installed by deploy/install.sh) Cmnd_Alias HAMLAB_QUIET = /usr/sbin/smartctl, /usr/bin/smartctl, /usr/sbin/pwrstat, /usr/bin/journalctl, /usr/bin/tail, /usr/bin/cat, /usr/bin/ls, /usr/bin/crontab Defaults!HAMLAB_QUIET !syslog, !pam_session, !pam_setcred SUDOERS if sudo visudo -cf "$quiet" >/dev/null 2>&1; then sudo install -m 0440 -o root -g root "$quiet" /etc/sudoers.d/hamlab-quiet else echo "warning: sudoers snippet failed validation; skipped" >&2 fi rm -f "$quiet" sudo systemctl daemon-reload sudo systemctl enable hamlab.service >/dev/null if [ ! -f "$HOME_DIR/apps/hamlab/config.json" ]; then echo "No config yet. Set a password, then start: $PREFIX/hamlab passwd && sudo systemctl start hamlab" >&2 exit 0 fi sudo systemctl restart hamlab.service sleep 1 systemctl is-active hamlab.service && "$PREFIX/hamlab" version