# systemd unit for Hour Advantage.
#
# Layout assumed here (change the paths to suit):
#   /usr/local/bin/hourd                the binary
#   /var/lib/houradvantage                      the working directory, owned by root;
#                                           the service writes nothing to it
#   /etc/houradvantage/houradvantage.env           mode 0600: the database, the key that
#                                           seals the secrets kept in it (the same on
#                                           every instance; back it up) and the
#                                           admin login (every local user can reach
#                                           127.0.0.1:9090):
#                                           HOURADVANTAGE_DATABASE_URL=postgres://houradvantage:secret@db/houradvantage
#                                           HOURADVANTAGE_SECRET_KEY=<houradm gen-secret-key>
#                                           HOURADVANTAGE_ADMIN_USERNAME=admin
#                                           HOURADVANTAGE_ADMIN_PASSWORD_HASH=<houradm hash-password>
#                                           and, to listen elsewhere than 127.0.0.1:9090 (any
#                                           address but loopback needs the login above):
#                                           HOURADVANTAGE_ADMIN_LISTEN=0.0.0.0:9090
# Everything else (packages, listeners, endpoints, sinks, devices, events)
# lives in the Postgres database, and is changed while it runs with `hourctl ...`
# (admin API on 127.0.0.1:9090). The gateway writes no files: its log goes to
# stdout, which is the journal: journalctl -u houradvantage -f. As stdout is not a
# terminal, each line is one JSON object, which the journal keeps as it is.
#
# Setup:
#   sudo useradd --system --home-dir /var/lib/houradvantage --shell /usr/sbin/nologin houradvantage
#   sudo install -d -o root -g houradvantage -m 0750 /var/lib/houradvantage
#   sudo install -d /etc/houradvantage
#   sudo install -m 0600 /dev/null /etc/houradvantage/houradvantage.env
#   houradm gen-secret-key                     # for HOURADVANTAGE_SECRET_KEY
#   houradm hash-password                      # for HOURADVANTAGE_ADMIN_PASSWORD_HASH
#   sudoedit /etc/houradvantage/houradvantage.env        # the four lines above ($ has no special
#                                                # meaning in this file: paste the hash as it is)
#   sudo cp houradvantage.service /etc/systemd/system/
#   sudo systemctl daemon-reload
#   sudo systemctl enable --now houradvantage
#   # `hourctl` logs in with HOURADVANTAGE_ADMIN_USERNAME and HOURADVANTAGE_ADMIN_PASSWORD
#   # (the password itself, not the hash) in its own environment.
#   # Packages are kept in the database: push the official bundles from the
#   # Hour Advantage distribution once, in the folder that holds them (any
#   # instance; every instance sharing the database runs them):
#   hourctl pkg push --activate *.tar.gz

[Unit]
Description=Hour Advantage server
Wants=network-online.target
After=network-online.target
# With Postgres on this machine, start after it:
#After=network-online.target postgresql.service

[Service]
Type=simple
User=houradvantage
Group=houradvantage
WorkingDirectory=/var/lib/houradvantage
# The database URL, the secret key and the login are read from a root-only
# file (systemd reads it before dropping to User=) rather than written here.
# Not optional (no leading '-'): a path mistyped or a file missing stops the
# service with systemd's own message, where an ignored one would only show as
# the gateway lacking its database URL.
EnvironmentFile=/etc/houradvantage/houradvantage.env
ExecStart=/usr/local/bin/hourd run
# SIGTERM shuts the gateway down cleanly.
KillSignal=SIGTERM
# The gateway stops in about 20 s at the most: its drain (6 s), listeners (5 s),
# the attendance workers ending a batch (up to 15 s alongside) and the admin API (5 s).
TimeoutStopSec=40
Restart=on-failure
RestartSec=5
# Open files: every connection a listener holds is one (up to 10 000 each).
# systemd's soft limit is 1024; the server raises its soft limit to this
# hard one at startup and logs where it stands.
LimitNOFILE=65536

# Hardening: the gateway writes nothing, binds its listener ports and
# talks TCP (and to the journal over a Unix socket).
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ProtectClock=yes
ProtectHostname=yes
ProtectProc=invisible
RestrictSUIDSGID=yes
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
LockPersonality=yes
SystemCallArchitectures=native
SystemCallFilter=@system-service
CapabilityBoundingSet=
# Needed only to listen on a port below 1024 (for example clocks fixed to
# port 80): uncomment both lines.
#AmbientCapabilities=CAP_NET_BIND_SERVICE
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target
