@wpelevator/tug 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -35
- package/dist/agent-credential.js +3 -3
- package/dist/agent.d.ts +5 -7
- package/dist/agent.js +45 -67
- package/dist/backup-check.d.ts +1 -1
- package/dist/backup-config.js +32 -24
- package/dist/backup-destination.d.ts +1 -1
- package/dist/backup-destination.js +1 -1
- package/dist/backup-job.js +17 -5
- package/dist/backup-restore.js +69 -69
- package/dist/backup-scheduler.js +3 -1
- package/dist/cli-build.d.ts +2 -0
- package/dist/cli-build.js +15 -2
- package/dist/cli-config.d.ts +10 -2
- package/dist/cli-config.js +28 -49
- package/dist/cli-install.d.ts +10 -5
- package/dist/cli-install.js +27 -24
- package/dist/cli-live.js +4 -6
- package/dist/cli-onboarding.d.ts +31 -0
- package/dist/cli-onboarding.js +66 -0
- package/dist/cli-prompt.d.ts +9 -0
- package/dist/cli-prompt.js +60 -0
- package/dist/cli-secrets.d.ts +12 -0
- package/dist/cli-secrets.js +71 -0
- package/dist/cli-status.d.ts +40 -0
- package/dist/cli-status.js +91 -0
- package/dist/cli.js +63 -989
- package/dist/commands/backup.d.ts +2 -0
- package/dist/commands/backup.js +109 -0
- package/dist/commands/context.d.ts +54 -0
- package/dist/commands/context.js +96 -0
- package/dist/commands/control.d.ts +2 -0
- package/dist/commands/control.js +110 -0
- package/dist/commands/credential.d.ts +2 -0
- package/dist/commands/credential.js +36 -0
- package/dist/commands/deploy.d.ts +2 -0
- package/dist/commands/deploy.js +139 -0
- package/dist/commands/host.d.ts +18 -0
- package/dist/commands/host.js +99 -0
- package/dist/commands/init.d.ts +11 -0
- package/dist/commands/init.js +40 -0
- package/dist/commands/node.d.ts +28 -0
- package/dist/commands/node.js +320 -0
- package/dist/commands/observe.d.ts +2 -0
- package/dist/commands/observe.js +71 -0
- package/dist/commands/restore.d.ts +2 -0
- package/dist/commands/restore.js +85 -0
- package/dist/commands/secret.d.ts +2 -0
- package/dist/commands/secret.js +26 -0
- package/dist/commands/service.d.ts +2 -0
- package/dist/commands/service.js +62 -0
- package/dist/commands/volume.d.ts +2 -0
- package/dist/commands/volume.js +13 -0
- package/dist/container-spec.d.ts +5 -0
- package/dist/container-spec.js +11 -2
- package/dist/control-credential.d.ts +2 -0
- package/dist/control-credential.js +4 -4
- package/dist/control.d.ts +1 -1
- package/dist/control.js +96 -143
- package/dist/credentials.js +1 -1
- package/dist/db.d.ts +0 -10
- package/dist/db.js +1 -2
- package/dist/diagnostics.js +1 -1
- package/dist/ingress.d.ts +1 -2
- package/dist/ingress.js +1 -1
- package/dist/main.js +1 -1
- package/dist/node-address.d.ts +2 -0
- package/dist/node-address.js +6 -0
- package/dist/pki.d.ts +1 -1
- package/dist/pki.js +2 -2
- package/dist/protocol.d.ts +2 -34
- package/dist/reconciler.js +40 -7
- package/dist/runners.d.ts +0 -6
- package/dist/runners.js +1 -6
- package/dist/templates/control-compose.yml +1 -2
- package/package.json +1 -1
- package/dist/mesh.d.ts +0 -74
- package/dist/mesh.js +0 -219
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** True when both ends are a terminal and the run is not CI, so asking a question can be answered. */
|
|
2
|
+
export declare function canPrompt(env?: NodeJS.ProcessEnv): boolean;
|
|
3
|
+
export type Prompter = (question: string, hidden?: boolean) => Promise<string>;
|
|
4
|
+
/** Ask a line of text on the terminal; with `hidden` the typed characters are not echoed. */
|
|
5
|
+
export declare const ask: Prompter;
|
|
6
|
+
/** A yes/no question; an empty answer takes `fallback`. */
|
|
7
|
+
export declare function confirm(question: string, fallback?: boolean, prompt?: Prompter): Promise<boolean>;
|
|
8
|
+
/** Pick one of `options` by number; an empty answer takes the option at `fallback`. */
|
|
9
|
+
export declare function choose(question: string, options: readonly string[], fallback?: number, prompt?: Prompter): Promise<number>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import readline from 'node:readline';
|
|
2
|
+
import { Writable } from 'node:stream';
|
|
3
|
+
/** True when both ends are a terminal and the run is not CI, so asking a question can be answered. */
|
|
4
|
+
export function canPrompt(env = process.env) {
|
|
5
|
+
return !!process.stdin.isTTY && !!process.stdout.isTTY && !env.CI;
|
|
6
|
+
}
|
|
7
|
+
/** Ask a line of text on the terminal; with `hidden` the typed characters are not echoed. */
|
|
8
|
+
export const ask = async (question, hidden = false) => {
|
|
9
|
+
const muted = { on: false };
|
|
10
|
+
const output = new Writable({
|
|
11
|
+
write(chunk, _encoding, callback) {
|
|
12
|
+
if (!muted.on)
|
|
13
|
+
process.stdout.write(chunk);
|
|
14
|
+
callback();
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
const rl = readline.createInterface({ input: process.stdin, output, terminal: true });
|
|
18
|
+
try {
|
|
19
|
+
return await new Promise((resolve, reject) => {
|
|
20
|
+
rl.once('close', () => reject(new Error('input closed before an answer was given')));
|
|
21
|
+
rl.question(question, (answer) => {
|
|
22
|
+
if (hidden)
|
|
23
|
+
process.stdout.write('\n');
|
|
24
|
+
resolve(answer);
|
|
25
|
+
});
|
|
26
|
+
muted.on = hidden;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
rl.removeAllListeners('close');
|
|
31
|
+
rl.close();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/** A yes/no question; an empty answer takes `fallback`. */
|
|
35
|
+
export async function confirm(question, fallback = true, prompt = ask) {
|
|
36
|
+
for (;;) {
|
|
37
|
+
const answer = (await prompt(`${question} ${fallback ? '(Y/n)' : '(y/N)'} `))
|
|
38
|
+
.trim()
|
|
39
|
+
.toLowerCase();
|
|
40
|
+
if (answer === '')
|
|
41
|
+
return fallback;
|
|
42
|
+
if (answer === 'y' || answer === 'yes')
|
|
43
|
+
return true;
|
|
44
|
+
if (answer === 'n' || answer === 'no')
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Pick one of `options` by number; an empty answer takes the option at `fallback`. */
|
|
49
|
+
export async function choose(question, options, fallback = 0, prompt = ask) {
|
|
50
|
+
console.error(question);
|
|
51
|
+
options.forEach((option, index) => console.error(` ${index + 1}) ${option}`));
|
|
52
|
+
for (;;) {
|
|
53
|
+
const answer = (await prompt(`Choose 1-${options.length} (${fallback + 1}): `)).trim();
|
|
54
|
+
if (answer === '')
|
|
55
|
+
return fallback;
|
|
56
|
+
const index = Number(answer) - 1;
|
|
57
|
+
if (Number.isInteger(index) && index >= 0 && index < options.length)
|
|
58
|
+
return index;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Prompter } from './cli-prompt.ts';
|
|
2
|
+
/** Variable names a Compose file requires, in order of appearance; ones with a default are optional. */
|
|
3
|
+
export declare function requiredVariables(source: string): string[];
|
|
4
|
+
/**
|
|
5
|
+
* Variables the Compose file references that nothing supplies: not a control secret, the environment
|
|
6
|
+
* or `.env`. Compose would render them empty, which for a password is never intended.
|
|
7
|
+
*/
|
|
8
|
+
export declare function findMissingSecrets(file: string, secretNames: readonly string[], env?: NodeJS.ProcessEnv): Promise<string[]>;
|
|
9
|
+
/** A URL-safe random secret with 192 bits of entropy. */
|
|
10
|
+
export declare function generateSecret(): string;
|
|
11
|
+
/** Resolve each missing secret interactively; returns the values to store. */
|
|
12
|
+
export declare function promptForSecrets(names: readonly string[], prompt?: Prompter): Promise<Record<string, string>>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { ask } from "./cli-prompt.js";
|
|
5
|
+
/** `$$` is an escaped dollar; `${NAME}` is required only when it has no `:-`, `-`, `:+`, `+`, `:?` or `?` modifier. */
|
|
6
|
+
const REFERENCE = /\$\$|\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
7
|
+
/** Variable names a Compose file requires, in order of appearance; ones with a default are optional. */
|
|
8
|
+
export function requiredVariables(source) {
|
|
9
|
+
const names = new Set();
|
|
10
|
+
for (const line of source.split('\n')) {
|
|
11
|
+
if (/^\s*#/.test(line))
|
|
12
|
+
continue;
|
|
13
|
+
for (const match of line.matchAll(REFERENCE)) {
|
|
14
|
+
const name = match[1] ?? match[2];
|
|
15
|
+
if (name)
|
|
16
|
+
names.add(name);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return [...names];
|
|
20
|
+
}
|
|
21
|
+
/** Variables Compose reads from the `.env` file next to the Compose file. */
|
|
22
|
+
async function dotEnvNames(dir) {
|
|
23
|
+
try {
|
|
24
|
+
const text = await fs.readFile(path.join(dir, '.env'), 'utf8');
|
|
25
|
+
return new Set(text
|
|
26
|
+
.split('\n')
|
|
27
|
+
.map((line) => /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line)?.[1])
|
|
28
|
+
.filter((name) => !!name));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return new Set();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Variables the Compose file references that nothing supplies: not a control secret, the environment
|
|
36
|
+
* or `.env`. Compose would render them empty, which for a password is never intended.
|
|
37
|
+
*/
|
|
38
|
+
export async function findMissingSecrets(file, secretNames, env = process.env) {
|
|
39
|
+
const known = new Set(secretNames);
|
|
40
|
+
const dotEnv = await dotEnvNames(path.dirname(file));
|
|
41
|
+
// An unreadable file is reported by `docker compose config` with its own message.
|
|
42
|
+
const source = await fs.readFile(file, 'utf8').catch(() => '');
|
|
43
|
+
return requiredVariables(source).filter((name) => !known.has(name) && env[name] === undefined && !dotEnv.has(name));
|
|
44
|
+
}
|
|
45
|
+
/** A URL-safe random secret with 192 bits of entropy. */
|
|
46
|
+
export function generateSecret() {
|
|
47
|
+
return crypto.randomBytes(24).toString('base64url');
|
|
48
|
+
}
|
|
49
|
+
/** Resolve each missing secret interactively; returns the values to store. */
|
|
50
|
+
export async function promptForSecrets(names, prompt = ask) {
|
|
51
|
+
const values = {};
|
|
52
|
+
for (const name of names) {
|
|
53
|
+
for (;;) {
|
|
54
|
+
const choice = (await prompt(`${name} is missing. [g]enerate a strong password or [e]nter a value? (g) `))
|
|
55
|
+
.trim()
|
|
56
|
+
.toLowerCase();
|
|
57
|
+
if (choice === '' || choice === 'g') {
|
|
58
|
+
values[name] = generateSecret();
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
if (choice === 'e') {
|
|
62
|
+
const value = await prompt(`${name}: `, true);
|
|
63
|
+
if (value) {
|
|
64
|
+
values[name] = value;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return values;
|
|
71
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** The control status endpoint returns database rows and a JSON-encoded node report. */
|
|
2
|
+
export interface StatusResponse {
|
|
3
|
+
projects: {
|
|
4
|
+
id: string;
|
|
5
|
+
current_deployment_id: string | null;
|
|
6
|
+
}[];
|
|
7
|
+
deployments: {
|
|
8
|
+
id: string;
|
|
9
|
+
version: string;
|
|
10
|
+
state: string;
|
|
11
|
+
error_message: string | null;
|
|
12
|
+
}[];
|
|
13
|
+
nodes: {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
status: string;
|
|
17
|
+
last_seen_at: string | null;
|
|
18
|
+
}[];
|
|
19
|
+
statuses: {
|
|
20
|
+
project_id: string;
|
|
21
|
+
node_id: string;
|
|
22
|
+
node: string | null;
|
|
23
|
+
health: string;
|
|
24
|
+
observed_version: string | null;
|
|
25
|
+
status_json: string;
|
|
26
|
+
}[];
|
|
27
|
+
}
|
|
28
|
+
/** Render observations without treating a stale node's last report as current health. */
|
|
29
|
+
export declare function formatStatus(status: StatusResponse): string;
|
|
30
|
+
export interface NodeListing {
|
|
31
|
+
name: string;
|
|
32
|
+
status: string;
|
|
33
|
+
address: string | null;
|
|
34
|
+
ingress: number | boolean;
|
|
35
|
+
labels: string[];
|
|
36
|
+
last_seen_at: string | null;
|
|
37
|
+
disabled_at?: string | null;
|
|
38
|
+
}
|
|
39
|
+
/** A table of the registered nodes: state, inter-node address, ingress role, labels and last contact. */
|
|
40
|
+
export declare function formatNodes(nodes: NodeListing[]): string;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** Render observations without treating a stale node's last report as current health. */
|
|
2
|
+
export function formatStatus(status) {
|
|
3
|
+
const lines = [];
|
|
4
|
+
const nodes = new Map(status.nodes.map((node) => [node.id, node]));
|
|
5
|
+
for (const project of [...status.projects].sort((a, b) => a.id.localeCompare(b.id))) {
|
|
6
|
+
const deployment = status.deployments.find((item) => item.id === project.current_deployment_id);
|
|
7
|
+
lines.push(`Project ${project.id}`);
|
|
8
|
+
if (deployment) {
|
|
9
|
+
lines.push(` Deployment: ${deployment.version} (${deployment.state})`);
|
|
10
|
+
if (deployment.error_message)
|
|
11
|
+
lines.push(` Error: ${deployment.error_message}`);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
lines.push(project.current_deployment_id
|
|
15
|
+
? ' Deployment: current deployment not in recent results'
|
|
16
|
+
: ' Deployment: none');
|
|
17
|
+
}
|
|
18
|
+
const reports = status.statuses
|
|
19
|
+
.filter((item) => item.project_id === project.id)
|
|
20
|
+
.sort((a, b) => (a.node || a.node_id).localeCompare(b.node || b.node_id));
|
|
21
|
+
if (!reports.length)
|
|
22
|
+
lines.push(' No node reports');
|
|
23
|
+
for (const report of reports) {
|
|
24
|
+
const node = nodes.get(report.node_id);
|
|
25
|
+
lines.push(` Node ${report.node || report.node_id}: ${node?.status || 'unknown'}; last reported ${report.health}${report.observed_version ? ` (${report.observed_version})` : ''}`);
|
|
26
|
+
try {
|
|
27
|
+
const detail = JSON.parse(report.status_json);
|
|
28
|
+
if (detail &&
|
|
29
|
+
typeof detail === 'object' &&
|
|
30
|
+
'error' in detail &&
|
|
31
|
+
typeof detail.error === 'string' &&
|
|
32
|
+
detail.error)
|
|
33
|
+
lines.push(` Error: ${detail.error}`);
|
|
34
|
+
if (detail &&
|
|
35
|
+
typeof detail === 'object' &&
|
|
36
|
+
'serviceStates' in detail &&
|
|
37
|
+
detail.serviceStates &&
|
|
38
|
+
typeof detail.serviceStates === 'object') {
|
|
39
|
+
lines.push(...serviceStateLines(detail.serviceStates));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
lines.push(' Report details unavailable');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!status.projects.length)
|
|
48
|
+
lines.push('No projects');
|
|
49
|
+
lines.push('', 'Nodes');
|
|
50
|
+
if (!status.nodes.length)
|
|
51
|
+
lines.push(' No nodes');
|
|
52
|
+
for (const node of [...status.nodes].sort((a, b) => a.name.localeCompare(b.name)))
|
|
53
|
+
lines.push(` ${node.name}: ${node.status}; last seen ${node.last_seen_at || 'never'}`);
|
|
54
|
+
return lines.join('\n');
|
|
55
|
+
}
|
|
56
|
+
/** A table of the registered nodes: state, inter-node address, ingress role, labels and last contact. */
|
|
57
|
+
export function formatNodes(nodes) {
|
|
58
|
+
if (!nodes.length)
|
|
59
|
+
return 'No nodes';
|
|
60
|
+
const rows = [
|
|
61
|
+
['NODE', 'STATUS', 'ADDRESS', 'INGRESS', 'LABELS', 'LAST SEEN'],
|
|
62
|
+
...[...nodes]
|
|
63
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
64
|
+
.map((node) => [
|
|
65
|
+
node.name,
|
|
66
|
+
node.disabled_at ? 'disabled' : node.status,
|
|
67
|
+
node.address || '-',
|
|
68
|
+
node.ingress ? 'yes' : 'no',
|
|
69
|
+
node.labels.join(',') || '-',
|
|
70
|
+
node.last_seen_at || 'never',
|
|
71
|
+
]),
|
|
72
|
+
];
|
|
73
|
+
const widths = rows[0].map((_, column) => Math.max(...rows.map((row) => row[column].length)));
|
|
74
|
+
return rows
|
|
75
|
+
.map((row) => row
|
|
76
|
+
.map((cell, column) => cell.padEnd(widths[column]))
|
|
77
|
+
.join(' ')
|
|
78
|
+
.trimEnd())
|
|
79
|
+
.join('\n');
|
|
80
|
+
}
|
|
81
|
+
function serviceStateLines(serviceStates) {
|
|
82
|
+
return Object.entries(serviceStates)
|
|
83
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
84
|
+
.flatMap(([name, state]) => {
|
|
85
|
+
const item = (state && typeof state === 'object' ? state : {});
|
|
86
|
+
if (typeof item.rolloutState !== 'string')
|
|
87
|
+
return [];
|
|
88
|
+
const error = typeof item.rolloutError === 'string' && item.rolloutError ? ` — ${item.rolloutError}` : '';
|
|
89
|
+
return [` ${name}: ${item.rolloutState}${error}`];
|
|
90
|
+
});
|
|
91
|
+
}
|