From 412d73e7e114a5a6d3b4a261995e40efd8fdb673 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 19 Oct 2026 10:16:34 +1100 Subject: [PATCH] feat(install): install and update a named instance side by side --- install.sh | 107 +++++++++++++++++++++------- internal/app/archied/update.go | 8 ++- internal/app/archied/update_host.go | 15 +++- 3 files changed, 103 insertions(+), 27 deletions(-) diff --git a/install.sh b/install.sh index 4a2e3ca21..a49490a56 100755 --- a/install.sh +++ b/install.sh @@ -16,10 +16,9 @@ XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME}/.config}" XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}" XDG_BIN_HOME="${XDG_BIN_HOME:-${HOME}/.local/bin}" -ARCHIE_CONFIG_DIR="${XDG_CONFIG_HOME}/archie" -ARCHIE_DATA_DIR="${XDG_DATA_HOME}/archie" ARCHIE_BIN_DIR="${XDG_BIN_HOME}" -ENV_FILE="${ARCHIE_CONFIG_DIR}/env" +INSTANCE=archie +PORT_BASE="" INSTALL_SYSTEMD=true ENABLE_LINGER=true @@ -39,6 +38,10 @@ Options: --version X.Y.Z Install this release instead of the latest --from-source Build from a checkout (this one, or a fresh clone) instead of a release; needs Go, git and jq + --instance NAME Install a named instance beside others (default: archie); + its dirs are /NAME and its units end in -NAME + --port-base N Use ports N..N+6 for web, health, gateway, state store, + state store readiness, messaging and daemon --no-systemd Skip systemd user units --no-linger Skip loginctl enable-linger --no-start Install the units but do not start them @@ -51,6 +54,8 @@ VERSION="" while [ $# -gt 0 ]; do case "$1" in --version) VERSION="${2:?--version needs a value}"; shift ;; + --instance) INSTANCE="${2:?--instance needs a value}"; shift ;; + --port-base) PORT_BASE="${2:?--port-base needs a value}"; shift ;; --from-source) FROM_SOURCE=true ;; --no-systemd) INSTALL_SYSTEMD=false; AUTO_START=false ;; --no-linger) ENABLE_LINGER=false ;; @@ -62,6 +67,36 @@ while [ $# -gt 0 ]; do shift done +if ! [[ "${INSTANCE}" =~ ^[a-z][a-z0-9-]{0,30}$ ]]; then + echo "Error: --instance '${INSTANCE}' must match ^[a-z][a-z0-9-]{0,30}\$" >&2; exit 1 +fi +if [ -n "${PORT_BASE}" ] && ! { [[ "${PORT_BASE}" =~ ^[0-9]+$ ]] && [ "${PORT_BASE}" -ge 1024 ] && [ "${PORT_BASE}" -le 65529 ]; }; then + echo "Error: --port-base must be a number from 1024 to 65529" >&2; exit 1 +fi +export ARCHIE_INSTANCE="${INSTANCE}" +ARCHIE_CONFIG_DIR="${XDG_CONFIG_HOME}/${INSTANCE}" +ARCHIE_DATA_DIR="${XDG_DATA_HOME}/${INSTANCE}" +ENV_FILE="${ARCHIE_CONFIG_DIR}/env" + +# unit maps a base unit name to this instance's: unchanged for the default +# instance, suffixed with - otherwise. +unit() { + if [ "${INSTANCE}" = archie ]; then echo "$1"; else echo "$1-${INSTANCE}"; fi +} +U_DAEMON="$(unit archied)" +U_STATE="$(unit archie-state-store)" +U_GATEWAY="$(unit archie-gateway)" +U_UI="$(unit archie-ui)" +U_MESSAGING="$(unit archie-messaging)" + +# Ports in --port-base order; without it, today's defaults. +if [ -n "${PORT_BASE}" ]; then + P_WEB=${PORT_BASE}; P_HEALTH=$((PORT_BASE + 1)); P_GATEWAY=$((PORT_BASE + 2)) + P_STATE=$((PORT_BASE + 3)); P_READY=$((PORT_BASE + 4)); P_MESSAGING=$((PORT_BASE + 5)); P_DAEMON=$((PORT_BASE + 6)) +else + P_WEB=8484; P_HEALTH=8485; P_GATEWAY=8585; P_STATE=9090; P_READY=9091; P_MESSAGING=8586; P_DAEMON=8587 +fi + need() { for cmd in "$@"; do command -v "$cmd" &>/dev/null || { echo "Error: '$cmd' is required but not installed." >&2; exit 1; } @@ -163,7 +198,21 @@ if [ ! -f "${ARCHIE_CONFIG_DIR}/config.toml" ]; then echo "ERROR: archied setup could not generate ${ARCHIE_CONFIG_DIR}/config.toml" >&2 exit 1 fi + if [ -n "${PORT_BASE}" ]; then + # The generated config names the default addresses; move them to this + # instance's ports and pin the services the template leaves commented. + sed -i -e "s/127\.0\.0\.1:8484/127.0.0.1:${P_WEB}/g" -e "s/127\.0\.0\.1:8485/127.0.0.1:${P_HEALTH}/g" \ + -e "s/127\.0\.0\.1:8585/127.0.0.1:${P_GATEWAY}/g" -e "s/127\.0\.0\.1:9090/127.0.0.1:${P_STATE}/g" \ + -e "s/127\.0\.0\.1:8586/127.0.0.1:${P_MESSAGING}/g" -e "s/127\.0\.0\.1:8587/127.0.0.1:${P_DAEMON}/g" \ + "${ARCHIE_CONFIG_DIR}/config.toml" + for section in messaging:${P_MESSAGING} daemon:${P_DAEMON}; do + name="${section%%:*}" port="${section#*:}" + grep -q "^\[services\.${name}\]" "${ARCHIE_CONFIG_DIR}/config.toml" || + printf '\n[services.%s]\ntarget = "127.0.0.1:%s"\nlisten = "127.0.0.1:%s"\n' "${name}" "${port}" "${port}" >>"${ARCHIE_CONFIG_DIR}/config.toml" + done + fi else + [ -n "${PORT_BASE}" ] && echo " [WARN] --port-base only applies to a new config; ${ARCHIE_CONFIG_DIR}/config.toml keeps its addresses." >&2 echo " [SKIP] ${ARCHIE_CONFIG_DIR}/config.toml already exists. Re-run 'archied setup' to change it." fi @@ -205,9 +254,9 @@ services: volumes: pg_data: EOF - if ! docker compose -p archie --env-file "${ENV_FILE}" -f "${PG_COMPOSE}" up -d --wait; then + if ! docker compose -p "${INSTANCE}" --env-file "${ENV_FILE}" -f "${PG_COMPOSE}" up -d --wait; then echo " [WARN] The bundled PostgreSQL did not start, so the services are installed but not started." >&2 - echo " Check: docker compose -p archie -f ${PG_COMPOSE} logs" >&2 + echo " Check: docker compose -p "${INSTANCE}" -f ${PG_COMPOSE} logs" >&2 AUTO_START=false fi fi @@ -241,7 +290,7 @@ if [ "${INSTALL_SYSTEMD}" = true ] && command -v systemctl &>/dev/null && [ -d " # description now, not the mechanism: a fresh install must not need four # hand-written units before its own updater will run, and # the contents must not live in two places to drift apart. - cat < "${SYSTEMD_USER_DIR}/archied.service" + cat < "${SYSTEMD_USER_DIR}/${U_DAEMON}.service" [Unit] Description=Archie Core Orchestrator Daemon After=network.target @@ -258,6 +307,7 @@ NotifyAccess=main WatchdogSec=30s ExecStart=${ARCHIE_BIN_DIR}/archied -config ${ARCHIE_CONFIG_DIR}/config.toml EnvironmentFile=-${ENV_FILE} +Environment=ARCHIE_INSTANCE=${INSTANCE} # archied reloads its configuration on SIGHUP. Without ExecReload, # 'systemctl --user reload archied' fails and the operator has to know # to run 'systemctl --user kill -s HUP' instead. @@ -271,7 +321,7 @@ EOF # The State Store owns the task data and is the process everything else dials, so # its unit is the first dependency the others name. - cat < "${SYSTEMD_USER_DIR}/archie-state-store.service" + cat < "${SYSTEMD_USER_DIR}/${U_STATE}.service" [Unit] Description=Archie State Store Service After=network.target @@ -279,8 +329,9 @@ After=network.target [Service] Type=notify NotifyAccess=main -ExecStart=${ARCHIE_BIN_DIR}/archie-state-store -config ${ARCHIE_CONFIG_DIR}/config.toml -listen 127.0.0.1:9090 -ready-addr 127.0.0.1:9091 +ExecStart=${ARCHIE_BIN_DIR}/archie-state-store -config ${ARCHIE_CONFIG_DIR}/config.toml -listen 127.0.0.1:${P_STATE} -ready-addr 127.0.0.1:${P_READY} EnvironmentFile=-${ENV_FILE} +Environment=ARCHIE_INSTANCE=${INSTANCE} Restart=on-failure RestartSec=5s @@ -288,7 +339,7 @@ RestartSec=5s WantedBy=default.target EOF - cat < "${SYSTEMD_USER_DIR}/archie-gateway.service" + cat < "${SYSTEMD_USER_DIR}/${U_GATEWAY}.service" [Unit] Description=Archie Gateway Service After=network.target @@ -296,8 +347,9 @@ After=network.target [Service] Type=notify NotifyAccess=main -ExecStart=${ARCHIE_BIN_DIR}/archie-gateway -config ${ARCHIE_CONFIG_DIR}/config.toml -listen 127.0.0.1:8585 +ExecStart=${ARCHIE_BIN_DIR}/archie-gateway -config ${ARCHIE_CONFIG_DIR}/config.toml -listen 127.0.0.1:${P_GATEWAY} EnvironmentFile=-${ENV_FILE} +Environment=ARCHIE_INSTANCE=${INSTANCE} Restart=on-failure RestartSec=5s @@ -305,16 +357,17 @@ RestartSec=5s WantedBy=default.target EOF - cat < "${SYSTEMD_USER_DIR}/archie-ui.service" + cat < "${SYSTEMD_USER_DIR}/${U_UI}.service" [Unit] Description=Archie UI Service -After=network.target archie-state-store.service -Wants=archie-state-store.service +After=network.target ${U_STATE}.service +Wants=${U_STATE}.service [Service] Type=simple ExecStart=${ARCHIE_BIN_DIR}/archie-ui -config ${ARCHIE_CONFIG_DIR}/config.toml EnvironmentFile=-${ENV_FILE} +Environment=ARCHIE_INSTANCE=${INSTANCE} Restart=on-failure RestartSec=5s @@ -324,17 +377,18 @@ EOF # The Telegram, email and webhook channels run here, not in archied: without # this unit a host looks healthy while its channels are dead. - cat < "${SYSTEMD_USER_DIR}/archie-messaging.service" + cat < "${SYSTEMD_USER_DIR}/${U_MESSAGING}.service" [Unit] Description=Archie Messaging Service -After=network.target archie-gateway.service archie-state-store.service -Wants=archie-gateway.service archie-state-store.service +After=network.target ${U_GATEWAY}.service ${U_STATE}.service +Wants=${U_GATEWAY}.service ${U_STATE}.service [Service] Type=notify NotifyAccess=main ExecStart=${ARCHIE_BIN_DIR}/archie-messaging -config ${ARCHIE_CONFIG_DIR}/config.toml EnvironmentFile=-${ENV_FILE} +Environment=ARCHIE_INSTANCE=${INSTANCE} Restart=on-failure RestartSec=5s @@ -342,9 +396,12 @@ RestartSec=5s WantedBy=default.target EOF + INSTANCE_UNITS="" + for base in $TOPOLOGY_UNITS; do INSTANCE_UNITS="${INSTANCE_UNITS:+${INSTANCE_UNITS} }$(unit "${base}")"; done + systemctl --user daemon-reload || true SERVICE_INSTALLED=true - for unit in $TOPOLOGY_UNITS; do + for unit in $INSTANCE_UNITS; do test -f "${SYSTEMD_USER_DIR}/${unit}.service" || { echo "missing required unit: $unit" >&2; exit 1; } echo " Installed ${SYSTEMD_USER_DIR}/${unit}.service" done @@ -371,8 +428,8 @@ EOF # not race a dial it depends on. if [ "${AUTO_START}" = true ]; then echo "==> Enabling and starting Archie services..." - systemctl --user enable --now $TOPOLOGY_UNITS || - echo " Notice: Could not start every service automatically. Check: systemctl --user status archied archie-state-store archie-gateway archie-ui archie-messaging" + systemctl --user enable --now $INSTANCE_UNITS || + echo " Notice: Could not start every service automatically. Check: systemctl --user status ${INSTANCE_UNITS}" fi fi @@ -399,13 +456,15 @@ fi if [ "${SERVICE_INSTALLED}" = true ] && [ "${AUTO_START}" = true ]; then echo "Service Management:" - echo " - Units installed : archied archie-state-store archie-gateway archie-ui archie-messaging" - echo " - View live logs : journalctl --user -u archied -f" - echo " - Service status : systemctl --user status archied archie-state-store archie-gateway archie-ui archie-messaging" - echo " - Restart daemon : systemctl --user restart archied" + echo " - Units installed : ${INSTANCE_UNITS}" + echo " - View live logs : journalctl --user -u ${U_DAEMON} -f" + echo " - Service status : systemctl --user status ${INSTANCE_UNITS}" + echo " - Restart daemon : systemctl --user restart ${U_DAEMON}" else echo "Manual Startup: start the five services as in https://github.com/${REPO}/blob/main/deployments/README.md" fi echo "" -echo "Dashboard: http://127.0.0.1:8484 Change the config: archied setup" +SETUP_HINT="archied setup" +[ "${INSTANCE}" = archie ] || SETUP_HINT="ARCHIE_INSTANCE=${INSTANCE} archied setup" +echo "Dashboard: http://127.0.0.1:${P_WEB} Change the config: ${SETUP_HINT}" echo "" diff --git a/internal/app/archied/update.go b/internal/app/archied/update.go index a9d017b7b..a0160b67a 100644 --- a/internal/app/archied/update.go +++ b/internal/app/archied/update.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "syscall" "time" @@ -175,7 +176,7 @@ func spawnWatchdog(ctx context.Context, managed bool, binary, configPath string, // The units load the env file beside the config for database and // service credentials; the watchdog needs the same ones. envFile := "EnvironmentFile=-" + filepath.Join(filepath.Dir(configPath), "env") - run := append([]string{"--user", "--collect", "--quiet", "--unit=archied-update-watchdog", "--setenv=" + stateEnv, "--property=" + envFile, binary}, args...) + run := append([]string{"--user", "--collect", "--quiet", "--unit=" + instanceUnit("archied-update-watchdog"), "--setenv=" + stateEnv, "--setenv=ARCHIE_INSTANCE=" + configuration.InstanceName(), "--property=" + envFile, binary}, args...) if output, err := exec.CommandContext(ctx, "systemd-run", run...).CombinedOutput(); err != nil { return fmt.Errorf("start the update watchdog: %w: %s", err, output) } @@ -212,6 +213,11 @@ func checkUpdateTopology(ctx context.Context, configPath string, required releas for _, section := range required.ConfigSections { sections[section] = hasConfigSection(document, section) } + // release.json names base units; this instance's units carry its suffix. + required.Units = slices.Clone(required.Units) + for i, unit := range required.Units { + required.Units[i] = instanceUnit(unit) + } units := make(map[string]bool) var observeErr error if managed { diff --git a/internal/app/archied/update_host.go b/internal/app/archied/update_host.go index 52e461080..bb517f475 100644 --- a/internal/app/archied/update_host.go +++ b/internal/app/archied/update_host.go @@ -12,8 +12,19 @@ import ( "github.com/samcharles93/archie-core/internal/app/statestore" "github.com/samcharles93/archie-core/internal/domain/health" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" ) +// instanceUnit names this instance's systemd unit for a base unit name: +// unchanged for the default instance and suffixed with - otherwise, +// so several instances install side by side. +func instanceUnit(base string) string { + if name := configuration.InstanceName(); name != configuration.DefaultInstance { + return base + "-" + name + } + return base +} + // systemdUnits drives the archie user units through systemctl --user. type systemdUnits struct{} @@ -23,11 +34,11 @@ func (systemdUnits) Managed(ctx context.Context) bool { } func (systemdUnits) Start(ctx context.Context, unit string) error { - return systemctl(ctx, "restart", unit+".service") + return systemctl(ctx, "restart", instanceUnit(unit)+".service") } func (systemdUnits) Stop(ctx context.Context, unit string) error { - return systemctl(ctx, "stop", unit+".service") + return systemctl(ctx, "stop", instanceUnit(unit)+".service") } func systemctl(ctx context.Context, args ...string) error {