@mark1russell7/stack-setup 0.0.0-bootstrap.0 → 0.1.0
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/LICENSE +21 -0
- package/README.md +154 -1
- package/build/add-on.d.ts +12 -0
- package/build/add-on.d.ts.map +1 -0
- package/build/add-on.js +23 -0
- package/build/add-on.js.map +1 -0
- package/build/index.d.ts +148 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +507 -0
- package/build/index.js.map +1 -0
- package/build/join-DS3ZZ5hb.js +447 -0
- package/build/join-DS3ZZ5hb.js.map +1 -0
- package/build/layer-DWfBSz3H.js +58 -0
- package/build/layer-DWfBSz3H.js.map +1 -0
- package/build/layer-Db50psxJ.d.ts +151 -0
- package/build/layer-Db50psxJ.d.ts.map +1 -0
- package/build/schema.d.ts +21 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +117 -0
- package/build/schema.js.map +1 -0
- package/package.json +71 -6
package/build/index.js
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
import { a as stepPaths, c as textVariables, i as stepAddress, l as unitPath, n as STEP_KINDS, o as stepTexts, r as isVariableRead, s as textParts, t as SETUP_SCHEMA } from "./layer-DWfBSz3H.js";
|
|
2
|
+
import { c as profileOrder, i as hasSetupError, l as profilesBefore, n as SETUP_CODES, o as modeText, r as checkSetup, s as hostProfiles, t as joinSetup, u as setupHosts } from "./join-DS3ZZ5hb.js";
|
|
3
|
+
import { escapeShell } from "@mark1russell7/text/shell";
|
|
4
|
+
// src/script.ts
|
|
5
|
+
/** The exit codes of a setup script. */
|
|
6
|
+
const SETUP_EXIT = {
|
|
7
|
+
ok: 0,
|
|
8
|
+
failed: 1,
|
|
9
|
+
changes: 2,
|
|
10
|
+
usage: 64
|
|
11
|
+
};
|
|
12
|
+
/** The modes of a setup script, its first argument. `apply` is the default. */
|
|
13
|
+
const SETUP_MODES = ["apply", "check"];
|
|
14
|
+
const HELPERS = [
|
|
15
|
+
["installed", [
|
|
16
|
+
"# This function tells if the package $1 is installed.",
|
|
17
|
+
"installed() {",
|
|
18
|
+
` [ "$(dpkg-query -W -f='\${Status}' "$1" 2>/dev/null || true)" = "install ok installed" ]`,
|
|
19
|
+
"}"
|
|
20
|
+
]],
|
|
21
|
+
["apt_install", [
|
|
22
|
+
"# This function installs the packages. The first call reads the lists of the packages.",
|
|
23
|
+
"apt_install() {",
|
|
24
|
+
" if [ \"$apt_updated\" -eq 0 ]; then",
|
|
25
|
+
" apt-get update -q || return 1",
|
|
26
|
+
" apt_updated=1",
|
|
27
|
+
" fi",
|
|
28
|
+
" DEBIAN_FRONTEND=noninteractive apt-get install -y -q --no-install-recommends \"$@\"",
|
|
29
|
+
"}"
|
|
30
|
+
]],
|
|
31
|
+
["in_group", [
|
|
32
|
+
"# This function tells if the user $1 is in the group $2.",
|
|
33
|
+
"in_group() {",
|
|
34
|
+
` id -nG "$1" 2>/dev/null | tr ' ' '\\n' | grep -qxF "$2"`,
|
|
35
|
+
"}"
|
|
36
|
+
]],
|
|
37
|
+
["login_shell", [
|
|
38
|
+
"# This function gives the login shell of the user $1.",
|
|
39
|
+
"login_shell() {",
|
|
40
|
+
" getent passwd \"$1\" | cut -d: -f7",
|
|
41
|
+
"}"
|
|
42
|
+
]],
|
|
43
|
+
["user_home", [
|
|
44
|
+
"# This function gives the home folder of the user $1.",
|
|
45
|
+
"user_home() {",
|
|
46
|
+
" getent passwd \"$1\" | cut -d: -f6",
|
|
47
|
+
"}"
|
|
48
|
+
]],
|
|
49
|
+
["file_is", [
|
|
50
|
+
"# This function tells if the file $1 has the text of the temporary file, the mode $2, the owner $3 and the group $4.",
|
|
51
|
+
"file_is() {",
|
|
52
|
+
` [ -f "$1" ] && cmp -s "$tmp" "$1" && [ "$(stat -c '%a %U %G' "$1")" = "$2 $3 $4" ]`,
|
|
53
|
+
"}"
|
|
54
|
+
]],
|
|
55
|
+
["swap_on", [
|
|
56
|
+
"# This function tells if the swap file $1 is on.",
|
|
57
|
+
"swap_on() {",
|
|
58
|
+
" swapon --show=NAME --noheadings 2>/dev/null | grep -qxF \"$1\"",
|
|
59
|
+
"}"
|
|
60
|
+
]],
|
|
61
|
+
["in_fstab", [
|
|
62
|
+
"# This function tells if /etc/fstab has the swap file $1.",
|
|
63
|
+
"in_fstab() {",
|
|
64
|
+
` awk -v path="$1" '$1 == path && $3 == "swap" { found = 1 } END { exit !found }' /etc/fstab`,
|
|
65
|
+
"}"
|
|
66
|
+
]]
|
|
67
|
+
];
|
|
68
|
+
/** The shell functions that report a step. */
|
|
69
|
+
const REPORTS = [
|
|
70
|
+
"# These functions report a step.",
|
|
71
|
+
"ok() {",
|
|
72
|
+
" echo \"ok: $1\"",
|
|
73
|
+
"}",
|
|
74
|
+
"change() {",
|
|
75
|
+
" changes=$((changes + 1))",
|
|
76
|
+
" echo \"change: $1\"",
|
|
77
|
+
"}",
|
|
78
|
+
"changed() {",
|
|
79
|
+
" changes=$((changes + 1))",
|
|
80
|
+
" echo \"changed: $1\"",
|
|
81
|
+
"}",
|
|
82
|
+
"fail() {",
|
|
83
|
+
" echo \"failed: $1: $2\" >&2",
|
|
84
|
+
` exit ${String(SETUP_EXIT.failed)}`,
|
|
85
|
+
"}"
|
|
86
|
+
];
|
|
87
|
+
/**
|
|
88
|
+
* This function gives one word of the shell for a text. A literal part is quoted with `escapeShell` of `@mark1russell7/text/shell`.
|
|
89
|
+
* A variable part is `"$NAME"`. Thus the shell expands only the variables of the text.
|
|
90
|
+
*/
|
|
91
|
+
function shellWord(text) {
|
|
92
|
+
const parts = [];
|
|
93
|
+
for (const part of textParts(text)) {
|
|
94
|
+
const last = parts.at(-1);
|
|
95
|
+
if (typeof part === "string" && typeof last === "string") parts[parts.length - 1] = last + part;
|
|
96
|
+
else parts.push(part);
|
|
97
|
+
}
|
|
98
|
+
const word = parts.map((part) => typeof part === "string" ? escapeShell(part) : `"$${part.$var}"`).join("");
|
|
99
|
+
return word === "" ? "''" : word;
|
|
100
|
+
}
|
|
101
|
+
const q = escapeShell;
|
|
102
|
+
/** This function gives the line that writes a text to the temporary file. */
|
|
103
|
+
function writeTmp(text) {
|
|
104
|
+
return `printf '%s' ${shellWord(text)} >"$tmp"`;
|
|
105
|
+
}
|
|
106
|
+
function packageCode(step) {
|
|
107
|
+
if (step.names.length === 0) return {
|
|
108
|
+
helpers: [],
|
|
109
|
+
check: ["true"],
|
|
110
|
+
apply: [":"],
|
|
111
|
+
tmp: false
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
helpers: ["installed", "apt_install"],
|
|
115
|
+
check: step.names.map((name) => `installed ${q(name)}`),
|
|
116
|
+
apply: [`apt_install ${step.names.map(q).join(" ")} || return 1`],
|
|
117
|
+
tmp: false
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function userCode(step) {
|
|
121
|
+
const name = q(step.name);
|
|
122
|
+
const shell = q(step.shell ?? "/bin/bash");
|
|
123
|
+
const groups = step.groups ?? [];
|
|
124
|
+
const helpers = ["login_shell"];
|
|
125
|
+
const check = [`id -u ${name} >/dev/null 2>&1`, `[ "$(login_shell ${name})" = ${shell} ]`];
|
|
126
|
+
const apply = [
|
|
127
|
+
`if ! id -u ${name} >/dev/null 2>&1; then`,
|
|
128
|
+
` useradd --create-home --shell ${shell} ${name} || return 1`,
|
|
129
|
+
"fi",
|
|
130
|
+
`if [ "$(login_shell ${name})" != ${shell} ]; then`,
|
|
131
|
+
` usermod --shell ${shell} ${name} || return 1`,
|
|
132
|
+
"fi"
|
|
133
|
+
];
|
|
134
|
+
if (groups.length > 0) {
|
|
135
|
+
helpers.push("in_group");
|
|
136
|
+
const tests = groups.map((group) => `in_group ${name} ${q(group)}`);
|
|
137
|
+
check.push(...tests);
|
|
138
|
+
apply.push(`if ! { ${tests.join(" && ")}; }; then`, ` usermod -aG ${q(groups.join(","))} ${name} || return 1`, "fi");
|
|
139
|
+
}
|
|
140
|
+
const keys = step.keys;
|
|
141
|
+
if (keys === void 0) return {
|
|
142
|
+
helpers,
|
|
143
|
+
check,
|
|
144
|
+
apply,
|
|
145
|
+
tmp: false
|
|
146
|
+
};
|
|
147
|
+
helpers.push("user_home", "file_is");
|
|
148
|
+
const write = keys.length === 0 ? ": >\"$tmp\"" : `printf '%s\\n' ${keys.map(q).join(" ")} >"$tmp"`;
|
|
149
|
+
check.push(write, `home=$(user_home ${name})`, "[ -n \"$home\" ]", `[ "$(stat -c '%a %U' "$home/.ssh")" = ${q(`700 ${step.name}`)} ]`, `file_is "$home/.ssh/authorized_keys" 600 ${name} "$(id -gn ${name})"`);
|
|
150
|
+
apply.push(`home=$(user_home ${name}) || return 1`, "[ -n \"$home\" ] || return 1", `group=$(id -gn ${name}) || return 1`, "mkdir -p \"$home/.ssh\" || return 1", "chmod 700 \"$home/.ssh\" || return 1", `chown ${name}:"$group" "$home/.ssh" || return 1`, `${write} || return 1`, `install -m 600 -o ${name} -g "$group" "$tmp" "$home/.ssh/authorized_keys" || return 1`);
|
|
151
|
+
return {
|
|
152
|
+
helpers,
|
|
153
|
+
check,
|
|
154
|
+
apply,
|
|
155
|
+
tmp: true
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function fileCode(step) {
|
|
159
|
+
const path = q(step.path);
|
|
160
|
+
const mode = modeText(step.mode ?? "644");
|
|
161
|
+
const owner = q(step.owner ?? "root");
|
|
162
|
+
const group = q(step.group ?? "root");
|
|
163
|
+
const write = writeTmp(step.text);
|
|
164
|
+
return {
|
|
165
|
+
helpers: ["file_is"],
|
|
166
|
+
check: [write, `file_is ${path} ${mode} ${owner} ${group}`],
|
|
167
|
+
apply: [`${write} || return 1`, `install -D -m ${mode} -o ${owner} -g ${group} "$tmp" ${path} || return 1`],
|
|
168
|
+
tmp: true
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function downloadCode(step) {
|
|
172
|
+
const path = q(step.path);
|
|
173
|
+
const mode = modeText(step.mode ?? "644");
|
|
174
|
+
const sha = q(step.sha256);
|
|
175
|
+
return {
|
|
176
|
+
helpers: [],
|
|
177
|
+
check: [
|
|
178
|
+
`[ -f ${path} ]`,
|
|
179
|
+
`[ "$(sha256sum ${path} | cut -d' ' -f1)" = ${sha} ]`,
|
|
180
|
+
`[ "$(stat -c '%a %U %G' ${path})" = ${q(`${mode} root root`)} ]`
|
|
181
|
+
],
|
|
182
|
+
apply: [
|
|
183
|
+
`curl -fsSL --proto =https --proto-redir =https --tlsv1.2 -o "$tmp" ${q(step.url)} || return 1`,
|
|
184
|
+
`if [ "$(sha256sum "$tmp" | cut -d' ' -f1)" != ${sha} ]; then`,
|
|
185
|
+
" echo \"setup: the SHA-256 of the download does not agree\" >&2",
|
|
186
|
+
" return 1",
|
|
187
|
+
"fi",
|
|
188
|
+
`install -D -m ${mode} -o root -g root "$tmp" ${path} || return 1`
|
|
189
|
+
],
|
|
190
|
+
tmp: true
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function swapCode(step) {
|
|
194
|
+
const path = q(step.path);
|
|
195
|
+
const form = q(`${String(step.mib * 1048576)} 600 root`);
|
|
196
|
+
return {
|
|
197
|
+
helpers: ["swap_on", "in_fstab"],
|
|
198
|
+
check: [
|
|
199
|
+
`swap_on ${path}`,
|
|
200
|
+
`[ "$(stat -c '%s %a %U' ${path})" = ${form} ]`,
|
|
201
|
+
`in_fstab ${path}`
|
|
202
|
+
],
|
|
203
|
+
apply: [
|
|
204
|
+
`if [ "$(stat -c '%s %a %U' ${path} 2>/dev/null || true)" != ${form} ]; then`,
|
|
205
|
+
` if swap_on ${path}; then`,
|
|
206
|
+
` swapoff ${path} || return 1`,
|
|
207
|
+
" fi",
|
|
208
|
+
` rm -f ${path} || return 1`,
|
|
209
|
+
` dd if=/dev/zero of=${path} bs=1M count=${String(step.mib)} status=none || return 1`,
|
|
210
|
+
` chmod 600 ${path} || return 1`,
|
|
211
|
+
` mkswap ${path} || return 1`,
|
|
212
|
+
"fi",
|
|
213
|
+
`if ! swap_on ${path}; then`,
|
|
214
|
+
` swapon ${path} || return 1`,
|
|
215
|
+
"fi",
|
|
216
|
+
`if ! in_fstab ${path}; then`,
|
|
217
|
+
` printf '%s none swap sw 0 0\\n' ${path} >>/etc/fstab || return 1`,
|
|
218
|
+
"fi"
|
|
219
|
+
],
|
|
220
|
+
tmp: false
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function serviceCode(step) {
|
|
224
|
+
const name = q(step.name);
|
|
225
|
+
const unit = q(unitPath(step.name));
|
|
226
|
+
const write = writeTmp(step.unit);
|
|
227
|
+
const file = `file_is ${unit} 644 root root`;
|
|
228
|
+
if (step.enable === false) return {
|
|
229
|
+
helpers: ["file_is"],
|
|
230
|
+
check: [write, file],
|
|
231
|
+
apply: [
|
|
232
|
+
`${write} || return 1`,
|
|
233
|
+
`if ! ${file}; then`,
|
|
234
|
+
` install -D -m 644 -o root -g root "$tmp" ${unit} || return 1`,
|
|
235
|
+
" systemctl daemon-reload || return 1",
|
|
236
|
+
"fi"
|
|
237
|
+
],
|
|
238
|
+
tmp: true
|
|
239
|
+
};
|
|
240
|
+
return {
|
|
241
|
+
helpers: ["file_is"],
|
|
242
|
+
check: [
|
|
243
|
+
write,
|
|
244
|
+
file,
|
|
245
|
+
`systemctl is-enabled --quiet ${name}`,
|
|
246
|
+
`systemctl is-active --quiet ${name}`
|
|
247
|
+
],
|
|
248
|
+
apply: [
|
|
249
|
+
"unit_new=0",
|
|
250
|
+
`${write} || return 1`,
|
|
251
|
+
`if ! ${file}; then`,
|
|
252
|
+
` install -D -m 644 -o root -g root "$tmp" ${unit} || return 1`,
|
|
253
|
+
" systemctl daemon-reload || return 1",
|
|
254
|
+
" unit_new=1",
|
|
255
|
+
"fi",
|
|
256
|
+
`systemctl enable --quiet ${name} || return 1`,
|
|
257
|
+
"if [ \"$unit_new\" -eq 1 ]; then",
|
|
258
|
+
` systemctl restart ${name} || return 1`,
|
|
259
|
+
"else",
|
|
260
|
+
` systemctl start ${name} || return 1`,
|
|
261
|
+
"fi"
|
|
262
|
+
],
|
|
263
|
+
tmp: true
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function commandCode(step) {
|
|
267
|
+
const start = step.as === void 0 || step.as === "root" ? "env" : `runuser -u ${q(step.as)} --`;
|
|
268
|
+
const line = (command) => [start, ...command.map(shellWord)].join(" ");
|
|
269
|
+
return {
|
|
270
|
+
helpers: [],
|
|
271
|
+
check: [line(step.check)],
|
|
272
|
+
apply: [`${line(step.run)} || return 1`],
|
|
273
|
+
tmp: false
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/** This function gives the code of a step. */
|
|
277
|
+
function stepCode(step) {
|
|
278
|
+
switch (step.kind) {
|
|
279
|
+
case "package": return packageCode(step);
|
|
280
|
+
case "user": return userCode(step);
|
|
281
|
+
case "file": return fileCode(step);
|
|
282
|
+
case "download": return downloadCode(step);
|
|
283
|
+
case "swap": return swapCode(step);
|
|
284
|
+
case "service": return serviceCode(step);
|
|
285
|
+
case "command": return commandCode(step);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* This function gives the lines of one step: its two functions and the report.
|
|
290
|
+
* The exit code of the check is the number of its first condition that fails. Thus a fault after the change names the condition, and it prints no value.
|
|
291
|
+
*/
|
|
292
|
+
function stepLines(number, address, code) {
|
|
293
|
+
const check = `check_${String(number)}`;
|
|
294
|
+
const apply = `apply_${String(number)}`;
|
|
295
|
+
const at = q(address);
|
|
296
|
+
const conditions = code.check.map((condition, index) => ` ${condition} || return ${String(index + 1)}`);
|
|
297
|
+
return [
|
|
298
|
+
`# The step ${address}`,
|
|
299
|
+
`${check}() {`,
|
|
300
|
+
...conditions,
|
|
301
|
+
"}",
|
|
302
|
+
`${apply}() {`,
|
|
303
|
+
...code.apply.map((line) => ` ${line}`),
|
|
304
|
+
"}",
|
|
305
|
+
`if ${check} >/dev/null 2>&1; then`,
|
|
306
|
+
` ok ${at}`,
|
|
307
|
+
"elif [ \"$mode\" = check ]; then",
|
|
308
|
+
` change ${at}`,
|
|
309
|
+
"else",
|
|
310
|
+
` ${apply} >&2 || fail ${at} "the change failed"`,
|
|
311
|
+
` ${check} >/dev/null 2>&1 || fail ${at} "the condition $? of the check fails after the change"`,
|
|
312
|
+
` changed ${at}`,
|
|
313
|
+
"fi"
|
|
314
|
+
];
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* This function gives the setup script of one host. The gate `checkSetup` comes first: a layer with an error gives no script.
|
|
318
|
+
*
|
|
319
|
+
* The script:
|
|
320
|
+
* - It is POSIX `sh`, and it passes `shellcheck -s sh`. It needs root, `apt`, `systemd` and the GNU tools of Debian or Ubuntu.
|
|
321
|
+
* - Its first argument is the mode. `apply` makes each change. `check` makes no change, and it exits with 2 when a step has a change.
|
|
322
|
+
* - It exits with 64 for a usage fault. A usage fault is a mode that is not known, a start that is not root, or a variable with no value.
|
|
323
|
+
* - Each step starts its check first. Only a step with a failing check makes its change, and then its check must pass.
|
|
324
|
+
* - It reads the value of each variable from its environment. It names the variables of the steps of this host only.
|
|
325
|
+
* - It holds no description of a record. Only names, paths and the texts of the steps are in it.
|
|
326
|
+
*
|
|
327
|
+
* The steps come in the order of `profileOrder`: a profile comes after each profile of the host whose part its part needs.
|
|
328
|
+
*
|
|
329
|
+
* @param layer - The setup layer.
|
|
330
|
+
* @param stack - The stack.
|
|
331
|
+
* @param host - The `id` of the host.
|
|
332
|
+
* @returns The script, or the faults of the gate.
|
|
333
|
+
*/
|
|
334
|
+
function setupScript(layer, stack, host) {
|
|
335
|
+
const faults = checkSetup(layer, stack);
|
|
336
|
+
if (hasSetupError(faults)) return {
|
|
337
|
+
ok: false,
|
|
338
|
+
faults
|
|
339
|
+
};
|
|
340
|
+
const { profiles } = profileOrder(layer, stack, host);
|
|
341
|
+
if (profiles.length === 0) return {
|
|
342
|
+
ok: false,
|
|
343
|
+
faults: [...faults, {
|
|
344
|
+
level: "error",
|
|
345
|
+
code: "setup-host",
|
|
346
|
+
at: host,
|
|
347
|
+
message: `No profile of the layer names the host "${host}".`,
|
|
348
|
+
host
|
|
349
|
+
}]
|
|
350
|
+
};
|
|
351
|
+
const steps = profiles.flatMap((profile) => profile.steps.map((step) => ({
|
|
352
|
+
address: stepAddress(profile, step),
|
|
353
|
+
step,
|
|
354
|
+
code: stepCode(step)
|
|
355
|
+
})));
|
|
356
|
+
const variables = [];
|
|
357
|
+
for (const { step } of steps) for (const text of stepTexts(step)) for (const name of textVariables(text)) if (!variables.includes(name)) variables.push(name);
|
|
358
|
+
const helpers = new Set(steps.flatMap(({ code }) => code.helpers));
|
|
359
|
+
const tmp = steps.some(({ code }) => code.tmp);
|
|
360
|
+
const lines = [
|
|
361
|
+
"#!/bin/sh",
|
|
362
|
+
"# shellcheck disable=SC2016",
|
|
363
|
+
`# The setup of the host ${JSON.stringify(host)}, from a layer ${SETUP_SCHEMA} of a system stack.`,
|
|
364
|
+
"# Each literal text is in single quotes, so the shell expands nothing in it. The shell expands only the variables of the layer.",
|
|
365
|
+
"# Start it as root with the mode apply or check. The mode check makes no change.",
|
|
366
|
+
"# The exit codes: 0 when each step is ok, 1 when a step fails, 2 when the mode check finds a change, 64 for a usage fault.",
|
|
367
|
+
"set -eu",
|
|
368
|
+
"",
|
|
369
|
+
"mode=${1:-apply}",
|
|
370
|
+
"case $mode in",
|
|
371
|
+
` ${SETUP_MODES.join(" | ")}) ;;`,
|
|
372
|
+
" *)",
|
|
373
|
+
` echo "usage: $0 [${SETUP_MODES.join("|")}]" >&2`,
|
|
374
|
+
` exit ${String(SETUP_EXIT.usage)}`,
|
|
375
|
+
" ;;",
|
|
376
|
+
"esac",
|
|
377
|
+
"if [ \"$(id -u)\" -ne 0 ]; then",
|
|
378
|
+
" echo \"setup: start the script as root\" >&2",
|
|
379
|
+
` exit ${String(SETUP_EXIT.usage)}`,
|
|
380
|
+
"fi"
|
|
381
|
+
];
|
|
382
|
+
for (const name of variables) lines.push(`if [ -z "\${${name}-}" ]; then`, ` echo "setup: the variable ${name} has no value" >&2`, ` exit ${String(SETUP_EXIT.usage)}`, "fi");
|
|
383
|
+
lines.push("");
|
|
384
|
+
if (tmp) lines.push("tmp=$(mktemp)", `trap 'rm -f "$tmp"' EXIT`, `trap 'exit ${String(SETUP_EXIT.failed)}' INT TERM HUP`);
|
|
385
|
+
lines.push("changes=0");
|
|
386
|
+
if (helpers.has("apt_install")) lines.push("apt_updated=0");
|
|
387
|
+
for (const [name, text] of HELPERS) if (helpers.has(name)) lines.push("", ...text);
|
|
388
|
+
if (steps.length > 0) lines.push("", ...REPORTS);
|
|
389
|
+
steps.forEach(({ address, code }, index) => lines.push("", ...stepLines(index + 1, address, code)));
|
|
390
|
+
lines.push("", "echo \"changes: $changes\"", "if [ \"$mode\" = check ] && [ \"$changes\" -gt 0 ]; then", ` exit ${String(SETUP_EXIT.changes)}`, "fi", "");
|
|
391
|
+
return {
|
|
392
|
+
ok: true,
|
|
393
|
+
script: lines.join("\n")
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
// src/sample.ts
|
|
397
|
+
/** The unit of the sample service. Each name in it is an example. */
|
|
398
|
+
const SAMPLE_UNIT = [
|
|
399
|
+
"[Unit]",
|
|
400
|
+
"Description=The alert program",
|
|
401
|
+
"After=network-online.target",
|
|
402
|
+
"Wants=network-online.target",
|
|
403
|
+
"",
|
|
404
|
+
"[Service]",
|
|
405
|
+
"User=alerts",
|
|
406
|
+
"EnvironmentFile=/etc/alerts/env",
|
|
407
|
+
"ExecStart=/opt/alerts/alerts",
|
|
408
|
+
"Restart=on-failure",
|
|
409
|
+
"",
|
|
410
|
+
"[Install]",
|
|
411
|
+
"WantedBy=multi-user.target",
|
|
412
|
+
""
|
|
413
|
+
].join("\n");
|
|
414
|
+
/**
|
|
415
|
+
* A sample setup layer for `SAMPLE_STACK` of the core. Each name, address and checksum is an example: the file at the address does not exist.
|
|
416
|
+
*
|
|
417
|
+
* - The profile `program` sets up the part `program-cloud` on the host `cloud-host`: the program, its environment file and its service.
|
|
418
|
+
* - The profile `base` sets up the host itself: the packages, a swap file and the user of the program.
|
|
419
|
+
*
|
|
420
|
+
* The layer has `program` first. The part `program-cloud` needs `cloud-host`, so the order of the setup has `base` first.
|
|
421
|
+
*/
|
|
422
|
+
const SAMPLE_SETUP = {
|
|
423
|
+
schema: SETUP_SCHEMA,
|
|
424
|
+
variables: [{
|
|
425
|
+
name: "ALERTS_TOKEN",
|
|
426
|
+
sensitive: true,
|
|
427
|
+
description: "The token of the push service"
|
|
428
|
+
}],
|
|
429
|
+
profiles: [{
|
|
430
|
+
name: "program",
|
|
431
|
+
part: "program-cloud",
|
|
432
|
+
hosts: ["cloud-host"],
|
|
433
|
+
steps: [
|
|
434
|
+
{
|
|
435
|
+
kind: "download",
|
|
436
|
+
id: "binary",
|
|
437
|
+
url: "https://example.org/alerts/1.4.0/alerts",
|
|
438
|
+
sha256: "0000000000000000000000000000000000000000000000000000000000000000",
|
|
439
|
+
path: "/opt/alerts/alerts",
|
|
440
|
+
mode: "755"
|
|
441
|
+
},
|
|
442
|
+
{
|
|
443
|
+
kind: "file",
|
|
444
|
+
id: "env",
|
|
445
|
+
path: "/etc/alerts/env",
|
|
446
|
+
text: [
|
|
447
|
+
"ALERTS_TOKEN=",
|
|
448
|
+
{ $var: "ALERTS_TOKEN" },
|
|
449
|
+
"\n"
|
|
450
|
+
],
|
|
451
|
+
mode: "600"
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
kind: "command",
|
|
455
|
+
id: "data",
|
|
456
|
+
run: [
|
|
457
|
+
"install",
|
|
458
|
+
"-d",
|
|
459
|
+
"-o",
|
|
460
|
+
"alerts",
|
|
461
|
+
"-g",
|
|
462
|
+
"alerts",
|
|
463
|
+
"/var/lib/alerts"
|
|
464
|
+
],
|
|
465
|
+
check: [
|
|
466
|
+
"test",
|
|
467
|
+
"-d",
|
|
468
|
+
"/var/lib/alerts"
|
|
469
|
+
]
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
kind: "service",
|
|
473
|
+
id: "service",
|
|
474
|
+
name: "alerts",
|
|
475
|
+
unit: SAMPLE_UNIT
|
|
476
|
+
}
|
|
477
|
+
]
|
|
478
|
+
}, {
|
|
479
|
+
name: "base",
|
|
480
|
+
part: "cloud-host",
|
|
481
|
+
hosts: ["cloud-host"],
|
|
482
|
+
steps: [
|
|
483
|
+
{
|
|
484
|
+
kind: "package",
|
|
485
|
+
id: "packages",
|
|
486
|
+
manager: "apt",
|
|
487
|
+
names: ["ca-certificates", "curl"]
|
|
488
|
+
},
|
|
489
|
+
{
|
|
490
|
+
kind: "swap",
|
|
491
|
+
id: "swap",
|
|
492
|
+
path: "/swapfile",
|
|
493
|
+
mib: 1024
|
|
494
|
+
},
|
|
495
|
+
{
|
|
496
|
+
kind: "user",
|
|
497
|
+
id: "user",
|
|
498
|
+
name: "alerts",
|
|
499
|
+
groups: ["systemd-journal"],
|
|
500
|
+
shell: "/usr/sbin/nologin"
|
|
501
|
+
}
|
|
502
|
+
]
|
|
503
|
+
}]
|
|
504
|
+
};
|
|
505
|
+
export { SAMPLE_SETUP, SETUP_CODES, SETUP_EXIT, SETUP_MODES, SETUP_SCHEMA, STEP_KINDS, checkSetup, hasSetupError, hostProfiles, isVariableRead, joinSetup, profileOrder, profilesBefore, setupHosts, setupScript, shellWord, stepAddress, stepPaths, stepTexts, textParts, textVariables, unitPath };
|
|
506
|
+
|
|
507
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/script.ts","../src/sample.ts"],"sourcesContent":["import type { Stack } from \"@mark1russell7/system-stack\";\nimport { escapeShell } from \"@mark1russell7/text/shell\";\nimport { checkSetup, hasSetupError, type SetupFault } from \"./check.ts\";\nimport { DEFAULT_MODE, DEFAULT_SHELL, modeText } from \"./forms.ts\";\nimport {\n stepAddress,\n stepTexts,\n textParts,\n textVariables,\n unitPath,\n SETUP_SCHEMA,\n type CommandStep,\n type DownloadStep,\n type FileStep,\n type PackageStep,\n type ServiceStep,\n type SetupLayer,\n type SetupText,\n type Step,\n type SwapStep,\n type UserStep,\n} from \"./layer.ts\";\nimport { profileOrder } from \"./order.ts\";\n\n// The generator of the setup script of one host. The script is POSIX `sh`, and each step checks before it changes:\n// a step with a passing check changes nothing. A second run of the script thus shows no change.\n// The script holds no value of a variable. It reads each value from its environment when it runs.\n\n/** The exit codes of a setup script. */\nexport const SETUP_EXIT: { readonly ok: 0; readonly failed: 1; readonly changes: 2; readonly usage: 64 } = { ok: 0, failed: 1, changes: 2, usage: 64 };\n\n/** The modes of a setup script, its first argument. `apply` is the default. */\nexport const SETUP_MODES: readonly [\"apply\", \"check\"] = [\"apply\", \"check\"];\n\n/** The result of the generator: the script, or the faults of the gate. */\nexport type SetupScript = { readonly ok: true; readonly script: string } | { readonly ok: false; readonly faults: readonly SetupFault[] };\n\n/** The shell functions that the steps use. The script has only the functions that its steps use. */\ntype Helper = \"installed\" | \"apt_install\" | \"in_group\" | \"login_shell\" | \"user_home\" | \"file_is\" | \"swap_on\" | \"in_fstab\";\n\nconst HELPERS: readonly (readonly [Helper, readonly string[]])[] = [\n [\n \"installed\",\n [\n \"# This function tells if the package $1 is installed.\",\n \"installed() {\",\n ` [ \"$(dpkg-query -W -f='\\${Status}' \"$1\" 2>/dev/null || true)\" = \"install ok installed\" ]`,\n \"}\",\n ],\n ],\n [\n \"apt_install\",\n [\n \"# This function installs the packages. The first call reads the lists of the packages.\",\n \"apt_install() {\",\n ' if [ \"$apt_updated\" -eq 0 ]; then',\n \" apt-get update -q || return 1\",\n \" apt_updated=1\",\n \" fi\",\n ' DEBIAN_FRONTEND=noninteractive apt-get install -y -q --no-install-recommends \"$@\"',\n \"}\",\n ],\n ],\n [\n \"in_group\",\n [\n \"# This function tells if the user $1 is in the group $2.\",\n \"in_group() {\",\n ` id -nG \"$1\" 2>/dev/null | tr ' ' '\\\\n' | grep -qxF \"$2\"`,\n \"}\",\n ],\n ],\n [\"login_shell\", [\"# This function gives the login shell of the user $1.\", \"login_shell() {\", ' getent passwd \"$1\" | cut -d: -f7', \"}\"]],\n [\"user_home\", [\"# This function gives the home folder of the user $1.\", \"user_home() {\", ' getent passwd \"$1\" | cut -d: -f6', \"}\"]],\n [\n \"file_is\",\n [\n \"# This function tells if the file $1 has the text of the temporary file, the mode $2, the owner $3 and the group $4.\",\n \"file_is() {\",\n ` [ -f \"$1\" ] && cmp -s \"$tmp\" \"$1\" && [ \"$(stat -c '%a %U %G' \"$1\")\" = \"$2 $3 $4\" ]`,\n \"}\",\n ],\n ],\n [\"swap_on\", [\"# This function tells if the swap file $1 is on.\", \"swap_on() {\", ' swapon --show=NAME --noheadings 2>/dev/null | grep -qxF \"$1\"', \"}\"]],\n [\n \"in_fstab\",\n [\n \"# This function tells if /etc/fstab has the swap file $1.\",\n \"in_fstab() {\",\n ` awk -v path=\"$1\" '$1 == path && $3 == \"swap\" { found = 1 } END { exit !found }' /etc/fstab`,\n \"}\",\n ],\n ],\n];\n\n/** The shell functions that report a step. */\nconst REPORTS: readonly string[] = [\n \"# These functions report a step.\",\n \"ok() {\",\n ' echo \"ok: $1\"',\n \"}\",\n \"change() {\",\n \" changes=$((changes + 1))\",\n ' echo \"change: $1\"',\n \"}\",\n \"changed() {\",\n \" changes=$((changes + 1))\",\n ' echo \"changed: $1\"',\n \"}\",\n \"fail() {\",\n ' echo \"failed: $1: $2\" >&2',\n ` exit ${String(SETUP_EXIT.failed)}`,\n \"}\",\n];\n\n/** The code of one step: its check, its change and the shell functions that they use. */\ninterface StepCode {\n readonly helpers: readonly Helper[];\n /** The conditions of the check. The check passes when each condition passes. */\n readonly check: readonly string[];\n /** The lines of the change. Each command line ends with `|| return 1`. */\n readonly apply: readonly string[];\n /** `true`: the step uses the temporary file. */\n readonly tmp: boolean;\n}\n\n/**\n * This function gives one word of the shell for a text. A literal part is quoted with `escapeShell` of `@mark1russell7/text/shell`.\n * A variable part is `\"$NAME\"`. Thus the shell expands only the variables of the text.\n */\nexport function shellWord(text: SetupText): string {\n // Two literal parts next to each other become one literal, so a quoted part is next to a variable or at an end.\n const parts: (string | { readonly $var: string })[] = [];\n for (const part of textParts(text)) {\n const last = parts.at(-1);\n if (typeof part === \"string\" && typeof last === \"string\") parts[parts.length - 1] = last + part;\n else parts.push(part);\n }\n const word = parts.map((part) => (typeof part === \"string\" ? escapeShell(part) : `\"$${part.$var}\"`)).join(\"\");\n return word === \"\" ? \"''\" : word;\n}\n\nconst q = escapeShell;\n\n/** This function gives the line that writes a text to the temporary file. */\nfunction writeTmp(text: SetupText): string {\n return `printf '%s' ${shellWord(text)} >\"$tmp\"`;\n}\n\nfunction packageCode(step: PackageStep): StepCode {\n if (step.names.length === 0) return { helpers: [], check: [\"true\"], apply: [\":\"], tmp: false };\n return {\n helpers: [\"installed\", \"apt_install\"],\n check: step.names.map((name) => `installed ${q(name)}`),\n apply: [`apt_install ${step.names.map(q).join(\" \")} || return 1`],\n tmp: false,\n };\n}\n\nfunction userCode(step: UserStep): StepCode {\n const name = q(step.name);\n const shell = q(step.shell ?? DEFAULT_SHELL);\n const groups = step.groups ?? [];\n const helpers: Helper[] = [\"login_shell\"];\n const check = [`id -u ${name} >/dev/null 2>&1`, `[ \"$(login_shell ${name})\" = ${shell} ]`];\n const apply = [\n `if ! id -u ${name} >/dev/null 2>&1; then`,\n ` useradd --create-home --shell ${shell} ${name} || return 1`,\n \"fi\",\n `if [ \"$(login_shell ${name})\" != ${shell} ]; then`,\n ` usermod --shell ${shell} ${name} || return 1`,\n \"fi\",\n ];\n if (groups.length > 0) {\n helpers.push(\"in_group\");\n const tests = groups.map((group) => `in_group ${name} ${q(group)}`);\n check.push(...tests);\n apply.push(`if ! { ${tests.join(\" && \")}; }; then`, ` usermod -aG ${q(groups.join(\",\"))} ${name} || return 1`, \"fi\");\n }\n const keys = step.keys;\n if (keys === undefined) return { helpers, check, apply, tmp: false };\n helpers.push(\"user_home\", \"file_is\");\n // An empty list of keys gives an empty file. A printf with no argument writes one line break.\n const write = keys.length === 0 ? ': >\"$tmp\"' : `printf '%s\\\\n' ${keys.map(q).join(\" \")} >\"$tmp\"`;\n check.push(write, `home=$(user_home ${name})`, '[ -n \"$home\" ]', `[ \"$(stat -c '%a %U' \"$home/.ssh\")\" = ${q(`700 ${step.name}`)} ]`, `file_is \"$home/.ssh/authorized_keys\" 600 ${name} \"$(id -gn ${name})\"`);\n // The group is explicit: `chown user:` with no group does not set the login group with each `chown`, for example the Rust coreutils.\n apply.push(\n `home=$(user_home ${name}) || return 1`,\n '[ -n \"$home\" ] || return 1',\n `group=$(id -gn ${name}) || return 1`,\n 'mkdir -p \"$home/.ssh\" || return 1',\n 'chmod 700 \"$home/.ssh\" || return 1',\n `chown ${name}:\"$group\" \"$home/.ssh\" || return 1`,\n `${write} || return 1`,\n `install -m 600 -o ${name} -g \"$group\" \"$tmp\" \"$home/.ssh/authorized_keys\" || return 1`,\n );\n return { helpers, check, apply, tmp: true };\n}\n\nfunction fileCode(step: FileStep): StepCode {\n const path = q(step.path);\n const mode = modeText(step.mode ?? DEFAULT_MODE);\n const owner = q(step.owner ?? \"root\");\n const group = q(step.group ?? \"root\");\n const write = writeTmp(step.text);\n return {\n helpers: [\"file_is\"],\n check: [write, `file_is ${path} ${mode} ${owner} ${group}`],\n apply: [`${write} || return 1`, `install -D -m ${mode} -o ${owner} -g ${group} \"$tmp\" ${path} || return 1`],\n tmp: true,\n };\n}\n\nfunction downloadCode(step: DownloadStep): StepCode {\n const path = q(step.path);\n const mode = modeText(step.mode ?? DEFAULT_MODE);\n const sha = q(step.sha256);\n return {\n helpers: [],\n check: [`[ -f ${path} ]`, `[ \"$(sha256sum ${path} | cut -d' ' -f1)\" = ${sha} ]`, `[ \"$(stat -c '%a %U %G' ${path})\" = ${q(`${mode} root root`)} ]`],\n apply: [\n `curl -fsSL --proto =https --proto-redir =https --tlsv1.2 -o \"$tmp\" ${q(step.url)} || return 1`,\n `if [ \"$(sha256sum \"$tmp\" | cut -d' ' -f1)\" != ${sha} ]; then`,\n ' echo \"setup: the SHA-256 of the download does not agree\" >&2',\n \" return 1\",\n \"fi\",\n `install -D -m ${mode} -o root -g root \"$tmp\" ${path} || return 1`,\n ],\n tmp: true,\n };\n}\n\nfunction swapCode(step: SwapStep): StepCode {\n const path = q(step.path);\n const form = q(`${String(step.mib * 1048576)} 600 root`);\n return {\n helpers: [\"swap_on\", \"in_fstab\"],\n check: [`swap_on ${path}`, `[ \"$(stat -c '%s %a %U' ${path})\" = ${form} ]`, `in_fstab ${path}`],\n apply: [\n `if [ \"$(stat -c '%s %a %U' ${path} 2>/dev/null || true)\" != ${form} ]; then`,\n ` if swap_on ${path}; then`,\n ` swapoff ${path} || return 1`,\n \" fi\",\n ` rm -f ${path} || return 1`,\n ` dd if=/dev/zero of=${path} bs=1M count=${String(step.mib)} status=none || return 1`,\n ` chmod 600 ${path} || return 1`,\n ` mkswap ${path} || return 1`,\n \"fi\",\n `if ! swap_on ${path}; then`,\n ` swapon ${path} || return 1`,\n \"fi\",\n `if ! in_fstab ${path}; then`,\n ` printf '%s none swap sw 0 0\\\\n' ${path} >>/etc/fstab || return 1`,\n \"fi\",\n ],\n tmp: false,\n };\n}\n\nfunction serviceCode(step: ServiceStep): StepCode {\n const name = q(step.name);\n const unit = q(unitPath(step.name));\n const write = writeTmp(step.unit);\n const file = `file_is ${unit} 644 root root`;\n if (step.enable === false) {\n return {\n helpers: [\"file_is\"],\n check: [write, file],\n apply: [`${write} || return 1`, `if ! ${file}; then`, ` install -D -m 644 -o root -g root \"$tmp\" ${unit} || return 1`, \" systemctl daemon-reload || return 1\", \"fi\"],\n tmp: true,\n };\n }\n return {\n helpers: [\"file_is\"],\n check: [write, file, `systemctl is-enabled --quiet ${name}`, `systemctl is-active --quiet ${name}`],\n apply: [\n \"unit_new=0\",\n `${write} || return 1`,\n `if ! ${file}; then`,\n ` install -D -m 644 -o root -g root \"$tmp\" ${unit} || return 1`,\n \" systemctl daemon-reload || return 1\",\n \" unit_new=1\",\n \"fi\",\n `systemctl enable --quiet ${name} || return 1`,\n 'if [ \"$unit_new\" -eq 1 ]; then',\n ` systemctl restart ${name} || return 1`,\n \"else\",\n ` systemctl start ${name} || return 1`,\n \"fi\",\n ],\n tmp: true,\n };\n}\n\nfunction commandCode(step: CommandStep): StepCode {\n // `env` starts the program from the PATH, so no function, builtin or keyword of the shell is the program.\n const start = step.as === undefined || step.as === \"root\" ? \"env\" : `runuser -u ${q(step.as)} --`;\n const line = (command: readonly SetupText[]): string => [start, ...command.map(shellWord)].join(\" \");\n return { helpers: [], check: [line(step.check)], apply: [`${line(step.run)} || return 1`], tmp: false };\n}\n\n/** This function gives the code of a step. */\nfunction stepCode(step: Step): StepCode {\n switch (step.kind) {\n case \"package\":\n return packageCode(step);\n case \"user\":\n return userCode(step);\n case \"file\":\n return fileCode(step);\n case \"download\":\n return downloadCode(step);\n case \"swap\":\n return swapCode(step);\n case \"service\":\n return serviceCode(step);\n case \"command\":\n return commandCode(step);\n }\n}\n\n/**\n * This function gives the lines of one step: its two functions and the report.\n * The exit code of the check is the number of its first condition that fails. Thus a fault after the change names the condition, and it prints no value.\n */\nfunction stepLines(number: number, address: string, code: StepCode): string[] {\n const check = `check_${String(number)}`;\n const apply = `apply_${String(number)}`;\n const at = q(address);\n const conditions = code.check.map((condition, index) => ` ${condition} || return ${String(index + 1)}`);\n return [\n `# The step ${address}`,\n `${check}() {`,\n ...conditions,\n \"}\",\n `${apply}() {`,\n ...code.apply.map((line) => ` ${line}`),\n \"}\",\n `if ${check} >/dev/null 2>&1; then`,\n ` ok ${at}`,\n 'elif [ \"$mode\" = check ]; then',\n ` change ${at}`,\n \"else\",\n ` ${apply} >&2 || fail ${at} \"the change failed\"`,\n ` ${check} >/dev/null 2>&1 || fail ${at} \"the condition $? of the check fails after the change\"`,\n ` changed ${at}`,\n \"fi\",\n ];\n}\n\n/**\n * This function gives the setup script of one host. The gate `checkSetup` comes first: a layer with an error gives no script.\n *\n * The script:\n * - It is POSIX `sh`, and it passes `shellcheck -s sh`. It needs root, `apt`, `systemd` and the GNU tools of Debian or Ubuntu.\n * - Its first argument is the mode. `apply` makes each change. `check` makes no change, and it exits with 2 when a step has a change.\n * - It exits with 64 for a usage fault. A usage fault is a mode that is not known, a start that is not root, or a variable with no value.\n * - Each step starts its check first. Only a step with a failing check makes its change, and then its check must pass.\n * - It reads the value of each variable from its environment. It names the variables of the steps of this host only.\n * - It holds no description of a record. Only names, paths and the texts of the steps are in it.\n *\n * The steps come in the order of `profileOrder`: a profile comes after each profile of the host whose part its part needs.\n *\n * @param layer - The setup layer.\n * @param stack - The stack.\n * @param host - The `id` of the host.\n * @returns The script, or the faults of the gate.\n */\nexport function setupScript(layer: SetupLayer, stack: Stack, host: string): SetupScript {\n const faults = checkSetup(layer, stack);\n if (hasSetupError(faults)) return { ok: false, faults };\n const { profiles } = profileOrder(layer, stack, host);\n if (profiles.length === 0) {\n return { ok: false, faults: [...faults, { level: \"error\", code: \"setup-host\", at: host, message: `No profile of the layer names the host \"${host}\".`, host }] };\n }\n const steps = profiles.flatMap((profile) => profile.steps.map((step) => ({ address: stepAddress(profile, step), step, code: stepCode(step) })));\n const variables: string[] = [];\n for (const { step } of steps) for (const text of stepTexts(step)) for (const name of textVariables(text)) if (!variables.includes(name)) variables.push(name);\n const helpers = new Set(steps.flatMap(({ code }) => code.helpers));\n const tmp = steps.some(({ code }) => code.tmp);\n const lines: string[] = [\n \"#!/bin/sh\",\n // A directive next to the shebang holds for the whole file. Each literal text is in single quotes on purpose, so the shell expands no \"$\" in it.\n \"# shellcheck disable=SC2016\",\n `# The setup of the host ${JSON.stringify(host)}, from a layer ${SETUP_SCHEMA} of a system stack.`,\n \"# Each literal text is in single quotes, so the shell expands nothing in it. The shell expands only the variables of the layer.\",\n \"# Start it as root with the mode apply or check. The mode check makes no change.\",\n \"# The exit codes: 0 when each step is ok, 1 when a step fails, 2 when the mode check finds a change, 64 for a usage fault.\",\n \"set -eu\",\n \"\",\n \"mode=${1:-apply}\",\n \"case $mode in\",\n ` ${SETUP_MODES.join(\" | \")}) ;;`,\n \" *)\",\n ` echo \"usage: $0 [${SETUP_MODES.join(\"|\")}]\" >&2`,\n ` exit ${String(SETUP_EXIT.usage)}`,\n \" ;;\",\n \"esac\",\n 'if [ \"$(id -u)\" -ne 0 ]; then',\n ' echo \"setup: start the script as root\" >&2',\n ` exit ${String(SETUP_EXIT.usage)}`,\n \"fi\",\n ];\n for (const name of variables) {\n lines.push(`if [ -z \"\\${${name}-}\" ]; then`, ` echo \"setup: the variable ${name} has no value\" >&2`, ` exit ${String(SETUP_EXIT.usage)}`, \"fi\");\n }\n lines.push(\"\");\n if (tmp) lines.push('tmp=$(mktemp)', `trap 'rm -f \"$tmp\"' EXIT`, `trap 'exit ${String(SETUP_EXIT.failed)}' INT TERM HUP`);\n lines.push(\"changes=0\");\n if (helpers.has(\"apt_install\")) lines.push(\"apt_updated=0\");\n for (const [name, text] of HELPERS) if (helpers.has(name)) lines.push(\"\", ...text);\n if (steps.length > 0) lines.push(\"\", ...REPORTS);\n steps.forEach(({ address, code }, index) => lines.push(\"\", ...stepLines(index + 1, address, code)));\n lines.push(\n \"\",\n 'echo \"changes: $changes\"',\n 'if [ \"$mode\" = check ] && [ \"$changes\" -gt 0 ]; then',\n ` exit ${String(SETUP_EXIT.changes)}`,\n \"fi\",\n \"\",\n );\n return { ok: true, script: lines.join(\"\\n\") };\n}\n","import { SETUP_SCHEMA, type SetupLayer } from \"./layer.ts\";\n\n/** The unit of the sample service. Each name in it is an example. */\nconst SAMPLE_UNIT = [\n \"[Unit]\",\n \"Description=The alert program\",\n \"After=network-online.target\",\n \"Wants=network-online.target\",\n \"\",\n \"[Service]\",\n \"User=alerts\",\n \"EnvironmentFile=/etc/alerts/env\",\n \"ExecStart=/opt/alerts/alerts\",\n \"Restart=on-failure\",\n \"\",\n \"[Install]\",\n \"WantedBy=multi-user.target\",\n \"\",\n].join(\"\\n\");\n\n/**\n * A sample setup layer for `SAMPLE_STACK` of the core. Each name, address and checksum is an example: the file at the address does not exist.\n *\n * - The profile `program` sets up the part `program-cloud` on the host `cloud-host`: the program, its environment file and its service.\n * - The profile `base` sets up the host itself: the packages, a swap file and the user of the program.\n *\n * The layer has `program` first. The part `program-cloud` needs `cloud-host`, so the order of the setup has `base` first.\n */\nexport const SAMPLE_SETUP: SetupLayer = {\n schema: SETUP_SCHEMA,\n variables: [{ name: \"ALERTS_TOKEN\", sensitive: true, description: \"The token of the push service\" }],\n profiles: [\n {\n name: \"program\",\n part: \"program-cloud\",\n hosts: [\"cloud-host\"],\n steps: [\n {\n kind: \"download\",\n id: \"binary\",\n url: \"https://example.org/alerts/1.4.0/alerts\",\n sha256: \"0000000000000000000000000000000000000000000000000000000000000000\",\n path: \"/opt/alerts/alerts\",\n mode: \"755\",\n },\n { kind: \"file\", id: \"env\", path: \"/etc/alerts/env\", text: [\"ALERTS_TOKEN=\", { $var: \"ALERTS_TOKEN\" }, \"\\n\"], mode: \"600\" },\n { kind: \"command\", id: \"data\", run: [\"install\", \"-d\", \"-o\", \"alerts\", \"-g\", \"alerts\", \"/var/lib/alerts\"], check: [\"test\", \"-d\", \"/var/lib/alerts\"] },\n { kind: \"service\", id: \"service\", name: \"alerts\", unit: SAMPLE_UNIT },\n ],\n },\n {\n name: \"base\",\n part: \"cloud-host\",\n hosts: [\"cloud-host\"],\n steps: [\n { kind: \"package\", id: \"packages\", manager: \"apt\", names: [\"ca-certificates\", \"curl\"] },\n { kind: \"swap\", id: \"swap\", path: \"/swapfile\", mib: 1024 },\n { kind: \"user\", id: \"user\", name: \"alerts\", groups: [\"systemd-journal\"], shell: \"/usr/sbin/nologin\" },\n ],\n },\n ],\n};\n"],"mappings":";;;;;AA6BA,MAAa,aAA8F;CAAE,IAAI;CAAG,QAAQ;CAAG,SAAS;CAAG,OAAO;AAAG;;AAGrJ,MAAa,cAA2C,CAAC,SAAS,OAAO;AAQzE,MAAM,UAA6D;CACjE,CACE,aACA;EACE;EACA;EACA;EACA;CACF,CACF;CACA,CACE,eACA;EACE;EACA;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