Lets a server be cleanly removed from agent management: uninstall.sh stops/disables the systemd timer and deletes the unit files, the installed script, and the credentials env file, without touching the server's entry or task history in the app. install.sh now prints the uninstall command on success, and the Servers page has an "Uninstall" button that reveals the same command (safe to show anytime since it carries no secret).
73 lines
2.1 KiB
Bash
73 lines
2.1 KiB
Bash
#!/usr/bin/env bash
|
|
# Installs the Schedule Task Manager agent as a systemd timer.
|
|
#
|
|
# Usage:
|
|
# curl -fsSL https://schedule.example.lan/agent/linux/install.sh | \
|
|
# API_URL=https://schedule.example.lan API_TOKEN=stm_xxx bash
|
|
#
|
|
set -euo pipefail
|
|
|
|
: "${API_URL:?Set API_URL to your Schedule Task Manager URL, e.g. https://schedule.example.lan}"
|
|
: "${API_TOKEN:?Set API_TOKEN to the per-server token generated on the Servers page}"
|
|
|
|
INSTALL_DIR="/usr/local/bin"
|
|
CONFIG_DIR="/etc"
|
|
SYSTEMD_DIR="/etc/systemd/system"
|
|
INTERVAL_MINUTES="${INTERVAL_MINUTES:-15}"
|
|
|
|
if [[ "$EUID" -ne 0 ]]; then
|
|
echo "This installer must be run as root (it installs a systemd timer)." >&2
|
|
exit 1
|
|
fi
|
|
|
|
for bin in curl jq systemctl; do
|
|
if ! command -v "$bin" >/dev/null 2>&1; then
|
|
echo "Required dependency '$bin' is not installed." >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
echo "Installing Schedule Task Manager agent from $API_URL ..."
|
|
|
|
curl -fsSL "$API_URL/agent/linux/report-tasks.sh" -o "$INSTALL_DIR/schedule-task-manager-agent.sh"
|
|
chmod 755 "$INSTALL_DIR/schedule-task-manager-agent.sh"
|
|
|
|
umask 077
|
|
cat > "$CONFIG_DIR/schedule-task-manager-agent.env" <<EOF
|
|
API_URL=$API_URL
|
|
API_TOKEN=$API_TOKEN
|
|
EOF
|
|
chmod 600 "$CONFIG_DIR/schedule-task-manager-agent.env"
|
|
|
|
cat > "$SYSTEMD_DIR/schedule-task-manager-agent.service" <<EOF
|
|
[Unit]
|
|
Description=Report scheduled tasks to Schedule Task Manager
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
EnvironmentFile=$CONFIG_DIR/schedule-task-manager-agent.env
|
|
ExecStart=$INSTALL_DIR/schedule-task-manager-agent.sh
|
|
EOF
|
|
|
|
cat > "$SYSTEMD_DIR/schedule-task-manager-agent.timer" <<EOF
|
|
[Unit]
|
|
Description=Periodically report scheduled tasks to Schedule Task Manager
|
|
|
|
[Timer]
|
|
OnBootSec=2min
|
|
OnUnitActiveSec=${INTERVAL_MINUTES}min
|
|
Persistent=true
|
|
|
|
[Install]
|
|
WantedBy=timers.target
|
|
EOF
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable --now schedule-task-manager-agent.timer
|
|
|
|
echo "Installed. Running an initial report now..."
|
|
"$INSTALL_DIR/schedule-task-manager-agent.sh"
|
|
|
|
echo "Done. The agent reports every ${INTERVAL_MINUTES} minute(s) via the 'schedule-task-manager-agent.timer' systemd timer."
|
|
echo "To remove it later: curl -fsSL $API_URL/agent/linux/uninstall.sh | bash"
|