80 lines
2.9 KiB
Bash
80 lines
2.9 KiB
Bash
#!/bin/sh
|
|
# Installs hammerhead as a systemd service from the files in this folder.
|
|
#
|
|
# sudo ./install.sh install or upgrade (your settings and data are kept)
|
|
# sudo ./install.sh --start the same, then start (or restart) the service
|
|
# sudo ./install.sh --uninstall remove the service and binary (keeps the settings and data)
|
|
#
|
|
# Files: /usr/local/lib/hammerhead/hammerhead (linked from /usr/local/bin/hammerhead), /etc/systemd/system/hammerhead.service,
|
|
# /etc/hammerhead/hammerhead.env (your settings and secrets), /var/lib/hammerhead (data).
|
|
# Set DESTDIR=/some/dir to stage everything there without touching the system.
|
|
set -eu
|
|
NAME=hammerhead
|
|
HERE=$(cd "$(dirname "$0")" && pwd)
|
|
ROOT=${DESTDIR:-}
|
|
BIN=$ROOT/usr/local/bin/$NAME
|
|
UNIT=$ROOT/etc/systemd/system/$NAME.service
|
|
CONF=$ROOT/etc/$NAME
|
|
ENVFILE=$CONF/$NAME.env
|
|
DATA=$ROOT/var/lib/$NAME
|
|
LIB=$ROOT/usr/local/lib/$NAME # the program lives here, owned by the service, so auto-update can replace it
|
|
|
|
say() { printf '%s\n' "$*"; }
|
|
real() { [ -z "${DESTDIR:-}" ]; } # are we touching the real system?
|
|
|
|
if real; then
|
|
[ "$(id -u)" = 0 ] || { say "Run this as root: sudo $0 $*"; exit 1; }
|
|
command -v systemctl > /dev/null 2>&1 || { say "systemd wasn't found. Run $NAME directly (see the README) or use Docker."; exit 1; }
|
|
fi
|
|
|
|
if [ "${1:-}" = "--uninstall" ]; then
|
|
if real; then
|
|
systemctl disable --now "$NAME" > /dev/null 2>&1 || true
|
|
fi
|
|
rm -f "$UNIT" "$BIN" "$LIB/$NAME"
|
|
if real; then
|
|
systemctl daemon-reload || true
|
|
fi
|
|
say "Removed $NAME. Left in place: $CONF and $DATA (delete them yourself if you want them gone)."
|
|
exit 0
|
|
fi
|
|
|
|
[ -f "$HERE/$NAME" ] || { say "Can't find the $NAME binary next to this script."; exit 1; }
|
|
|
|
if real && ! id "$NAME" > /dev/null 2>&1; then
|
|
useradd --system --home-dir "/var/lib/$NAME" --shell /usr/sbin/nologin "$NAME"
|
|
say "Created the system user '$NAME'."
|
|
fi
|
|
|
|
mkdir -p "$ROOT/usr/local/bin" "$ROOT/etc/systemd/system" "$CONF" "$DATA"
|
|
mkdir -p "$LIB"
|
|
install -m 0755 "$HERE/$NAME" "$LIB/$NAME.new"
|
|
mv -f "$LIB/$NAME.new" "$LIB/$NAME" # atomic, so upgrading a running service is safe
|
|
rm -f "$BIN"
|
|
LINK=/usr/local/lib/$NAME/$NAME
|
|
[ -z "${DESTDIR:-}" ] || LINK=$LIB/$NAME # staging: point inside the staging folder
|
|
ln -s "$LINK" "$BIN"
|
|
install -m 0644 "$HERE/$NAME.service" "$UNIT"
|
|
if [ ! -f "$ENVFILE" ]; then
|
|
install -m 0600 "$HERE/.env.example" "$ENVFILE"
|
|
say "Created $ENVFILE. Edit it before starting."
|
|
fi
|
|
if real; then
|
|
chown -R "$NAME:$NAME" "$DATA" "$LIB"
|
|
fi
|
|
|
|
if real; then
|
|
systemctl daemon-reload
|
|
systemctl enable "$NAME" > /dev/null 2>&1
|
|
if [ "${1:-}" = "--start" ] || systemctl is-active --quiet "$NAME"; then
|
|
systemctl restart "$NAME"
|
|
say "$NAME is running."
|
|
fi
|
|
fi
|
|
|
|
say "Installed $("$LIB/$NAME" -version 2> /dev/null || echo "$NAME")."
|
|
say "Next:"
|
|
say " 1. edit $ENVFILE"
|
|
say " 2. systemctl start $NAME (or run this again with --start)"
|
|
say " 3. journalctl -u $NAME -f to watch it"
|