@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,105 @@
|
|
|
1
|
+
import type { DockerContext } from './context.js';
|
|
2
|
+
export declare const LABEL: {
|
|
3
|
+
readonly instance: "dev.foliwake.instance";
|
|
4
|
+
readonly role: "dev.foliwake.role";
|
|
5
|
+
readonly traces: "dev.foliwake.traces";
|
|
6
|
+
readonly cli: "dev.foliwake.cli";
|
|
7
|
+
};
|
|
8
|
+
/** How an instance records traces; `shared` is reserved for one Jaeger of several instances (later). */
|
|
9
|
+
export type TracesMode = 'none' | 'jaeger' | 'external';
|
|
10
|
+
export declare const TRACES_MODES: readonly TracesMode[];
|
|
11
|
+
export declare const DEFAULT_INSTANCE = "foliwake";
|
|
12
|
+
/** Why `name` cannot name an instance, or null. Same form as FOLIWAKE_ENVIRONMENT. */
|
|
13
|
+
export declare function instanceNameProblem(name: string): string | null;
|
|
14
|
+
/** The data volume of an instance, as Compose names it. */
|
|
15
|
+
export declare const dataVolume: (name: string) => string;
|
|
16
|
+
/** Container environment values a command may show. Secret values are never kept, only whether they are set. */
|
|
17
|
+
export interface ShownEnv {
|
|
18
|
+
publicOrigin: string | null;
|
|
19
|
+
ownerEmail: string | null;
|
|
20
|
+
domain: string | null;
|
|
21
|
+
storageEndpoint: string | null;
|
|
22
|
+
otlpEndpoint: string | null;
|
|
23
|
+
storageKeySet: boolean;
|
|
24
|
+
otlpHeadersSet: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface PublishedPort {
|
|
27
|
+
host: string;
|
|
28
|
+
port: number;
|
|
29
|
+
containerPort: number;
|
|
30
|
+
}
|
|
31
|
+
export interface ContainerInfo {
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
service: string;
|
|
35
|
+
role: string;
|
|
36
|
+
state: string;
|
|
37
|
+
health: string | null;
|
|
38
|
+
startedAt: string | null;
|
|
39
|
+
image: string;
|
|
40
|
+
imageId: string;
|
|
41
|
+
ports: PublishedPort[];
|
|
42
|
+
traces: string | null;
|
|
43
|
+
cli: string | null;
|
|
44
|
+
env: ShownEnv;
|
|
45
|
+
}
|
|
46
|
+
export interface VolumeInfo {
|
|
47
|
+
name: string;
|
|
48
|
+
role: string | null;
|
|
49
|
+
}
|
|
50
|
+
export interface Instance {
|
|
51
|
+
name: string;
|
|
52
|
+
containers: ContainerInfo[];
|
|
53
|
+
volumes: VolumeInfo[];
|
|
54
|
+
}
|
|
55
|
+
/** The container of a service of an instance (`foliwake` or `jaeger`). */
|
|
56
|
+
export declare const containerOf: (instance: Instance, role: string) => ContainerInfo | undefined;
|
|
57
|
+
/** Only the shown values out of a container's `KEY=value` environment; the rest is dropped here. */
|
|
58
|
+
export declare function shownEnv(entries: readonly string[]): ShownEnv;
|
|
59
|
+
interface InspectedContainer {
|
|
60
|
+
Id: string;
|
|
61
|
+
Name: string;
|
|
62
|
+
Image: string;
|
|
63
|
+
Config: {
|
|
64
|
+
Image: string;
|
|
65
|
+
Labels: Record<string, string> | null;
|
|
66
|
+
Env: string[] | null;
|
|
67
|
+
};
|
|
68
|
+
State: {
|
|
69
|
+
Status: string;
|
|
70
|
+
StartedAt?: string;
|
|
71
|
+
Health?: {
|
|
72
|
+
Status: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
NetworkSettings?: {
|
|
76
|
+
Ports?: Record<string, Array<{
|
|
77
|
+
HostIp: string;
|
|
78
|
+
HostPort: string;
|
|
79
|
+
}> | null> | null;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** The published ports of `docker inspect` (`8080/tcp` → host address and port). */
|
|
83
|
+
export declare function publishedPorts(ports: InspectedContainer['NetworkSettings']): PublishedPort[];
|
|
84
|
+
export declare function containerInfo(raw: InspectedContainer): ContainerInfo;
|
|
85
|
+
/** Every instance on this engine, by its labels: containers and volumes (volumes alone: "data only"). */
|
|
86
|
+
export declare function discoverInstances(ctx: DockerContext, only?: string): Promise<Instance[]>;
|
|
87
|
+
/** Every name Compose gives the resources of instance `name` (template services, volumes, network). */
|
|
88
|
+
export declare function resourceNames(name: string): {
|
|
89
|
+
containers: string[];
|
|
90
|
+
volumes: string[];
|
|
91
|
+
networks: string[];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* What on this engine would collide with instance `name` and is not that instance: a container,
|
|
95
|
+
* volume or network with one of the names Compose would give it, or anything of Compose project
|
|
96
|
+
* `name`, without the instance label. Null when there is none. Such resources are never touched:
|
|
97
|
+
* `install` refuses the name and `configure` refuses to add Jaeger (Compose would only warn and
|
|
98
|
+
* then use a foreign volume).
|
|
99
|
+
*/
|
|
100
|
+
export declare function foreignUse(ctx: DockerContext, name: string): Promise<string | null>;
|
|
101
|
+
/** The instance a command acts on: the named one, the only one, or the one chosen in a terminal. */
|
|
102
|
+
export declare function chooseInstance(ctx: DockerContext, name: string | undefined, { allowDataOnly }?: {
|
|
103
|
+
allowDataOnly?: boolean | undefined;
|
|
104
|
+
}): Promise<Instance>;
|
|
105
|
+
export {};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { DockerFailure, DockerUsageError } from './context.js';
|
|
2
|
+
import { failureText, jsonLines, printable } from './runner.js';
|
|
3
|
+
/** The form of an instance name: the form of a site or org id (and of FOLIWAKE_ENVIRONMENT) in the engine. */
|
|
4
|
+
const ID_FORM = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
5
|
+
export const LABEL = {
|
|
6
|
+
instance: 'dev.foliwake.instance',
|
|
7
|
+
role: 'dev.foliwake.role',
|
|
8
|
+
traces: 'dev.foliwake.traces',
|
|
9
|
+
cli: 'dev.foliwake.cli',
|
|
10
|
+
};
|
|
11
|
+
const COMPOSE_PROJECT = 'com.docker.compose.project';
|
|
12
|
+
const COMPOSE_SERVICE = 'com.docker.compose.service';
|
|
13
|
+
export const TRACES_MODES = ['none', 'jaeger', 'external'];
|
|
14
|
+
export const DEFAULT_INSTANCE = 'foliwake';
|
|
15
|
+
/** Why `name` cannot name an instance, or null. Same form as FOLIWAKE_ENVIRONMENT. */
|
|
16
|
+
export function instanceNameProblem(name) {
|
|
17
|
+
if (ID_FORM.test(name))
|
|
18
|
+
return null;
|
|
19
|
+
const form = 'use 1 to 40 lowercase letters, digits and dashes, starting with a letter or digit';
|
|
20
|
+
return `"${printable(name)}" cannot name an instance: ${form}.`;
|
|
21
|
+
}
|
|
22
|
+
/** The data volume of an instance, as Compose names it. */
|
|
23
|
+
export const dataVolume = (name) => `${name}_data`;
|
|
24
|
+
/** The container of a service of an instance (`foliwake` or `jaeger`). */
|
|
25
|
+
export const containerOf = (instance, role) => instance.containers.find((container) => container.role === role);
|
|
26
|
+
/** Only the shown values out of a container's `KEY=value` environment; the rest is dropped here. */
|
|
27
|
+
export function shownEnv(entries) {
|
|
28
|
+
const value = (key) => {
|
|
29
|
+
const entry = entries.find((line) => line.startsWith(`${key}=`));
|
|
30
|
+
const text = entry === undefined ? '' : entry.slice(key.length + 1);
|
|
31
|
+
return text === '' ? null : text;
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
publicOrigin: value('FOLIWAKE_PUBLIC_ORIGIN'),
|
|
35
|
+
ownerEmail: value('FOLIWAKE_OWNER_EMAIL'),
|
|
36
|
+
domain: value('FOLIWAKE_DOMAIN'),
|
|
37
|
+
storageEndpoint: value('FOLIWAKE_STORAGE_ENDPOINT'),
|
|
38
|
+
otlpEndpoint: value('OTEL_EXPORTER_OTLP_ENDPOINT'),
|
|
39
|
+
storageKeySet: value('FOLIWAKE_STORAGE_SECRET_ACCESS_KEY') !== null,
|
|
40
|
+
otlpHeadersSet: value('OTEL_EXPORTER_OTLP_HEADERS') !== null,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** The published ports of `docker inspect` (`8080/tcp` → host address and port). */
|
|
44
|
+
export function publishedPorts(ports) {
|
|
45
|
+
const result = [];
|
|
46
|
+
for (const [key, bindings] of Object.entries(ports?.Ports ?? {})) {
|
|
47
|
+
for (const binding of bindings ?? [])
|
|
48
|
+
result.push({ host: binding.HostIp, port: Number(binding.HostPort), containerPort: Number.parseInt(key, 10) });
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
export function containerInfo(raw) {
|
|
53
|
+
const labels = raw.Config.Labels ?? {};
|
|
54
|
+
return {
|
|
55
|
+
id: raw.Id,
|
|
56
|
+
name: raw.Name.replace(/^\//, ''),
|
|
57
|
+
service: labels[COMPOSE_SERVICE] ?? '',
|
|
58
|
+
role: labels[LABEL.role] ?? '',
|
|
59
|
+
state: raw.State.Status,
|
|
60
|
+
health: raw.State.Health?.Status ?? null,
|
|
61
|
+
startedAt: raw.State.StartedAt ?? null,
|
|
62
|
+
image: raw.Config.Image,
|
|
63
|
+
imageId: raw.Image,
|
|
64
|
+
ports: publishedPorts(raw.NetworkSettings),
|
|
65
|
+
traces: labels[LABEL.traces] ?? null,
|
|
66
|
+
cli: labels[LABEL.cli] ?? null,
|
|
67
|
+
env: shownEnv(raw.Config.Env ?? []),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async function inspect(ctx, kind, ids) {
|
|
71
|
+
if (!ids.length)
|
|
72
|
+
return [];
|
|
73
|
+
const result = await ctx.docker.run([kind, 'inspect', ...ids]);
|
|
74
|
+
if (result.code !== 0)
|
|
75
|
+
throw new DockerFailure(`docker ${kind} inspect failed: ${failureText(result)}`);
|
|
76
|
+
return JSON.parse(result.stdout);
|
|
77
|
+
}
|
|
78
|
+
/** Every instance on this engine, by its labels: containers and volumes (volumes alone: "data only"). */
|
|
79
|
+
export async function discoverInstances(ctx, only) {
|
|
80
|
+
const filter = only === undefined ? LABEL.instance : `${LABEL.instance}=${only}`;
|
|
81
|
+
const ps = await ctx.docker.run(['ps', '-a', '--filter', `label=${filter}`, '--format', 'json']);
|
|
82
|
+
if (ps.code !== 0)
|
|
83
|
+
throw new DockerFailure(`docker ps failed: ${failureText(ps)}`);
|
|
84
|
+
const ids = jsonLines(ps.stdout).map((row) => row.ID);
|
|
85
|
+
const containers = await inspect(ctx, 'container', ids);
|
|
86
|
+
const vols = await ctx.docker.run(['volume', 'ls', '--filter', `label=${filter}`, '--format', 'json']);
|
|
87
|
+
if (vols.code !== 0)
|
|
88
|
+
throw new DockerFailure(`docker volume ls failed: ${failureText(vols)}`);
|
|
89
|
+
const volumeNames = jsonLines(vols.stdout).map((row) => row.Name);
|
|
90
|
+
const volumes = await inspect(ctx, 'volume', volumeNames);
|
|
91
|
+
return groupByInstance(ctx, containers, volumes);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Containers and volumes grouped by their instance label. A label that is not an instance name is
|
|
95
|
+
* skipped and reported once: the name becomes a settings folder path and a Compose project, and
|
|
96
|
+
* anyone who can reach the engine can set a label (`../..` would point `remove` outside the folder).
|
|
97
|
+
*/
|
|
98
|
+
function groupByInstance(ctx, containers, volumes) {
|
|
99
|
+
const byName = new Map();
|
|
100
|
+
const skipped = new Set();
|
|
101
|
+
const instanceOf = (name) => {
|
|
102
|
+
if (!name)
|
|
103
|
+
return null;
|
|
104
|
+
if (instanceNameProblem(name)) {
|
|
105
|
+
skipped.add(name);
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
let instance = byName.get(name);
|
|
109
|
+
if (!instance)
|
|
110
|
+
byName.set(name, (instance = { name, containers: [], volumes: [] }));
|
|
111
|
+
return instance;
|
|
112
|
+
};
|
|
113
|
+
for (const raw of containers)
|
|
114
|
+
instanceOf(raw.Config.Labels?.[LABEL.instance])?.containers.push(containerInfo(raw));
|
|
115
|
+
for (const raw of volumes) {
|
|
116
|
+
instanceOf(raw.Labels?.[LABEL.instance])?.volumes.push({ name: raw.Name, role: raw.Labels?.[LABEL.role] ?? null });
|
|
117
|
+
}
|
|
118
|
+
for (const label of skipped) {
|
|
119
|
+
const what = `containers and volumes labelled ${LABEL.instance}="${printable(label)}"`;
|
|
120
|
+
ctx.err(`Skipped ${what}: not an instance name; they are left as they are.`);
|
|
121
|
+
}
|
|
122
|
+
return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
123
|
+
}
|
|
124
|
+
/** Every name Compose gives the resources of instance `name` (template services, volumes, network). */
|
|
125
|
+
export function resourceNames(name) {
|
|
126
|
+
return {
|
|
127
|
+
containers: [`${name}-foliwake-1`, `${name}-jaeger-1`],
|
|
128
|
+
volumes: [dataVolume(name), `${name}_jaeger-data`],
|
|
129
|
+
networks: [`${name}_default`],
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async function listed(ctx, args) {
|
|
133
|
+
const result = await ctx.docker.run([...args, '--format', 'json']);
|
|
134
|
+
if (result.code !== 0)
|
|
135
|
+
throw new DockerFailure(`docker ${args.join(' ')} failed: ${failureText(result)}`);
|
|
136
|
+
return jsonLines(result.stdout);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* What on this engine would collide with instance `name` and is not that instance: a container,
|
|
140
|
+
* volume or network with one of the names Compose would give it, or anything of Compose project
|
|
141
|
+
* `name`, without the instance label. Null when there is none. Such resources are never touched:
|
|
142
|
+
* `install` refuses the name and `configure` refuses to add Jaeger (Compose would only warn and
|
|
143
|
+
* then use a foreign volume).
|
|
144
|
+
*/
|
|
145
|
+
export async function foreignUse(ctx, name) {
|
|
146
|
+
const ours = (row) => (row.Labels ?? '').split(',').includes(`${LABEL.instance}=${name}`);
|
|
147
|
+
const ofProject = (row) => (row.Labels ?? '').split(',').includes(`${COMPOSE_PROJECT}=${name}`);
|
|
148
|
+
const names = resourceNames(name);
|
|
149
|
+
const containers = await listed(ctx, ['ps', '-a']);
|
|
150
|
+
const container = containers.find((row) => (ofProject(row) || names.containers.includes(row.Names ?? '')) && !ours(row));
|
|
151
|
+
if (container)
|
|
152
|
+
return `container ${container.Names}`;
|
|
153
|
+
const volumes = await listed(ctx, ['volume', 'ls']);
|
|
154
|
+
const volume = volumes.find((row) => (ofProject(row) || names.volumes.includes(row.Name ?? '')) && !ours(row));
|
|
155
|
+
if (volume)
|
|
156
|
+
return `volume ${volume.Name}`;
|
|
157
|
+
const networks = await listed(ctx, ['network', 'ls']);
|
|
158
|
+
const network = networks.find((row) => (ofProject(row) || names.networks.includes(row.Name ?? '')) && !ours(row));
|
|
159
|
+
return network ? `network ${network.Name}` : null;
|
|
160
|
+
}
|
|
161
|
+
/** The instance a command acts on: the named one, the only one, or the one chosen in a terminal. */
|
|
162
|
+
export async function chooseInstance(ctx, name, { allowDataOnly = false } = {}) {
|
|
163
|
+
if (name !== undefined) {
|
|
164
|
+
const problem = instanceNameProblem(name);
|
|
165
|
+
if (problem)
|
|
166
|
+
throw new DockerUsageError(problem);
|
|
167
|
+
}
|
|
168
|
+
const all = (await discoverInstances(ctx, name)).filter((instance) => allowDataOnly || instance.containers.length > 0);
|
|
169
|
+
if (name !== undefined) {
|
|
170
|
+
const found = all.find((instance) => instance.name === name);
|
|
171
|
+
if (!found)
|
|
172
|
+
throw new DockerUsageError(`There is no Foliwake instance "${name}" on this Docker engine. \`foliwake docker list\` shows them.`);
|
|
173
|
+
return found;
|
|
174
|
+
}
|
|
175
|
+
if (all.length === 1)
|
|
176
|
+
return all[0];
|
|
177
|
+
if (all.length === 0)
|
|
178
|
+
throw new DockerUsageError('There is no Foliwake instance on this Docker engine. Create one with `foliwake docker install`.');
|
|
179
|
+
if (!ctx.interactive)
|
|
180
|
+
throw new DockerUsageError(`There are several Foliwake instances (${all.map((instance) => instance.name).join(', ')}); name one.`);
|
|
181
|
+
const { askerOf } = await import('./asker.js');
|
|
182
|
+
const picked = await askerOf(ctx).select('Which instance?', all.map((instance) => ({ value: instance.name, label: instance.name })), all[0].name);
|
|
183
|
+
return all.find((instance) => instance.name === picked);
|
|
184
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DockerContext } from './context.js';
|
|
2
|
+
import { type Instance } from './instance.js';
|
|
3
|
+
/** Compose's "port is already allocated" in words that say what to do. */
|
|
4
|
+
export declare function translatePortError(text: string, name: string): string;
|
|
5
|
+
export type LifecycleVerb = 'start' | 'stop' | 'restart';
|
|
6
|
+
/** `docker compose -p <name> start|stop|restart`. */
|
|
7
|
+
export declare function runLifecycle(ctx: DockerContext, instance: Instance, verb: LifecycleVerb): Promise<number>;
|
|
8
|
+
export interface LogsOptions {
|
|
9
|
+
follow?: boolean;
|
|
10
|
+
since?: string;
|
|
11
|
+
tail?: string;
|
|
12
|
+
service?: string;
|
|
13
|
+
}
|
|
14
|
+
/** `docker compose -p <name> logs` of one service, with this terminal attached. */
|
|
15
|
+
export declare function runLogs(ctx: DockerContext, instance: Instance, options: LogsOptions): Promise<number>;
|
|
16
|
+
/** A new one-time sign-in link (`foliwake account link` in the container); the owner's by default. */
|
|
17
|
+
export declare function runLink(ctx: DockerContext, instance: Instance, email: string | undefined): Promise<number>;
|
|
18
|
+
/** The CLI inside the server container, with this terminal attached. */
|
|
19
|
+
export declare function runExec(ctx: DockerContext, instance: Instance, args: readonly string[], tty: boolean): Promise<number>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DockerFailure, DockerUsageError } from './context.js';
|
|
2
|
+
import { containerOf } from './instance.js';
|
|
3
|
+
import { failureText } from './runner.js';
|
|
4
|
+
import { composeEnv } from './settings-files.js';
|
|
5
|
+
/** Compose's "port is already allocated" in words that say what to do. */
|
|
6
|
+
export function translatePortError(text, name) {
|
|
7
|
+
const port = /(?:Bind for [^:]*:|bind [^:]*:|:)(\d+) failed: port is already allocated|address already in use/i.exec(text);
|
|
8
|
+
if (!port)
|
|
9
|
+
return text;
|
|
10
|
+
const which = port[1] ? `Port ${port[1]}` : 'A port of the instance';
|
|
11
|
+
return (`${which} is taken by another program or container since the instance was set up. Free it, or move ` +
|
|
12
|
+
`the instance: \`foliwake docker configure ${name} --port <port>\`.`);
|
|
13
|
+
}
|
|
14
|
+
const DONE = { start: 'Started', stop: 'Stopped', restart: 'Restarted' };
|
|
15
|
+
/** `docker compose -p <name> start|stop|restart`. */
|
|
16
|
+
export async function runLifecycle(ctx, instance, verb) {
|
|
17
|
+
const result = await ctx.docker.run(['compose', '-p', instance.name, verb], { env: composeEnv(ctx.env) });
|
|
18
|
+
if (result.code !== 0)
|
|
19
|
+
throw new DockerFailure(`Could not ${verb} ${instance.name}: ${translatePortError(failureText(result), instance.name)}`);
|
|
20
|
+
ctx.out(`${DONE[verb]} ${instance.name}.`);
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
/** `docker compose -p <name> logs` of one service, with this terminal attached. */
|
|
24
|
+
export async function runLogs(ctx, instance, options) {
|
|
25
|
+
const service = options.service ?? 'foliwake';
|
|
26
|
+
if (service !== 'foliwake' && service !== 'jaeger')
|
|
27
|
+
throw new DockerUsageError(`--service must be foliwake or jaeger; got ${JSON.stringify(service)}.`);
|
|
28
|
+
if (service === 'jaeger' && !containerOf(instance, 'jaeger')) {
|
|
29
|
+
throw new DockerUsageError(`Instance ${instance.name} has no Jaeger (\`foliwake docker configure ${instance.name} --traces jaeger\` ` +
|
|
30
|
+
`adds one).`);
|
|
31
|
+
}
|
|
32
|
+
if (service === 'foliwake')
|
|
33
|
+
ctx.err('The log holds one-time sign-in links: keep it private.');
|
|
34
|
+
const args = ['compose', '-p', instance.name, 'logs', '--no-log-prefix'];
|
|
35
|
+
if (options.follow)
|
|
36
|
+
args.push('--follow');
|
|
37
|
+
if (options.since)
|
|
38
|
+
args.push('--since', options.since);
|
|
39
|
+
if (options.tail)
|
|
40
|
+
args.push('--tail', options.tail);
|
|
41
|
+
return ctx.docker.attached([...args, service], { env: composeEnv(ctx.env) });
|
|
42
|
+
}
|
|
43
|
+
function serverContainer(instance) {
|
|
44
|
+
const server = containerOf(instance, 'server');
|
|
45
|
+
if (!server) {
|
|
46
|
+
throw new DockerUsageError(`Instance ${instance.name} has no server container (data only?): \`foliwake docker install ${instance.name}\` ` +
|
|
47
|
+
`starts it again.`);
|
|
48
|
+
}
|
|
49
|
+
if (server.state !== 'running')
|
|
50
|
+
throw new DockerUsageError(`Instance ${instance.name} is ${server.state}: \`foliwake docker start ${instance.name}\` first.`);
|
|
51
|
+
return server;
|
|
52
|
+
}
|
|
53
|
+
/** A new one-time sign-in link (`foliwake account link` in the container); the owner's by default. */
|
|
54
|
+
export async function runLink(ctx, instance, email) {
|
|
55
|
+
const server = serverContainer(instance);
|
|
56
|
+
const address = email ?? server.env.ownerEmail;
|
|
57
|
+
if (!address)
|
|
58
|
+
throw new DockerUsageError(`The owner's e-mail of ${instance.name} is not known: pass --email.`);
|
|
59
|
+
const result = await ctx.docker.run(['exec', server.id, 'foliwake', 'account', 'link', '--email', address]);
|
|
60
|
+
if (result.code !== 0)
|
|
61
|
+
throw new DockerFailure(failureText(result));
|
|
62
|
+
ctx.out(result.stdout.trim());
|
|
63
|
+
ctx.err('The link works once, for 15 minutes; anyone who has it can sign in as that account.');
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
/** The CLI inside the server container, with this terminal attached. */
|
|
67
|
+
export async function runExec(ctx, instance, args, tty) {
|
|
68
|
+
const server = serverContainer(instance);
|
|
69
|
+
return ctx.docker.attached(['exec', '-i', ...(tty ? ['-t'] : []), server.id, 'foliwake', ...args]);
|
|
70
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DockerContext } from './context.js';
|
|
2
|
+
import type { Instance } from './instance.js';
|
|
3
|
+
export declare const DEFAULT_PORT = 8080;
|
|
4
|
+
export declare const DEFAULT_JAEGER_PORT = 16686;
|
|
5
|
+
/** True when `port` can be bound on `host` now. */
|
|
6
|
+
export declare function canListen(host: string, port: number): Promise<boolean>;
|
|
7
|
+
/** Ports published by the instances on this engine, with the instance that holds each. */
|
|
8
|
+
export declare function portsInUse(instances: readonly Instance[]): Map<number, string>;
|
|
9
|
+
/** Why `port` cannot be used on `host`, or null: another instance publishes it, or something listens on it. */
|
|
10
|
+
export declare function portProblem(ctx: DockerContext, used: Map<number, string>, host: string, port: number): Promise<string | null>;
|
|
11
|
+
/** The first usable port from `start` on `host`, or null when none of the next SEARCH is. */
|
|
12
|
+
export declare function firstFreePort(ctx: DockerContext, used: Map<number, string>, host: string, start: number): Promise<number | null>;
|
|
13
|
+
/** A port number from text (1–65535), or null. */
|
|
14
|
+
export declare function parsePort(text: string): number | null;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Ports of a new instance: taken = published by another Foliwake instance (labels → ports) or not
|
|
2
|
+
// bindable on the chosen interface now (a test listen). The default is the first free port from
|
|
3
|
+
// 8080 (the Jaeger UI from 16686, always 127.0.0.1). A port typed by hand is never swapped for
|
|
4
|
+
// another silently: the public address must match it.
|
|
5
|
+
import net from 'node:net';
|
|
6
|
+
export const DEFAULT_PORT = 8080;
|
|
7
|
+
export const DEFAULT_JAEGER_PORT = 16686;
|
|
8
|
+
const SEARCH = 100;
|
|
9
|
+
/** True when `port` can be bound on `host` now. */
|
|
10
|
+
export function canListen(host, port) {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
const server = net.createServer();
|
|
13
|
+
server.unref();
|
|
14
|
+
server.once('error', () => resolve(false));
|
|
15
|
+
server.listen({ host, port, exclusive: true }, () => server.close(() => resolve(true)));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** Ports published by the instances on this engine, with the instance that holds each. */
|
|
19
|
+
export function portsInUse(instances) {
|
|
20
|
+
const used = new Map();
|
|
21
|
+
for (const instance of instances) {
|
|
22
|
+
for (const container of instance.containers)
|
|
23
|
+
for (const published of container.ports)
|
|
24
|
+
used.set(published.port, instance.name);
|
|
25
|
+
}
|
|
26
|
+
return used;
|
|
27
|
+
}
|
|
28
|
+
/** Why `port` cannot be used on `host`, or null: another instance publishes it, or something listens on it. */
|
|
29
|
+
export async function portProblem(ctx, used, host, port) {
|
|
30
|
+
const holder = used.get(port);
|
|
31
|
+
if (holder)
|
|
32
|
+
return `${port} is used by instance ${holder}`;
|
|
33
|
+
const free = await (ctx.portFree ?? canListen)(host, port);
|
|
34
|
+
return free ? null : `${port} is used by another program`;
|
|
35
|
+
}
|
|
36
|
+
/** The first usable port from `start` on `host`, or null when none of the next SEARCH is. */
|
|
37
|
+
export async function firstFreePort(ctx, used, host, start) {
|
|
38
|
+
for (let port = start; port < start + SEARCH && port <= 65535; port++) {
|
|
39
|
+
if ((await portProblem(ctx, used, host, port)) === null)
|
|
40
|
+
return port;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/** A port number from text (1–65535), or null. */
|
|
45
|
+
export function parsePort(text) {
|
|
46
|
+
if (!/^\d{1,5}$/.test(text))
|
|
47
|
+
return null;
|
|
48
|
+
const port = Number(text);
|
|
49
|
+
return port >= 1 && port <= 65535 ? port : null;
|
|
50
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DockerContext } from './context.js';
|
|
2
|
+
/** This process as a DockerContext; questions only when `interactive` (default: a terminal). */
|
|
3
|
+
export declare function processDockerContext(interactive: boolean | undefined): DockerContext;
|
|
4
|
+
/** What to print and the exit code for an error of the docker module, or null for any other error. */
|
|
5
|
+
export declare function dockerExit(error: unknown): {
|
|
6
|
+
message: string;
|
|
7
|
+
code: number;
|
|
8
|
+
} | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { DockerCancelled, DockerFailure, DockerUsageError } from './context.js';
|
|
2
|
+
import { DockerMissing, execDocker } from './runner.js';
|
|
3
|
+
import { cliVersion } from '../version.js';
|
|
4
|
+
/** This process as a DockerContext; questions only when `interactive` (default: a terminal). */
|
|
5
|
+
export function processDockerContext(interactive) {
|
|
6
|
+
const terminal = process.stdin.isTTY === true && process.stdout.isTTY === true;
|
|
7
|
+
return {
|
|
8
|
+
docker: execDocker,
|
|
9
|
+
env: process.env,
|
|
10
|
+
out: (line = '') => void process.stdout.write(`${line}\n`),
|
|
11
|
+
err: (line = '') => void process.stderr.write(`${line}\n`),
|
|
12
|
+
interactive: interactive ?? terminal,
|
|
13
|
+
prompts: { input: process.stdin, output: process.stdout },
|
|
14
|
+
cliVersion: cliVersion(),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** What to print and the exit code for an error of the docker module, or null for any other error. */
|
|
18
|
+
export function dockerExit(error) {
|
|
19
|
+
if (error instanceof DockerUsageError)
|
|
20
|
+
return { message: error.message, code: 2 };
|
|
21
|
+
if (error instanceof DockerCancelled)
|
|
22
|
+
return { message: 'Cancelled; nothing more was changed.', code: 130 };
|
|
23
|
+
if (error instanceof DockerFailure || error instanceof DockerMissing)
|
|
24
|
+
return { message: error.message, code: 1 };
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type DockerContext } from './context.js';
|
|
2
|
+
import { type Instance } from './instance.js';
|
|
3
|
+
export interface RemoveRequest {
|
|
4
|
+
deleteData?: boolean;
|
|
5
|
+
yes?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function runRemove(ctx: DockerContext, instance: Instance, request: RemoveRequest): Promise<number>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// `remove [name] [--delete-data] [--yes]`: stop and remove the containers and the network
|
|
2
|
+
// (`docker compose -p <name> down`) and this computer's settings folder. The data volume is kept
|
|
3
|
+
// unless deleting it is asked for, and then only after the instance name is typed (in a terminal,
|
|
4
|
+
// always) or with --delete-data --yes (without one). `list` shows a kept volume as "data only";
|
|
5
|
+
// `install` reuses it.
|
|
6
|
+
import { askerOf } from './asker.js';
|
|
7
|
+
import { DockerFailure, DockerUsageError } from './context.js';
|
|
8
|
+
import { dataVolume } from './instance.js';
|
|
9
|
+
import { failureText, printable } from './runner.js';
|
|
10
|
+
import { composeEnv, removeInstanceFiles } from './settings-files.js';
|
|
11
|
+
function describe(instance) {
|
|
12
|
+
const services = instance.containers.map((container) => container.service || container.name);
|
|
13
|
+
const containers = services.length > 1 ? 'containers' : 'container';
|
|
14
|
+
const what = services.length ? `${services.length} ${containers} (${services.join(', ')}), network` : 'no containers';
|
|
15
|
+
const volumeNames = instance.volumes.map((volume) => volume.name);
|
|
16
|
+
const parts = [what, volumeNames.length ? `volumes ${volumeNames.join(', ')}` : 'no volumes'];
|
|
17
|
+
return `Instance ${instance.name}: ${parts.join(', ')}.`;
|
|
18
|
+
}
|
|
19
|
+
/** Whether to delete the volumes: from the flags, else asked (typing the name); null: cancelled. */
|
|
20
|
+
async function decide(ctx, instance, request) {
|
|
21
|
+
if (!ctx.interactive) {
|
|
22
|
+
if (!request.yes) {
|
|
23
|
+
const kept = request.deleteData ? '' : ' (the data volume is kept unless --delete-data is given too)';
|
|
24
|
+
throw new DockerUsageError(`Without a terminal, \`remove\` needs --yes${kept}.`);
|
|
25
|
+
}
|
|
26
|
+
return request.deleteData === true;
|
|
27
|
+
}
|
|
28
|
+
const asker = askerOf(ctx);
|
|
29
|
+
if (!request.yes && !(await asker.confirm('Stop and remove it?', false)))
|
|
30
|
+
return null;
|
|
31
|
+
const choices = [
|
|
32
|
+
{ value: 'keep', label: 'Keep' },
|
|
33
|
+
{ value: 'delete', label: 'Delete', hint: 'cannot be undone' },
|
|
34
|
+
];
|
|
35
|
+
const remove = request.deleteData ??
|
|
36
|
+
(await asker.select('The data volume (sites, accounts, storage)', choices, 'keep')) === 'delete';
|
|
37
|
+
if (!remove)
|
|
38
|
+
return false;
|
|
39
|
+
// In a terminal, deleting the data always takes the name typed, --yes or not.
|
|
40
|
+
const typed = await asker.text(`Type ${instance.name} to delete its data for good`);
|
|
41
|
+
if (typed !== instance.name) {
|
|
42
|
+
ctx.err('The name did not match; the data is kept.');
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
export async function runRemove(ctx, instance, request) {
|
|
48
|
+
ctx.err(printable(describe(instance)));
|
|
49
|
+
if (!instance.containers.length && request.deleteData !== true && !ctx.interactive) {
|
|
50
|
+
ctx.out(`Only the data of ${instance.name} is left; \`--delete-data --yes\` deletes it.`);
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
const deleteData = await decide(ctx, instance, request);
|
|
54
|
+
if (deleteData === null) {
|
|
55
|
+
ctx.err('Nothing was changed.');
|
|
56
|
+
return 130;
|
|
57
|
+
}
|
|
58
|
+
if (instance.containers.length) {
|
|
59
|
+
const down = await ctx.docker.run(['compose', '-p', instance.name, 'down', '--remove-orphans'], {
|
|
60
|
+
env: composeEnv(ctx.env),
|
|
61
|
+
});
|
|
62
|
+
if (down.code !== 0)
|
|
63
|
+
throw new DockerFailure(`Could not remove ${instance.name}: ${failureText(down)}`);
|
|
64
|
+
}
|
|
65
|
+
if (deleteData) {
|
|
66
|
+
for (const volume of instance.volumes) {
|
|
67
|
+
const removed = await ctx.docker.run(['volume', 'rm', volume.name]);
|
|
68
|
+
if (removed.code !== 0)
|
|
69
|
+
throw new DockerFailure(`Could not delete volume ${volume.name}: ${failureText(removed)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
await removeInstanceFiles(ctx.env, instance.name);
|
|
73
|
+
const kept = instance.volumes.find((volume) => volume.name === dataVolume(instance.name));
|
|
74
|
+
if (deleteData)
|
|
75
|
+
ctx.out(`Removed ${instance.name} and its data.`);
|
|
76
|
+
else if (kept)
|
|
77
|
+
ctx.out(`Removed ${instance.name}. Data kept in ${kept.name}; ` +
|
|
78
|
+
`\`foliwake docker install ${instance.name}\` offers to reuse it.`);
|
|
79
|
+
else
|
|
80
|
+
ctx.out(`Removed ${instance.name}.`);
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DockerContext } from './context.js';
|
|
2
|
+
import { type Instance } from './instance.js';
|
|
3
|
+
export interface InstanceSummary {
|
|
4
|
+
name: string;
|
|
5
|
+
status: string;
|
|
6
|
+
address: string | null;
|
|
7
|
+
image: string | null;
|
|
8
|
+
imageId: string | null;
|
|
9
|
+
traces: string;
|
|
10
|
+
jaegerUi: string | null;
|
|
11
|
+
dataVolume: string | null;
|
|
12
|
+
}
|
|
13
|
+
export declare function summarize(instance: Instance): InstanceSummary;
|
|
14
|
+
/** A plain text table: each column as wide as its widest cell; Docker's text printable (runner.ts). */
|
|
15
|
+
export declare function table(cells: string[][]): string[];
|
|
16
|
+
export declare function listLines(summaries: readonly InstanceSummary[]): string[];
|
|
17
|
+
export interface InstanceStatus extends InstanceSummary {
|
|
18
|
+
startedAt: string | null;
|
|
19
|
+
ports: string[];
|
|
20
|
+
ownerEmail: string | null;
|
|
21
|
+
storage: string;
|
|
22
|
+
storageKey: 'set' | 'not set' | null;
|
|
23
|
+
otlpEndpoint: string | null;
|
|
24
|
+
otlpHeaders: 'set' | 'not set';
|
|
25
|
+
previousImage: string | null;
|
|
26
|
+
settings: string | null;
|
|
27
|
+
cli: string | null;
|
|
28
|
+
/** The instance's Varnish (`--cache varnish`): where the live sites are cached; null without one. */
|
|
29
|
+
cache: string | null;
|
|
30
|
+
}
|
|
31
|
+
export declare function instanceStatus(ctx: DockerContext, instance: Instance): Promise<InstanceStatus>;
|
|
32
|
+
export declare function statusLines(status: InstanceStatus): string[];
|