@openupstream/cli 0.2.2 → 0.4.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 +55 -1
- package/dist/buckets.d.ts +30 -0
- package/dist/buckets.js +54 -0
- package/dist/buckets.js.map +1 -0
- package/dist/index.js +301 -4
- package/dist/index.js.map +1 -1
- package/dist/proxy.d.ts +63 -0
- package/dist/proxy.js +208 -0
- package/dist/proxy.js.map +1 -0
- package/dist/routes.d.ts +39 -0
- package/dist/routes.js +91 -0
- package/dist/routes.js.map +1 -0
- package/dist/usage.d.ts +20 -0
- package/dist/usage.js +68 -0
- package/dist/usage.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -2
package/dist/routes.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `oup domain` and `oup route`: which Route an address names, what a Route
|
|
3
|
+
* goes to, and how Domains and Routes are printed. Pure, so it is tested
|
|
4
|
+
* without an API.
|
|
5
|
+
*/
|
|
6
|
+
import { HOSTNAME, PATH_PREFIX } from '@openupstream/sdk';
|
|
7
|
+
import { err, ok } from './result.js';
|
|
8
|
+
/** A hostname as typed, the way the API keeps it: lowercase, without a scheme, a path or a final dot. */
|
|
9
|
+
export const hostnameOf = (typed) => typed
|
|
10
|
+
.trim()
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.replace(/^https?:\/\//, '')
|
|
13
|
+
.replace(/\/.*$/, '')
|
|
14
|
+
.replace(/\.$/, '');
|
|
15
|
+
/** What is wrong with a custom domain's hostname; undefined when nothing is. */
|
|
16
|
+
export const hostnameProblem = (hostname) => HOSTNAME.test(hostname) ? undefined : `${hostname === '' ? 'that' : hostname} is not a hostname; give one like www.example.com`;
|
|
17
|
+
/** The Domain `hostname` of the Project. */
|
|
18
|
+
export const domainNamed = (domains, hostname, project) => {
|
|
19
|
+
const found = domains.find((domain) => domain.hostname === hostname);
|
|
20
|
+
return found === undefined ? err(`no Domain ${hostname} in ${project}; oup domain list shows them`) : ok(found);
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The Domain and the path prefix an address names: `www.example.com/docs`,
|
|
24
|
+
* `www.example.com` (its `/`), or `/docs` alone for the Project's default
|
|
25
|
+
* Domain. A scheme in front is ignored.
|
|
26
|
+
*/
|
|
27
|
+
export const parseAddress = (address, domains, project) => {
|
|
28
|
+
const typed = address.trim().replace(/^https?:\/\//, '');
|
|
29
|
+
const slash = typed.indexOf('/');
|
|
30
|
+
const hostname = (slash === -1 ? typed : typed.slice(0, slash)).toLowerCase().replace(/\.$/, '');
|
|
31
|
+
const pathPrefix = slash === -1 ? '/' : typed.slice(slash);
|
|
32
|
+
if (!PATH_PREFIX.test(pathPrefix)) {
|
|
33
|
+
return err(`${pathPrefix} is not a path of a Route: / or /segment[/segment…], without a slash at its end`);
|
|
34
|
+
}
|
|
35
|
+
// A word without a dot is no hostname: most likely a path that lacks its slash.
|
|
36
|
+
if (hostname !== '' && !hostname.includes('.')) {
|
|
37
|
+
return err(`${typed} is not an address: give <hostname>/<path>, or a path that starts with a slash, as in /${typed}`);
|
|
38
|
+
}
|
|
39
|
+
if (hostname !== '') {
|
|
40
|
+
const domain = domainNamed(domains, hostname, project);
|
|
41
|
+
return domain.ok ? ok({ domain: domain.value, pathPrefix }) : domain;
|
|
42
|
+
}
|
|
43
|
+
const byDefault = domains.find((domain) => domain.kind === 'default');
|
|
44
|
+
return byDefault === undefined ? err(`${project} has no default Domain; name the hostname, as in www.example.com${pathPrefix}`) : ok({ domain: byDefault, pathPrefix });
|
|
45
|
+
};
|
|
46
|
+
/** The Route for the API: to the Bucket or the Service of that name, one of the two. */
|
|
47
|
+
export const routeInput = (pathPrefix, flags, buckets, services, project) => {
|
|
48
|
+
if ((flags.bucket === undefined) === (flags.service === undefined)) {
|
|
49
|
+
return err('a Route goes to one thing: give --bucket <name> or --service <name>');
|
|
50
|
+
}
|
|
51
|
+
if (flags.bucket !== undefined) {
|
|
52
|
+
const bucket = buckets.find((candidate) => candidate.name === flags.bucket);
|
|
53
|
+
return bucket === undefined ? err(`no Bucket ${flags.bucket} in ${project}`) : ok({ pathPrefix, bucketId: bucket.id, spaFallback: flags.spa ?? false });
|
|
54
|
+
}
|
|
55
|
+
if (flags.spa === true) {
|
|
56
|
+
return err('--spa is for a Bucket: a Service answers every path itself');
|
|
57
|
+
}
|
|
58
|
+
const service = services.find((candidate) => candidate.name === flags.service);
|
|
59
|
+
return service === undefined
|
|
60
|
+
? err(`no Service ${flags.service} in ${project}`)
|
|
61
|
+
: service.port === null
|
|
62
|
+
? err(`the Service ${service.name} has no port, so it serves no HTTP; give it one with oup service deploy ${service.name} --port <port>`)
|
|
63
|
+
: ok({ pathPrefix, serviceId: service.id });
|
|
64
|
+
};
|
|
65
|
+
/** What the API refused, with the flag to change where the API names a field instead. */
|
|
66
|
+
export const routeRefusal = (message, flags) => /spaFallback/.test(message) ? `${message}: ${flags.spa === true ? 'leave --spa out' : 'add --spa'}, as on its other Routes` : message;
|
|
67
|
+
/** Where a custom domain's CNAME points: `ingress.<customer domain>`, the customer domain being what follows the default Domain's first label. */
|
|
68
|
+
export const ingressOf = (domains) => {
|
|
69
|
+
const hostname = domains.find((domain) => domain.kind === 'default')?.hostname;
|
|
70
|
+
return hostname === undefined ? undefined : `ingress.${hostname.slice(hostname.indexOf('.') + 1)}`;
|
|
71
|
+
};
|
|
72
|
+
/** Whether a Domain is served: the default one always, a custom one once verified. */
|
|
73
|
+
const state = (domain) => (domain.kind === 'default' ? 'default' : domain.verification.state === 'verified' ? 'verified' : 'not verified');
|
|
74
|
+
export const formatDomains = (domains) => domains.map((domain) => `${domain.hostname.padEnd(48)} ${state(domain)}`);
|
|
75
|
+
/** The DNS records a custom domain needs, for one that is not verified yet; nothing for the others. */
|
|
76
|
+
export const dnsRecords = (domain, ingress) => domain.kind === 'custom' && domain.verification.state === 'pending'
|
|
77
|
+
? [
|
|
78
|
+
`TXT ${domain.verification.txtRecord.name} ${domain.verification.txtRecord.value}`,
|
|
79
|
+
...(ingress === undefined ? [] : [`CNAME ${domain.hostname} ${ingress}`]),
|
|
80
|
+
]
|
|
81
|
+
: [];
|
|
82
|
+
/** What a Route goes to, in words; a Resource that is gone by its kind alone. */
|
|
83
|
+
const target = (route, buckets, services) => {
|
|
84
|
+
const to = route.target;
|
|
85
|
+
return to.kind === 'bucket'
|
|
86
|
+
? `Bucket ${buckets.find((bucket) => bucket.id === to.bucketId)?.name ?? '(gone)'}${to.spaFallback ? ' (single-page app)' : ''}`
|
|
87
|
+
: `Service ${services.find((service) => service.id === to.serviceId)?.name ?? '(gone)'}`;
|
|
88
|
+
};
|
|
89
|
+
/** Every Route as `hostname/path -> target`, Domain after Domain; a Domain that is not verified says so, since nothing is served at it. */
|
|
90
|
+
export const formatRoutes = (domains, routes, buckets, services) => domains.flatMap((domain) => (routes[domain.id] ?? []).map((route) => `${`${domain.hostname}${route.pathPrefix}`.padEnd(56)} ${target(route, buckets, services)}${state(domain) === 'not verified' ? ' (Domain not verified)' : ''}`));
|
|
91
|
+
//# sourceMappingURL=routes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAA4B,QAAQ,EAAE,WAAW,EAA6C,MAAM,mBAAmB,CAAC;AAC/H,OAAO,EAAe,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEnD,yGAAyG;AACzG,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAU,EAAE,CAChD,KAAK;KACA,IAAI,EAAE;KACN,WAAW,EAAE;KACb,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC;KAC3B,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;KACpB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AAE5B,gFAAgF;AAChF,MAAM,CAAC,M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KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,cAAc,CAAC,CAAC,CAAC,0BAA0B,CAAC,CAAC,CAAC,EAAE,EAAE,CAC9K,CACJ,CAAC"}
|
package/dist/usage.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `oup usage`: what a Project's Resources use now, as a table. Pure, so it
|
|
3
|
+
* is tested without an API. The number formats are the dashboard's
|
|
4
|
+
* (`dashboard/src/lib/usage.ts`), which cannot be imported from here.
|
|
5
|
+
*/
|
|
6
|
+
import type { Measured, ProjectUsage } from '@openupstream/sdk';
|
|
7
|
+
/** Bytes in the largest unit that keeps the number at 1 or more; 1 GB is 2^30 bytes, as the sizes are. */
|
|
8
|
+
export declare const formatBytes: (bytes: number) => string;
|
|
9
|
+
/** Millicores as CPUs: `0.13 CPU`. */
|
|
10
|
+
export declare const formatCpu: (millicores: number) => string;
|
|
11
|
+
/** `used of allocated`, whichever of the two is known; `-` when neither is. */
|
|
12
|
+
export declare const formatMeasured: (measured: Measured, format: (value: number) => string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* The Project's Resources with what each uses now of what it was given,
|
|
15
|
+
* and with `instances` every running copy below its Resource. Says when
|
|
16
|
+
* nothing is measured, and that this is not what is billed.
|
|
17
|
+
*/
|
|
18
|
+
export declare const formatUsage: (usage: ProjectUsage, project: string, options: {
|
|
19
|
+
readonly instances: boolean;
|
|
20
|
+
}) => string[];
|
package/dist/usage.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
2
|
+
/** Bytes in the largest unit that keeps the number at 1 or more; 1 GB is 2^30 bytes, as the sizes are. */
|
|
3
|
+
export const formatBytes = (bytes) => {
|
|
4
|
+
const power = bytes < 1 ? 0 : Math.min(UNITS.length - 1, Math.floor(Math.log2(bytes) / 10));
|
|
5
|
+
const value = bytes / 2 ** (10 * power);
|
|
6
|
+
return `${power === 0 || value >= 100 ? Math.round(value) : value.toFixed(1)} ${UNITS[power]}`;
|
|
7
|
+
};
|
|
8
|
+
/** Millicores as CPUs: `0.13 CPU`. */
|
|
9
|
+
export const formatCpu = (millicores) => `${(millicores / 1000).toFixed(2)} CPU`;
|
|
10
|
+
/** `used of allocated`, whichever of the two is known; `-` when neither is. */
|
|
11
|
+
export const formatMeasured = (measured, format) => measured.used !== null && measured.allocated !== null
|
|
12
|
+
? `${format(measured.used)} of ${format(measured.allocated)}`
|
|
13
|
+
: measured.used !== null
|
|
14
|
+
? format(measured.used)
|
|
15
|
+
: measured.allocated !== null
|
|
16
|
+
? `${format(measured.allocated)} allocated`
|
|
17
|
+
: '-';
|
|
18
|
+
/** Rows as lines, every column as wide as its widest cell. */
|
|
19
|
+
const table = (rows, indent = '') => {
|
|
20
|
+
const widths = (rows[0] ?? []).map((_, column) => Math.max(...rows.map((row) => (row[column] ?? '').length)));
|
|
21
|
+
return rows.map((row) => `${indent}${row.map((cell, column) => cell.padEnd(widths[column] ?? 0)).join(' ')}`.trimEnd());
|
|
22
|
+
};
|
|
23
|
+
const KIND = { database: 'Database', service: 'Service', job: 'Job', bucket: 'Bucket' };
|
|
24
|
+
const resourceRow = (resource) => [
|
|
25
|
+
KIND[resource.kind],
|
|
26
|
+
resource.name,
|
|
27
|
+
formatMeasured(resource.cpuMillicores, formatCpu),
|
|
28
|
+
formatMeasured(resource.memoryBytes, formatBytes),
|
|
29
|
+
formatMeasured(resource.storageBytes, formatBytes),
|
|
30
|
+
];
|
|
31
|
+
const orDash = (value, format) => (value === null ? '-' : format(value));
|
|
32
|
+
const instanceRow = (instance) => [
|
|
33
|
+
`${instance.role} ${instance.name}`,
|
|
34
|
+
instance.site ?? 'not placed',
|
|
35
|
+
`${instance.state}${instance.restarts === 0 ? '' : `, ${instance.restarts} ${instance.restarts === 1 ? 'restart' : 'restarts'}`}`,
|
|
36
|
+
orDash(instance.cpuMillicores, formatCpu),
|
|
37
|
+
orDash(instance.memoryBytes, formatBytes),
|
|
38
|
+
orDash(instance.storageBytes, formatBytes),
|
|
39
|
+
];
|
|
40
|
+
/**
|
|
41
|
+
* The Project's Resources with what each uses now of what it was given,
|
|
42
|
+
* and with `instances` every running copy below its Resource. Says when
|
|
43
|
+
* nothing is measured, and that this is not what is billed.
|
|
44
|
+
*/
|
|
45
|
+
export const formatUsage = (usage, project, options) => {
|
|
46
|
+
if (usage.resources.length === 0) {
|
|
47
|
+
return [`No Resources in ${project}.`];
|
|
48
|
+
}
|
|
49
|
+
const header = ['KIND', 'NAME', 'CPU', 'MEMORY', 'STORAGE'];
|
|
50
|
+
const rows = table([header, ...usage.resources.map(resourceRow)]);
|
|
51
|
+
// One table for the instances of all Resources, so their columns line up down the page.
|
|
52
|
+
const instanceLines = table(usage.resources.flatMap((resource) => resource.instances.map(instanceRow)), ' ');
|
|
53
|
+
const firstOf = usage.resources.map((_, index) => usage.resources.slice(0, index).reduce((sum, resource) => sum + resource.instances.length, 0));
|
|
54
|
+
const lines = options.instances
|
|
55
|
+
? [
|
|
56
|
+
rows[0] ?? '',
|
|
57
|
+
...usage.resources.flatMap((resource, index) => [rows[index + 1] ?? '', ...instanceLines.slice(firstOf[index] ?? 0, (firstOf[index] ?? 0) + resource.instances.length)]),
|
|
58
|
+
]
|
|
59
|
+
: rows;
|
|
60
|
+
return [
|
|
61
|
+
...lines,
|
|
62
|
+
'',
|
|
63
|
+
usage.measured
|
|
64
|
+
? `Measured ${usage.measuredAt}: use now of what was given, not what is billed.`
|
|
65
|
+
: 'CPU, memory and disk are not measured right now: this installation has no Prometheus, or it did not answer.',
|
|
66
|
+
];
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.js","sourceRoot":"","sources":["../src/usage.ts"],"names":[],"mappings":"AAOA,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,I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
|
"name": "@openupstream/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "oup, the openupstream command line tool",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "^24.0.0",
|
|
31
|
+
"@types/pg": "^8.23.1",
|
|
32
|
+
"@types/ws": "^8.18.2",
|
|
31
33
|
"tsx": "^4.19.0",
|
|
32
34
|
"typescript": "^5.7.0"
|
|
33
35
|
},
|
|
@@ -36,6 +38,8 @@
|
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
40
|
"@aws-sdk/client-s3": "^3.1161.0",
|
|
39
|
-
"@openupstream/sdk": "^0.
|
|
41
|
+
"@openupstream/sdk": "^0.5.0",
|
|
42
|
+
"pg": "^8.23.1",
|
|
43
|
+
"ws": "^8.22.0"
|
|
40
44
|
}
|
|
41
45
|
}
|