@foliwake/cli 0.10.2 → 0.11.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/CHANGELOG.md +60 -0
- package/README.md +196 -14
- package/config/docker/compose.varnish.yaml +44 -0
- package/config/docker/compose.yaml +165 -0
- package/dist/commands/dev.d.ts +3 -0
- package/dist/commands/dev.js +58 -21
- package/dist/commands/docker/backup.d.ts +12 -0
- package/dist/commands/docker/backup.js +29 -0
- package/dist/commands/docker/configure.d.ts +23 -0
- package/dist/commands/docker/configure.js +73 -0
- package/dist/commands/docker/exec.d.ts +8 -0
- package/dist/commands/docker/exec.js +26 -0
- package/dist/commands/docker/install.d.ts +33 -0
- package/dist/commands/docker/install.js +126 -0
- package/dist/commands/docker/link.d.ts +12 -0
- package/dist/commands/docker/link.js +24 -0
- package/dist/commands/docker/list.d.ts +7 -0
- package/dist/commands/docker/list.js +16 -0
- package/dist/commands/docker/logs.d.ts +15 -0
- package/dist/commands/docker/logs.js +33 -0
- package/dist/commands/docker/remove.d.ts +13 -0
- package/dist/commands/docker/remove.js +35 -0
- package/dist/commands/docker/restart.d.ts +9 -0
- package/dist/commands/docker/restart.js +18 -0
- package/dist/commands/docker/restore.d.ts +14 -0
- package/dist/commands/docker/restore.js +34 -0
- package/dist/commands/docker/start.d.ts +9 -0
- package/dist/commands/docker/start.js +18 -0
- package/dist/commands/docker/status.d.ts +10 -0
- package/dist/commands/docker/status.js +22 -0
- package/dist/commands/docker/stop.d.ts +9 -0
- package/dist/commands/docker/stop.js +18 -0
- package/dist/commands/docker/update.d.ts +14 -0
- package/dist/commands/docker/update.js +42 -0
- package/dist/commands/project/publish.d.ts +1 -0
- package/dist/commands/project/publish.js +9 -2
- package/dist/core-command.d.ts +4 -1
- package/dist/core-command.js +6 -2
- package/dist/core-types.d.ts +1 -0
- package/dist/dev/container.d.ts +45 -0
- package/dist/dev/container.js +128 -0
- package/dist/dev/remote-html.js +2 -2
- package/dist/docker/asker.d.ts +4 -0
- package/dist/docker/asker.js +34 -0
- package/dist/docker/backup.d.ts +21 -0
- package/dist/docker/backup.js +114 -0
- package/dist/docker/cache-plan.d.ts +17 -0
- package/dist/docker/cache-plan.js +58 -0
- package/dist/docker/compose-file.d.ts +18 -0
- package/dist/docker/compose-file.js +58 -0
- package/dist/docker/context.d.ts +55 -0
- package/dist/docker/context.js +12 -0
- package/dist/docker/engine.d.ts +8 -0
- package/dist/docker/engine.js +51 -0
- package/dist/docker/env-block.d.ts +19 -0
- package/dist/docker/env-block.js +117 -0
- package/dist/docker/env-keys.d.ts +4 -0
- package/dist/docker/env-keys.js +33 -0
- package/dist/docker/env-verify.d.ts +17 -0
- package/dist/docker/env-verify.js +62 -0
- package/dist/docker/install-plan.d.ts +81 -0
- package/dist/docker/install-plan.js +332 -0
- package/dist/docker/install.d.ts +22 -0
- package/dist/docker/install.js +118 -0
- package/dist/docker/instance.d.ts +105 -0
- package/dist/docker/instance.js +184 -0
- package/dist/docker/lifecycle.d.ts +19 -0
- package/dist/docker/lifecycle.js +70 -0
- package/dist/docker/ports.d.ts +14 -0
- package/dist/docker/ports.js +50 -0
- package/dist/docker/process-context.d.ts +8 -0
- package/dist/docker/process-context.js +26 -0
- package/dist/docker/remove.d.ts +7 -0
- package/dist/docker/remove.js +82 -0
- package/dist/docker/report.d.ts +32 -0
- package/dist/docker/report.js +109 -0
- package/dist/docker/runner.d.ts +28 -0
- package/dist/docker/runner.js +55 -0
- package/dist/docker/settings-files.d.ts +40 -0
- package/dist/docker/settings-files.js +125 -0
- package/dist/docker/trace-url.d.ts +5 -0
- package/dist/docker/trace-url.js +36 -0
- package/dist/docker/update.d.ts +29 -0
- package/dist/docker/update.js +317 -0
- package/dist/docker-command.d.ts +17 -0
- package/dist/docker-command.js +37 -0
- package/dist/project-preview.d.ts +8 -0
- package/dist/project-preview.js +27 -5
- package/dist/project-publish.d.ts +8 -7
- package/dist/project-publish.js +124 -57
- package/dist/publish-code.d.ts +75 -0
- package/dist/publish-code.js +223 -0
- package/dist/render/site-status.js +2 -2
- package/package.json +13 -8
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// `foliwake docker list` and `status`: what Docker says about each instance (labels, state,
|
|
2
|
+
// health, image, ports, the shown part of the environment) plus whether this computer has its
|
|
3
|
+
// settings folder. Secret values are never read into these reports: only "set" / "not set".
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { containerOf, dataVolume } from './instance.js';
|
|
6
|
+
import { printable } from './runner.js';
|
|
7
|
+
import { instanceDir, readEnvFile } from './settings-files.js';
|
|
8
|
+
const shortId = (id) => (id ? id.replace(/^sha256:/, '').slice(0, 12) : null);
|
|
9
|
+
function stateOf(server) {
|
|
10
|
+
if (!server)
|
|
11
|
+
return 'data only';
|
|
12
|
+
if (server.state !== 'running')
|
|
13
|
+
return server.state === 'exited' || server.state === 'created' ? 'stopped' : server.state;
|
|
14
|
+
return server.health ? `running, ${server.health}` : 'running';
|
|
15
|
+
}
|
|
16
|
+
export function summarize(instance) {
|
|
17
|
+
const server = containerOf(instance, 'server');
|
|
18
|
+
const jaeger = containerOf(instance, 'jaeger');
|
|
19
|
+
const ui = jaeger?.ports.find((port) => port.containerPort === 16686);
|
|
20
|
+
return {
|
|
21
|
+
name: instance.name,
|
|
22
|
+
status: stateOf(server),
|
|
23
|
+
address: server?.env.publicOrigin ?? null,
|
|
24
|
+
image: server?.image ?? null,
|
|
25
|
+
imageId: shortId(server?.imageId),
|
|
26
|
+
traces: server?.traces ?? 'none',
|
|
27
|
+
jaegerUi: ui ? `http://${ui.host}:${ui.port}` : null,
|
|
28
|
+
dataVolume: instance.volumes.find((volume) => volume.name === dataVolume(instance.name))?.name ?? null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** A plain text table: each column as wide as its widest cell; Docker's text printable (runner.ts). */
|
|
32
|
+
export function table(cells) {
|
|
33
|
+
const rows = cells.map((row) => row.map(printable));
|
|
34
|
+
const widths = rows[0].map((_, column) => Math.max(...rows.map((row) => row[column].length)));
|
|
35
|
+
return rows.map((row) => row
|
|
36
|
+
.map((cell, column) => (column === row.length - 1 ? cell : cell.padEnd(widths[column])))
|
|
37
|
+
.join(' ')
|
|
38
|
+
.trimEnd());
|
|
39
|
+
}
|
|
40
|
+
export function listLines(summaries) {
|
|
41
|
+
if (!summaries.length)
|
|
42
|
+
return ['No Foliwake instance on this Docker engine. `foliwake docker install` creates one.'];
|
|
43
|
+
const rows = summaries.map((s) => [
|
|
44
|
+
s.name,
|
|
45
|
+
s.status,
|
|
46
|
+
s.address ?? '—',
|
|
47
|
+
s.image ? `${s.image} @${(s.imageId ?? '').slice(0, 7)}` : '—',
|
|
48
|
+
s.traces === 'jaeger' && s.jaegerUi ? `Jaeger ${s.jaegerUi}` : s.status === 'data only' ? '—' : s.traces,
|
|
49
|
+
(s.dataVolume ?? '—') +
|
|
50
|
+
(s.status === 'data only' ? ` (removed; \`foliwake docker install ${s.name}\` reuses it)` : ''),
|
|
51
|
+
]);
|
|
52
|
+
return table([['NAME', 'STATUS', 'ADDRESS', 'IMAGE', 'TRACES', 'DATA'], ...rows]);
|
|
53
|
+
}
|
|
54
|
+
/** `Varnish http://<address>:<port>` of the instance's cache container, or null. */
|
|
55
|
+
function cacheText(instance) {
|
|
56
|
+
const port = containerOf(instance, 'cache')?.ports.find((published) => published.containerPort === 80);
|
|
57
|
+
return port ? `Varnish http://${port.host}:${port.port} (live sites)` : null;
|
|
58
|
+
}
|
|
59
|
+
export async function instanceStatus(ctx, instance) {
|
|
60
|
+
const server = containerOf(instance, 'server');
|
|
61
|
+
const dir = instanceDir(ctx.env, instance.name);
|
|
62
|
+
const files = existsSync(dir) ? await readEnvFile(ctx.env, instance.name) : null;
|
|
63
|
+
const storageEndpoint = server?.env.storageEndpoint ?? null;
|
|
64
|
+
return {
|
|
65
|
+
...summarize(instance),
|
|
66
|
+
startedAt: server?.state === 'running' ? server.startedAt : null,
|
|
67
|
+
ports: instance.containers.flatMap((container) => container.ports.map((port) => `${port.host}:${port.port} → ${container.service}:${port.containerPort}`)),
|
|
68
|
+
ownerEmail: server?.env.ownerEmail ?? null,
|
|
69
|
+
storage: storageEndpoint ?? (server ? `the data volume (${dataVolume(instance.name)})` : '—'),
|
|
70
|
+
storageKey: storageEndpoint ? (server?.env.storageKeySet ? 'set' : 'not set') : null,
|
|
71
|
+
otlpEndpoint: server?.env.otlpEndpoint ?? null,
|
|
72
|
+
otlpHeaders: server?.env.otlpHeadersSet ? 'set' : 'not set',
|
|
73
|
+
previousImage: files?.values.FOLIWAKE_PREVIOUS_IMAGE || null,
|
|
74
|
+
settings: files ? dir : null,
|
|
75
|
+
cli: server?.cli ?? null,
|
|
76
|
+
cache: cacheText(instance),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function tracesText(status) {
|
|
80
|
+
if (status.traces === 'jaeger')
|
|
81
|
+
return `Jaeger ${status.jaegerUi ?? ''}`.trim();
|
|
82
|
+
if (status.traces === 'external')
|
|
83
|
+
return `OTLP ${status.otlpEndpoint ?? ''} (headers: ${status.otlpHeaders})`;
|
|
84
|
+
return status.traces;
|
|
85
|
+
}
|
|
86
|
+
export function statusLines(status) {
|
|
87
|
+
const lines = [
|
|
88
|
+
['Status', status.status + (status.startedAt ? ` (since ${status.startedAt})` : '')],
|
|
89
|
+
['Address', status.address],
|
|
90
|
+
['Image', status.image ? `${status.image} @${status.imageId}` : null],
|
|
91
|
+
['Previous image', status.previousImage],
|
|
92
|
+
['Ports', status.ports.join(', ') || null],
|
|
93
|
+
['Owner', status.ownerEmail],
|
|
94
|
+
['Storage', status.storage],
|
|
95
|
+
['S3 key', status.storageKey],
|
|
96
|
+
['Traces', tracesText(status)],
|
|
97
|
+
['Cache', status.cache],
|
|
98
|
+
['Data volume', status.dataVolume],
|
|
99
|
+
[
|
|
100
|
+
'Settings',
|
|
101
|
+
status.settings ??
|
|
102
|
+
'(files missing on this computer: start, stop, restart, logs, link and remove work; ' +
|
|
103
|
+
'update and configure do not)',
|
|
104
|
+
],
|
|
105
|
+
['Written by', status.cli ? `foliwake ${status.cli}` : null],
|
|
106
|
+
];
|
|
107
|
+
const shown = lines.filter(([, value]) => value !== null).map(([key, value]) => ` ${key.padEnd(15)}${value}`);
|
|
108
|
+
return [status.name, ...shown].map(printable);
|
|
109
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface DockerResult {
|
|
2
|
+
code: number;
|
|
3
|
+
stdout: string;
|
|
4
|
+
stderr: string;
|
|
5
|
+
}
|
|
6
|
+
export interface DockerCall {
|
|
7
|
+
/** The process environment of the call (default: the command's own). */
|
|
8
|
+
env?: NodeJS.ProcessEnv;
|
|
9
|
+
}
|
|
10
|
+
export interface DockerRunner {
|
|
11
|
+
/** `docker <args>`, output captured. Never throws for a non-zero exit: the code says it. */
|
|
12
|
+
run(args: readonly string[], call?: DockerCall): Promise<DockerResult>;
|
|
13
|
+
/** `docker <args>` with this terminal's input and output; the exit code. */
|
|
14
|
+
attached(args: readonly string[], call?: DockerCall): Promise<number>;
|
|
15
|
+
}
|
|
16
|
+
/** Docker itself could not be started (not installed, not on PATH). */
|
|
17
|
+
export declare class DockerMissing extends Error {
|
|
18
|
+
}
|
|
19
|
+
export declare const execDocker: DockerRunner;
|
|
20
|
+
/** The JSON lines of `--format json` (one object per line; some commands print an array). */
|
|
21
|
+
export declare function jsonLines<T>(text: string): T[];
|
|
22
|
+
/** A docker call that failed: its first lines of stderr, for a message. */
|
|
23
|
+
export declare function failureText(result: DockerResult): string;
|
|
24
|
+
/**
|
|
25
|
+
* Text Docker reports (labels, environment values) as it may be printed in a terminal: anyone who can
|
|
26
|
+
* reach the engine can set it, so a control character is shown as its \u escape, never sent as is.
|
|
27
|
+
*/
|
|
28
|
+
export declare function printable(text: string): string;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// How `foliwake docker` runs the docker CLI: execFile without a shell (arguments are never parsed
|
|
2
|
+
// by one), output captured for `--format json`; or with the terminal attached for `logs -f`,
|
|
3
|
+
// `exec` and pulls. Tests give their own runner (test/docker/fake-docker.js).
|
|
4
|
+
import { execFile, spawn } from 'node:child_process';
|
|
5
|
+
/** Docker itself could not be started (not installed, not on PATH). */
|
|
6
|
+
export class DockerMissing extends Error {
|
|
7
|
+
}
|
|
8
|
+
const MAX_OUTPUT = 64 * 1024 * 1024;
|
|
9
|
+
export const execDocker = {
|
|
10
|
+
run(args, call = {}) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
execFile('docker', [...args], { env: call.env ?? process.env, maxBuffer: MAX_OUTPUT, encoding: 'utf8' }, (error, stdout, stderr) => {
|
|
13
|
+
const failure = error;
|
|
14
|
+
if (failure && failure.code === 'ENOENT')
|
|
15
|
+
return reject(new DockerMissing('The `docker` command was not found. Install Docker (https://docs.docker.com/get-docker/) and try ' +
|
|
16
|
+
'again.'));
|
|
17
|
+
const code = failure ? (typeof failure.code === 'number' ? failure.code : 1) : 0;
|
|
18
|
+
resolve({ code, stdout, stderr });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
attached(args, call = {}) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const child = spawn('docker', [...args], { env: call.env ?? process.env, stdio: 'inherit' });
|
|
25
|
+
child.on('error', (error) => reject(error.code === 'ENOENT' ? new DockerMissing('The `docker` command was not found.') : error));
|
|
26
|
+
child.on('exit', (code, signal) => resolve(code ?? (signal ? 130 : 1)));
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
/** The JSON lines of `--format json` (one object per line; some commands print an array). */
|
|
31
|
+
export function jsonLines(text) {
|
|
32
|
+
const trimmed = text.trim();
|
|
33
|
+
if (trimmed === '')
|
|
34
|
+
return [];
|
|
35
|
+
if (trimmed.startsWith('['))
|
|
36
|
+
return JSON.parse(trimmed);
|
|
37
|
+
return trimmed
|
|
38
|
+
.split('\n')
|
|
39
|
+
.filter((line) => line.trim() !== '')
|
|
40
|
+
.map((line) => JSON.parse(line));
|
|
41
|
+
}
|
|
42
|
+
/** A docker call that failed: its first lines of stderr, for a message. */
|
|
43
|
+
export function failureText(result) {
|
|
44
|
+
const text = result.stderr.trim() || result.stdout.trim();
|
|
45
|
+
return text.split('\n').slice(-6).join('\n') || `exit code ${result.code}`;
|
|
46
|
+
}
|
|
47
|
+
// C0 and C1 controls, DEL, and the line and bidirectional controls that could reorder what is shown.
|
|
48
|
+
const UNPRINTABLE = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g;
|
|
49
|
+
/**
|
|
50
|
+
* Text Docker reports (labels, environment values) as it may be printed in a terminal: anyone who can
|
|
51
|
+
* reach the engine can set it, so a control character is shown as its \u escape, never sent as is.
|
|
52
|
+
*/
|
|
53
|
+
export function printable(text) {
|
|
54
|
+
return text.replace(UNPRINTABLE, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
55
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type EnvFile } from './env-block.js';
|
|
2
|
+
/**
|
|
3
|
+
* The settings folder of instance `name`: one folder right in <config>/docker. Any other name is
|
|
4
|
+
* refused, as every caller writes into this folder or removes it recursively.
|
|
5
|
+
*/
|
|
6
|
+
export declare function instanceDir(env: NodeJS.ProcessEnv, name: string): string;
|
|
7
|
+
/** The .env of an instance, or null when this computer has no files for it. */
|
|
8
|
+
export declare function readEnvFile(env: NodeJS.ProcessEnv, name: string): Promise<EnvFile | null>;
|
|
9
|
+
/** Writes compose.yaml (0644) and .env (0600) into the instance folder (0700). */
|
|
10
|
+
export declare function writeInstanceFiles(env: NodeJS.ProcessEnv, name: string, files: {
|
|
11
|
+
compose: string;
|
|
12
|
+
env: EnvFile;
|
|
13
|
+
}): Promise<string>;
|
|
14
|
+
/** The two files of an instance exactly as they are, to put back when a command fails. */
|
|
15
|
+
export interface FilesSnapshot {
|
|
16
|
+
compose: string;
|
|
17
|
+
env: string;
|
|
18
|
+
}
|
|
19
|
+
/** What was there before a write: no folder at all (the write creates it), or both files exactly. */
|
|
20
|
+
export type PreviousFiles = {
|
|
21
|
+
kind: 'none';
|
|
22
|
+
} | {
|
|
23
|
+
kind: 'files';
|
|
24
|
+
snapshot: FilesSnapshot;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The instance's files as they are, to put back if the coming write fails. Refuses (nothing is
|
|
28
|
+
* changed) a folder whose files are missing or unreadable: they are never guessed, replaced or
|
|
29
|
+
* deleted.
|
|
30
|
+
*/
|
|
31
|
+
export declare function previousFiles(env: NodeJS.ProcessEnv, name: string): Promise<PreviousFiles>;
|
|
32
|
+
/** Puts back what was there: the files byte for byte, or, when this write created the folder, no folder. */
|
|
33
|
+
export declare function undoWrite(env: NodeJS.ProcessEnv, name: string, previous: PreviousFiles): Promise<void>;
|
|
34
|
+
/** Writes a snapshot back byte for byte (.env 0600, compose.yaml 0644). */
|
|
35
|
+
export declare function restoreInstanceFiles(env: NodeJS.ProcessEnv, name: string, snapshot: FilesSnapshot): Promise<void>;
|
|
36
|
+
export declare function removeInstanceFiles(env: NodeJS.ProcessEnv, name: string): Promise<void>;
|
|
37
|
+
/** `docker compose` with the instance's files; the process environment without our keys, so .env wins. */
|
|
38
|
+
export declare function composeArgs(env: NodeJS.ProcessEnv, name: string, rest: readonly string[]): string[];
|
|
39
|
+
/** The environment of a compose call: no managed key nor COMPOSE_* project setting from the shell. */
|
|
40
|
+
export declare function composeEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The files of an instance on this computer: ~/.config/foliwake/docker/<name>/ (configDir(), the
|
|
2
|
+
// folder of the sign-ins; 0700) with compose.yaml (0644, variable names only) and .env (0600, every
|
|
3
|
+
// answer of `install`, secrets included). Not a project folder: no command looks at the current
|
|
4
|
+
// directory. In .env the CLI writes only its block (env-block.ts); the operator's lines are kept.
|
|
5
|
+
import { chmod, lstat, mkdir, open, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { configDir } from '../settings.js';
|
|
8
|
+
import { DockerUsageError } from './context.js';
|
|
9
|
+
import { parseEnv, renderEnv } from './env-block.js';
|
|
10
|
+
import { ENV_KEYS } from './env-keys.js';
|
|
11
|
+
import { instanceNameProblem } from './instance.js';
|
|
12
|
+
import { printable } from './runner.js';
|
|
13
|
+
/**
|
|
14
|
+
* The settings folder of instance `name`: one folder right in <config>/docker. Any other name is
|
|
15
|
+
* refused, as every caller writes into this folder or removes it recursively.
|
|
16
|
+
*/
|
|
17
|
+
export function instanceDir(env, name) {
|
|
18
|
+
const root = path.join(configDir(env), 'docker');
|
|
19
|
+
const dir = path.join(root, name);
|
|
20
|
+
const problem = instanceNameProblem(name);
|
|
21
|
+
if (problem || path.dirname(dir) !== root) {
|
|
22
|
+
throw new DockerUsageError(problem ?? `"${printable(name)}" cannot name an instance.`);
|
|
23
|
+
}
|
|
24
|
+
return dir;
|
|
25
|
+
}
|
|
26
|
+
/** The .env of an instance, or null when this computer has no files for it. */
|
|
27
|
+
export async function readEnvFile(env, name) {
|
|
28
|
+
try {
|
|
29
|
+
const file = path.join(instanceDir(env, name), '.env');
|
|
30
|
+
return parseEnv(await readFile(file, 'utf8'), file);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error.code === 'ENOENT')
|
|
34
|
+
return null;
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Writes a file through a temporary one created with `mode`, then renamed into place. */
|
|
39
|
+
async function writePrivate(file, text, mode) {
|
|
40
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
41
|
+
const handle = await open(temporary, 'w', mode);
|
|
42
|
+
try {
|
|
43
|
+
await handle.writeFile(text);
|
|
44
|
+
await handle.chmod(mode);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
await handle.close();
|
|
48
|
+
}
|
|
49
|
+
await rename(temporary, file);
|
|
50
|
+
}
|
|
51
|
+
/** Writes compose.yaml (0644) and .env (0600) into the instance folder (0700). */
|
|
52
|
+
export async function writeInstanceFiles(env, name, files) {
|
|
53
|
+
const dir = instanceDir(env, name);
|
|
54
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
55
|
+
await chmod(dir, 0o700);
|
|
56
|
+
await writePrivate(path.join(dir, '.env'), renderEnv(files.env), 0o600);
|
|
57
|
+
await writeFile(path.join(dir, 'compose.yaml.tmp'), files.compose, { mode: 0o644 });
|
|
58
|
+
await rename(path.join(dir, 'compose.yaml.tmp'), path.join(dir, 'compose.yaml'));
|
|
59
|
+
return dir;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The instance's files as they are, to put back if the coming write fails. Refuses (nothing is
|
|
63
|
+
* changed) a folder whose files are missing or unreadable: they are never guessed, replaced or
|
|
64
|
+
* deleted.
|
|
65
|
+
*/
|
|
66
|
+
export async function previousFiles(env, name) {
|
|
67
|
+
const dir = instanceDir(env, name);
|
|
68
|
+
const exists = await lstat(dir).then(() => true, (error) => {
|
|
69
|
+
if (error.code === 'ENOENT')
|
|
70
|
+
return false;
|
|
71
|
+
throw error;
|
|
72
|
+
});
|
|
73
|
+
if (!exists)
|
|
74
|
+
return { kind: 'none' };
|
|
75
|
+
const read = (file) => readFile(path.join(dir, file), 'utf8').catch((error) => {
|
|
76
|
+
throw new DockerUsageError(`The settings of ${name} are incomplete: ${path.join(dir, file)} cannot be read (${error.code ?? 'error'}). ` +
|
|
77
|
+
`Nothing was changed. Restore the file, or remove the instance (\`foliwake docker remove ${name}\`) ` +
|
|
78
|
+
`and install it again.`);
|
|
79
|
+
});
|
|
80
|
+
return { kind: 'files', snapshot: { compose: await read('compose.yaml'), env: await read('.env') } };
|
|
81
|
+
}
|
|
82
|
+
/** Puts back what was there: the files byte for byte, or, when this write created the folder, no folder. */
|
|
83
|
+
export async function undoWrite(env, name, previous) {
|
|
84
|
+
if (previous.kind === 'files')
|
|
85
|
+
await restoreInstanceFiles(env, name, previous.snapshot);
|
|
86
|
+
else
|
|
87
|
+
await removeInstanceFiles(env, name);
|
|
88
|
+
}
|
|
89
|
+
/** Writes a snapshot back byte for byte (.env 0600, compose.yaml 0644). */
|
|
90
|
+
export async function restoreInstanceFiles(env, name, snapshot) {
|
|
91
|
+
const dir = instanceDir(env, name);
|
|
92
|
+
await writePrivate(path.join(dir, '.env'), snapshot.env, 0o600);
|
|
93
|
+
await writePrivate(path.join(dir, 'compose.yaml'), snapshot.compose, 0o644);
|
|
94
|
+
}
|
|
95
|
+
export async function removeInstanceFiles(env, name) {
|
|
96
|
+
await rm(instanceDir(env, name), { recursive: true, force: true });
|
|
97
|
+
}
|
|
98
|
+
/** `docker compose` with the instance's files; the process environment without our keys, so .env wins. */
|
|
99
|
+
export function composeArgs(env, name, rest) {
|
|
100
|
+
const dir = instanceDir(env, name);
|
|
101
|
+
return [
|
|
102
|
+
'compose',
|
|
103
|
+
'-p',
|
|
104
|
+
name,
|
|
105
|
+
'-f',
|
|
106
|
+
path.join(dir, 'compose.yaml'),
|
|
107
|
+
'--env-file',
|
|
108
|
+
path.join(dir, '.env'),
|
|
109
|
+
'--project-directory',
|
|
110
|
+
dir,
|
|
111
|
+
...rest,
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
/** The environment of a compose call: no managed key nor COMPOSE_* project setting from the shell. */
|
|
115
|
+
export function composeEnv(env) {
|
|
116
|
+
const result = {};
|
|
117
|
+
for (const [key, value] of Object.entries(env)) {
|
|
118
|
+
const managed = ENV_KEYS.includes(key) || key.startsWith('JAEGER_');
|
|
119
|
+
const projectSetting = /^COMPOSE_(FILE|PROJECT_NAME|PROFILES|ENV_FILES|PATH_SEPARATOR)$/.test(key);
|
|
120
|
+
if (managed || projectSetting)
|
|
121
|
+
continue;
|
|
122
|
+
result[key] = value;
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why `template` is no usable trace URL template, or null when it is one: an http or https URL with
|
|
3
|
+
* `{traceId}` exactly once (outside its host) and no other placeholder, no space, user name or password.
|
|
4
|
+
*/
|
|
5
|
+
export declare function traceUrlTemplateProblem(template: string): string | null;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The check of a trace URL template (FOLIWAKE_TRACE_URL_TEMPLATE) that `docker install` and `configure`
|
|
2
|
+
// write: the same rule as the engine's (src/observability/trace-link.ts there), which reads the value.
|
|
3
|
+
const PLACEHOLDER = '{traceId}';
|
|
4
|
+
/** The template with `{traceId}` replaced by `digit` 32 times, as a URL; null when it is none. */
|
|
5
|
+
function sampleUrl(parts, digit) {
|
|
6
|
+
try {
|
|
7
|
+
return new URL(parts.join(digit.repeat(32)));
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Why `template` is no usable trace URL template, or null when it is one: an http or https URL with
|
|
15
|
+
* `{traceId}` exactly once (outside its host) and no other placeholder, no space, user name or password.
|
|
16
|
+
*/
|
|
17
|
+
export function traceUrlTemplateProblem(template) {
|
|
18
|
+
const parts = template.split(PLACEHOLDER);
|
|
19
|
+
if (parts.length !== 2)
|
|
20
|
+
return `it must hold ${PLACEHOLDER} exactly once`;
|
|
21
|
+
if (/[{}]/.test(parts.join('')))
|
|
22
|
+
return `${PLACEHOLDER} must be its only placeholder`;
|
|
23
|
+
if (/\s/.test(template))
|
|
24
|
+
return 'it must not hold spaces';
|
|
25
|
+
const one = sampleUrl(parts, '1');
|
|
26
|
+
const other = sampleUrl(parts, 'f');
|
|
27
|
+
if (!one || !other)
|
|
28
|
+
return 'it is not a URL';
|
|
29
|
+
if (one.protocol !== 'http:' && one.protocol !== 'https:')
|
|
30
|
+
return 'it must be an http or https URL';
|
|
31
|
+
if (one.username !== '' || one.password !== '')
|
|
32
|
+
return 'it must not hold a user name or password';
|
|
33
|
+
if (one.origin !== other.origin)
|
|
34
|
+
return `${PLACEHOLDER} must not be part of its host`;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type DockerContext } from './context.js';
|
|
2
|
+
import { type PullPolicy } from './install.js';
|
|
3
|
+
import { type Instance } from './instance.js';
|
|
4
|
+
/**
|
|
5
|
+
* `update [--image REF] [--pull missing] [--no-backup]`: pull, compare; when the image changed, back up the
|
|
6
|
+
* data volume, recreate, then refresh every site's cached HTML. A failed pull fails; `--pull missing`
|
|
7
|
+
* (explicit) uses an image already on this engine, e.g. to roll back to a local build by its id.
|
|
8
|
+
*/
|
|
9
|
+
export declare function runUpdate(ctx: DockerContext, instance: Instance, image: string | undefined, pull?: PullPolicy, { backup }?: {
|
|
10
|
+
backup?: boolean;
|
|
11
|
+
}): Promise<number>;
|
|
12
|
+
export interface ConfigureRequest {
|
|
13
|
+
traces?: string;
|
|
14
|
+
port?: string;
|
|
15
|
+
bind?: string;
|
|
16
|
+
publicOrigin?: string;
|
|
17
|
+
domain?: string;
|
|
18
|
+
image?: string;
|
|
19
|
+
jaegerStore?: string;
|
|
20
|
+
jaegerPort?: string;
|
|
21
|
+
otlpEndpoint?: string;
|
|
22
|
+
/** The collector's trace page with `{traceId}`; '' removes it. Only with external traces. */
|
|
23
|
+
traceUrlTemplate?: string;
|
|
24
|
+
/** `none` or `varnish` (cache-plan.ts), with its port. */
|
|
25
|
+
cache?: string;
|
|
26
|
+
cachePort?: string;
|
|
27
|
+
}
|
|
28
|
+
/** `configure`: change the given settings, then recreate (the data volume stays). */
|
|
29
|
+
export declare function runConfigure(ctx: DockerContext, instance: Instance, request: ConfigureRequest): Promise<number>;
|