CluPilotCloud/deploy/install-agent.sh

60 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Installs the update agent's systemd timer. Root, once per machine, idempotent.
#
# Separate from install.sh because the installed base never runs install.sh
# again — existing servers upgrade through deploy/update.sh, which runs as the
# service account and cannot write to /etc/systemd. Without this as its own
# entry point, every server that already exists would show the update button
# permanently disabled.
#
# sudo bash deploy/install-agent.sh
#
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
APP_USER="${APP_USER:-clupilot}"
[[ $EUID -eq 0 ]] || { echo "Please run as root: sudo bash deploy/install-agent.sh" >&2; exit 1; }
id -u "$APP_USER" >/dev/null 2>&1 || { echo "No such account: $APP_USER" >&2; exit 1; }
install -o "$APP_USER" -g "$APP_USER" -d "$ROOT/storage/app/deploy"
chmod +x "$ROOT/deploy/update-agent.sh"
cat > /etc/systemd/system/clupilot-update-agent.service <<EOF
[Unit]
Description=CluPilot update agent
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
User=$APP_USER
WorkingDirectory=$ROOT
ExecStart=$ROOT/deploy/update-agent.sh
EOF
cat > /etc/systemd/system/clupilot-update-agent.timer <<'EOF'
[Unit]
Description=Check for CluPilot updates and run requested ones
[Timer]
# Every five minutes: fast enough that the button feels like it did something,
# slow enough that fetching from the repository is not a nuisance.
OnBootSec=3min
OnUnitActiveSec=5min
AccuracySec=30s
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now clupilot-update-agent.timer >/dev/null
# Run it once now, so the panel has a status to show instead of "never reported
# in" for the first five minutes.
systemctl start clupilot-update-agent.service || true
echo "Update agent installed and running (clupilot-update-agent.timer)."