@openupstream/cli 0.0.0-stage → 0.2.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 +141 -2
- package/dist/credentials.d.ts +21 -0
- package/dist/credentials.js +34 -0
- package/dist/credentials.js.map +1 -0
- package/dist/databases.d.ts +50 -0
- package/dist/databases.js +69 -0
- package/dist/databases.js.map +1 -0
- package/dist/deploy.d.ts +39 -0
- package/dist/deploy.js +142 -0
- package/dist/deploy.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +817 -0
- package/dist/index.js.map +1 -0
- package/dist/jobs.d.ts +71 -0
- package/dist/jobs.js +95 -0
- package/dist/jobs.js.map +1 -0
- package/dist/json.d.ts +2 -0
- package/dist/json.js +10 -0
- package/dist/json.js.map +1 -0
- package/dist/logs.d.ts +24 -0
- package/dist/logs.js +27 -0
- package/dist/logs.js.map +1 -0
- package/dist/oidc.d.ts +47 -0
- package/dist/oidc.js +127 -0
- package/dist/oidc.js.map +1 -0
- package/dist/project.d.ts +34 -0
- package/dist/project.js +112 -0
- package/dist/project.js.map +1 -0
- package/dist/result.d.ts +10 -0
- package/dist/result.js +3 -0
- package/dist/result.js.map +1 -0
- package/dist/services.d.ts +65 -0
- package/dist/services.js +50 -0
- package/dist/services.js.map +1 -0
- package/dist/session.d.ts +9 -0
- package/dist/session.js +82 -0
- package/dist/session.js.map +1 -0
- package/dist/stdin.d.ts +10 -0
- package/dist/stdin.js +47 -0
- package/dist/stdin.js.map +1 -0
- package/dist/version.d.ts +5 -0
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/dist/wait.d.ts +31 -0
- package/dist/wait.js +30 -0
- package/dist/wait.js.map +1 -0
- package/package.json +39 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,817 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* oup, the openupstream CLI.
|
|
4
|
+
*
|
|
5
|
+
* oup login [--api <url>] [--no-browser] sign in through the browser (device grant)
|
|
6
|
+
* oup logout
|
|
7
|
+
* oup project list | create <name> [--account <name|id>] [--link]
|
|
8
|
+
* oup link <project> this directory's Project (.openupstream.json)
|
|
9
|
+
* oup pg list | show <name> | delete <name>
|
|
10
|
+
* oup pg create <name> [--memory <MB>] [--storage <GB>] [--site …] waits until ready
|
|
11
|
+
* oup pg restore <source> <name> [--time <RFC 3339>] … a new Database as a copy of <source>
|
|
12
|
+
* oup pg resize <name> [--memory <MB>] [--storage <GB>] storage only grows
|
|
13
|
+
* oup service list | show <name> | delete <name>
|
|
14
|
+
* oup service deploy <name> --image <ref> [--port … | --no-port] …
|
|
15
|
+
* oup service logs <name> [--tail <n>] [-f] [--timestamps]
|
|
16
|
+
* oup job list | show <name> | runs <name> | delete <name>
|
|
17
|
+
* oup job deploy <name> --image <ref> [--schedule …] … [-- command…]
|
|
18
|
+
* oup job run <name> [--env NAME=value] [-- args…] waits for the outcome
|
|
19
|
+
* oup job stop <name> <run>
|
|
20
|
+
* oup job logs <name> [<run>] [--tail <n>] [-f] [--timestamps] the newest run by default
|
|
21
|
+
* oup secret list | set <NAME> | delete <NAME> a value is read from stdin
|
|
22
|
+
* oup site deploy <dir> --bucket <name> [--keep] [--force]
|
|
23
|
+
* oup version
|
|
24
|
+
*
|
|
25
|
+
* Database, service, job and secret commands act on the linked Project; `--project`
|
|
26
|
+
* or `OUP_PROJECT` names another.
|
|
27
|
+
*
|
|
28
|
+
* Planned (ARCHITECTURE.md build order): oup bucket, oup routes, oup usage.
|
|
29
|
+
*
|
|
30
|
+
* `OUP_API_KEY` (a madAuth API key) replaces `oup login`, e.g. in CI, with
|
|
31
|
+
* `OUP_API_URL` for another API than https://openupstream.com/api.
|
|
32
|
+
*/
|
|
33
|
+
import { spawn } from 'node:child_process';
|
|
34
|
+
import { parseArgs } from 'node:util';
|
|
35
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
36
|
+
import { COMPUTE_SITES, DEFAULT_API_URL, OpenUpstreamClient, SDK_VERSION, cliLogin, } from '@openupstream/sdk';
|
|
37
|
+
import { credentialsPath, readCredentials, removeCredentials, writeCredentials } from './credentials.js';
|
|
38
|
+
import { deploy } from './deploy.js';
|
|
39
|
+
import { discover, emailOf, startDeviceSignIn, waitForApproval } from './oidc.js';
|
|
40
|
+
import { accountFor, findLink, formatProjects, projectFor, resolveProject, writeLink } from './project.js';
|
|
41
|
+
import { JOB_FLAGS, formatJobs, formatRuns, isFinished, outcomeOf, parseEnv, planJobDeploy, splitAtDashes, strayFlag, } from './jobs.js';
|
|
42
|
+
import { formatLogEntry, parseTail } from './logs.js';
|
|
43
|
+
import { DATABASE_DEFAULTS, formatDatabases, growthOf, growthSettings, planRestore } from './databases.js';
|
|
44
|
+
import { formatServices, planServiceDeploy, waitUntilRunning } from './services.js';
|
|
45
|
+
import { apiAccess } from './session.js';
|
|
46
|
+
import { readSecretValue } from './stdin.js';
|
|
47
|
+
import { waitUntil } from './wait.js';
|
|
48
|
+
import { CLI_VERSION } from './version.js';
|
|
49
|
+
const USAGE = `usage:
|
|
50
|
+
oup login [--api <url>] [--no-browser]
|
|
51
|
+
oup logout
|
|
52
|
+
oup project list
|
|
53
|
+
oup project create <name> [--account <name|id>] [--link]
|
|
54
|
+
oup link <project>
|
|
55
|
+
oup pg list
|
|
56
|
+
oup pg show <name>
|
|
57
|
+
oup pg create <name> [--memory <MB>] [--storage <GB>] [--site hetzner-fsn1|ovh-lim] [--max-storage <GB> | --no-auto-grow] [--no-wait]
|
|
58
|
+
oup pg restore <source> <name> [--time <RFC 3339>] [--memory <MB>] [--storage <GB>] [--site …] [--max-storage <GB> | --no-auto-grow] [--no-wait]
|
|
59
|
+
oup pg resize <name> [--memory <MB>] [--storage <GB>] [--max-storage <GB> | --no-max-storage] [--auto-grow | --no-auto-grow] [--no-wait]
|
|
60
|
+
oup pg delete <name>
|
|
61
|
+
oup service list
|
|
62
|
+
oup service show <name>
|
|
63
|
+
oup service deploy <name> --image <ref> [--port <n> | --no-port] [--replicas <n>] [--memory <MB>] [--cpu <millicores>]
|
|
64
|
+
[--site hetzner-fsn1|ovh-lim] [--registry-user <user> --registry-password-stdin] [--no-wait]
|
|
65
|
+
oup service logs <name> [--tail <n>] [-f | --follow] [--timestamps]
|
|
66
|
+
oup service delete <name>
|
|
67
|
+
oup job list
|
|
68
|
+
oup job show <name>
|
|
69
|
+
oup job deploy <name> --image <ref> [--schedule <cron> | --no-schedule] [--time-zone <IANA name>]
|
|
70
|
+
[--timeout <seconds>] [--memory <MB>] [--cpu <millicores>] [--site hetzner-fsn1|ovh-lim]
|
|
71
|
+
[--registry-user <user> --registry-password-stdin] [--no-wait] [--no-command | -- <command> <arg>…]
|
|
72
|
+
oup job run <name> [--env NAME=value]… [--no-wait] [-- <arg>…]
|
|
73
|
+
oup job runs <name>
|
|
74
|
+
oup job stop <name> <run>
|
|
75
|
+
oup job logs <name> [<run>] [--tail <n>] [-f | --follow] [--timestamps]
|
|
76
|
+
oup job delete <name>
|
|
77
|
+
oup secret list
|
|
78
|
+
oup secret set <NAME> (the value from stdin)
|
|
79
|
+
oup secret delete <NAME>
|
|
80
|
+
oup site deploy <dir> --bucket <name> [--keep] [--force]
|
|
81
|
+
oup version
|
|
82
|
+
|
|
83
|
+
Database, service, job and secret commands take --project <name|id>; without
|
|
84
|
+
it, the Project linked to this directory (oup link) or OUP_PROJECT.
|
|
85
|
+
|
|
86
|
+
pg create gives a Database 256 MB and 1 GB unless told otherwise. pg restore
|
|
87
|
+
creates a new Database that starts as a copy of <source> at --time, or at
|
|
88
|
+
the latest point its backups reach; <source> keeps running. Its size and
|
|
89
|
+
Site default to the source's. A deleted Database can be restored by its id
|
|
90
|
+
(oup pg delete prints it) within the backup retention window, with --storage.
|
|
91
|
+
Storage grows by itself by half once 90% full, without a limit unless
|
|
92
|
+
--max-storage sets one; --no-auto-grow leaves it to pg resize.
|
|
93
|
+
pg resize grows the storage online; a new memory size restarts the
|
|
94
|
+
instances one after the other (writes pause for seconds).
|
|
95
|
+
|
|
96
|
+
logs prints the last lines (100 by default) of every replica or of a run,
|
|
97
|
+
with -f also new ones as they are written, for up to an hour. Nothing is
|
|
98
|
+
kept: what a pod wrote before it was replaced is gone.`;
|
|
99
|
+
const fail = (message) => {
|
|
100
|
+
console.error(`oup: ${message}`);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
};
|
|
103
|
+
/** Opens `url` in the browser on this machine; not over SSH, where there is none to open. */
|
|
104
|
+
const openBrowser = (url) => {
|
|
105
|
+
if (process.env['SSH_CONNECTION'] !== undefined) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const [command, ...args] = process.platform === 'darwin' ? ['open', url] : process.platform === 'win32' ? ['cmd', '/c', 'start', '""', url] : ['xdg-open', url];
|
|
109
|
+
const child = spawn(command, args, { stdio: 'ignore', detached: true });
|
|
110
|
+
child.on('error', () => undefined);
|
|
111
|
+
child.unref();
|
|
112
|
+
return true;
|
|
113
|
+
};
|
|
114
|
+
async function login(args) {
|
|
115
|
+
const { values } = parseArgs({ args, options: { api: { type: 'string' }, 'no-browser': { type: 'boolean' } } });
|
|
116
|
+
const apiUrl = values.api ?? process.env['OUP_API_URL'] ?? DEFAULT_API_URL;
|
|
117
|
+
const settings = await cliLogin(apiUrl);
|
|
118
|
+
if (!settings.ok) {
|
|
119
|
+
fail(settings.error.status === 404 ? `${apiUrl} has no CLI sign-in set up` : `cannot reach ${apiUrl}: ${settings.error.message}`);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const { issuer, clientId } = settings.value;
|
|
123
|
+
const discovery = await discover(issuer);
|
|
124
|
+
if (!discovery.ok) {
|
|
125
|
+
return fail(`cannot reach madAuth: ${discovery.error}`);
|
|
126
|
+
}
|
|
127
|
+
const device = await startDeviceSignIn(discovery.value, clientId);
|
|
128
|
+
if (!device.ok) {
|
|
129
|
+
return fail(device.error);
|
|
130
|
+
}
|
|
131
|
+
const opened = !values['no-browser'] && openBrowser(device.value.verificationUriComplete);
|
|
132
|
+
console.log(opened
|
|
133
|
+
? `Approve the sign-in in your browser; check that it shows the code ${device.value.userCode}.\nIf no browser opened, go to ${device.value.verificationUriComplete}`
|
|
134
|
+
: `Open ${device.value.verificationUri} and enter the code ${device.value.userCode}\n(or go to ${device.value.verificationUriComplete})`);
|
|
135
|
+
const tokens = await waitForApproval(discovery.value, clientId, device.value);
|
|
136
|
+
if (!tokens.ok) {
|
|
137
|
+
return fail(tokens.error);
|
|
138
|
+
}
|
|
139
|
+
const email = emailOf(tokens.value.idToken);
|
|
140
|
+
await writeCredentials(credentialsPath(), {
|
|
141
|
+
apiUrl,
|
|
142
|
+
issuer,
|
|
143
|
+
clientId,
|
|
144
|
+
refreshToken: tokens.value.refreshToken,
|
|
145
|
+
accessToken: tokens.value.accessToken,
|
|
146
|
+
accessTokenExpiresAt: tokens.value.expiresAt,
|
|
147
|
+
...(tokens.value.idToken === undefined ? {} : { idToken: tokens.value.idToken }),
|
|
148
|
+
...(email === undefined ? {} : { email }),
|
|
149
|
+
});
|
|
150
|
+
console.log(`Signed in${email === undefined ? '' : ` as ${email}`}.`);
|
|
151
|
+
}
|
|
152
|
+
/** Forgets the tokens here and ends the CLI's refresh tokens at madAuth. */
|
|
153
|
+
async function logout() {
|
|
154
|
+
const path = credentialsPath();
|
|
155
|
+
const credentials = await readCredentials(path);
|
|
156
|
+
await removeCredentials(path);
|
|
157
|
+
if (credentials?.idToken !== undefined) {
|
|
158
|
+
const discovery = await discover(credentials.issuer);
|
|
159
|
+
if (discovery.ok && discovery.value.endSessionEndpoint !== undefined) {
|
|
160
|
+
const url = new URL(discovery.value.endSessionEndpoint);
|
|
161
|
+
url.searchParams.set('id_token_hint', credentials.idToken);
|
|
162
|
+
await fetch(url, { redirect: 'manual', signal: AbortSignal.timeout(15_000) }).catch(() => undefined);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
console.log('Signed out.');
|
|
166
|
+
}
|
|
167
|
+
/** The API client for the signed-in user (or OUP_API_KEY). */
|
|
168
|
+
async function apiClient() {
|
|
169
|
+
const access = await apiAccess(credentialsPath());
|
|
170
|
+
if (!access.ok) {
|
|
171
|
+
return fail(access.error);
|
|
172
|
+
}
|
|
173
|
+
return new OpenUpstreamClient({ baseUrl: access.value.apiUrl, token: access.value.token });
|
|
174
|
+
}
|
|
175
|
+
async function listProjects(client) {
|
|
176
|
+
const projects = await client.listProjects();
|
|
177
|
+
return projects.ok ? projects.value : fail(projects.error.message);
|
|
178
|
+
}
|
|
179
|
+
/** The client and the Project this command acts on: --project, OUP_PROJECT, or the linked one. */
|
|
180
|
+
async function inProject(flag) {
|
|
181
|
+
const client = await apiClient();
|
|
182
|
+
const project = await projectFor(await listProjects(client), flag ?? process.env['OUP_PROJECT'], process.cwd());
|
|
183
|
+
return project.ok ? { client, project: project.value } : fail(project.error);
|
|
184
|
+
}
|
|
185
|
+
async function listAccounts(client) {
|
|
186
|
+
const accounts = await client.listAccounts();
|
|
187
|
+
return accounts.ok ? accounts.value : fail(accounts.error.message);
|
|
188
|
+
}
|
|
189
|
+
/** `oup project list` and `oup project create`: the caller's Projects, and a new one in an Account of theirs. */
|
|
190
|
+
async function project(args) {
|
|
191
|
+
const [verb, ...rest] = args;
|
|
192
|
+
const { values, positionals } = parseArgs({
|
|
193
|
+
args: rest,
|
|
194
|
+
allowPositionals: true,
|
|
195
|
+
options: { account: { type: 'string' }, link: { type: 'boolean' } },
|
|
196
|
+
});
|
|
197
|
+
if (verb === 'list' && positionals.length === 0 && values.account === undefined && values.link === undefined) {
|
|
198
|
+
const client = await apiClient();
|
|
199
|
+
const [projects, accounts, found] = await Promise.all([listProjects(client), listAccounts(client), findLink(process.cwd())]);
|
|
200
|
+
const lines = formatProjects(projects, accounts, found.ok ? found.value?.link.projectId : undefined);
|
|
201
|
+
console.log(lines.length === 0 ? 'No Projects yet; oup project create <name> makes one.' : lines.join('\n'));
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const [name, ...extra] = positionals;
|
|
205
|
+
if (verb !== 'create' || name === undefined || extra.length > 0) {
|
|
206
|
+
return fail(USAGE);
|
|
207
|
+
}
|
|
208
|
+
const client = await apiClient();
|
|
209
|
+
const account = accountFor(await listAccounts(client), values.account);
|
|
210
|
+
if (!account.ok) {
|
|
211
|
+
return fail(account.error);
|
|
212
|
+
}
|
|
213
|
+
const created = await client.createProject({ accountId: account.value.id, name });
|
|
214
|
+
if (!created.ok) {
|
|
215
|
+
return fail(created.error.message);
|
|
216
|
+
}
|
|
217
|
+
console.log(`Created the Project ${created.value.name} (${created.value.id}) in the Account ${account.value.name}.`);
|
|
218
|
+
if (values.link === true) {
|
|
219
|
+
const path = await writeLink(process.cwd(), created.value);
|
|
220
|
+
console.log(`Linked to it in ${path}.`);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
console.log(`oup link ${created.value.name} makes the commands in a directory act on it.`);
|
|
224
|
+
}
|
|
225
|
+
async function link(args) {
|
|
226
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, options: {} });
|
|
227
|
+
const [reference] = positionals;
|
|
228
|
+
if (reference === undefined) {
|
|
229
|
+
return fail(USAGE);
|
|
230
|
+
}
|
|
231
|
+
const project = resolveProject(await listProjects(await apiClient()), reference);
|
|
232
|
+
if (!project.ok) {
|
|
233
|
+
return fail(project.error);
|
|
234
|
+
}
|
|
235
|
+
const path = await writeLink(process.cwd(), project.value);
|
|
236
|
+
console.log(`Linked to the Project ${project.value.name} (${project.value.id}) in ${path}.`);
|
|
237
|
+
}
|
|
238
|
+
/** A positive whole number from a flag; undefined when it was not given. */
|
|
239
|
+
const count = (flag, value) => {
|
|
240
|
+
if (value === undefined) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
const n = Number(value);
|
|
244
|
+
return Number.isInteger(n) && n > 0 ? n : fail(`--${flag} must be a positive whole number, not ${value}`);
|
|
245
|
+
};
|
|
246
|
+
const isSite = (value) => COMPUTE_SITES.includes(value);
|
|
247
|
+
/** A Database's flags of one verb that another verb does not take. */
|
|
248
|
+
const PG_FLAGS = {
|
|
249
|
+
list: [],
|
|
250
|
+
show: [],
|
|
251
|
+
delete: [],
|
|
252
|
+
create: ['memory', 'storage', 'site', 'no-wait', 'max-storage', 'no-auto-grow'],
|
|
253
|
+
restore: ['memory', 'storage', 'site', 'no-wait', 'time', 'max-storage', 'no-auto-grow'],
|
|
254
|
+
resize: ['memory', 'storage', 'no-wait', 'max-storage', 'no-max-storage', 'auto-grow', 'no-auto-grow'],
|
|
255
|
+
};
|
|
256
|
+
async function pg(args) {
|
|
257
|
+
const [verb, ...rest] = args;
|
|
258
|
+
const { values, positionals } = parseArgs({
|
|
259
|
+
args: rest,
|
|
260
|
+
allowPositionals: true,
|
|
261
|
+
options: {
|
|
262
|
+
project: { type: 'string' },
|
|
263
|
+
memory: { type: 'string' },
|
|
264
|
+
storage: { type: 'string' },
|
|
265
|
+
site: { type: 'string' },
|
|
266
|
+
time: { type: 'string' },
|
|
267
|
+
'no-wait': { type: 'boolean' },
|
|
268
|
+
'max-storage': { type: 'string' },
|
|
269
|
+
'no-max-storage': { type: 'boolean' },
|
|
270
|
+
'auto-grow': { type: 'boolean' },
|
|
271
|
+
'no-auto-grow': { type: 'boolean' },
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
if (verb === undefined || !Object.hasOwn(PG_FLAGS, verb)) {
|
|
275
|
+
return fail(USAGE);
|
|
276
|
+
}
|
|
277
|
+
const allowed = PG_FLAGS[verb];
|
|
278
|
+
const stray = Object.keys(values).find((flag) => flag !== 'project' && !allowed.includes(flag));
|
|
279
|
+
if (stray !== undefined) {
|
|
280
|
+
return fail(`oup pg ${verb} does not take --${stray}`);
|
|
281
|
+
}
|
|
282
|
+
const [name, second] = positionals;
|
|
283
|
+
if ((verb !== 'list' && name === undefined) || (verb === 'restore' && second === undefined)) {
|
|
284
|
+
return fail(USAGE);
|
|
285
|
+
}
|
|
286
|
+
if (values.site !== undefined && !isSite(values.site)) {
|
|
287
|
+
return fail(`--site is one of ${COMPUTE_SITES.join(', ')}`);
|
|
288
|
+
}
|
|
289
|
+
const memoryMb = count('memory', values.memory);
|
|
290
|
+
const storageGb = count('storage', values.storage);
|
|
291
|
+
const homeSite = values.site !== undefined && isSite(values.site) ? values.site : undefined;
|
|
292
|
+
const growth = growthSettings(values);
|
|
293
|
+
if (!growth.ok) {
|
|
294
|
+
return fail(growth.reason);
|
|
295
|
+
}
|
|
296
|
+
const { client, project } = await inProject(values.project);
|
|
297
|
+
const databases = await client.listDatabases(project.id);
|
|
298
|
+
if (!databases.ok) {
|
|
299
|
+
return fail(databases.error.message);
|
|
300
|
+
}
|
|
301
|
+
const existing = databases.value.find((candidate) => candidate.name === name);
|
|
302
|
+
const found = () => existing ?? fail(`no Database ${name} in ${project.name}`);
|
|
303
|
+
switch (verb) {
|
|
304
|
+
case 'list':
|
|
305
|
+
console.log(databases.value.length === 0 ? `No Databases in ${project.name}.` : formatDatabases(databases.value).join('\n'));
|
|
306
|
+
return;
|
|
307
|
+
case 'show':
|
|
308
|
+
return console.log(JSON.stringify(found(), null, 2));
|
|
309
|
+
case 'delete': {
|
|
310
|
+
const database = found();
|
|
311
|
+
const deleted = await client.deleteDatabase(project.id, database.id);
|
|
312
|
+
return deleted.ok
|
|
313
|
+
? console.log(`Deleted ${name}. Its backups are kept for the retention window; until then\n oup pg restore ${database.id} <name> --storage ${database.storageGb}\nbrings it back as a new Database.`)
|
|
314
|
+
: fail(deleted.error.message);
|
|
315
|
+
}
|
|
316
|
+
case 'resize': {
|
|
317
|
+
const database = found();
|
|
318
|
+
if (memoryMb === undefined && storageGb === undefined && Object.keys(growth.settings).length === 0) {
|
|
319
|
+
return fail('oup pg resize needs --memory, --storage, --max-storage, --no-max-storage, --auto-grow or --no-auto-grow');
|
|
320
|
+
}
|
|
321
|
+
const resized = await client.updateDatabase(project.id, database.id, {
|
|
322
|
+
...(memoryMb === undefined ? {} : { memoryMb }),
|
|
323
|
+
...(storageGb === undefined ? {} : { storageGb }),
|
|
324
|
+
...growth.settings,
|
|
325
|
+
});
|
|
326
|
+
if (!resized.ok) {
|
|
327
|
+
return fail(resized.error.message);
|
|
328
|
+
}
|
|
329
|
+
console.log(`${name}: ${database.memoryMb} → ${resized.value.memoryMb} MB, ${database.storageGb} → ${resized.value.storageGb} GB, storage grows ${growthOf(resized.value)}`);
|
|
330
|
+
// Only the growth settings changed: nothing to wait for.
|
|
331
|
+
if (values['no-wait'] === true || (memoryMb === undefined && storageGb === undefined)) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
return waitReady(client, project.id, database.id, name);
|
|
335
|
+
}
|
|
336
|
+
case 'create':
|
|
337
|
+
case 'restore': {
|
|
338
|
+
const planned = verb === 'create'
|
|
339
|
+
? {
|
|
340
|
+
ok: true,
|
|
341
|
+
input: {
|
|
342
|
+
name: name,
|
|
343
|
+
memoryMb: memoryMb ?? DATABASE_DEFAULTS.memoryMb,
|
|
344
|
+
storageGb: storageGb ?? DATABASE_DEFAULTS.storageGb,
|
|
345
|
+
...(homeSite === undefined ? {} : { homeSite }),
|
|
346
|
+
},
|
|
347
|
+
}
|
|
348
|
+
: planRestore(databases.value, {
|
|
349
|
+
source: name,
|
|
350
|
+
name: second,
|
|
351
|
+
...(values.time === undefined ? {} : { time: values.time }),
|
|
352
|
+
...(memoryMb === undefined ? {} : { memoryMb }),
|
|
353
|
+
...(storageGb === undefined ? {} : { storageGb }),
|
|
354
|
+
...(homeSite === undefined ? {} : { homeSite }),
|
|
355
|
+
});
|
|
356
|
+
if (!planned.ok) {
|
|
357
|
+
return fail(planned.reason);
|
|
358
|
+
}
|
|
359
|
+
const created = await client.createDatabase(project.id, { ...planned.input, ...growth.settings });
|
|
360
|
+
if (!created.ok) {
|
|
361
|
+
return fail(created.error.message);
|
|
362
|
+
}
|
|
363
|
+
const { name: createdName, memoryMb: mb, storageGb: gb, homeSite: site, id } = created.value;
|
|
364
|
+
console.log(`${verb === 'create' ? 'Created' : 'Restoring into'} ${createdName}: ${mb} MB, ${gb} GB (grows ${growthOf(created.value)}), primary in ${site}`);
|
|
365
|
+
if (values['no-wait'] === true) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
return waitReady(client, project.id, id, createdName);
|
|
369
|
+
}
|
|
370
|
+
default:
|
|
371
|
+
return fail(USAGE);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
/** Until the Database is ready; every state it passes through is reported once. */
|
|
375
|
+
async function waitReady(client, projectId, databaseId, name) {
|
|
376
|
+
// A restore first fetches the base backup and replays WAL; a new memory size restarts both instances.
|
|
377
|
+
const ready = await waitUntil(() => client.getDatabase(projectId, databaseId), (database) => database.state === 'ready', {
|
|
378
|
+
timeoutMs: 20 * 60_000,
|
|
379
|
+
intervalMs: 5_000,
|
|
380
|
+
report: (state) => console.log(` ${state}`),
|
|
381
|
+
});
|
|
382
|
+
return ready.ok ? undefined : fail(`${name} is not ready: ${ready.error}`);
|
|
383
|
+
}
|
|
384
|
+
async function service(args) {
|
|
385
|
+
const [verb, ...rest] = args;
|
|
386
|
+
const { values, positionals } = parseArgs({
|
|
387
|
+
args: rest,
|
|
388
|
+
allowPositionals: true,
|
|
389
|
+
options: {
|
|
390
|
+
project: { type: 'string' },
|
|
391
|
+
image: { type: 'string' },
|
|
392
|
+
port: { type: 'string' },
|
|
393
|
+
'no-port': { type: 'boolean' },
|
|
394
|
+
replicas: { type: 'string' },
|
|
395
|
+
memory: { type: 'string' },
|
|
396
|
+
cpu: { type: 'string' },
|
|
397
|
+
site: { type: 'string' },
|
|
398
|
+
'registry-user': { type: 'string' },
|
|
399
|
+
'registry-password-stdin': { type: 'boolean' },
|
|
400
|
+
'no-wait': { type: 'boolean' },
|
|
401
|
+
tail: { type: 'string' },
|
|
402
|
+
follow: { type: 'boolean', short: 'f' },
|
|
403
|
+
timestamps: { type: 'boolean' },
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
const [name] = positionals;
|
|
407
|
+
if (verb !== 'list' && name === undefined) {
|
|
408
|
+
return fail(USAGE);
|
|
409
|
+
}
|
|
410
|
+
const logFlag = ['tail', 'follow', 'timestamps'].find((flag) => values[flag] !== undefined);
|
|
411
|
+
if (verb !== 'logs' && logFlag !== undefined) {
|
|
412
|
+
return fail(`oup service ${verb} does not take --${logFlag}`);
|
|
413
|
+
}
|
|
414
|
+
const { client, project } = await inProject(values.project);
|
|
415
|
+
const services = await client.listServices(project.id);
|
|
416
|
+
if (!services.ok) {
|
|
417
|
+
return fail(services.error.message);
|
|
418
|
+
}
|
|
419
|
+
const existing = services.value.find((candidate) => candidate.name === name);
|
|
420
|
+
switch (verb) {
|
|
421
|
+
case 'list':
|
|
422
|
+
console.log(services.value.length === 0 ? `No Services in ${project.name}.` : formatServices(services.value).join('\n'));
|
|
423
|
+
return;
|
|
424
|
+
case 'show':
|
|
425
|
+
return existing === undefined ? fail(`no Service ${name} in ${project.name}`) : console.log(JSON.stringify(existing, null, 2));
|
|
426
|
+
case 'delete': {
|
|
427
|
+
if (existing === undefined) {
|
|
428
|
+
return fail(`no Service ${name} in ${project.name}`);
|
|
429
|
+
}
|
|
430
|
+
const deleted = await client.deleteService(project.id, existing.id);
|
|
431
|
+
return deleted.ok ? console.log(`Deleted ${name}; its pods stop within a minute.`) : fail(deleted.error.message);
|
|
432
|
+
}
|
|
433
|
+
case 'deploy':
|
|
434
|
+
return serviceDeploy(client, project, existing, { ...values, name: name });
|
|
435
|
+
case 'logs': {
|
|
436
|
+
if (existing === undefined) {
|
|
437
|
+
return fail(`no Service ${name} in ${project.name}`);
|
|
438
|
+
}
|
|
439
|
+
const tail = parseTail(values.tail);
|
|
440
|
+
if (!tail.ok) {
|
|
441
|
+
return fail(tail.error);
|
|
442
|
+
}
|
|
443
|
+
return printLogs(await client.serviceLogs(project.id, existing.id, logQuery(tail.value, values.follow)), {
|
|
444
|
+
prefix: true,
|
|
445
|
+
timestamps: values.timestamps === true,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
default:
|
|
449
|
+
return fail(USAGE);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
async function serviceDeploy(client, project, existing, flags) {
|
|
453
|
+
if (flags.image === undefined) {
|
|
454
|
+
return fail('oup service deploy needs --image');
|
|
455
|
+
}
|
|
456
|
+
if (flags.site !== undefined && !isSite(flags.site)) {
|
|
457
|
+
return fail(`--site is one of ${COMPUTE_SITES.join(', ')}`);
|
|
458
|
+
}
|
|
459
|
+
if (flags.port !== undefined && flags['no-port'] === true) {
|
|
460
|
+
return fail('--port and --no-port exclude each other');
|
|
461
|
+
}
|
|
462
|
+
const registryAuth = await registryAuthFrom(flags);
|
|
463
|
+
const port = flags['no-port'] === true ? null : count('port', flags.port);
|
|
464
|
+
const replicas = count('replicas', flags.replicas);
|
|
465
|
+
const memoryMb = count('memory', flags.memory);
|
|
466
|
+
const cpuMillicores = count('cpu', flags.cpu);
|
|
467
|
+
const desired = {
|
|
468
|
+
name: flags.name,
|
|
469
|
+
image: flags.image,
|
|
470
|
+
...(port === undefined ? {} : { port }),
|
|
471
|
+
...(replicas === undefined ? {} : { replicas }),
|
|
472
|
+
...(memoryMb === undefined ? {} : { memoryMb }),
|
|
473
|
+
...(cpuMillicores === undefined ? {} : { cpuMillicores }),
|
|
474
|
+
...(flags.site === undefined || !isSite(flags.site) ? {} : { homeSite: flags.site }),
|
|
475
|
+
...(registryAuth === undefined ? {} : { registryAuth }),
|
|
476
|
+
};
|
|
477
|
+
const plan = planServiceDeploy(existing, desired);
|
|
478
|
+
const deployed = plan.kind === 'invalid'
|
|
479
|
+
? fail(plan.reason)
|
|
480
|
+
: plan.kind === 'create'
|
|
481
|
+
? await client.createService(project.id, plan.input)
|
|
482
|
+
: plan.kind === 'update'
|
|
483
|
+
? await client.updateService(project.id, plan.serviceId, plan.patch)
|
|
484
|
+
: await client.deployService(project.id, plan.serviceId);
|
|
485
|
+
if (!deployed.ok) {
|
|
486
|
+
return fail(deployed.error.message);
|
|
487
|
+
}
|
|
488
|
+
const verb = plan.kind === 'create' ? 'Created' : plan.kind === 'update' ? 'Changed' : 'Deployed';
|
|
489
|
+
console.log(`${verb} ${flags.name}: ${deployed.value.image} at ${deployed.value.imageDigest}`);
|
|
490
|
+
if (flags['no-wait'] === true) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
const running = await waitUntilRunning(() => client.getService(project.id, deployed.value.id), {
|
|
494
|
+
timeoutMs: 5 * 60_000,
|
|
495
|
+
intervalMs: 3_000,
|
|
496
|
+
report: (state) => console.log(` ${state}`),
|
|
497
|
+
});
|
|
498
|
+
return running.ok ? undefined : fail(`${flags.name} is not running: ${running.error}`);
|
|
499
|
+
}
|
|
500
|
+
const logQuery = (tail, follow) => ({
|
|
501
|
+
...(tail === undefined ? {} : { tail }),
|
|
502
|
+
...(follow === true ? { follow: true } : {}),
|
|
503
|
+
});
|
|
504
|
+
/** Prints a log stream as it arrives. */
|
|
505
|
+
async function printLogs(opened, options) {
|
|
506
|
+
if (!opened.ok) {
|
|
507
|
+
return fail(opened.error.message);
|
|
508
|
+
}
|
|
509
|
+
for await (const entry of opened.value) {
|
|
510
|
+
console.log(formatLogEntry(entry, options));
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/** Registry credentials from `--registry-user` and the password on stdin; undefined without them. */
|
|
514
|
+
async function registryAuthFrom(flags) {
|
|
515
|
+
const username = flags['registry-user'];
|
|
516
|
+
if ((username === undefined) !== (flags['registry-password-stdin'] !== true)) {
|
|
517
|
+
return fail('--registry-user and --registry-password-stdin go together');
|
|
518
|
+
}
|
|
519
|
+
if (username === undefined) {
|
|
520
|
+
return undefined;
|
|
521
|
+
}
|
|
522
|
+
const password = await readSecretValue('Registry password: ');
|
|
523
|
+
return password === undefined || password === '' ? fail('no registry password on stdin') : { username, password };
|
|
524
|
+
}
|
|
525
|
+
async function job(args) {
|
|
526
|
+
const [verb, ...rest] = args;
|
|
527
|
+
// After `--`: the command of `deploy`, the args of `run`; never flags.
|
|
528
|
+
const { flags, rest: trailing } = splitAtDashes(rest);
|
|
529
|
+
const { values, positionals } = parseArgs({
|
|
530
|
+
args: flags,
|
|
531
|
+
allowPositionals: true,
|
|
532
|
+
options: {
|
|
533
|
+
project: { type: 'string' },
|
|
534
|
+
image: { type: 'string' },
|
|
535
|
+
schedule: { type: 'string' },
|
|
536
|
+
'no-schedule': { type: 'boolean' },
|
|
537
|
+
'time-zone': { type: 'string' },
|
|
538
|
+
timeout: { type: 'string' },
|
|
539
|
+
memory: { type: 'string' },
|
|
540
|
+
cpu: { type: 'string' },
|
|
541
|
+
site: { type: 'string' },
|
|
542
|
+
'no-command': { type: 'boolean' },
|
|
543
|
+
env: { type: 'string', multiple: true },
|
|
544
|
+
'registry-user': { type: 'string' },
|
|
545
|
+
'registry-password-stdin': { type: 'boolean' },
|
|
546
|
+
'no-wait': { type: 'boolean' },
|
|
547
|
+
tail: { type: 'string' },
|
|
548
|
+
follow: { type: 'boolean', short: 'f' },
|
|
549
|
+
timestamps: { type: 'boolean' },
|
|
550
|
+
},
|
|
551
|
+
});
|
|
552
|
+
const [name, runId] = positionals;
|
|
553
|
+
if (verb === undefined || !Object.hasOwn(JOB_FLAGS, verb) || (verb !== 'list' && name === undefined)) {
|
|
554
|
+
return fail(USAGE);
|
|
555
|
+
}
|
|
556
|
+
const stray = strayFlag(verb, values);
|
|
557
|
+
if (stray !== undefined || (trailing !== undefined && verb !== 'deploy' && verb !== 'run')) {
|
|
558
|
+
return fail(`oup job ${verb} does not take ${stray === undefined ? 'arguments after --' : `--${stray}`}`);
|
|
559
|
+
}
|
|
560
|
+
const { client, project } = await inProject(values.project);
|
|
561
|
+
const jobs = await client.listJobs(project.id);
|
|
562
|
+
if (!jobs.ok) {
|
|
563
|
+
return fail(jobs.error.message);
|
|
564
|
+
}
|
|
565
|
+
const existing = jobs.value.find((candidate) => candidate.name === name);
|
|
566
|
+
const found = () => existing ?? fail(`no Job ${name} in ${project.name}`);
|
|
567
|
+
switch (verb) {
|
|
568
|
+
case 'list':
|
|
569
|
+
console.log(jobs.value.length === 0 ? `No Jobs in ${project.name}.` : formatJobs(jobs.value).join('\n'));
|
|
570
|
+
return;
|
|
571
|
+
case 'show':
|
|
572
|
+
return console.log(JSON.stringify(found(), null, 2));
|
|
573
|
+
case 'runs': {
|
|
574
|
+
const runs = await client.listJobRuns(project.id, found().id);
|
|
575
|
+
if (!runs.ok) {
|
|
576
|
+
return fail(runs.error.message);
|
|
577
|
+
}
|
|
578
|
+
console.log(runs.value.length === 0 ? `No runs of ${name} in the last day.` : formatRuns(runs.value).join('\n'));
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
case 'stop': {
|
|
582
|
+
if (runId === undefined) {
|
|
583
|
+
return fail(USAGE);
|
|
584
|
+
}
|
|
585
|
+
const stopped = await client.deleteJobRun(project.id, found().id, runId);
|
|
586
|
+
return stopped.ok ? console.log(`Stopped and removed ${runId}.`) : fail(stopped.error.message);
|
|
587
|
+
}
|
|
588
|
+
case 'logs': {
|
|
589
|
+
const tail = parseTail(values.tail);
|
|
590
|
+
if (!tail.ok) {
|
|
591
|
+
return fail(tail.error);
|
|
592
|
+
}
|
|
593
|
+
const jobId = found().id;
|
|
594
|
+
const run = runId ?? (await newestRun(client, project, jobId, name));
|
|
595
|
+
return printLogs(await client.jobRunLogs(project.id, jobId, run, logQuery(tail.value, values.follow)), {
|
|
596
|
+
prefix: false,
|
|
597
|
+
timestamps: values.timestamps === true,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
case 'delete': {
|
|
601
|
+
const deleted = await client.deleteJob(project.id, found().id);
|
|
602
|
+
return deleted.ok ? console.log(`Deleted ${name}; its runs stop within a minute.`) : fail(deleted.error.message);
|
|
603
|
+
}
|
|
604
|
+
case 'deploy':
|
|
605
|
+
return jobDeploy(client, project, existing, { ...values, name: name, command: trailing });
|
|
606
|
+
case 'run':
|
|
607
|
+
return jobRun(client, project, found(), { env: values.env ?? [], args: trailing, wait: values['no-wait'] !== true });
|
|
608
|
+
default:
|
|
609
|
+
return fail(USAGE);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
/** The id of the Job's newest run; the API lists them newest first. */
|
|
613
|
+
async function newestRun(client, project, jobId, name) {
|
|
614
|
+
const runs = await client.listJobRuns(project.id, jobId);
|
|
615
|
+
if (!runs.ok) {
|
|
616
|
+
return fail(runs.error.message);
|
|
617
|
+
}
|
|
618
|
+
return runs.value[0]?.id ?? fail(`no runs of ${name} in the last day`);
|
|
619
|
+
}
|
|
620
|
+
async function jobDeploy(client, project, existing, flags) {
|
|
621
|
+
if (flags.image === undefined) {
|
|
622
|
+
return fail('oup job deploy needs --image');
|
|
623
|
+
}
|
|
624
|
+
if (flags.command !== undefined && flags['no-command'] === true) {
|
|
625
|
+
return fail('a command after -- and --no-command exclude each other');
|
|
626
|
+
}
|
|
627
|
+
if (flags.schedule !== undefined && flags['no-schedule'] === true) {
|
|
628
|
+
return fail('--schedule and --no-schedule exclude each other');
|
|
629
|
+
}
|
|
630
|
+
if (flags.site !== undefined && !isSite(flags.site)) {
|
|
631
|
+
return fail(`--site is one of ${COMPUTE_SITES.join(', ')}`);
|
|
632
|
+
}
|
|
633
|
+
if (flags.command !== undefined && flags.command.length === 0) {
|
|
634
|
+
return fail('nothing after --; leave it out to keep the command');
|
|
635
|
+
}
|
|
636
|
+
const registryAuth = await registryAuthFrom(flags);
|
|
637
|
+
const timeoutSeconds = count('timeout', flags.timeout);
|
|
638
|
+
const memoryMb = count('memory', flags.memory);
|
|
639
|
+
const cpuMillicores = count('cpu', flags.cpu);
|
|
640
|
+
const schedule = flags['no-schedule'] === true ? null : flags.schedule;
|
|
641
|
+
const desired = {
|
|
642
|
+
name: flags.name,
|
|
643
|
+
image: flags.image,
|
|
644
|
+
...(flags['no-command'] === true ? { command: null } : flags.command === undefined ? {} : { command: flags.command }),
|
|
645
|
+
...(timeoutSeconds === undefined ? {} : { timeoutSeconds }),
|
|
646
|
+
...(memoryMb === undefined ? {} : { memoryMb }),
|
|
647
|
+
...(cpuMillicores === undefined ? {} : { cpuMillicores }),
|
|
648
|
+
...(schedule === undefined ? {} : { schedule }),
|
|
649
|
+
...(flags['time-zone'] === undefined ? {} : { timeZone: flags['time-zone'] }),
|
|
650
|
+
...(flags.site === undefined || !isSite(flags.site) ? {} : { homeSite: flags.site }),
|
|
651
|
+
...(registryAuth === undefined ? {} : { registryAuth }),
|
|
652
|
+
};
|
|
653
|
+
const plan = planJobDeploy(existing, desired);
|
|
654
|
+
const deployed = plan.kind === 'create'
|
|
655
|
+
? await client.createJob(project.id, plan.input)
|
|
656
|
+
: plan.kind === 'update'
|
|
657
|
+
? await client.updateJob(project.id, plan.jobId, plan.patch)
|
|
658
|
+
: await client.deployJob(project.id, plan.jobId);
|
|
659
|
+
if (!deployed.ok) {
|
|
660
|
+
return fail(deployed.error.message);
|
|
661
|
+
}
|
|
662
|
+
const verb = plan.kind === 'create' ? 'Created' : plan.kind === 'update' ? 'Changed' : 'Deployed';
|
|
663
|
+
const when = deployed.value.schedule === null ? 'on demand' : `on ${deployed.value.schedule} (${deployed.value.timeZone ?? 'UTC'})`;
|
|
664
|
+
console.log(`${verb} ${flags.name}, ${when}: ${deployed.value.image} at ${deployed.value.imageDigest}`);
|
|
665
|
+
if (flags['no-wait'] === true) {
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
const ready = await waitUntil(() => client.getJob(project.id, deployed.value.id), (current) => current.state === 'ready', {
|
|
669
|
+
timeoutMs: 2 * 60_000,
|
|
670
|
+
intervalMs: 3_000,
|
|
671
|
+
report: (state) => console.log(` ${state}`),
|
|
672
|
+
});
|
|
673
|
+
return ready.ok ? undefined : fail(`${flags.name} is not ready: ${ready.error}`);
|
|
674
|
+
}
|
|
675
|
+
/** Starts a One-off job and, unless told not to, waits for its outcome: exit 1 when it failed. */
|
|
676
|
+
async function jobRun(client, project, target, options) {
|
|
677
|
+
const env = parseEnv(options.env);
|
|
678
|
+
if (!env.ok) {
|
|
679
|
+
return fail(env.error);
|
|
680
|
+
}
|
|
681
|
+
const started = await client.startJobRun(project.id, target.id, {
|
|
682
|
+
...(options.args === undefined ? {} : { args: [...options.args] }),
|
|
683
|
+
...(Object.keys(env.value).length === 0 ? {} : { env: env.value }),
|
|
684
|
+
});
|
|
685
|
+
if (!started.ok) {
|
|
686
|
+
return fail(started.error.message);
|
|
687
|
+
}
|
|
688
|
+
console.log(`Started ${started.value.id}`);
|
|
689
|
+
if (!options.wait) {
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
// Pulling the image and scheduling the pod come before the Job's own timeout starts.
|
|
693
|
+
const finished = await waitUntil(() => client.getJobRun(project.id, target.id, started.value.id), isFinished, {
|
|
694
|
+
timeoutMs: (target.timeoutSeconds + 5 * 60) * 1000,
|
|
695
|
+
intervalMs: 2_000,
|
|
696
|
+
report: (state) => console.log(` ${state}`),
|
|
697
|
+
});
|
|
698
|
+
if (!finished.ok) {
|
|
699
|
+
return fail(`${started.value.id}: ${finished.error}`);
|
|
700
|
+
}
|
|
701
|
+
const outcome = outcomeOf(finished.value);
|
|
702
|
+
return finished.value.state === 'succeeded' ? console.log(outcome) : fail(`${started.value.id} ${outcome}`);
|
|
703
|
+
}
|
|
704
|
+
async function secret(args) {
|
|
705
|
+
const [verb, ...rest] = args;
|
|
706
|
+
const { values, positionals } = parseArgs({ args: rest, allowPositionals: true, options: { project: { type: 'string' } } });
|
|
707
|
+
const [name] = positionals;
|
|
708
|
+
if (verb !== 'list' && name === undefined) {
|
|
709
|
+
return fail(USAGE);
|
|
710
|
+
}
|
|
711
|
+
const { client, project } = await inProject(values.project);
|
|
712
|
+
switch (verb) {
|
|
713
|
+
case 'list': {
|
|
714
|
+
const secrets = await client.listSecrets(project.id);
|
|
715
|
+
if (!secrets.ok) {
|
|
716
|
+
return fail(secrets.error.message);
|
|
717
|
+
}
|
|
718
|
+
console.log(secrets.value.length === 0 ? `No Secrets in ${project.name}.` : secrets.value.map((s) => `${s.name.padEnd(32)} ${s.updatedAt}`).join('\n'));
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
case 'set': {
|
|
722
|
+
const value = await readSecretValue(`Value of ${name}: `);
|
|
723
|
+
if (value === undefined) {
|
|
724
|
+
return fail('cancelled');
|
|
725
|
+
}
|
|
726
|
+
// Most likely a pipe that was forgotten (CI, </dev/null); an empty value would replace the real one.
|
|
727
|
+
if (value === '') {
|
|
728
|
+
return fail(`no value for ${name} on stdin; pipe it in, e.g. printf %s "$VALUE" | oup secret set ${name}`);
|
|
729
|
+
}
|
|
730
|
+
const stored = await client.putSecret(project.id, { name: name, value });
|
|
731
|
+
return stored.ok ? console.log(`Set ${name} in ${project.name}; its Services restart with it.`) : fail(stored.error.message);
|
|
732
|
+
}
|
|
733
|
+
case 'delete': {
|
|
734
|
+
const deleted = await client.deleteSecret(project.id, name);
|
|
735
|
+
return deleted.ok ? console.log(`Deleted ${name} from ${project.name}.`) : fail(deleted.error.message);
|
|
736
|
+
}
|
|
737
|
+
default:
|
|
738
|
+
return fail(USAGE);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
async function siteDeploy(args) {
|
|
742
|
+
const { values, positionals } = parseArgs({
|
|
743
|
+
args,
|
|
744
|
+
allowPositionals: true,
|
|
745
|
+
options: { bucket: { type: 'string' }, keep: { type: 'boolean' }, force: { type: 'boolean' } },
|
|
746
|
+
});
|
|
747
|
+
const [dir] = positionals;
|
|
748
|
+
if (dir === undefined || values.bucket === undefined) {
|
|
749
|
+
return fail(USAGE);
|
|
750
|
+
}
|
|
751
|
+
const access = await apiAccess(credentialsPath());
|
|
752
|
+
if (!access.ok) {
|
|
753
|
+
return fail(access.error);
|
|
754
|
+
}
|
|
755
|
+
const client = new OpenUpstreamClient({ baseUrl: access.value.apiUrl, token: access.value.token });
|
|
756
|
+
const projects = await client.listProjects();
|
|
757
|
+
if (!projects.ok) {
|
|
758
|
+
return fail(projects.error.message);
|
|
759
|
+
}
|
|
760
|
+
// Bucket names are unique across all Projects.
|
|
761
|
+
const listed = await Promise.all(projects.value.map((project) => client.listBuckets(project.id)));
|
|
762
|
+
const found = listed.flatMap((buckets) => (buckets.ok ? buckets.value : [])).find((bucket) => bucket.name === values.bucket);
|
|
763
|
+
if (found === undefined) {
|
|
764
|
+
const failed = listed.find((buckets) => !buckets.ok);
|
|
765
|
+
return fail(failed !== undefined && !failed.ok ? failed.error.message : `no Bucket ${values.bucket} in your Projects`);
|
|
766
|
+
}
|
|
767
|
+
const details = await client.getBucket(found.projectId, found.id);
|
|
768
|
+
if (!details.ok) {
|
|
769
|
+
return fail(details.error.message);
|
|
770
|
+
}
|
|
771
|
+
const { endpoint, region, accessKeyId, secretAccessKey } = details.value.access;
|
|
772
|
+
if (secretAccessKey === undefined) {
|
|
773
|
+
return fail('this API key may not read the Bucket\'s key; it needs openupstream:write');
|
|
774
|
+
}
|
|
775
|
+
// The Bucket's endpoint is its own hostname, <bucket>.s3.<domain>; the client puts the name in front itself.
|
|
776
|
+
const host = new URL(endpoint).host.replace(`${found.name}.`, '');
|
|
777
|
+
const s3 = new S3Client({ endpoint: `https://${host}`, region, credentials: { accessKeyId, secretAccessKey }, forcePathStyle: false });
|
|
778
|
+
const plan = await deploy(s3, found.name, dir, { keep: values.keep ?? false, force: values.force ?? false, log: (line) => console.log(line) });
|
|
779
|
+
console.log(`${plan.uploads.length} uploaded, ${plan.unchanged} unchanged, ${plan.deletions.length} deleted`);
|
|
780
|
+
}
|
|
781
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
782
|
+
const run = async () => {
|
|
783
|
+
switch (command) {
|
|
784
|
+
case 'login':
|
|
785
|
+
return login(rest);
|
|
786
|
+
case 'logout':
|
|
787
|
+
return logout();
|
|
788
|
+
case 'project':
|
|
789
|
+
return project(rest);
|
|
790
|
+
case 'link':
|
|
791
|
+
return link(rest);
|
|
792
|
+
case 'pg':
|
|
793
|
+
return pg(rest);
|
|
794
|
+
case 'service':
|
|
795
|
+
return service(rest);
|
|
796
|
+
case 'job':
|
|
797
|
+
return job(rest);
|
|
798
|
+
case 'secret':
|
|
799
|
+
return secret(rest);
|
|
800
|
+
case 'site':
|
|
801
|
+
return rest[0] === 'deploy' ? siteDeploy(rest.slice(1)) : fail(USAGE);
|
|
802
|
+
case 'version':
|
|
803
|
+
case '--version':
|
|
804
|
+
case '-v':
|
|
805
|
+
console.log(`oup ${CLI_VERSION} (sdk ${SDK_VERSION})`);
|
|
806
|
+
return;
|
|
807
|
+
case undefined:
|
|
808
|
+
case 'help':
|
|
809
|
+
case '--help':
|
|
810
|
+
console.log(USAGE);
|
|
811
|
+
return;
|
|
812
|
+
default:
|
|
813
|
+
return fail(USAGE);
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
await run().catch((error) => fail(error instanceof Error ? error.message : String(error)));
|
|
817
|
+
//# sourceMappingURL=index.js.map
|