@edspencer/paddock 0.82.0 → 0.82.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/packages/server/dist/cli/args.js +2 -2
- package/packages/server/dist/cli/busy-command.js +1 -1
- package/packages/server/dist/cli/busy.js +10 -12
- package/packages/server/dist/cli/exit-codes.d.ts +21 -45
- package/packages/server/dist/cli/exit-codes.js +42 -45
- package/packages/server/dist/cli/output.js +5 -6
- package/packages/server/dist/cli/paddock.js +9 -9
- package/packages/server/dist/cli/service/index.d.ts +2 -2
- package/packages/server/dist/cli/service/index.js +30 -24
- package/packages/server/dist/cli/service/launchd.d.ts +31 -84
- package/packages/server/dist/cli/service/launchd.js +104 -304
- package/packages/server/dist/cli/service/logs.d.ts +9 -28
- package/packages/server/dist/cli/service/logs.js +33 -75
- package/packages/server/dist/cli/service/manager.d.ts +93 -0
- package/packages/server/dist/cli/service/manager.js +21 -0
- package/packages/server/dist/cli/service/path-env.d.ts +7 -18
- package/packages/server/dist/cli/service/path-env.js +30 -81
- package/packages/server/dist/cli/service/spec.d.ts +4 -31
- package/packages/server/dist/cli/service/spec.js +9 -0
- package/packages/server/dist/cli/service/systemd.d.ts +27 -101
- package/packages/server/dist/cli/service/systemd.js +106 -262
- package/packages/server/dist/cli/update/health.d.ts +48 -0
- package/packages/server/dist/cli/update/health.js +64 -0
- package/packages/server/dist/cli/update/install-method.d.ts +29 -76
- package/packages/server/dist/cli/update/install-method.js +103 -178
- package/packages/server/dist/cli/update/lock.d.ts +7 -12
- package/packages/server/dist/cli/update/lock.js +28 -62
- package/packages/server/dist/cli/update/package-manager.d.ts +39 -74
- package/packages/server/dist/cli/update/package-manager.js +95 -160
- package/packages/server/dist/cli/update/registry.d.ts +17 -54
- package/packages/server/dist/cli/update/registry.js +46 -89
- package/packages/server/dist/cli/update/semver.d.ts +18 -41
- package/packages/server/dist/cli/update/semver.js +74 -124
- package/packages/server/dist/cli/update/update-check.d.ts +15 -26
- package/packages/server/dist/cli/update/update-check.js +30 -60
- package/packages/server/dist/cli/update/update.d.ts +57 -91
- package/packages/server/dist/cli/update/update.js +230 -431
- package/packages/server/dist/cli/update-command.js +3 -5
- package/packages/server/package.json +1 -1
- package/packages/web/dist/assets/{DeleteProjectDialog-CphdGB3R.js → DeleteProjectDialog-CgHpQctt.js} +1 -1
- package/packages/web/dist/assets/DiscoverPage-CG6cf-0i.js +2 -0
- package/packages/web/dist/assets/{DiscoverView-B_kcF4sm.js → DiscoverView-BKqI0I1E.js} +1 -1
- package/packages/web/dist/assets/{InstanceConfigPage-CvUK5DKW.js → InstanceConfigPage-DL3raCK1.js} +1 -1
- package/packages/web/dist/assets/{MarkdownRenderer-DvZ-j_Ao.js → MarkdownRenderer-DcjEuf3s.js} +1 -1
- package/packages/web/dist/assets/{ProjectRedirect-DayHvUH2.js → ProjectRedirect-DerkA-nE.js} +1 -1
- package/packages/web/dist/assets/{ProjectView-CCw83gUi.js → ProjectView-NwfC3_w7.js} +3 -3
- package/packages/web/dist/assets/{ProjectsGrid-B3Klg2H4.js → ProjectsGrid-BeORa0UH.js} +1 -1
- package/packages/web/dist/assets/{RootHome-BxcDvpgI.js → RootHome-BYJs8qJ6.js} +1 -1
- package/packages/web/dist/assets/{Surfaces-7UlhFBSG.js → Surfaces-w-kEaAYU.js} +1 -1
- package/packages/web/dist/assets/{arc-DNkh5o5Q.js → arc-Bv56hasM.js} +1 -1
- package/packages/web/dist/assets/{architectureDiagram-3BPJPVTR-BrgN3oLF.js → architectureDiagram-3BPJPVTR-BHEknx9i.js} +1 -1
- package/packages/web/dist/assets/{blockDiagram-GPEHLZMM-D05XOJSk.js → blockDiagram-GPEHLZMM-DtxTMrPX.js} +1 -1
- package/packages/web/dist/assets/{c4Diagram-AAUBKEIU-CKaMBhMP.js → c4Diagram-AAUBKEIU-DRpEYOlM.js} +1 -1
- package/packages/web/dist/assets/channel-CdIiAqY2.js +2 -0
- package/packages/web/dist/assets/{chunk-2J33WTMH-8hgadCQc.js → chunk-2J33WTMH-S4s7GCg7.js} +1 -1
- package/packages/web/dist/assets/{chunk-4BX2VUAB-CBfEC-pu.js → chunk-4BX2VUAB-BkWVPLf1.js} +1 -1
- package/packages/web/dist/assets/{chunk-55IACEB6-DV1eMgHk.js → chunk-55IACEB6-4-FQp01F.js} +1 -1
- package/packages/web/dist/assets/{chunk-727SXJPM-Cfgl5fxY.js → chunk-727SXJPM-BeyDWdIb.js} +1 -1
- package/packages/web/dist/assets/{chunk-AQP2D5EJ-BXhqD295.js → chunk-AQP2D5EJ-ZSxKy745.js} +1 -1
- package/packages/web/dist/assets/{chunk-FMBD7UC4-TFGOPtek.js → chunk-FMBD7UC4-Bq8uV9j-.js} +1 -1
- package/packages/web/dist/assets/{chunk-ND2GUHAM-CHFM665R.js → chunk-ND2GUHAM-BOFsRyHk.js} +1 -1
- package/packages/web/dist/assets/{chunk-QZHKN3VN-CqEf6MMT.js → chunk-QZHKN3VN-uZ6oNc4m.js} +1 -1
- package/packages/web/dist/assets/classDiagram-4FO5ZUOK-DCCUHba0.js +2 -0
- package/packages/web/dist/assets/classDiagram-v2-Q7XG4LA2-DCCUHba0.js +2 -0
- package/packages/web/dist/assets/{core-DmEaKxBb.js → core-CYuJAM2U.js} +1 -1
- package/packages/web/dist/assets/{cose-bilkent-S5V4N54A-BnGI6jkr.js → cose-bilkent-S5V4N54A-DIzkVSFb.js} +1 -1
- package/packages/web/dist/assets/{dagre-BM42HDAG-CKoPx61E.js → dagre-BM42HDAG-CHR6jJBv.js} +1 -1
- package/packages/web/dist/assets/{diagram-2AECGRRQ-mMmtlBl1.js → diagram-2AECGRRQ-BAFXCH8Q.js} +1 -1
- package/packages/web/dist/assets/{diagram-5GNKFQAL-Cdz9Mtnt.js → diagram-5GNKFQAL-BJ5t5gbz.js} +1 -1
- package/packages/web/dist/assets/{diagram-KO2AKTUF-DQxM3wYj.js → diagram-KO2AKTUF-D9Xt0IVg.js} +1 -1
- package/packages/web/dist/assets/{diagram-LMA3HP47-BWrinPsq.js → diagram-LMA3HP47-Bq38u6sy.js} +1 -1
- package/packages/web/dist/assets/{diagram-OG6HWLK6-U6MYUve2.js → diagram-OG6HWLK6-CMQVqkpi.js} +1 -1
- package/packages/web/dist/assets/{documentTitle-DGjRXuTP.js → documentTitle-M1lrfXRU.js} +1 -1
- package/packages/web/dist/assets/{erDiagram-TEJ5UH35-xqMCsXxY.js → erDiagram-TEJ5UH35-C63M1g9b.js} +1 -1
- package/packages/web/dist/assets/{flowDiagram-I6XJVG4X-PMfy_czF.js → flowDiagram-I6XJVG4X-D3j8ACN7.js} +1 -1
- package/packages/web/dist/assets/{ganttDiagram-6RSMTGT7-whhYYE4N.js → ganttDiagram-6RSMTGT7-B1Fd1A2Y.js} +1 -1
- package/packages/web/dist/assets/{gitGraphDiagram-PVQCEYII-C51JW4tf.js → gitGraphDiagram-PVQCEYII-CEhZhDdR.js} +1 -1
- package/packages/web/dist/assets/{index-CHOtvlnO.js → index-1E9yHciS.js} +2 -2
- package/packages/web/dist/assets/{infoDiagram-5YYISTIA-SH8cr905.js → infoDiagram-5YYISTIA-Ctquaew6.js} +1 -1
- package/packages/web/dist/assets/{ishikawaDiagram-YF4QCWOH-BjqaoU3U.js → ishikawaDiagram-YF4QCWOH-DV4GvA1R.js} +1 -1
- package/packages/web/dist/assets/{journeyDiagram-JHISSGLW-CBmdEVrd.js → journeyDiagram-JHISSGLW-C_Cy9waA.js} +1 -1
- package/packages/web/dist/assets/{kanban-definition-UN3LZRKU-U8njjMLZ.js → kanban-definition-UN3LZRKU-CGr9ctEf.js} +1 -1
- package/packages/web/dist/assets/{linear-DF66GPs1.js → linear-D3U8NOkt.js} +1 -1
- package/packages/web/dist/assets/{mermaid.core-CouJz7Ph.js → mermaid.core-BiArdWKV.js} +4 -4
- package/packages/web/dist/assets/{mindmap-definition-RKZ34NQL-BYIf0oFt.js → mindmap-definition-RKZ34NQL-CZIpzj8Z.js} +1 -1
- package/packages/web/dist/assets/{pieDiagram-4H26LBE5-Cm8rgg09.js → pieDiagram-4H26LBE5-1MaeS3rt.js} +1 -1
- package/packages/web/dist/assets/{quadrantDiagram-W4KKPZXB-C3Il5kOH.js → quadrantDiagram-W4KKPZXB-B5pgQ54e.js} +1 -1
- package/packages/web/dist/assets/{requirementDiagram-4Y6WPE33-D3PE2nTe.js → requirementDiagram-4Y6WPE33-BNnxrpGr.js} +1 -1
- package/packages/web/dist/assets/{sankeyDiagram-5OEKKPKP-DdmvBM18.js → sankeyDiagram-5OEKKPKP-DsxVH-hM.js} +1 -1
- package/packages/web/dist/assets/{sequenceDiagram-3UESZ5HK-B8M_GAIq.js → sequenceDiagram-3UESZ5HK-Ke9LcFZI.js} +1 -1
- package/packages/web/dist/assets/{stateDiagram-AJRCARHV-DZkXnK1_.js → stateDiagram-AJRCARHV-UJzmflND.js} +1 -1
- package/packages/web/dist/assets/stateDiagram-v2-BHNVJYJU-DgUc3HkP.js +2 -0
- package/packages/web/dist/assets/{timeline-definition-PNZ67QCA-IHXq7rqi.js → timeline-definition-PNZ67QCA-BRjNALVI.js} +1 -1
- package/packages/web/dist/assets/{vennDiagram-CIIHVFJN-Dga7tLtT.js → vennDiagram-CIIHVFJN-ClMAspxz.js} +1 -1
- package/packages/web/dist/assets/{wardley-L42UT6IY-CmcwnEJ-.js → wardley-L42UT6IY-UPv4JEmz.js} +1 -1
- package/packages/web/dist/assets/{wardleyDiagram-YWT4CUSO-zHuSLrD4.js → wardleyDiagram-YWT4CUSO-BgYJbdEx.js} +1 -1
- package/packages/web/dist/assets/{xychartDiagram-2RQKCTM6-D93MU_ic.js → xychartDiagram-2RQKCTM6-Bfk5HjGx.js} +1 -1
- package/packages/web/dist/index.html +1 -1
- package/packages/web/dist/sw.js +1 -1
- package/packages/server/dist/cli/service/backend.d.ts +0 -115
- package/packages/server/dist/cli/service/backend.js +0 -19
- package/packages/web/dist/assets/DiscoverPage-C4OPLNO8.js +0 -2
- package/packages/web/dist/assets/channel-JrEedxB8.js +0 -2
- package/packages/web/dist/assets/classDiagram-4FO5ZUOK-CHJiQp-W.js +0 -2
- package/packages/web/dist/assets/classDiagram-v2-Q7XG4LA2-CHJiQp-W.js +0 -2
- package/packages/web/dist/assets/stateDiagram-v2-BHNVJYJU-CjCdT9Ls.js +0 -2
|
@@ -1,152 +1,56 @@
|
|
|
1
|
+
// From edspencer/cli-contract reference/service/systemd.ts @ 0.2; local changes: `.ts` import specifiers → `.js`.
|
|
2
|
+
// Linux: a systemd user unit (`systemctl --user`, SPEC §7), running as the user. Without linger the user's manager,
|
|
3
|
+
// and the service with it, stops at their last logout, SSH sessions included; `linger()` asks, and the tool prints
|
|
4
|
+
// the `sudo loginctl enable-linger` line rather than running it. Node built-ins only.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import path from 'node:path';
|
|
1
8
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* ## Not the server story
|
|
5
|
-
*
|
|
6
|
-
* Paddock already documents itself under systemd — `guides/proxmox-lxc.md`
|
|
7
|
-
* Path B, provisioned by the `paddock-deploy` Ansible recipes. That is a
|
|
8
|
-
* SYSTEM unit, running as a service user, behind a reverse proxy, with a token
|
|
9
|
-
* in its environment. This is the other one: a user unit, running as you, on
|
|
10
|
-
* loopback, using the Claude login you already have. They should not be
|
|
11
|
-
* confused, and this module writes nothing outside `~/.config/systemd/user`.
|
|
12
|
-
*
|
|
13
|
-
* ## Lingering
|
|
14
|
-
*
|
|
15
|
-
* A `--user` manager is started at your first login and stopped at your last
|
|
16
|
-
* logout, so by default the unit dies when you log out — including when you log
|
|
17
|
-
* out of an SSH session, which is how most people meet this. `loginctl
|
|
18
|
-
* enable-linger <user>` keeps the manager (and therefore Paddock) alive.
|
|
19
|
-
*
|
|
20
|
-
* That is called out in the install output rather than being run for the user:
|
|
21
|
-
* lingering is a real change to how the machine behaves after they leave it,
|
|
22
|
-
* and it needs a privilege this command otherwise never asks for.
|
|
23
|
-
*/
|
|
24
|
-
import fs from "node:fs";
|
|
25
|
-
import os from "node:os";
|
|
26
|
-
import path from "node:path";
|
|
27
|
-
import { SYSTEMD_UNIT } from "./spec.js";
|
|
28
|
-
/**
|
|
29
|
-
* `~/.config/systemd/user/paddock.service`, honouring `XDG_CONFIG_HOME`.
|
|
30
|
-
*
|
|
31
|
-
* ## An injected home outranks the ambient variable
|
|
32
|
-
*
|
|
33
|
-
* The precedence is: an explicit `xdgConfigHome` argument, then an explicitly
|
|
34
|
-
* **injected** `homeDir`, then — only when the caller named neither — the real
|
|
35
|
-
* environment. That middle rung is the whole point, and getting it wrong was a
|
|
36
|
-
* live bug rather than a style question.
|
|
37
|
-
*
|
|
38
|
-
* The first version read `process.env.XDG_CONFIG_HOME` before falling back to
|
|
39
|
-
* `homeDir`. On any machine with that variable set — CI, and plenty of Linux
|
|
40
|
-
* desktops — the `homeDir` parameter was therefore **a lie**: a caller passing a
|
|
41
|
-
* throwaway directory still resolved to the real config home. Two things
|
|
42
|
-
* followed. Test isolation quietly stopped working, so which assertion passed
|
|
43
|
-
* depended on test ORDER rather than test content. And the suite wrote a real
|
|
44
|
-
* `paddock.service` into the runner's actual config directory, outside every
|
|
45
|
-
* temp dir it thought it was confined to — which on a contributor's machine
|
|
46
|
-
* would have installed a unit they never asked for.
|
|
47
|
-
*
|
|
48
|
-
* So: passing a home is a statement about where home is, and it wins. The
|
|
49
|
-
* ambient read survives only on the branch where nobody said otherwise, which
|
|
50
|
-
* is the real-use branch and the one where honouring `XDG_CONFIG_HOME` is
|
|
51
|
-
* correct. (`paddock.ts` threads the variable in explicitly as well, so the
|
|
52
|
-
* production path does not depend on this fallback — it is here so that a
|
|
53
|
-
* caller with no opinion still gets the right answer.)
|
|
9
|
+
* `${XDG_CONFIG_HOME:-~/.config}/systemd/user/<unit>`. A home passed in beats the ambient `XDG_CONFIG_HOME`: a test
|
|
10
|
+
* that names a throwaway home must not write a unit into the real config folder because the variable was set.
|
|
54
11
|
*/
|
|
55
|
-
export function unitPath(homeDir, xdgConfigHome) {
|
|
56
|
-
const base = xdgConfigHome ??
|
|
57
|
-
|
|
58
|
-
? path.join(homeDir, ".config")
|
|
59
|
-
: (process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config")));
|
|
60
|
-
return path.join(base, "systemd", "user", SYSTEMD_UNIT);
|
|
12
|
+
export function unitPath(id, homeDir, xdgConfigHome) {
|
|
13
|
+
const base = xdgConfigHome ?? (homeDir !== undefined ? path.join(homeDir, '.config') : (process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), '.config')));
|
|
14
|
+
return path.join(base, 'systemd', 'user', id.unit);
|
|
61
15
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* `%` starts a specifier EVERYWHERE in a unit file, so it is doubled; `$`
|
|
69
|
-
* starts a variable in `ExecStart`, so it is doubled there too.
|
|
70
|
-
*/
|
|
71
|
-
const pct = (s) => s.replace(/%/g, "%%");
|
|
72
|
-
const cEscape = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\t/g, "\\t");
|
|
73
|
-
const cUnescape = (s) => s.replace(/\\(.)/g, (_, c) => (c === "n" ? "\n" : c === "t" ? "\t" : c));
|
|
74
|
-
/** One `ExecStart` word: specifiers and variables escaped, quoted when it has to be. */
|
|
16
|
+
// `%` starts a specifier anywhere in a unit file, so it's doubled; `$` starts a variable in `ExecStart`, so it's
|
|
17
|
+
// doubled there too.
|
|
18
|
+
const pct = (s) => s.replace(/%/g, '%%');
|
|
19
|
+
const cEscape = (s) => s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\t/g, '\\t');
|
|
20
|
+
const cUnescape = (s) => s.replace(/\\(.)/g, (_, c) => (c === 'n' ? '\n' : c === 't' ? '\t' : c));
|
|
21
|
+
/** One `ExecStart` word: specifiers and variables escaped, and quoted when it has to be. */
|
|
75
22
|
export function execWord(a) {
|
|
76
|
-
const s = pct(a).replace(/\$/g,
|
|
77
|
-
return s ===
|
|
23
|
+
const s = pct(a).replace(/\$/g, '$$$$');
|
|
24
|
+
return s === '' || /[\s"'\\;]/.test(s) ? `"${cEscape(s)}"` : s;
|
|
78
25
|
}
|
|
79
|
-
/**
|
|
80
|
-
* The other way: the words of an `ExecStart` line.
|
|
81
|
-
*
|
|
82
|
-
* Reads both shapes on purpose. A unit written before #796's quoting is a bare
|
|
83
|
-
* whitespace-separated line, and its owner is exactly the person whose `status`
|
|
84
|
-
* has to keep working — so unquoted words pass through unchanged, and quoted
|
|
85
|
-
* ones are unescaped.
|
|
86
|
-
*/
|
|
26
|
+
/** The words of an `ExecStart` line. Reads unquoted words too, so an older unit's `status` still works. */
|
|
87
27
|
export function execWords(line) {
|
|
88
|
-
|
|
89
|
-
for (const m of line.matchAll(/"((?:[^"\\]|\\.)*)"|(\S+)/g)) {
|
|
90
|
-
const w = m[1] !== undefined ? cUnescape(m[1]) : m[2];
|
|
91
|
-
out.push(w.replace(/\$\$/g, "$").replace(/%%/g, "%"));
|
|
92
|
-
}
|
|
93
|
-
return out;
|
|
28
|
+
return [...line.matchAll(/"((?:[^"\\]|\\.)*)"|(\S+)/g)].map((m) => (m[1] !== undefined ? cUnescape(m[1]) : m[2]).replace(/\$\$/g, '$').replace(/%%/g, '%'));
|
|
94
29
|
}
|
|
95
30
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* `Restart=always`, matching launchd's `KeepAlive=<true/>` (#872). It was
|
|
99
|
-
* `on-failure`, which for a service is the wrong default for the same reason it
|
|
100
|
-
* was wrong there: `start.ts` exits `0` on SIGTERM, so a logout — or anything
|
|
101
|
-
* else that signals the user manager — was read as an intended stop and Paddock
|
|
102
|
-
* stayed down.
|
|
103
|
-
*
|
|
104
|
-
* `always` does not make the unit unstoppable. systemd never restarts a unit it
|
|
105
|
-
* was itself asked to stop, so `systemctl --user stop` (and therefore
|
|
106
|
-
* `uninstall`) remains the clean, explicit way to put it down. That is the
|
|
107
|
-
* difference between an exit code, which has to guess at intent, and a command,
|
|
108
|
-
* which carries it.
|
|
109
|
-
*
|
|
110
|
-
* `WantedBy=default.target` is what makes `systemctl --user enable` mean
|
|
111
|
-
* "start at login" for a user unit; `multi-user.target` is the system-unit
|
|
112
|
-
* spelling and does nothing here.
|
|
113
|
-
*
|
|
114
|
-
* ## Logs go to a file, not the journal (SPEC §7)
|
|
115
|
-
*
|
|
116
|
-
* `StandardOutput=append:` / `StandardError=append:` name one file, the same
|
|
117
|
-
* one launchd's two keys name. The journal was the obvious choice and the wrong
|
|
118
|
-
* one for three reasons: `journalctl --user` shows nothing at all on a distro
|
|
119
|
-
* whose journal is volatile (the output is gone after a reboot, which is
|
|
120
|
-
* exactly when someone looks); it makes "where are the logs" have a different
|
|
121
|
-
* answer on each platform, so `paddock service logs` would be two
|
|
122
|
-
* implementations; and it rules out Windows, which the contract requires
|
|
123
|
-
* nothing rule out.
|
|
31
|
+
* The unit.
|
|
124
32
|
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* `%` starts a specifier anywhere in a unit file and `$` starts a variable in
|
|
131
|
-
* `ExecStart`, so both are escaped; a word containing whitespace, a quote or a
|
|
132
|
-
* backslash is quoted. Paths with a space in them are the common case
|
|
133
|
-
* (`/Users/Ed Spencer/.paddock`), and the old unqualified `join(" ")` wrote a
|
|
134
|
-
* unit systemd then split into the wrong arguments — a service that fails at
|
|
135
|
-
* login having printed nothing.
|
|
33
|
+
* - `Restart=always`, matching launchd's `KeepAlive`: a server exits 0 on SIGTERM, so `on-failure` would leave it
|
|
34
|
+
* down after a logout signals the user's manager. systemd never restarts a unit it was asked to stop.
|
|
35
|
+
* - Both streams `append:` to one file rather than the journal, which is volatile on many distributions and would
|
|
36
|
+
* make `logs` a different program on each OS. systemd creates the file, not its folder, so `install` makes that.
|
|
37
|
+
* - Every `ExecStart` word is escaped and quoted where needed: paths with spaces are common (`/Users/Ed Spencer/…`).
|
|
136
38
|
*/
|
|
137
|
-
export function renderUnit(spec) {
|
|
138
|
-
const exec = [spec.nodePath, spec.scriptPath, ...spec.args].map(execWord).join(
|
|
139
|
-
|
|
39
|
+
export function renderUnit(id, spec) {
|
|
40
|
+
const exec = [spec.nodePath, spec.scriptPath, ...spec.args].map(execWord).join(' ');
|
|
41
|
+
const env = Object.entries({ PATH: spec.pathEnv, ...spec.env })
|
|
42
|
+
.map(([k, v]) => `Environment="${cEscape(pct(`${k}=${v}`))}"`)
|
|
43
|
+
.join('\n');
|
|
44
|
+
return `# Generated by ${id.name}: \`${id.tool} service install\` rewrites this file.
|
|
140
45
|
# Node: ${spec.nodePath}
|
|
141
46
|
[Unit]
|
|
142
|
-
Description
|
|
143
|
-
Documentation
|
|
144
|
-
|
|
47
|
+
Description=${id.description}
|
|
48
|
+
${id.documentation !== undefined ? `Documentation=${id.documentation}\n` : ''}
|
|
145
49
|
[Service]
|
|
146
50
|
Type=simple
|
|
147
51
|
ExecStart=${exec}
|
|
148
52
|
WorkingDirectory=${pct(spec.workingDirectory)}
|
|
149
|
-
|
|
53
|
+
${env}
|
|
150
54
|
StandardOutput=append:${pct(spec.logPath)}
|
|
151
55
|
StandardError=append:${pct(spec.logPath)}
|
|
152
56
|
Restart=always
|
|
@@ -156,160 +60,101 @@ RestartSec=10
|
|
|
156
60
|
WantedBy=default.target
|
|
157
61
|
`;
|
|
158
62
|
}
|
|
159
|
-
/**
|
|
63
|
+
/** `ExecStart=`, read back as an argv. */
|
|
64
|
+
export function parseUnitArgv(text) {
|
|
65
|
+
const m = /^ExecStart=(.*)$/m.exec(text);
|
|
66
|
+
return m === null ? [] : execWords(m[1].trim());
|
|
67
|
+
}
|
|
68
|
+
/** The log the unit appends to, if it names one. */
|
|
160
69
|
export function parseUnitLogPath(text) {
|
|
161
70
|
const m = /^StandardOutput=append:(.*)$/m.exec(text);
|
|
162
|
-
return m === null ? undefined : m[1].trim().replace(/%%/g,
|
|
71
|
+
return m === null ? undefined : m[1].trim().replace(/%%/g, '%');
|
|
163
72
|
}
|
|
164
|
-
/**
|
|
165
|
-
* Does an installed unit carry `Restart=always` (#872)?
|
|
166
|
-
*
|
|
167
|
-
* The counterpart to `plistKeepsAlive`, and narrow for the same reason: the
|
|
168
|
-
* value it has to reject is the `on-failure` this file used to write, which
|
|
169
|
-
* leaves Paddock down after the graceful exit `start.ts` performs on SIGTERM.
|
|
170
|
-
* Anything unrecognised reads as "not the fixed shape", which at worst tells a
|
|
171
|
-
* hand-edited unit's owner to re-run install.
|
|
172
|
-
*/
|
|
73
|
+
/** Whether the unit has `Restart=always` (the older `on-failure` doesn't count). */
|
|
173
74
|
export function unitKeepsAlive(text) {
|
|
174
75
|
return /^Restart=always\s*$/m.test(text);
|
|
175
76
|
}
|
|
176
|
-
/**
|
|
177
|
-
export function
|
|
178
|
-
|
|
179
|
-
if (m === null)
|
|
180
|
-
return [];
|
|
181
|
-
return execWords(m[1].trim());
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* What a `systemctl` failure means, in the manager's own words where it has any.
|
|
185
|
-
*
|
|
186
|
-
* The `status === null` branch is the one QA found: `spawnSync` reports a
|
|
187
|
-
* missing binary that way, with no output at all, so the message was
|
|
188
|
-
* `systemctl --user enable --now failed (exit null).` and then a blank line —
|
|
189
|
-
* which tells someone on a container with no systemd nothing whatsoever. That
|
|
190
|
-
* is also the single likeliest way this command fails on a machine it was never
|
|
191
|
-
* going to work on.
|
|
192
|
-
*/
|
|
193
|
-
export function systemctlFailure(args, r) {
|
|
77
|
+
/** What a `systemctl` failure means, in the manager's words where it has any. */
|
|
78
|
+
export function systemctlFailure(id, args, r) {
|
|
79
|
+
// spawnSync reports a missing binary as status null with no output, which would print nothing useful.
|
|
194
80
|
if (r.status === null || r.status === 127) {
|
|
195
|
-
return (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
"Run `paddock serve` in a terminal, or keep it up with whatever this " +
|
|
199
|
-
"machine uses.");
|
|
81
|
+
return (`systemctl isn't available, so ${id.name} can't install a service here: it needs systemd's per-user instance ` +
|
|
82
|
+
`(\`systemctl --user\`), which a container or a distribution without systemd doesn't have. Run \`${id.tool} serve\` ` +
|
|
83
|
+
'in a terminal instead, or keep it running with whatever this machine uses.');
|
|
200
84
|
}
|
|
201
85
|
if (/Failed to connect to (user scope )?bus|DBUS_SESSION_BUS_ADDRESS|XDG_RUNTIME_DIR/.test(r.stderr)) {
|
|
202
|
-
return
|
|
203
|
-
"Run this from a login session, and check that $XDG_RUNTIME_DIR is set " +
|
|
204
|
-
"(usually /run/user/$(id -u)).");
|
|
86
|
+
return `can't reach systemd's per-user instance (${r.stderr.trim()}). Run this from a login session, with $XDG_RUNTIME_DIR set (usually /run/user/$(id -u)).`;
|
|
205
87
|
}
|
|
206
|
-
return
|
|
207
|
-
`${r.stderr || r.stdout}`.trim());
|
|
208
|
-
}
|
|
209
|
-
/** Run a `systemctl` and turn a non-zero exit into something actionable. */
|
|
210
|
-
function systemctlOrThrow(run, args) {
|
|
211
|
-
const r = run("systemctl", args);
|
|
212
|
-
if (r.status === 0)
|
|
213
|
-
return;
|
|
214
|
-
throw new Error(systemctlFailure(args, r));
|
|
88
|
+
return `systemctl ${args.join(' ')} failed (exit ${String(r.status)}): ${`${r.stderr || r.stdout}`.trim()}`;
|
|
215
89
|
}
|
|
216
|
-
export function
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
90
|
+
export function createSystemd(id, o) {
|
|
91
|
+
const { run } = o;
|
|
92
|
+
const file = unitPath(id, o.homeDir, o.xdgConfigHome);
|
|
93
|
+
const systemctl = (...args) => {
|
|
94
|
+
const r = run('systemctl', ['--user', ...args]);
|
|
95
|
+
if (r.status !== 0)
|
|
96
|
+
throw new Error(systemctlFailure(id, ['--user', ...args], r));
|
|
97
|
+
};
|
|
98
|
+
const isActive = () => run('systemctl', ['--user', 'is-active', id.unit]).stdout.trim() === 'active';
|
|
222
99
|
return {
|
|
223
|
-
platform:
|
|
100
|
+
platform: 'linux',
|
|
224
101
|
unitPath: file,
|
|
225
|
-
label:
|
|
102
|
+
label: id.unit,
|
|
103
|
+
kind: 'a systemd --user service',
|
|
226
104
|
install(spec) {
|
|
227
|
-
// Asked
|
|
228
|
-
|
|
229
|
-
const wasActive = fs.existsSync(file) && run("systemctl", ["--user", "is-active", SYSTEMD_UNIT]).stdout.trim() === "active";
|
|
105
|
+
// Asked before the unit is overwritten, while it still describes what's running.
|
|
106
|
+
const wasActive = fs.existsSync(file) && isActive();
|
|
230
107
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
231
108
|
fs.mkdirSync(spec.workingDirectory, { recursive: true });
|
|
232
|
-
// systemd creates the log FILE for `append:` but not its directory, and a
|
|
233
|
-
// missing directory fails the unit at start with the server never having
|
|
234
|
-
// run — no output anywhere, because the thing that would have written it
|
|
235
|
-
// is what failed.
|
|
236
109
|
fs.mkdirSync(path.dirname(spec.logPath), { recursive: true });
|
|
237
|
-
fs.writeFileSync(file, renderUnit(spec),
|
|
238
|
-
// Without
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
// reinstall over a running service wrote a new unit, left the old process serving, and
|
|
248
|
-
// reported success.
|
|
249
|
-
//
|
|
250
|
-
// That is not a theoretical gap: it is the one `paddock update`'s ROLLBACK walks into. The
|
|
251
|
-
// forward path stops the service first (SPEC §5 step 9), so `enable --now` starts the new
|
|
252
|
-
// version cleanly; the rollback runs `service install --refresh` while the FAILED version
|
|
253
|
-
// is still up, and without this the "restored" service was the broken one — which turns a
|
|
254
|
-
// clean exit 6 into an exit 7 and a machine nobody has fixed.
|
|
255
|
-
//
|
|
256
|
-
// Conditional rather than unconditional, even though `restart` would converge both states
|
|
257
|
-
// in one call: on a stopped unit that would be TWO starts milliseconds apart, and systemd's
|
|
258
|
-
// default start limit is five in ten seconds. Halving the headroom of a unit whose whole
|
|
259
|
-
// job is to come back is not worth saving a question.
|
|
260
|
-
//
|
|
261
|
-
// launchd needs no equivalent — its install is a bootout followed by a bootstrap, which
|
|
262
|
-
// already replaces a running job.
|
|
263
|
-
if (wasActive) {
|
|
264
|
-
const restartArgs = ["--user", "restart", SYSTEMD_UNIT];
|
|
265
|
-
const restarted = run("systemctl", restartArgs);
|
|
266
|
-
if (restarted.status !== 0)
|
|
267
|
-
throw new Error(systemctlFailure(restartArgs, restarted));
|
|
268
|
-
}
|
|
110
|
+
fs.writeFileSync(file, renderUnit(id, spec), 'utf8');
|
|
111
|
+
// Without a reload the manager keeps serving its cached copy of the old unit.
|
|
112
|
+
run('systemctl', ['--user', 'daemon-reload']);
|
|
113
|
+
systemctl('enable', '--now', id.unit);
|
|
114
|
+
// `enable --now` doesn't touch a unit that's already active: a reinstall over a running service would leave the
|
|
115
|
+
// old process serving and report success. That's the state `update`'s rollback refreshes in, while the failed
|
|
116
|
+
// version is still up. So restart, but only then: on a stopped unit it would be two starts milliseconds apart,
|
|
117
|
+
// and systemd's default limit is five starts in ten seconds.
|
|
118
|
+
if (wasActive)
|
|
119
|
+
systemctl('restart', id.unit);
|
|
269
120
|
},
|
|
270
121
|
uninstall() {
|
|
271
122
|
const existed = fs.existsSync(file);
|
|
272
|
-
run(
|
|
123
|
+
run('systemctl', ['--user', 'disable', '--now', id.unit]);
|
|
273
124
|
if (existed)
|
|
274
125
|
fs.rmSync(file, { force: true });
|
|
275
|
-
run(
|
|
126
|
+
run('systemctl', ['--user', 'daemon-reload']);
|
|
276
127
|
return existed;
|
|
277
128
|
},
|
|
278
129
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* comes back at your NEXT login, which is a different question from whether
|
|
283
|
-
* it is running now, and quietly answering both would make `stop` a
|
|
284
|
-
* surprise the following morning. `uninstall` is where that link is cut.
|
|
285
|
-
*
|
|
286
|
-
* `restart` re-reads the unit itself, but a `daemon-reload` first is what
|
|
287
|
-
* makes an EDITED unit take effect rather than the manager's cached copy —
|
|
288
|
-
* the same reason `install` reloads before enabling.
|
|
130
|
+
* `start` clears a failed state first: a unit that hit the start limit stays failed, and won't start, until
|
|
131
|
+
* `reset-failed`. None of the three touches enable or disable: whether it starts at the next login is a
|
|
132
|
+
* different question from whether it's running now.
|
|
289
133
|
*/
|
|
290
134
|
start() {
|
|
291
|
-
|
|
135
|
+
run('systemctl', ['--user', 'reset-failed', id.unit]);
|
|
136
|
+
systemctl('start', id.unit);
|
|
292
137
|
},
|
|
293
138
|
stop() {
|
|
294
|
-
|
|
139
|
+
systemctl('stop', id.unit);
|
|
295
140
|
},
|
|
141
|
+
/** A reload first, so an edited unit takes effect. */
|
|
296
142
|
restart() {
|
|
297
|
-
run(
|
|
298
|
-
|
|
143
|
+
run('systemctl', ['--user', 'daemon-reload']);
|
|
144
|
+
run('systemctl', ['--user', 'reset-failed', id.unit]);
|
|
145
|
+
systemctl('restart', id.unit);
|
|
299
146
|
},
|
|
300
147
|
status() {
|
|
301
148
|
if (!fs.existsSync(file))
|
|
302
149
|
return { registered: false, running: false, argv: [], keepsAlive: true };
|
|
303
|
-
const text = fs.readFileSync(file,
|
|
150
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
304
151
|
const argv = parseUnitArgv(text);
|
|
305
|
-
const
|
|
306
|
-
const pid = run("systemctl", ["--user", "show", "-p", "MainPID", "--value", SYSTEMD_UNIT]);
|
|
307
|
-
const mainPid = pid.stdout.trim();
|
|
152
|
+
const pid = run('systemctl', ['--user', 'show', '-p', 'MainPID', '--value', id.unit]).stdout.trim();
|
|
308
153
|
const logPath = parseUnitLogPath(text);
|
|
309
154
|
return {
|
|
310
155
|
registered: true,
|
|
311
|
-
running:
|
|
312
|
-
...(
|
|
156
|
+
running: isActive(),
|
|
157
|
+
...(pid !== '' && pid !== '0' ? { pid } : {}),
|
|
313
158
|
argv: argv.slice(2),
|
|
314
159
|
...(argv[0] !== undefined ? { nodePath: argv[0] } : {}),
|
|
315
160
|
...(argv[1] !== undefined ? { cliPath: argv[1] } : {}),
|
|
@@ -318,23 +163,22 @@ export function createSystemdBackend(run, homeDir, xdgConfigHome) {
|
|
|
318
163
|
};
|
|
319
164
|
},
|
|
320
165
|
/**
|
|
321
|
-
* `loginctl show-user <user> -p Linger` (
|
|
322
|
-
*
|
|
323
|
-
* Parsed rather than trusted to exit codes: `loginctl` is absent in plenty
|
|
324
|
-
* of containers and exits non-zero for a user logind has no record of, and
|
|
325
|
-
* neither of those is "linger is off". An unparseable answer is reported as
|
|
326
|
-
* unknown, which prints nothing — the alternative is handing a `sudo`
|
|
327
|
-
* command to someone whose machine has no `loginctl` to run it with.
|
|
166
|
+
* `loginctl show-user <user> -p Linger`, parsed rather than trusted to its exit code: no `loginctl` (a container)
|
|
167
|
+
* or no record of the user isn't "off". An answer it can't read is unknown, and the tool says nothing.
|
|
328
168
|
*/
|
|
329
169
|
linger() {
|
|
330
|
-
|
|
331
|
-
|
|
170
|
+
let user = o.user;
|
|
171
|
+
try {
|
|
172
|
+
user ??= os.userInfo().username;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
user = process.env.USER;
|
|
176
|
+
}
|
|
177
|
+
if (user === undefined)
|
|
178
|
+
return { applies: true };
|
|
179
|
+
const r = run('loginctl', ['show-user', user, '-p', 'Linger']);
|
|
332
180
|
const m = /Linger=(yes|no)/i.exec(`${r.stdout}${r.stderr}`);
|
|
333
|
-
return {
|
|
334
|
-
applies: true,
|
|
335
|
-
user,
|
|
336
|
-
...(m !== null ? { enabled: m[1].toLowerCase() === "yes" } : {}),
|
|
337
|
-
};
|
|
181
|
+
return { applies: true, user, ...(m !== null ? { enabled: m[1].toLowerCase() === 'yes' } : {}) };
|
|
338
182
|
},
|
|
339
183
|
};
|
|
340
184
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** What `/api/health` said. */
|
|
2
|
+
export interface HealthAnswer {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
/** The running server's version; absent from a tool that predates the contract. */
|
|
5
|
+
version?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface HealthOptions {
|
|
8
|
+
/** The server's base URL: `http://127.0.0.1:7233`. */
|
|
9
|
+
url: string;
|
|
10
|
+
fetch?: typeof globalThis.fetch;
|
|
11
|
+
}
|
|
12
|
+
/** Asks once. Undefined means nothing answered (with a 2xx), which is an answer, not an error. */
|
|
13
|
+
export declare function fetchHealth(o: HealthOptions & {
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
}): Promise<HealthAnswer | undefined>;
|
|
16
|
+
export interface WaitOptions extends HealthOptions {
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
intervalMs?: number;
|
|
19
|
+
now?: () => number;
|
|
20
|
+
sleep?: (ms: number) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* How long to wait before saying it didn't answer. Generous, because it's only spent when something is wrong, and
|
|
24
|
+
* too short a wait says "didn't start" about a service that's only slow to boot.
|
|
25
|
+
*/
|
|
26
|
+
export declare const READY_TIMEOUT_MS = 20000;
|
|
27
|
+
/** Polls until `/api/health` answers 2xx, or the time runs out. Each request has its own abort. */
|
|
28
|
+
export declare function waitForReady(o: WaitOptions): Promise<'ready' | 'timeout'>;
|
|
29
|
+
/** What `waitForVersion` found (SPEC §5 step 12). */
|
|
30
|
+
export type HealthVerdict = {
|
|
31
|
+
state: 'ok';
|
|
32
|
+
version: string;
|
|
33
|
+
}
|
|
34
|
+
/** Something answers, but as another version. */
|
|
35
|
+
| {
|
|
36
|
+
state: 'wrong-version';
|
|
37
|
+
version: string;
|
|
38
|
+
} | {
|
|
39
|
+
state: 'no-answer';
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Polls until `/api/health` reports exactly `version`. "It answers" isn't enough: the old server still shutting
|
|
43
|
+
* down, another instance on the port, and a supervisor that brought the old unit back all answer, and those are the
|
|
44
|
+
* failures a rollback is for. A wrong version and no answer are kept apart because they read differently in a report.
|
|
45
|
+
*/
|
|
46
|
+
export declare function waitForVersion(o: WaitOptions & {
|
|
47
|
+
version: string;
|
|
48
|
+
}): Promise<HealthVerdict>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// From edspencer/cli-contract reference/health.ts @ 0.2; local changes: `.ts` import specifiers → `.js`.
|
|
2
|
+
// Asking `/api/health` (SPEC §8): once, until it answers, or until it answers with one version. The service commands
|
|
3
|
+
// wait on it before they claim success, and `update` waits for the target version (SPEC §5 step 12). It's an HTTP
|
|
4
|
+
// request to the URL a person would open, not a pid lookup: launchd and systemd both report a crash-looping job as
|
|
5
|
+
// running for most of each restart interval. Node built-ins only.
|
|
6
|
+
const healthUrl = (base) => new URL('/api/health', base).toString();
|
|
7
|
+
/** Asks once. Undefined means nothing answered (with a 2xx), which is an answer, not an error. */
|
|
8
|
+
export async function fetchHealth(o) {
|
|
9
|
+
try {
|
|
10
|
+
const res = await (o.fetch ?? globalThis.fetch)(healthUrl(o.url), { signal: AbortSignal.timeout(o.timeoutMs ?? 2000) });
|
|
11
|
+
if (!res.ok)
|
|
12
|
+
return undefined;
|
|
13
|
+
const body = (await res.json());
|
|
14
|
+
return { ok: body.ok === true, ...(typeof body.version === 'string' ? { version: body.version } : {}) };
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* How long to wait before saying it didn't answer. Generous, because it's only spent when something is wrong, and
|
|
22
|
+
* too short a wait says "didn't start" about a service that's only slow to boot.
|
|
23
|
+
*/
|
|
24
|
+
export const READY_TIMEOUT_MS = 20_000;
|
|
25
|
+
const defaults = (o) => ({
|
|
26
|
+
timeoutMs: o.timeoutMs ?? READY_TIMEOUT_MS,
|
|
27
|
+
intervalMs: o.intervalMs ?? 250,
|
|
28
|
+
now: o.now ?? (() => Date.now()),
|
|
29
|
+
sleep: o.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))),
|
|
30
|
+
});
|
|
31
|
+
/** Polls until `/api/health` answers 2xx, or the time runs out. Each request has its own abort. */
|
|
32
|
+
export async function waitForReady(o) {
|
|
33
|
+
const { timeoutMs, intervalMs, now, sleep } = defaults(o);
|
|
34
|
+
const deadline = now() + timeoutMs;
|
|
35
|
+
for (;;) {
|
|
36
|
+
const answer = await fetchHealth({ ...o, timeoutMs: Math.max(250, Math.min(2000, deadline - now())) });
|
|
37
|
+
if (answer !== undefined)
|
|
38
|
+
return 'ready';
|
|
39
|
+
if (deadline - now() <= 0)
|
|
40
|
+
return 'timeout';
|
|
41
|
+
await sleep(intervalMs);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Polls until `/api/health` reports exactly `version`. "It answers" isn't enough: the old server still shutting
|
|
46
|
+
* down, another instance on the port, and a supervisor that brought the old unit back all answer, and those are the
|
|
47
|
+
* failures a rollback is for. A wrong version and no answer are kept apart because they read differently in a report.
|
|
48
|
+
*/
|
|
49
|
+
export async function waitForVersion(o) {
|
|
50
|
+
const { timeoutMs, intervalMs, now, sleep } = defaults(o);
|
|
51
|
+
const deadline = now() + timeoutMs;
|
|
52
|
+
let last;
|
|
53
|
+
for (;;) {
|
|
54
|
+
const answer = await fetchHealth({ ...o, timeoutMs: Math.max(250, Math.min(2000, deadline - now())) });
|
|
55
|
+
if (answer?.version !== undefined) {
|
|
56
|
+
if (answer.version === o.version)
|
|
57
|
+
return { state: 'ok', version: o.version };
|
|
58
|
+
last = answer.version;
|
|
59
|
+
}
|
|
60
|
+
if (deadline - now() <= 0)
|
|
61
|
+
return last !== undefined ? { state: 'wrong-version', version: last } : { state: 'no-answer' };
|
|
62
|
+
await sleep(intervalMs);
|
|
63
|
+
}
|
|
64
|
+
}
|