#!/bin/sh # PatchBay installer: dependencies (APT or XBPS), build, install, service units. # Usage: ./install.sh --role target|client [--init systemd|runit|openrc|none] [--no-deps] set -eu ROLE="" INIT=auto DEPS=1 PREFIX=${PREFIX:-/usr/local} while [ $# -gt 0 ]; do case "$1" in --role) ROLE=$2; shift 2 ;; --init) INIT=$2; shift 2 ;; --no-deps) DEPS=0; shift ;; -h|--help) sed -n '2,3p' "$0"; exit 0 ;; *) echo "unknown option $1" >&2; exit 2 ;; esac done case "$ROLE" in target|client) ;; *) echo "usage: $0 --role target|client [--init systemd|runit|openrc|none] [--no-deps]" >&2; exit 2 ;; esac [ "$(id -u)" = 0 ] || { echo "run as root" >&2; exit 1; } cd "$(dirname "$0")" say() { printf '==> %s\n' "$*"; } # Dependencies if [ $DEPS = 1 ]; then if command -v apt-get >/dev/null; then PKGS="build-essential pkg-config libssh2-1-dev libsqlite3-dev openssh-client" [ "$ROLE" = target ] && PKGS="$PKGS openssh-server python3 python3-flask python3-cryptography" say "installing packages via APT: $PKGS" apt-get update # shellcheck disable=SC2086 DEBIAN_FRONTEND=noninteractive apt-get install -y $PKGS elif command -v xbps-install >/dev/null; then PKGS="base-devel pkg-config libssh2-devel sqlite-devel openssh" [ "$ROLE" = target ] && PKGS="$PKGS shadow python3 python3-Flask python3-cryptography" say "installing packages via XBPS: $PKGS" xbps-install -Sy # shellcheck disable=SC2086 xbps-install -y $PKGS else echo "neither apt-get nor xbps-install found; install dependencies manually and use --no-deps" >&2 exit 1 fi fi # Build and install say "building" make clean >/dev/null make all make install PREFIX="$PREFIX" mkdir -p /etc/patchbay chmod 755 /etc/patchbay if [ ! -f /etc/patchbay/patchbay.conf ]; then install -m 600 "examples/patchbay.conf.$ROLE" /etc/patchbay/patchbay.conf if [ "$PREFIX" != /usr/local ]; then echo "DaemonPath = $PREFIX/sbin/patchbayd" >> /etc/patchbay/patchbay.conf fi NEWCONF=1 else NEWCONF=0 fi if [ "$ROLE" = target ]; then # The sshd login user: no password, shell needed for the forced command. if ! id patchbay >/dev/null 2>&1; then say "creating user patchbay" useradd --system --home-dir /nonexistent --no-create-home --shell /bin/sh patchbay fi usermod -p '*' patchbay fi # Service units if [ "$INIT" = auto ]; then if [ -d /run/systemd/system ]; then INIT=systemd elif command -v openrc-run >/dev/null; then INIT=openrc elif command -v sv >/dev/null; then INIT=runit else INIT=none fi fi SERVICES=patchbayd [ "$ROLE" = target ] && SERVICES="patchbayd patchbay-sshd patchbay-web" fix_prefix() { [ "$PREFIX" = /usr/local ] || sed -i "s|/usr/local|$PREFIX|g" "$1"; } case "$INIT" in systemd) for s in $SERVICES; do install -m 644 "init/systemd/$s.service" "/etc/systemd/system/$s.service" fix_prefix "/etc/systemd/system/$s.service" done systemctl daemon-reload ENABLE="systemctl enable --now $SERVICES" ;; openrc) for s in $SERVICES; do install -m 755 "init/openrc/$s" "/etc/init.d/$s" fix_prefix "/etc/init.d/$s" done ENABLE=$(for s in $SERVICES; do printf 'rc-update add %s default && rc-service %s start; ' "$s" "$s"; done) ;; runit) SVDIR=/etc/sv [ -d /etc/sv ] || SVDIR=/etc/runit/sv RUNSVDIR=/var/service [ -d /var/service ] || RUNSVDIR=/etc/runit/runsvdir/default for s in $SERVICES; do mkdir -p "$SVDIR/$s" install -m 755 "init/runit/$s/run" "$SVDIR/$s/run" fix_prefix "$SVDIR/$s/run" done ENABLE=$(for s in $SERVICES; do printf 'ln -s %s/%s %s/; ' "$SVDIR" "$s" "$RUNSVDIR"; done) ;; none) ENABLE="(no init system selected; run $PREFIX/sbin/patchbayd manually)" ;; *) echo "unknown init system $INIT" >&2; exit 2 ;; esac say "installed ($ROLE, $INIT)" [ $NEWCONF = 1 ] && echo " edit /etc/patchbay/patchbay.conf before starting" if [ "$ROLE" = client ]; then echo " add this client's key in the target's web UI (Settings -> Clients):" "$PREFIX/sbin/patchbayd" --pubkey 2>/dev/null | sed 's/^/ /' || \ echo " (no key yet; set IdentityFile or create /root/.ssh/id_ed25519, then run: patchbayd --pubkey)" fi echo " start with: $ENABLE"