@marinoscar/platform-cli 0.1.0-next.12 → 0.1.0-next.14
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 +26 -0
- package/dist/deploy/index.d.ts +1 -1
- package/dist/deploy/index.d.ts.map +1 -1
- package/dist/engine/commands/deploy.d.ts.map +1 -1
- package/dist/engine/commands/deploy.js +2 -0
- package/dist/engine/commands/deploy.js.map +1 -1
- package/dist/engine/deploy/checks/compose.d.ts +16 -0
- package/dist/engine/deploy/checks/compose.d.ts.map +1 -0
- package/dist/engine/deploy/checks/compose.js +149 -0
- package/dist/engine/deploy/checks/compose.js.map +1 -0
- package/dist/engine/deploy/checks/index.d.ts +2 -0
- package/dist/engine/deploy/checks/index.d.ts.map +1 -1
- package/dist/engine/deploy/checks/index.js +8 -0
- package/dist/engine/deploy/checks/index.js.map +1 -1
- package/dist/engine/deploy/checks/stack-ports.d.ts +4 -0
- package/dist/engine/deploy/checks/stack-ports.d.ts.map +1 -0
- package/dist/engine/deploy/checks/stack-ports.js +234 -0
- package/dist/engine/deploy/checks/stack-ports.js.map +1 -0
- package/dist/engine/deploy/checks/types.d.ts +16 -0
- package/dist/engine/deploy/checks/types.d.ts.map +1 -1
- package/dist/engine/deploy/checks/types.js.map +1 -1
- package/dist/engine/deploy/compose-command.d.ts +16 -0
- package/dist/engine/deploy/compose-command.d.ts.map +1 -0
- package/dist/engine/deploy/compose-command.js +40 -0
- package/dist/engine/deploy/compose-command.js.map +1 -0
- package/dist/engine/deploy/compose-containers.d.ts +101 -0
- package/dist/engine/deploy/compose-containers.d.ts.map +1 -0
- package/dist/engine/deploy/compose-containers.js +191 -0
- package/dist/engine/deploy/compose-containers.js.map +1 -0
- package/dist/engine/deploy/compose-port-variables.d.ts +18 -0
- package/dist/engine/deploy/compose-port-variables.d.ts.map +1 -0
- package/dist/engine/deploy/compose-port-variables.js +136 -0
- package/dist/engine/deploy/compose-port-variables.js.map +1 -0
- package/dist/engine/deploy/compose-ports.d.ts +98 -0
- package/dist/engine/deploy/compose-ports.d.ts.map +1 -0
- package/dist/engine/deploy/compose-ports.js +204 -0
- package/dist/engine/deploy/compose-ports.js.map +1 -0
- package/dist/engine/deploy/compose-project.d.ts +38 -32
- package/dist/engine/deploy/compose-project.d.ts.map +1 -1
- package/dist/engine/deploy/compose-project.js +78 -100
- package/dist/engine/deploy/compose-project.js.map +1 -1
- package/dist/engine/deploy/install.d.ts +2 -10
- package/dist/engine/deploy/install.d.ts.map +1 -1
- package/dist/engine/deploy/install.js +83 -31
- package/dist/engine/deploy/install.js.map +1 -1
- package/dist/engine/deploy/stack-ports-gate.d.ts +19 -0
- package/dist/engine/deploy/stack-ports-gate.d.ts.map +1 -0
- package/dist/engine/deploy/stack-ports-gate.js +54 -0
- package/dist/engine/deploy/stack-ports-gate.js.map +1 -0
- package/dist/engine/deploy/steps/registry.d.ts +2 -2
- package/dist/engine/deploy/steps/registry.d.ts.map +1 -1
- package/dist/engine/deploy/steps/registry.js +2 -0
- package/dist/engine/deploy/steps/registry.js.map +1 -1
- package/dist/engine/deploy/testing/fake-containers.d.ts +29 -0
- package/dist/engine/deploy/testing/fake-containers.d.ts.map +1 -0
- package/dist/engine/deploy/testing/fake-containers.js +32 -0
- package/dist/engine/deploy/testing/fake-containers.js.map +1 -0
- package/dist/engine/deploy/testing/fake-vps.d.ts.map +1 -1
- package/dist/engine/deploy/testing/fake-vps.js +6 -0
- package/dist/engine/deploy/testing/fake-vps.js.map +1 -1
- package/dist/engine/deploy/update.d.ts.map +1 -1
- package/dist/engine/deploy/update.js +46 -1
- package/dist/engine/deploy/update.js.map +1 -1
- package/dist/engine/index.d.ts +1 -1
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js.map +1 -1
- package/dist/engine/tui/screens/deploy/doctor.js +1 -0
- package/dist/engine/tui/screens/deploy/doctor.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { composeArgv, composeCwd } from './compose-command.js';
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// Which host ports will the composed stack publish? (#1024)
|
|
4
|
+
// =============================================================================
|
|
5
|
+
//
|
|
6
|
+
// `docker compose up -d` recreates the services that changed, in dependency
|
|
7
|
+
// order, and only THEN meets a host port something else holds. By that point the
|
|
8
|
+
// web, api and nginx containers have been replaced and the one that needed the
|
|
9
|
+
// port (`greptimedb` on 127.0.0.1:14003) cannot start: the site is down. So the
|
|
10
|
+
// question "will every port the stack publishes bind?" has to be answered before
|
|
11
|
+
// the first container is touched, and the only complete answer to "which ports
|
|
12
|
+
// does the stack publish?" is compose's own: the same files, the same project and
|
|
13
|
+
// the same `.env` `up` will read, merged and interpolated (`!override` and
|
|
14
|
+
// `${APP_BIND_PORT:-3535}` included), i.e. `docker compose config --format json`.
|
|
15
|
+
//
|
|
16
|
+
// ⚠ THAT OUTPUT CARRIES EVERY SECRET. The rendered `environment:` of each
|
|
17
|
+
// service holds POSTGRES_PASSWORD, JWT_SECRET and the rest, in clear. This module
|
|
18
|
+
// parses it in memory and hands back ports only; it never logs, journals or
|
|
19
|
+
// quotes stdout, not even to explain a parse failure. A failed command's message
|
|
20
|
+
// is stderr (the executor's tail), which compose does not fill with values.
|
|
21
|
+
//
|
|
22
|
+
// Read-only, argv only, never a shell string. `config` needs no daemon: whether
|
|
23
|
+
// Docker is reachable is the caller's question (`listComposeContainers`).
|
|
24
|
+
// =============================================================================
|
|
25
|
+
/** Hard stop on one published range, so a pathological `1-65535` cannot make a check probe 65k sockets. */
|
|
26
|
+
const MAX_RANGE_PORTS = 1024;
|
|
27
|
+
/** Compose renders the whole stack; a big project on a small VPS is slow, a hang is not. */
|
|
28
|
+
const CONFIG_TIMEOUT_MS = 60_000;
|
|
29
|
+
/** `docker compose config --format json` output that is not the stack this module can read. */
|
|
30
|
+
export class ComposeConfigError extends Error {
|
|
31
|
+
constructor(message) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = 'ComposeConfigError';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Splits a short-syntax port spec (`127.0.0.1:14003:4003/tcp`) at the colons
|
|
38
|
+
* that are not inside `${...}` or an IPv6 `[...]`, and returns the host side.
|
|
39
|
+
*
|
|
40
|
+
* Shared by the rendered-config fallback and the raw-file variable scan, so
|
|
41
|
+
* both read one grammar.
|
|
42
|
+
*
|
|
43
|
+
* @param spec - The spec, quotes and whitespace already removed.
|
|
44
|
+
* @returns the host address (`''` when none), the published token (which may
|
|
45
|
+
* still hold `${VAR:-default}`), and the protocol; `undefined` for a spec
|
|
46
|
+
* with no published side (`"80"`, a container-only port).
|
|
47
|
+
*/
|
|
48
|
+
export function splitPortSpec(spec) {
|
|
49
|
+
const parts = [];
|
|
50
|
+
let current = '';
|
|
51
|
+
let braces = 0;
|
|
52
|
+
let brackets = 0;
|
|
53
|
+
for (const char of spec) {
|
|
54
|
+
if (char === '{')
|
|
55
|
+
braces += 1;
|
|
56
|
+
else if (char === '}')
|
|
57
|
+
braces = Math.max(0, braces - 1);
|
|
58
|
+
else if (char === '[')
|
|
59
|
+
brackets += 1;
|
|
60
|
+
else if (char === ']')
|
|
61
|
+
brackets = Math.max(0, brackets - 1);
|
|
62
|
+
if (char === ':' && braces === 0 && brackets === 0) {
|
|
63
|
+
parts.push(current);
|
|
64
|
+
current = '';
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
current += char;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
parts.push(current);
|
|
71
|
+
// [hostIp:]published:target[/protocol]; fewer than two parts publishes nothing.
|
|
72
|
+
if (parts.length < 2)
|
|
73
|
+
return undefined;
|
|
74
|
+
const target = parts[parts.length - 1] ?? '';
|
|
75
|
+
const slash = target.indexOf('/');
|
|
76
|
+
const protocol = slash === -1 ? 'tcp' : target.slice(slash + 1).toLowerCase();
|
|
77
|
+
const published = parts[parts.length - 2] ?? '';
|
|
78
|
+
const hostIp = parts.length >= 3 ? parts.slice(0, parts.length - 2).join(':').replace(/^\[|\]$/g, '') : '';
|
|
79
|
+
return { hostIp, published, protocol: protocol === '' ? 'tcp' : protocol };
|
|
80
|
+
}
|
|
81
|
+
/** The ports a `published` token names: `3535`, `"3535"` or the range `"8000-8002"`. */
|
|
82
|
+
function expandPublished(published) {
|
|
83
|
+
if (published === undefined || published === null)
|
|
84
|
+
return [];
|
|
85
|
+
const text = String(published).trim();
|
|
86
|
+
// '' and 0 mean "Docker picks one": no claim to collide with.
|
|
87
|
+
if (text === '' || text === '0')
|
|
88
|
+
return [];
|
|
89
|
+
const [first = '', last = first] = text.split('-');
|
|
90
|
+
const low = Number(first);
|
|
91
|
+
const high = Number(last);
|
|
92
|
+
if (!Number.isInteger(low) || !Number.isInteger(high) || low < 1 || high < low || high > 65535) {
|
|
93
|
+
throw new ComposeConfigError(`unreadable published port ${JSON.stringify(text)}`);
|
|
94
|
+
}
|
|
95
|
+
const ports = [];
|
|
96
|
+
for (let port = low; port <= high && ports.length < MAX_RANGE_PORTS; port += 1)
|
|
97
|
+
ports.push(port);
|
|
98
|
+
return ports;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Every host port a rendered compose project publishes, from the JSON of
|
|
102
|
+
* `docker compose config --format json`.
|
|
103
|
+
*
|
|
104
|
+
* `published` is a string for compose v2 and a number elsewhere, and a range
|
|
105
|
+
* (`"8000-8002"`) is one entry per port; no `published` (or `0`) is a port
|
|
106
|
+
* Docker picks and is no claim; no `host_ip` is every interface; the protocol
|
|
107
|
+
* defaults to `tcp`. A service that lists a short-syntax string instead of the
|
|
108
|
+
* long form is read too. A range wider than 1024 ports is cut at 1024.
|
|
109
|
+
*
|
|
110
|
+
* @param json - The command's stdout.
|
|
111
|
+
* @returns the ports, deduplicated, in service order.
|
|
112
|
+
* @throws ComposeConfigError when the text is not a compose project. The
|
|
113
|
+
* message never quotes the text: it holds the stack's secrets.
|
|
114
|
+
* @stability experimental
|
|
115
|
+
*/
|
|
116
|
+
export function parseComposePorts(json) {
|
|
117
|
+
let document;
|
|
118
|
+
try {
|
|
119
|
+
document = JSON.parse(json);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
throw new ComposeConfigError('the output of `docker compose config --format json` is not JSON');
|
|
123
|
+
}
|
|
124
|
+
const services = document?.services;
|
|
125
|
+
if (typeof services !== 'object' || services === null || Array.isArray(services)) {
|
|
126
|
+
throw new ComposeConfigError('the output of `docker compose config --format json` has no services');
|
|
127
|
+
}
|
|
128
|
+
const found = [];
|
|
129
|
+
const seen = new Set();
|
|
130
|
+
const add = (entry) => {
|
|
131
|
+
const key = `${entry.service}|${entry.hostIp}|${entry.port}|${entry.protocol}`;
|
|
132
|
+
if (seen.has(key))
|
|
133
|
+
return;
|
|
134
|
+
seen.add(key);
|
|
135
|
+
found.push(entry);
|
|
136
|
+
};
|
|
137
|
+
for (const [service, definition] of Object.entries(services)) {
|
|
138
|
+
const ports = definition?.ports;
|
|
139
|
+
if (!Array.isArray(ports))
|
|
140
|
+
continue;
|
|
141
|
+
for (const raw of ports) {
|
|
142
|
+
if (typeof raw === 'string') {
|
|
143
|
+
const split = splitPortSpec(raw.replace(/["'\s]/g, ''));
|
|
144
|
+
if (split === undefined)
|
|
145
|
+
continue;
|
|
146
|
+
for (const port of expandPublished(split.published)) {
|
|
147
|
+
add({ service, hostIp: split.hostIp, port, protocol: split.protocol });
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (typeof raw !== 'object' || raw === null)
|
|
152
|
+
continue;
|
|
153
|
+
const entry = raw;
|
|
154
|
+
const hostIp = typeof entry.host_ip === 'string' ? entry.host_ip.replace(/^\[|\]$/g, '') : '';
|
|
155
|
+
const protocol = typeof entry.protocol === 'string' && entry.protocol !== '' ? entry.protocol.toLowerCase() : 'tcp';
|
|
156
|
+
for (const port of expandPublished(entry.published))
|
|
157
|
+
add({ service, hostIp, port, protocol });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return found;
|
|
161
|
+
}
|
|
162
|
+
/** The wordings Docker uses for "I cannot be asked", from the docker-daemon check plus the missing binary. */
|
|
163
|
+
const DOCKER_UNREACHABLE = /cannot connect to the docker daemon|failed to connect to the docker api|is the docker daemon running|docker\.sock|docker: command not found/i;
|
|
164
|
+
/** The failure's own words: the executor's message (argv and stderr tail), and stderr. Never stdout. */
|
|
165
|
+
function failureText(error) {
|
|
166
|
+
if (!(error instanceof Error))
|
|
167
|
+
return String(error);
|
|
168
|
+
const result = error.result;
|
|
169
|
+
const stderr = result?.stderr?.trim() ?? '';
|
|
170
|
+
return stderr === '' || error.message.includes(stderr) ? error.message : `${error.message}\n${stderr}`;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Renders the deployment's compose project exactly as `up -d` would and lists
|
|
174
|
+
* the host ports it publishes, with `docker compose config --format json` run
|
|
175
|
+
* under the run's project and files, in the checkout's compose directory. That
|
|
176
|
+
* directory is where compose finds the deployment's `.env`: no `--env-file` is
|
|
177
|
+
* passed by any `up`, so none is passed here, and the process environment
|
|
178
|
+
* overrides the file the same way. Never runs anything that changes a container.
|
|
179
|
+
*
|
|
180
|
+
* @param options - The command seam, the deployment and the project.
|
|
181
|
+
* @returns the ports, or why they could not be read.
|
|
182
|
+
* @stability experimental
|
|
183
|
+
*/
|
|
184
|
+
export async function readStackPorts(options) {
|
|
185
|
+
const cwd = composeCwd(options.deployRoot);
|
|
186
|
+
const argv = composeArgv(['config', '--format', 'json'], options.project, options.groups, cwd);
|
|
187
|
+
let stdout;
|
|
188
|
+
try {
|
|
189
|
+
({ stdout } = await options.run(argv, { cwd, timeoutMs: CONFIG_TIMEOUT_MS }));
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
const message = failureText(error);
|
|
193
|
+
return DOCKER_UNREACHABLE.test(message)
|
|
194
|
+
? { status: 'docker-unreachable', message }
|
|
195
|
+
: { status: 'render-failed', message };
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
return { status: 'ok', ports: parseComposePorts(stdout) };
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
return { status: 'render-failed', message: error instanceof Error ? error.message : String(error) };
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=compose-ports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose-ports.js","sourceRoot":"","sources":["../../../src/engine/deploy/compose-ports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAG/D,gFAAgF;AAChF,6DAA6D;AAC7D,gFAAgF;AAChF,EAAE;AACF,4EAA4E;AAC5E,iFAAiF;AACjF,+EAA+E;AAC/E,gFAAgF;AAChF,iFAAiF;AACjF,+EAA+E;AAC/E,kFAAkF;AAClF,2EAA2E;AAC3E,kFAAkF;AAClF,EAAE;AACF,0EAA0E;AAC1E,kFAAkF;AAClF,4EAA4E;AAC5E,iFAAiF;AACjF,4EAA4E;AAC5E,EAAE;AACF,gFAAgF;AAChF,0EAA0E;AAC1E,gFAAgF;AAEhF,2GAA2G;AAC3G,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,4FAA4F;AAC5F,MAAM,iBAAiB,GAAG,MAAM,CAAC;AAkBjC,+FAA+F;AAC/F,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,KAAK,MAAM,IAAI,IAAI,IAAI,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AAM,EAAE,eAAe,EAAE,OAAO,EAAE,CAAC;IAC3C,CAAC;IAED,IAAI,CAAC;QACH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,iBAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;IAC5D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;IACtG,CAAC;AACH,CAAC"}
|
|
@@ -1,34 +1,35 @@
|
|
|
1
|
+
import { type ComposeProjectCommandRunner } from './compose-containers.js';
|
|
1
2
|
import { type DeployState } from './state.js';
|
|
3
|
+
export { isPathUnder, type ComposeContainer, type ComposePortBinding, type ComposeProjectCommandRunner } from './compose-containers.js';
|
|
2
4
|
/**
|
|
3
|
-
* The
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* The evidence that decided which project a deployment folder runs under,
|
|
6
|
+
* strongest first. Lets a message say WHY a project was picked.
|
|
7
|
+
*
|
|
8
|
+
* - `bind-port`: a container of that project publishes the deployment's bind
|
|
9
|
+
* port (running ones first), which is the stack that serves the site.
|
|
10
|
+
* - `running`: it is the only project with a running container.
|
|
11
|
+
* - `only`: it is the only project with any container started from the folder.
|
|
7
12
|
*
|
|
8
13
|
* @stability experimental
|
|
9
14
|
*/
|
|
10
|
-
export type
|
|
11
|
-
cwd: string;
|
|
12
|
-
timeoutMs?: number | undefined;
|
|
13
|
-
}) => Promise<{
|
|
14
|
-
/** What the command printed on stdout. */
|
|
15
|
-
stdout: string;
|
|
16
|
-
}>;
|
|
15
|
+
export type ComposeProjectBasis = 'bind-port' | 'running' | 'only';
|
|
17
16
|
/**
|
|
18
17
|
* What Docker says about the compose project of a deployment folder.
|
|
19
18
|
*
|
|
20
19
|
* @stability experimental
|
|
21
20
|
*/
|
|
22
21
|
export type ComposeProjectDetection = {
|
|
23
|
-
/**
|
|
22
|
+
/** One project is decided by the strongest evidence among those started from inside the folder. */
|
|
24
23
|
kind: 'found';
|
|
25
24
|
/** That project's name. */
|
|
26
25
|
project: string;
|
|
26
|
+
/** The evidence that decided it. */
|
|
27
|
+
basis: ComposeProjectBasis;
|
|
27
28
|
} | {
|
|
28
29
|
/** No container was started from inside the folder. */
|
|
29
30
|
kind: 'none';
|
|
30
31
|
} | {
|
|
31
|
-
/** Several projects own containers started from inside the folder. */
|
|
32
|
+
/** Several projects own containers started from inside the folder and no evidence separates them. */
|
|
32
33
|
kind: 'ambiguous';
|
|
33
34
|
/** Their names, sorted. */
|
|
34
35
|
projects: string[];
|
|
@@ -37,30 +38,29 @@ export type ComposeProjectDetection = {
|
|
|
37
38
|
kind: 'unavailable';
|
|
38
39
|
};
|
|
39
40
|
/**
|
|
40
|
-
*
|
|
41
|
-
* NOT under `/opt/apps/app`. Both are resolved first; nothing touches the disk,
|
|
42
|
-
* because a container's recorded directory need not exist any more.
|
|
43
|
-
*
|
|
44
|
-
* @param path - The path to test.
|
|
45
|
-
* @param root - The folder it may be under.
|
|
46
|
-
* @returns true when `path` is `root` or below it.
|
|
47
|
-
* @stability experimental
|
|
48
|
-
*/
|
|
49
|
-
export declare function isPathUnder(path: string, root: string): boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Asks Docker which compose project(s) the containers started from inside
|
|
41
|
+
* Asks Docker which compose project the containers started from inside
|
|
52
42
|
* `deployRoot` belong to, stopped containers included. A container is the
|
|
53
43
|
* deployment's when its working-directory label is the deploy root or under it.
|
|
54
44
|
*
|
|
45
|
+
* Several projects can live in one folder (the live stack plus leftovers of
|
|
46
|
+
* another), so the answer is the project the STRONGEST evidence names, in
|
|
47
|
+
* order: (1) the project of a container that publishes `options.bindPort`
|
|
48
|
+
* (running ones first: a stopped leftover publishes it in its configuration
|
|
49
|
+
* too), (2) the only project with a running container, (3) the only project.
|
|
50
|
+
* Several projects and no evidence that separates them is `ambiguous`.
|
|
51
|
+
*
|
|
55
52
|
* Never throws: a Docker that cannot be asked is `unavailable`. Read-only
|
|
56
53
|
* (`docker ps`, `docker inspect`), and argv only, never a shell string.
|
|
57
54
|
*
|
|
58
55
|
* @param deployRoot - The deployment folder (`<apps-root>/<name>`).
|
|
59
56
|
* @param run - The command seam; pass the engine's `runCommand`.
|
|
60
|
-
* @
|
|
57
|
+
* @param options - `bindPort`: the deployment's loopback port, the strongest evidence when known.
|
|
58
|
+
* @returns the project found and why, none, several, or unavailable.
|
|
61
59
|
* @stability experimental
|
|
62
60
|
*/
|
|
63
|
-
export declare function detectComposeProject(deployRoot: string, run: ComposeProjectCommandRunner
|
|
61
|
+
export declare function detectComposeProject(deployRoot: string, run: ComposeProjectCommandRunner, options?: {
|
|
62
|
+
bindPort?: number | undefined;
|
|
63
|
+
}): Promise<ComposeProjectDetection>;
|
|
64
64
|
/** What `reconcileComposeProject` needs. */
|
|
65
65
|
export interface ReconcileComposeProjectOptions {
|
|
66
66
|
/** The deployment folder. */
|
|
@@ -81,12 +81,18 @@ export interface ReconcileComposeProjectResult {
|
|
|
81
81
|
* Makes the carried compose project match the one Docker says the deployment
|
|
82
82
|
* runs under, when the record is absent or the legacy default.
|
|
83
83
|
*
|
|
84
|
+
* Docker names the project by the strongest evidence (see
|
|
85
|
+
* `detectComposeProject`), the state's bind port first: the stack that holds
|
|
86
|
+
* the port is the one that serves, whatever leftovers share the folder.
|
|
87
|
+
*
|
|
84
88
|
* Changes nothing (the state comes back as given) when there is no state, when
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
89
|
+
* Docker cannot be asked or knows no container of this folder, or when it
|
|
90
|
+
* already agrees. A project recorded on purpose is never overwritten; when the
|
|
91
|
+
* container holding the bind port belongs to ANOTHER project it is only
|
|
92
|
+
* reported (a warning), and the `bind-port-owner` check fails the run before
|
|
93
|
+
* anything starts. An ambiguous answer, with nothing decisive, changes nothing
|
|
94
|
+
* and returns a warning naming the projects and the state file: it is never
|
|
95
|
+
* silent, because keeping `compose` unseen is how a second stack got started.
|
|
90
96
|
*
|
|
91
97
|
* On `found` with a different project: the returned state carries it, and when
|
|
92
98
|
* a state file is on disk it is rewritten through the engine's own writer (same
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compose-project.d.ts","sourceRoot":"","sources":["../../../src/engine/deploy/compose-project.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compose-project.d.ts","sourceRoot":"","sources":["../../../src/engine/deploy/compose-project.ts"],"names":[],"mappings":"AAGA,OAAO,EAKL,KAAK,2BAA2B,EACjC,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAA+B,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAE3E,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,KAAK,kBAAkB,EAAE,KAAK,2BAA2B,EAAE,MAAM,yBAAyB,CAAC;AAkCxI;;;;;;;;;;GAUG;AACH,MAAM,MAAM,mBAAmB,GAAG,WAAW,GAAG,SAAS,GAAG,MAAM,CAAC;AAEnE;;;;GAIG;AACH,MAAM,MAAM,uBAAuB,GAC/B;IACE,mGAAmG;IACnG,IAAI,EAAE,OAAO,CAAC;IACd,2BAA2B;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,oCAAoC;IACpC,KAAK,EAAE,mBAAmB,CAAC;CAC5B,GACD;IACE,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;CACd,GACD;IACE,qGAAqG;IACrG,IAAI,EAAE,WAAW,CAAC;IAClB,2BAA2B;IAC3B,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB,GACD;IACE,mFAAmF;IACnF,IAAI,EAAE,aAAa,CAAC;CACrB,CAAC;AAON;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,oBAAoB,CACxC,UAAU,EAAE,MAAM,EAClB,GAAG,EAAE,2BAA2B,EAChC,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAO,GAC9C,OAAO,CAAC,uBAAuB,CAAC,CA6BlC;AAED,4CAA4C;AAC5C,MAAM,WAAW,8BAA8B;IAC7C,6BAA6B;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,wFAAwF;IACxF,KAAK,EAAE,WAAW,GAAG,SAAS,CAAC;IAC/B,wDAAwD;IACxD,UAAU,EAAE,2BAA2B,CAAC;CACzC;AAED,8CAA8C;AAC9C,MAAM,WAAW,6BAA6B;IAC5C,2GAA2G;IAC3G,KAAK,EAAE,WAAW,GAAG,SAAS,CAAC;IAC/B,mFAAmF;IACnF,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAuBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,8BAA8B,GACtC,OAAO,CAAC,6BAA6B,CAAC,CAkExC"}
|
|
@@ -1,109 +1,71 @@
|
|
|
1
1
|
import { copyFileSync, chmodSync, constants, existsSync } from 'node:fs';
|
|
2
|
-
import {
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { isPathUnder, listComposeContainers, publishesPort, } from './compose-containers.js';
|
|
3
4
|
import { LEGACY_COMPOSE_PROJECT } from './install.js';
|
|
4
5
|
import { deployStatePath, writeState } from './state.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// `composeProjectFor` answers the legacy `compose` for a state file that records
|
|
10
|
-
// no project, and that rule is right for a server whose stack really runs under
|
|
11
|
-
// `compose` (the default project of `infra/compose`). It is wrong for a server
|
|
12
|
-
// installed by a pre-platform CLI that ran compose with an app-named `-p`
|
|
13
|
-
// project and recorded nothing: the engine then passes `-p compose` to every
|
|
14
|
-
// `docker compose` call and to the deploy checks, looks for a stack that is not
|
|
15
|
-
// there and, worse, would START a second one beside the real stack, which holds
|
|
16
|
-
// the bind port. An outage caused by bookkeeping.
|
|
17
|
-
//
|
|
18
|
-
// A recorded name is only a record. What Docker says is the ground truth: every
|
|
19
|
-
// container compose starts carries the directory it was run from
|
|
20
|
-
// (`com.docker.compose.project.working_dir`), and a deployment's stack is run
|
|
21
|
-
// from inside its own folder. So where the state is loaded for `update`,
|
|
22
|
-
// `status` and `doctor`, this reads that label off every container, and when
|
|
23
|
-
// the record is absent (or only the legacy default, which an earlier attempt
|
|
24
|
-
// may have written back) and Docker names exactly one other project for this
|
|
25
|
-
// folder, that project is recorded. A project that was recorded on purpose is
|
|
26
|
-
// never overwritten, and Docker is not even asked.
|
|
27
|
-
// =============================================================================
|
|
28
|
-
/** The label a container carries naming its compose project. */
|
|
29
|
-
const COMPOSE_PROJECT_LABEL = 'com.docker.compose.project';
|
|
30
|
-
/** The label naming the directory compose was run from. */
|
|
31
|
-
const COMPOSE_WORKING_DIR_LABEL = 'com.docker.compose.project.working_dir';
|
|
32
|
-
/** Short by design: a wedged Docker socket must cost seconds, not the command. */
|
|
33
|
-
const DOCKER_QUERY_TIMEOUT_MS = 10_000;
|
|
34
|
-
/**
|
|
35
|
-
* One line per container: compose project, then compose working directory.
|
|
36
|
-
*
|
|
37
|
-
* `{{if .Config.Labels}}` guards a container created without any labels, whose
|
|
38
|
-
* `Labels` is null rather than an empty map (`index` on nil renders
|
|
39
|
-
* `<no value>` into the line otherwise); the separator sits outside the guards
|
|
40
|
-
* so every line has both fields.
|
|
41
|
-
*/
|
|
42
|
-
const INSPECT_FORMAT = `{{if .Config.Labels}}{{index .Config.Labels "${COMPOSE_PROJECT_LABEL}"}}{{end}}|` +
|
|
43
|
-
`{{if .Config.Labels}}{{index .Config.Labels "${COMPOSE_WORKING_DIR_LABEL}"}}{{end}}`;
|
|
44
|
-
/**
|
|
45
|
-
* Whether `path` is `root` or inside it, by path segment: `/opt/apps/app2` is
|
|
46
|
-
* NOT under `/opt/apps/app`. Both are resolved first; nothing touches the disk,
|
|
47
|
-
* because a container's recorded directory need not exist any more.
|
|
48
|
-
*
|
|
49
|
-
* @param path - The path to test.
|
|
50
|
-
* @param root - The folder it may be under.
|
|
51
|
-
* @returns true when `path` is `root` or below it.
|
|
52
|
-
* @stability experimental
|
|
53
|
-
*/
|
|
54
|
-
export function isPathUnder(path, root) {
|
|
55
|
-
const relation = relative(resolve(root), resolve(path));
|
|
56
|
-
return relation === '' || (relation !== '..' && !relation.startsWith(`..${sep}`) && !isAbsolute(relation));
|
|
6
|
+
export { isPathUnder } from './compose-containers.js';
|
|
7
|
+
/** The sorted, unique project names of `containers`. */
|
|
8
|
+
function projectsOf(containers) {
|
|
9
|
+
return [...new Set(containers.map((container) => container.project))].sort();
|
|
57
10
|
}
|
|
58
11
|
/**
|
|
59
|
-
* Asks Docker which compose project
|
|
12
|
+
* Asks Docker which compose project the containers started from inside
|
|
60
13
|
* `deployRoot` belong to, stopped containers included. A container is the
|
|
61
14
|
* deployment's when its working-directory label is the deploy root or under it.
|
|
62
15
|
*
|
|
16
|
+
* Several projects can live in one folder (the live stack plus leftovers of
|
|
17
|
+
* another), so the answer is the project the STRONGEST evidence names, in
|
|
18
|
+
* order: (1) the project of a container that publishes `options.bindPort`
|
|
19
|
+
* (running ones first: a stopped leftover publishes it in its configuration
|
|
20
|
+
* too), (2) the only project with a running container, (3) the only project.
|
|
21
|
+
* Several projects and no evidence that separates them is `ambiguous`.
|
|
22
|
+
*
|
|
63
23
|
* Never throws: a Docker that cannot be asked is `unavailable`. Read-only
|
|
64
24
|
* (`docker ps`, `docker inspect`), and argv only, never a shell string.
|
|
65
25
|
*
|
|
66
26
|
* @param deployRoot - The deployment folder (`<apps-root>/<name>`).
|
|
67
27
|
* @param run - The command seam; pass the engine's `runCommand`.
|
|
68
|
-
* @
|
|
28
|
+
* @param options - `bindPort`: the deployment's loopback port, the strongest evidence when known.
|
|
29
|
+
* @returns the project found and why, none, several, or unavailable.
|
|
69
30
|
* @stability experimental
|
|
70
31
|
*/
|
|
71
|
-
export async function detectComposeProject(deployRoot, run) {
|
|
32
|
+
export async function detectComposeProject(deployRoot, run, options = {}) {
|
|
72
33
|
try {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (
|
|
34
|
+
const listed = await listComposeContainers(run);
|
|
35
|
+
if (listed === undefined)
|
|
36
|
+
return { kind: 'unavailable' };
|
|
37
|
+
const here = listed.filter((container) => container.project !== '' && container.workingDir !== '' && isPathUnder(container.workingDir, deployRoot));
|
|
38
|
+
const projects = projectsOf(here);
|
|
39
|
+
const [only] = projects;
|
|
40
|
+
if (only === undefined)
|
|
80
41
|
return { kind: 'none' };
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
const project = line.slice(0, separator).trim();
|
|
89
|
-
const workingDir = line.slice(separator + 1).trim();
|
|
90
|
-
if (project === '' || workingDir === '')
|
|
91
|
-
continue;
|
|
92
|
-
if (isPathUnder(workingDir, deployRoot))
|
|
93
|
-
projects.add(project);
|
|
42
|
+
const { bindPort } = options;
|
|
43
|
+
if (bindPort !== undefined) {
|
|
44
|
+
const claimants = here.filter((container) => publishesPort(container, bindPort));
|
|
45
|
+
const serving = claimants.filter((container) => container.running);
|
|
46
|
+
const [holder, ...others] = projectsOf(serving.length > 0 ? serving : claimants);
|
|
47
|
+
if (holder !== undefined && others.length === 0)
|
|
48
|
+
return { kind: 'found', project: holder, basis: 'bind-port' };
|
|
94
49
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return { kind: 'ambiguous', projects: found };
|
|
50
|
+
if (projects.length === 1)
|
|
51
|
+
return { kind: 'found', project: only, basis: 'only' };
|
|
52
|
+
const [live, ...otherLive] = projectsOf(here.filter((container) => container.running));
|
|
53
|
+
if (live !== undefined && otherLive.length === 0)
|
|
54
|
+
return { kind: 'found', project: live, basis: 'running' };
|
|
55
|
+
return { kind: 'ambiguous', projects };
|
|
102
56
|
}
|
|
103
57
|
catch {
|
|
104
58
|
return { kind: 'unavailable' };
|
|
105
59
|
}
|
|
106
60
|
}
|
|
61
|
+
/** Why a project was picked, as a clause for the operator. */
|
|
62
|
+
function basisText(basis, bindPort) {
|
|
63
|
+
if (basis === 'bind-port')
|
|
64
|
+
return `it holds port ${bindPort ?? 'the bind port'}`;
|
|
65
|
+
if (basis === 'running')
|
|
66
|
+
return 'it is the only project with a running container here';
|
|
67
|
+
return 'it is the only project with containers here';
|
|
68
|
+
}
|
|
107
69
|
/**
|
|
108
70
|
* Copies the previous state file once to `<file>.bak` (mode 0600) and keeps an
|
|
109
71
|
* existing backup: the one from before the first change is the one worth having.
|
|
@@ -123,12 +85,18 @@ function keepBackup(path) {
|
|
|
123
85
|
* Makes the carried compose project match the one Docker says the deployment
|
|
124
86
|
* runs under, when the record is absent or the legacy default.
|
|
125
87
|
*
|
|
88
|
+
* Docker names the project by the strongest evidence (see
|
|
89
|
+
* `detectComposeProject`), the state's bind port first: the stack that holds
|
|
90
|
+
* the port is the one that serves, whatever leftovers share the folder.
|
|
91
|
+
*
|
|
126
92
|
* Changes nothing (the state comes back as given) when there is no state, when
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
93
|
+
* Docker cannot be asked or knows no container of this folder, or when it
|
|
94
|
+
* already agrees. A project recorded on purpose is never overwritten; when the
|
|
95
|
+
* container holding the bind port belongs to ANOTHER project it is only
|
|
96
|
+
* reported (a warning), and the `bind-port-owner` check fails the run before
|
|
97
|
+
* anything starts. An ambiguous answer, with nothing decisive, changes nothing
|
|
98
|
+
* and returns a warning naming the projects and the state file: it is never
|
|
99
|
+
* silent, because keeping `compose` unseen is how a second stack got started.
|
|
132
100
|
*
|
|
133
101
|
* On `found` with a different project: the returned state carries it, and when
|
|
134
102
|
* a state file is on disk it is rewritten through the engine's own writer (same
|
|
@@ -150,30 +118,40 @@ export async function reconcileComposeProject(options) {
|
|
|
150
118
|
try {
|
|
151
119
|
if (state === undefined)
|
|
152
120
|
return { state };
|
|
153
|
-
// A recorded, non-legacy project is a decision: never overwritten, and
|
|
154
|
-
// Docker is not even asked.
|
|
155
|
-
const recorded = state.composeProject;
|
|
156
|
-
if (recorded !== undefined && recorded !== LEGACY_COMPOSE_PROJECT)
|
|
157
|
-
return { state };
|
|
158
121
|
const root = resolve(options.deployRoot);
|
|
159
|
-
const
|
|
122
|
+
const bindPort = typeof state.bindPort === 'number' ? state.bindPort : undefined;
|
|
123
|
+
const detection = await detectComposeProject(root, options.runCommand, { bindPort });
|
|
124
|
+
const recorded = state.composeProject;
|
|
160
125
|
const shown = recorded ?? 'none';
|
|
161
126
|
const path = deployStatePath(root);
|
|
127
|
+
// A recorded, non-legacy project is a decision: never overwritten. But the
|
|
128
|
+
// stack that holds the port living under ANOTHER project is worth saying,
|
|
129
|
+
// since this run would start a second one beside it.
|
|
130
|
+
if (recorded !== undefined && recorded !== LEGACY_COMPOSE_PROJECT) {
|
|
131
|
+
if (detection.kind === 'found' && detection.basis === 'bind-port' && detection.project !== recorded) {
|
|
132
|
+
return {
|
|
133
|
+
state,
|
|
134
|
+
message: `warning: the state file records compose project ${recorded} for ${root}, but the stack holding port ${bindPort ?? ''} ` +
|
|
135
|
+
`runs under ${detection.project}; the record was not changed. Set "composeProject" to ${detection.project} in ${path}, ` +
|
|
136
|
+
`or remove the leftover containers of ${detection.project} if they are not the live stack.`,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return { state };
|
|
140
|
+
}
|
|
162
141
|
if (detection.kind === 'ambiguous') {
|
|
163
|
-
// The engine's effective project is among them: its default works, nothing to say.
|
|
164
|
-
if (detection.projects.includes(LEGACY_COMPOSE_PROJECT))
|
|
165
|
-
return { state };
|
|
166
142
|
return {
|
|
167
143
|
state,
|
|
168
144
|
message: `warning: containers of ${root} run under several compose projects ` +
|
|
169
|
-
`(${detection.projects.join(', ')})
|
|
170
|
-
`
|
|
145
|
+
`(${detection.projects.join(', ')}) and none of them holds port ${bindPort ?? 'the bind port'} alone or is the only one running; ` +
|
|
146
|
+
`the state file records ${shown}, so the compose project was not changed. ` +
|
|
147
|
+
`Set "composeProject" in ${path} to the right one, or remove the leftover containers.`,
|
|
171
148
|
};
|
|
172
149
|
}
|
|
173
150
|
if (detection.kind !== 'found' || detection.project === LEGACY_COMPOSE_PROJECT)
|
|
174
151
|
return { state };
|
|
175
152
|
const reconciled = { ...state, composeProject: detection.project };
|
|
176
|
-
const note = `note: recorded compose project ${detection.project} for ${root}
|
|
153
|
+
const note = `note: recorded compose project ${detection.project} for ${root} ` +
|
|
154
|
+
`(${basisText(detection.basis, bindPort)}; the state file had ${shown}).`;
|
|
177
155
|
// No file on disk, or one whose recorded root is elsewhere (a moved
|
|
178
156
|
// folder): `writeState` writes to `state.deployRoot`, and creating that
|
|
179
157
|
// directory somewhere else is worse than not recording. Carry it in memory.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compose-project.js","sourceRoot":"","sources":["../../../src/engine/deploy/compose-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACzE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"compose-project.js","sourceRoot":"","sources":["../../../src/engine/deploy/compose-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EACL,WAAW,EACX,qBAAqB,EACrB,aAAa,GAGd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,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