The unit on the host had acquired ten flags and two settings by hand - the database, the cluster registry, the Terraform roots, the management window, the single sign-on certificate and ReadWritePaths - while this script still wrote the read-only portal of months ago. Running it would have produced a portal that starts, answers, and cannot reach its own database, and none of that is visible in the output. It now writes the live unit, argument for argument, and creates the two things systemd refuses to start without: /etc/hpcod/db.env and the writable state directory under /var/lib. A missing EnvironmentFile or a ReadWritePaths naming a directory that does not exist are not ignored. Then it reads the unit back through systemctl and aborts if any flag is missing. A here-doc whose variable expanded to nothing produces a valid file and a portal that is quietly less than it was. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
207 lines
8.1 KiB
Bash
Executable File
207 lines
8.1 KiB
Bash
Executable File
#!/bin/bash
|
|
# Install the HPC-on-Demand portal on the Caddy host.
|
|
#
|
|
# It lives here rather than on a tenant head node for two reasons: it must
|
|
# reach every backing cloud's API, and binding it to loopback on the same host
|
|
# as Caddy means the reverse proxy is the only thing that can reach it at all.
|
|
#
|
|
# It is no longer read-only. The portal allocates cluster ids from PostgreSQL,
|
|
# runs Terraform, publishes the result and proxies to it, and every one of
|
|
# those needs a flag or a writable path that this script once did not write.
|
|
#
|
|
# That gap was not theoretical. The unit on the host had acquired ten flags
|
|
# and two settings by hand - the database, the cluster registry, the
|
|
# Terraform roots, the management window, the single sign-on certificate,
|
|
# and ReadWritePaths - while this file still wrote the read-only portal of
|
|
# months ago. Running it would have produced a portal that starts, answers,
|
|
# and cannot reach its own database. A deployment script that silently
|
|
# downgrades production is worse than no deployment script, because it is
|
|
# the one thing nobody re-reads before running.
|
|
#
|
|
# Expects the binary at /tmp/hpcod.
|
|
set -euo pipefail
|
|
|
|
BIN=/usr/local/bin/hpcod
|
|
SRC=/tmp/hpcod
|
|
ETC=/etc/hpcod
|
|
VARLIB=/var/lib/hpcod
|
|
SVCUSER=hpcod
|
|
UNIT=/etc/systemd/system/hpcod-portal.service
|
|
|
|
# Site-specific, and the only things here that differ between installations.
|
|
CLUSTER_DOMAIN=${CLUSTER_DOMAIN:-hpc.example.org}
|
|
MGMT_FIRST=${MGMT_FIRST:-10.0.0.160}
|
|
MGMT_COUNT=${MGMT_COUNT:-40}
|
|
|
|
[ -f "${SRC}" ] || { echo "binary not found at ${SRC}"; exit 1; }
|
|
|
|
echo "==> service account"
|
|
id -u ${SVCUSER} >/dev/null 2>&1 || \
|
|
useradd --system --no-create-home --shell /usr/sbin/nologin ${SVCUSER}
|
|
|
|
echo "==> binary"
|
|
install -m 0755 -o root -g root "${SRC}" "${BIN}"
|
|
install -d -m 0750 -o root -g ${SVCUSER} "${ETC}"
|
|
|
|
echo "==> site catalogue"
|
|
if [ ! -f "${ETC}/sites.yaml" ]; then
|
|
cat > "${ETC}/sites.yaml" <<'EOF'
|
|
sites:
|
|
- id: hv01
|
|
name: Lab cluster
|
|
kind: proxmox
|
|
settings:
|
|
endpoint: https://10.0.0.10:8006
|
|
token: env:HV01_TOKEN
|
|
insecure_tls: "true"
|
|
timeout: 20s
|
|
cpu_overcommit: "2.0"
|
|
EOF
|
|
echo " wrote ${ETC}/sites.yaml"
|
|
fi
|
|
chown root:${SVCUSER} "${ETC}/sites.yaml"; chmod 0640 "${ETC}/sites.yaml"
|
|
|
|
# The application catalogue, generated by image/apps/build-push.sh --catalog.
|
|
# Shipped from the repository rather than seeded here: an entry in it is a
|
|
# promise that an image exists in the registry, and this script cannot check
|
|
# that. A missing file is an empty catalogue and the portal still starts -
|
|
# which is correct for a site that publishes no applications, and was also
|
|
# what every site looked like while the flag below was missing entirely.
|
|
echo "==> application catalogue"
|
|
APPS_SRC=${APPS_SRC:-$(dirname "$0")/../hpcod/config/apps.yaml}
|
|
if [ -f "${APPS_SRC}" ]; then
|
|
install -m 0640 -o root -g ${SVCUSER} "${APPS_SRC}" "${ETC}/apps.yaml"
|
|
# Read back what landed, not what install printed. A catalogue the portal
|
|
# cannot parse leaves it offering nothing, and the only symptom is a plan
|
|
# page with no Applications card - which reads as "none published".
|
|
echo " $(grep -c '^ - name:' "${ETC}/apps.yaml" || echo 0) entries in ${ETC}/apps.yaml"
|
|
else
|
|
echo " ${APPS_SRC} does not exist; the portal will publish no applications"
|
|
rm -f "${ETC}/apps.yaml"
|
|
fi
|
|
|
|
echo "==> credentials"
|
|
if [ ! -f "${ETC}/env" ]; then
|
|
echo "HV01_TOKEN=CHANGE_ME" > "${ETC}/env"
|
|
echo " !! edit ${ETC}/env and put the real read-only Proxmox token in it"
|
|
fi
|
|
chown root:${SVCUSER} "${ETC}/env"; chmod 0640 "${ETC}/env"
|
|
|
|
# The database DSN, in its own file because it holds a password and /etc/hpcod/env
|
|
# does not. systemd refuses to start a unit whose EnvironmentFile is missing, so
|
|
# a placeholder is written rather than left absent - a portal that will not start
|
|
# says why, and one that is missing a flag does not.
|
|
if [ ! -f "${ETC}/db.env" ]; then
|
|
echo 'HPCOD_DB_DSN=postgres://hpcod:CHANGE_ME@127.0.0.1:5432/hpcod' > "${ETC}/db.env"
|
|
echo " !! edit ${ETC}/db.env and put the real DSN in it"
|
|
fi
|
|
chown root:${SVCUSER} "${ETC}/db.env"; chmod 0640 "${ETC}/db.env"
|
|
|
|
# The portal writes here: a cluster's certificate arrives when it is built and
|
|
# the registry beside it is rewritten by rename. It is not under /etc because
|
|
# ProtectSystem=strict makes /etc read-only, and the only way to keep the old
|
|
# layout working would have been to make the whole of /etc/hpcod writable -
|
|
# where the user file, the database password and the proxy key live.
|
|
#
|
|
# Created here because ReadWritePaths on a path that does not exist is not
|
|
# ignored: systemd refuses to start the unit at all.
|
|
echo "==> writable state"
|
|
install -d -m 0750 -o ${SVCUSER} -g ${SVCUSER} "${VARLIB}" \
|
|
"${VARLIB}/clusters" "${VARLIB}/terraform" "${VARLIB}/terraform/envs"
|
|
|
|
echo "==> portal login"
|
|
command -v htpasswd >/dev/null || apt-get install -y -qq apache2-utils
|
|
if [ ! -f "${ETC}/users" ]; then
|
|
PW=$(head -c 40 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | cut -c1-20)
|
|
htpasswd -bBC 12 -c "${ETC}/users" admin "${PW}" >/dev/null 2>&1
|
|
printf '\n portal login: admin / %s\n\n' "${PW}"
|
|
printf 'UI_USER=admin\nUI_PASS=%s\n' "${PW}" > /root/.hpcod-portal-login
|
|
chmod 600 /root/.hpcod-portal-login
|
|
echo " (also saved to /root/.hpcod-portal-login)"
|
|
fi
|
|
chown root:${SVCUSER} "${ETC}/users"; chmod 0640 "${ETC}/users"
|
|
|
|
echo "==> systemd unit"
|
|
cat > "${UNIT}" <<EOF
|
|
[Unit]
|
|
Description=HPC-on-Demand portal (discovery, placement, and cluster allocation)
|
|
After=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=${SVCUSER}
|
|
Group=${SVCUSER}
|
|
EnvironmentFile=${ETC}/env
|
|
# The control plane database. Separate file because it holds a password
|
|
# and is 0640 root:hpcod, while /etc/hpcod/env is not.
|
|
EnvironmentFile=${ETC}/db.env
|
|
WorkingDirectory=${ETC}
|
|
ExecStart=${BIN} serve -listen 127.0.0.1:9880 \\
|
|
-c ${ETC}/sites.yaml -users ${ETC}/users -trusted-proxy 127.0.0.1 \\
|
|
-apps ${ETC}/apps.yaml \\
|
|
-clusters ${VARLIB}/clusters.yaml \\
|
|
-cluster-certs ${VARLIB}/clusters \\
|
|
-cluster-domain ${CLUSTER_DOMAIN} \\
|
|
-tf-envs ${VARLIB}/terraform/envs \\
|
|
-tf-template ${VARLIB}/terraform/template \\
|
|
-mgmt-first ${MGMT_FIRST} -mgmt-count ${MGMT_COUNT} \\
|
|
-portal-cert ${ETC}/proxy-client.crt -portal-key ${ETC}/proxy-client.key
|
|
Restart=on-failure
|
|
RestartSec=3
|
|
|
|
NoNewPrivileges=yes
|
|
PrivateTmp=yes
|
|
PrivateDevices=yes
|
|
ProtectSystem=strict
|
|
ReadWritePaths=${VARLIB}
|
|
ProtectHome=yes
|
|
ProtectKernelTunables=yes
|
|
ProtectKernelModules=yes
|
|
ProtectControlGroups=yes
|
|
# AF_UNIX as well as the two IP families: the database is reached over a
|
|
# local socket when the DSN names one, and without it the portal starts and
|
|
# then cannot connect - which reads as a database problem rather than a
|
|
# sandbox one.
|
|
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
|
RestrictNamespaces=yes
|
|
LockPersonality=yes
|
|
MemoryDenyWriteExecute=yes
|
|
SystemCallArchitectures=native
|
|
CapabilityBoundingSet=
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable hpcod-portal >/dev/null 2>&1 || true
|
|
|
|
# Read the unit back through systemd, not the file we just wrote. A here-doc
|
|
# that expanded a variable to nothing produces a valid file and an ExecStart
|
|
# missing a flag, and the portal would start without a database and answer
|
|
# every page as though no cluster had ever been built.
|
|
echo "==> what systemd now has"
|
|
EXEC=$(systemctl show hpcod-portal -p ExecStart --value)
|
|
MISSING=
|
|
for flag in -db -apps -clusters -cluster-domain -tf-envs -tf-template \
|
|
-mgmt-first -portal-cert; do
|
|
case "$flag" in
|
|
# -db is not on the command line: it comes from HPCOD_DB_DSN in db.env.
|
|
-db) [ -s "${ETC}/db.env" ] || MISSING="$MISSING db.env" ;;
|
|
*) case "$EXEC" in *"$flag "*) ;; *) MISSING="$MISSING $flag" ;; esac ;;
|
|
esac
|
|
done
|
|
if [ -n "$MISSING" ]; then
|
|
echo "ABORT: the installed unit is missing:$MISSING"
|
|
echo " the portal would start and be quietly less than it was."
|
|
exit 1
|
|
fi
|
|
echo " ExecStart carries every flag this portal needs"
|
|
systemctl show hpcod-portal -p ReadWritePaths --value | grep -q "${VARLIB}" || {
|
|
echo "ABORT: ReadWritePaths does not cover ${VARLIB}; publishing a cluster would fail"
|
|
exit 1
|
|
}
|
|
echo " ${VARLIB} is writable under ProtectSystem=strict"
|
|
|
|
echo "HPCOD PORTAL INSTALLED (start it once ${ETC}/env and ${ETC}/db.env are real)"
|