From 6b672aaa7b34798d488a41fb15e50e9f2af3e931 Mon Sep 17 00:00:00 2001 From: Marco Craveiro Date: Sat, 5 Dec 2026 16:43:56 +0000 Subject: [PATCH] [build] Ship a systemd user unit for the service One unit serves every environment of a repository's database, as the Emacs daemon does, not one unit per environment: the checkout names the repository and its database holds every environment. It listens on 21080, outside ORE Studio's range whose base is 20600. The release carries the unit, an example environment file and an installer. The installer adds the checkout's .env as a drop-in so the database password reaches the service, then enables and starts it. Co-Authored-By: Claude Code --- .../run_compass_beside_a_project/story.org | 1 + ...sk_ship_a_systemd_unit_for_the_service.org | 106 ++++++++++++++++++ .../architecture/installed_compass.org | 6 +- .../how_do_i_run_compass_as_a_service.org | 69 ++++++++++++ share/CMakeLists.txt | 10 +- share/systemd/compass.service | 37 ++++++ share/systemd/install.sh | 40 +++++++ share/systemd/service.env.example | 4 + 8 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/task_ship_a_systemd_unit_for_the_service.org create mode 100644 doc/recipes/compass/how_do_i_run_compass_as_a_service.org create mode 100644 share/systemd/compass.service create mode 100755 share/systemd/install.sh create mode 100644 share/systemd/service.env.example diff --git a/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/story.org b/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/story.org index d6b8aea3..f2e0191a 100644 --- a/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/story.org +++ b/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/story.org @@ -64,6 +64,7 @@ development, ORE Studio development, and ORE Studio production. | [[id:6CAD6D96-B800-4B93-ADC9-3C0E7FF8C1FC][Annotate pages inline]] | BACKLOG | | | A reviewer leaves margin annotations on a page's paragraphs and their state, without editing the page. | | [[id:B65B5CED-71AA-440E-B2C3-19D5562BCBC1][Run tests in production and route the results]] | BACKLOG | | | A testing team runs the sprint's scenarios against a deployed environment and the recorded campaign reaches the development team's git. | | [[id:BB5F0800-1DD0-4E3A-B917-FF06209C7152][Serve production readers]] | BACKLOG | | | The production deployment serves the manual, the project's documents and compass's over compass.web's endpoints and MCP, with views and roles that admit test writes only. | +| [[id:4711A670-529E-4051-A8D8-27E574BD1379][Ship a systemd unit for the service]] | DONE | 2026-12-05 | 2026-12-05 | One user unit serves every environment of a repository's database, on port 21080 outside ORE Studio's range, and the release carries the unit and an installer. | * Decisions diff --git a/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/task_ship_a_systemd_unit_for_the_service.org b/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/task_ship_a_systemd_unit_for_the_service.org new file mode 100644 index 00000000..ef5d117d --- /dev/null +++ b/doc/agile/versions/v0/sprint_02/run_compass_beside_a_project/task_ship_a_systemd_unit_for_the_service.org @@ -0,0 +1,106 @@ +:PROPERTIES: +:ID: 4711A670-529E-4051-A8D8-27E574BD1379 +:END: +#+title: Task: Ship a systemd unit for the service +#+description: One user unit serves every environment of a repository's database, on port 21080 outside ORE Studio's range, and the release carries the unit and an installer. +#+type: task +#+level: s1 +#+filetags: :systemd:service:sprint_02:v0:run_compass_beside_a_project: +#+owner: marco +#+branch: +#+pr: +#+blocked_on: +#+blocked_since: +#+created: 2026-12-05 +#+updated: 2026-12-05 +#+environment: +#+todo: DISCOVERED BACKLOG STARTED BLOCKED | DONE ABANDONED + +This page documents a [[id:31C868B9-306E-4282-BA8C-07E647021B34][task]] in the [[id:955957FD-5E73-41E8-B2C0-C9A6EE7997BD][Run compass beside a project]] story. It captures the goal, current status, acceptance, and any notes or results. + +* Goal + +One systemd user unit serves every environment of a repository's database, +as the Emacs daemon is one unit, on port 21080 outside ORE Studio's range. +The release carries the unit, an example environment file and an installer +that points it at a checkout and starts it. + +* Status + +| Field | Value | +|--------------+----------------------------------------| +| State | DONE | +| Parent story | [[id:955957FD-5E73-41E8-B2C0-C9A6EE7997BD][Run compass beside a project]] | +| Now | Shipped: the unit is installed and active against an ORE Studio checkout. | +| Waiting on | Nothing. | +| Next | Nothing. | +| Last touched | 2026-12-05 | + +* Acceptance + +- =share/compass/systemd/= carries =compass.service=, =service.env.example= + and =install.sh=, and the package installs it. +- The unit is one user unit for every environment of the checkout's + database, not one per environment. +- =install.sh --checkout = writes the environment file and the unit, + adds the checkout's =.env= as a drop-in, and starts the service. +- The service listens on 21080, outside ORE Studio's 20600 base, and answers + compass.web, the HTTP routes and MCP. + +* Plan + +1. Write the user unit: =EnvironmentFile= for the checkout and address, + =ExecStart=%h/.local/bin/compass-ng -C ${COMPASS_CHECKOUT} serve + --listen ${COMPASS_LISTEN}=, restart on failure. +2. Write =install.sh= to write both files, add the checkout's =.env= as a + drop-in so =PGPASSWORD= reaches the service, and enable it. +3. Stage the directory beside the MASD library and install it under + =share/compass/systemd=. +4. Verify on this machine against an ORE Studio checkout. + +* Measurements + +Every number this task claims about a tree, with the command that produced it +and the commit it was measured at. A number without both is not a measurement, +because a clean-up record describes a tree and trunk moves under a branch: a +consumer count, a gate's component count and a suite's size all change with no +code changing, and the reader of a bare number cannot tell which tree it came +from. Where the raw output matters, put it in an =evidence/= file beside the +task and name that file in the Command column. + +| What | Value | Command | Commit | +|------+-------+---------+--------| +| Service state | active, 42 routes, MCP at =/mcp= | =systemctl --user is-active compass.service= | working tree, 2026-12-05 | +| compass.web | HTTP 200, 1586 bytes | =curl -fsS http://127.0.0.1:21080/= | working tree, 2026-12-05 | +| Unit syntax | no diagnostics from compass.service | =systemd-analyze verify share/systemd/compass.service= | working tree, 2026-12-05 | + +* Notes + +* Test Scenarios + +Manual QA scenarios (scaffolded via =compass add test_scenario=) +that verify this task. Link +new ones here as they're created; the scenario doc itself links back +via its "Verifies task" field. + +| Scenario | State | Notes | +|----------+-------+-------| +| | | | + +* PRs + +| PR | Title | +|----+-------| +| | | + +* Review + +| # | Comment summary | File | Decision | Notes | +|---+-----------------+------+----------+-------| +| | | | | | + +* Result + +The unit runs against =ores_dev_bright_faraday= and serves every environment +of its database on 21080. The checkout's =.env= supplies the database +password through a drop-in; install.sh adds it by reference. diff --git a/doc/knowledge/architecture/installed_compass.org b/doc/knowledge/architecture/installed_compass.org index 6652f059..d994c009 100644 --- a/doc/knowledge/architecture/installed_compass.org +++ b/doc/knowledge/architecture/installed_compass.org @@ -28,13 +28,17 @@ A release installs, under =~/.local/opt/compass//=: | =bin/compass= | the one binary; the compilers, codecs and service | | =share/compass/masd/= | the MASD library: templates, archetype pages, the catalogue | | =share/compass/web/= | compass.web's built pages | +| =share/compass/systemd/= | a user unit, its environment example and installer | | =THIRD_PARTY_NOTICES= | the licences of every library linked | =install_compass.sh= installs a named release after checking its checksum, installs a build of =main= with =--build=, and reads a pinned version file so a project can state which release it needs. The binary resolves its library and web pages beside its own directory, so a project reads them from the -release, not from a checkout of compass. +release, not from a checkout of compass. =share/compass/systemd/= holds one +user unit for every environment of the checkout's database, as the Emacs +daemon is one unit: =install.sh --checkout = points it at a worktree and +starts it on port 21080, outside ORE Studio's range. Each project has its own PostgreSQL database, named in the repository's local settings (=.git/compass/local.org=). A database holds *environments*: one per diff --git a/doc/recipes/compass/how_do_i_run_compass_as_a_service.org b/doc/recipes/compass/how_do_i_run_compass_as_a_service.org new file mode 100644 index 00000000..732ce3cb --- /dev/null +++ b/doc/recipes/compass/how_do_i_run_compass_as_a_service.org @@ -0,0 +1,69 @@ +:PROPERTIES: +:ID: A3805F2A-9815-44B6-8205-1D06B727E6D5 +:END: +#+title: How do I run compass as a service? +#+description: Run one compass service for every environment of a repository's database, as a systemd user unit on a port outside ORE Studio's range. +#+type: recipe +#+level: cross +#+filetags: :compass:systemd:recipe: +#+created: 2026-12-05 +#+updated: 2026-12-05 + +A compass service is one user unit for every environment of a repository's +database, as the Emacs daemon is one unit, rather than one unit per +environment as ORE Studio's services are. The checkout names the repository; +the service answers every environment synced from its worktrees. It listens +on 21080, outside ORE Studio's range, whose base is 20600. + +* Question + +How do I run compass as a service for all of my ORE Studio environments? + +* Answer + +1. /Install/ a compass release, which carries the unit under + =share/compass/systemd/=; see + [[id:92AD0A4C-4A68-431E-8581-55E6C56DC2EB][How do I install or update the shared compass?]]. + +2. /Install and start/ the service, naming any worktree of the repository. + The script writes =~/.config/compass/service.env= and the user unit, + adds the checkout's =.env= as a drop-in so the database password reaches + the service, then enables and starts it: + + #+begin_src sh :results verbatim +share/compass/systemd/install.sh \ + --checkout ~/Development/OreStudio/ores_dev_bright_faraday + #+end_src + + Use =--listen = to move it from =127.0.0.1:21080=. + +3. /Check/ that it answers, and that the environments are the ones synced + into the repository's database: + + #+begin_src sh :results verbatim +systemctl --user status compass.service +curl -fsS http://127.0.0.1:21080/compass/environments + #+end_src + +* Script + +- =share/compass/systemd/install.sh= writes the environment file, installs + the unit and starts it. +- =share/compass/systemd/compass.service= is the unit: a systemd user unit + whose =EnvironmentFile= is =~/.config/compass/service.env=, with + =COMPASS_CHECKOUT= and =COMPASS_LISTEN=. +- =share/compass/systemd/service.env.example= is the environment file to + copy when installing by hand. A drop-in under + =~/.config/systemd/user/compass.service.d/= adds the checkout's =.env=, + where =PGPASSWORD= lives. + +* Tested by + +A manual smoke test: start the unit against a synced checkout and open +=compass.web= at the address. CI has no user systemd manager, so no job runs +it. + +* See also + +- [[id:4CA0B3E0-B33D-47A3-BCC6-7BF9DEFBA7CF][Installed compass]], the three + uses the service must serve. diff --git a/share/CMakeLists.txt b/share/CMakeLists.txt index 910ab8b8..ac43d980 100644 --- a/share/CMakeLists.txt +++ b/share/CMakeLists.txt @@ -23,12 +23,18 @@ # gone from the build tree too. set(masd_library_source ${CMAKE_CURRENT_SOURCE_DIR}/masd) set(masd_library_staged ${COMPASS_PUBLISH_DIRECTORY}/share/compass/masd) +set(systemd_source ${CMAKE_CURRENT_SOURCE_DIR}/systemd) +set(systemd_staged ${COMPASS_PUBLISH_DIRECTORY}/share/compass/systemd) add_custom_target(compass.masd_library ALL COMMAND "${CMAKE_COMMAND}" -E rm -rf "${masd_library_staged}" COMMAND "${CMAKE_COMMAND}" -E copy_directory "${masd_library_source}" "${masd_library_staged}" - COMMENT "Staging the MASD library beside the compass binary" + COMMAND "${CMAKE_COMMAND}" -E rm -rf "${systemd_staged}" + COMMAND "${CMAKE_COMMAND}" -E copy_directory "${systemd_source}" + "${systemd_staged}" + COMMENT "Staging the MASD library and the systemd unit beside the" + "compass binary" VERBATIM) # The codegen tests resolve @compass against their own binary in @@ -38,3 +44,5 @@ if(TARGET compass.codegen.tests) endif() install(DIRECTORY ${masd_library_source}/ DESTINATION share/compass/masd) +install(DIRECTORY ${systemd_source}/ DESTINATION share/compass/systemd + USE_SOURCE_PERMISSIONS) diff --git a/share/systemd/compass.service b/share/systemd/compass.service new file mode 100644 index 00000000..f42ec64c --- /dev/null +++ b/share/systemd/compass.service @@ -0,0 +1,37 @@ +# Installed with compass as share/compass/systemd/compass.service. +# +# This is one user unit for every environment of a repository's database, +# like the Emacs daemon, not one unit per environment: the checkout names +# the repository, and the service answers every environment synced from its +# worktrees. +# +# Install it with install.sh beside this file, or by hand: +# +# mkdir -p ~/.config/compass ~/.config/systemd/user +# printf 'COMPASS_CHECKOUT=%s\nCOMPASS_LISTEN=%s\n' \ +# "$HOME/Development/OreStudio/" '127.0.0.1:21080' \ +# > ~/.config/compass/service.env +# cp compass.service ~/.config/systemd/user/ +# systemctl --user daemon-reload +# systemctl --user enable --now compass.service +# +# The port is outside ORE Studio's range, whose base is 20600. The +# checkout's .env carries the database password; install.sh adds it as a +# drop-in EnvironmentFile when the checkout has one. + +[Unit] +Description=Compass document service +Documentation=https://github.com/MASD-Project/compass +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +EnvironmentFile=%h/.config/compass/service.env +WorkingDirectory=%h +ExecStart=%h/.local/bin/compass-ng -C ${COMPASS_CHECKOUT} serve --listen ${COMPASS_LISTEN} +Restart=on-failure +RestartSec=3 + +[Install] +WantedBy=default.target diff --git a/share/systemd/install.sh b/share/systemd/install.sh new file mode 100755 index 00000000..3a8ad42b --- /dev/null +++ b/share/systemd/install.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Installs compass as a systemd user service and starts it. +set -euo pipefail + +usage() { + echo "usage: install.sh --checkout [--listen ]" >&2 + echo " --checkout a worktree of the repository to serve" >&2 + echo " --listen the address to listen on (default: 127.0.0.1:21080)" >&2 +} + +checkout="" +listen="127.0.0.1:21080" +while [ $# -gt 0 ]; do + case "$1" in + --checkout) checkout="${2:-}"; shift 2 ;; + --listen) listen="${2:-}"; shift 2 ;; + -h|--help) usage; exit 0 ;; + *) usage; exit 2 ;; + esac +done +[ -n "$checkout" ] || { usage; exit 2; } +[ -e "$checkout/.git" ] || { + echo "install.sh: $checkout is not a git checkout" >&2; exit 1; } +[ -x "${HOME}/.local/bin/compass-ng" ] || { + echo "install.sh: ${HOME}/.local/bin/compass-ng is missing; install" \ + "compass first" >&2; exit 1; } + +config="${XDG_CONFIG_HOME:-$HOME/.config}" +mkdir -p "$config/compass" "$config/systemd/user/compass.service.d" +printf 'COMPASS_CHECKOUT=%s\nCOMPASS_LISTEN=%s\n' "$checkout" "$listen" \ + > "$config/compass/service.env" +install -m 644 "$(dirname "$0")/compass.service" \ + "$config/systemd/user/compass.service" +if [ -f "$checkout/.env" ]; then + printf '[Service]\nEnvironmentFile=-%s/.env\n' "$checkout" \ + > "$config/systemd/user/compass.service.d/50-checkout-env.conf" +fi +systemctl --user daemon-reload +systemctl --user enable --now compass.service +systemctl --user --no-pager --lines=0 status compass.service diff --git a/share/systemd/service.env.example b/share/systemd/service.env.example new file mode 100644 index 00000000..fa523dda --- /dev/null +++ b/share/systemd/service.env.example @@ -0,0 +1,4 @@ +# Copy to ~/.config/compass/service.env. +# The checkout names the repository; its database holds every environment. +COMPASS_CHECKOUT=/home/you/Development/OreStudio/ores_dev_bright_faraday +COMPASS_LISTEN=127.0.0.1:21080