[Unit]
Description=DECNET Self-Updater (mTLS)
Documentation=https://git.resacachile.cl/anti/DECNET/wiki/Remote-Updates
After=network-online.target
Wants=network-online.target
# Deliberately NOT After=decnet-agent.service — the updater must come up even
# when the agent is broken, since that is exactly when it is most useful.

[Service]
Type=simple
User={{ user }}
Group={{ group }}
WorkingDirectory={{ install_dir }}
EnvironmentFile=-{{ install_dir }}/.env.local
Environment=DECNET_SYSTEM_LOGS=/var/log/decnet/decnet.updater.log
ExecStart={{ venv_dir }}/bin/decnet updater \
    --host 0.0.0.0 --port 8766 \
    --updater-dir /etc/decnet/updater \
    --install-dir {{ install_dir }} \
    --agent-dir /etc/decnet/agent
StandardOutput=append:/var/log/decnet/decnet.updater.log
StandardError=append:/var/log/decnet/decnet.updater.log

# The updater SIGTERMs the agent and spawns a new one. Same User=decnet means
# signalling is allowed without CAP_KILL. It does not need NET_ADMIN/NET_RAW
# itself — the new agent process picks those up from decnet-agent.service when
# systemd restarts it (or from the agent's own unit's AmbientCapabilities when
# spawned by the updater as a direct child).
CapabilityBoundingSet=
AmbientCapabilities=

# Security Hardening
NoNewPrivileges=yes
ProtectSystem=full
ProtectHome=read-only
PrivateTmp=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
LockPersonality=yes
# Writes release slots, pip installs into venv, manages agent.pid.
ReadWritePaths={{ install_dir }} /var/log/decnet

Restart=on-failure
RestartSec=5
# Self-update replaces the process image via os.execv; the new binary answers
# /health within 30 s. Give it headroom before systemd's own termination.
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target
