@solti/cli 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +57 -2
- package/dist/api/client.js +166 -0
- package/dist/auth/oauth.js +342 -0
- package/dist/commands/account.js +87 -0
- package/dist/commands/connection.js +59 -0
- package/dist/commands/cronjob.js +81 -0
- package/dist/commands/job.js +155 -0
- package/dist/commands/key.js +32 -0
- package/dist/commands/organization.js +59 -0
- package/dist/commands/profile.js +83 -0
- package/dist/commands/project.js +101 -0
- package/dist/commands/resource.js +26 -0
- package/dist/config/paths.js +25 -0
- package/dist/config/store.js +97 -0
- package/dist/index.js +329 -0
- package/dist/lib/parse.js +136 -0
- package/package.json +29 -4
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { request } from "../api/client.js";
|
|
2
|
+
export async function connectionList() {
|
|
3
|
+
// Trailing slash: collection routes live at /1/connections/ (a bare
|
|
4
|
+
// /1/connections 307-redirects to an http:// Location behind the LB).
|
|
5
|
+
const data = (await request("connections/"));
|
|
6
|
+
const items = data.connections ?? [];
|
|
7
|
+
if (items.length === 0) {
|
|
8
|
+
console.log("No connections. Create one with `solti connection create`.");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
for (const c of items)
|
|
12
|
+
console.log(`${c.name} (${c.type})`);
|
|
13
|
+
}
|
|
14
|
+
export async function connectionGet(name) {
|
|
15
|
+
const c = (await request(`connections/${name}`));
|
|
16
|
+
for (const [key, value] of Object.entries(c)) {
|
|
17
|
+
if (key === "encrypted_credentials")
|
|
18
|
+
continue;
|
|
19
|
+
console.log(`${key}: ${typeof value === "object" ? JSON.stringify(value) : value}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function connectionCreate(options) {
|
|
23
|
+
const body = { name: options.name, type: options.type };
|
|
24
|
+
const fs = await import("node:fs");
|
|
25
|
+
switch (options.type) {
|
|
26
|
+
case "aws":
|
|
27
|
+
body.access_key_id = options.accessKey;
|
|
28
|
+
body.secret_access_key = options.secretKey;
|
|
29
|
+
break;
|
|
30
|
+
case "gcp":
|
|
31
|
+
body.gcp_project_id = options.projectId;
|
|
32
|
+
body.keyfile_json = options.keyfile !== undefined ? fs.readFileSync(options.keyfile, "utf8") : undefined;
|
|
33
|
+
break;
|
|
34
|
+
case "azure":
|
|
35
|
+
body.client_id = options.accessKey;
|
|
36
|
+
body.client_secret = options.secretKey;
|
|
37
|
+
body.subscription_id = options.projectId;
|
|
38
|
+
body.tenant_id = options.tenantId;
|
|
39
|
+
break;
|
|
40
|
+
case "r2":
|
|
41
|
+
body.account_id = options.account;
|
|
42
|
+
body.access_key_id = options.accessKey;
|
|
43
|
+
body.secret_access_key = options.secretKey;
|
|
44
|
+
break;
|
|
45
|
+
case "github":
|
|
46
|
+
body.access_token = options.token;
|
|
47
|
+
break;
|
|
48
|
+
default:
|
|
49
|
+
console.error(`Unknown connection type: ${options.type}`);
|
|
50
|
+
process.exitCode = 1;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const created = (await request("connections/", { method: "POST", body }));
|
|
54
|
+
console.log(`Connection created: ${created.name} (${created.type})`);
|
|
55
|
+
}
|
|
56
|
+
export async function connectionDelete(name) {
|
|
57
|
+
await request(`connections/${name}`, { method: "DELETE" });
|
|
58
|
+
console.log(`Connection ${name} deleted.`);
|
|
59
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { request } from "../api/client.js";
|
|
3
|
+
export async function cronjobList() {
|
|
4
|
+
// Trailing slash: collection routes live at /1/cronjobs/ (see key.ts).
|
|
5
|
+
const data = (await request("cronjobs/"));
|
|
6
|
+
const items = data.cronjobs ?? [];
|
|
7
|
+
if (items.length === 0) {
|
|
8
|
+
console.log("No cronjobs. Create one with `solti cronjob create --name <n> --schedule <cron> --image <img>`.");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
for (const c of items) {
|
|
12
|
+
console.log(`${c.name} | ${c.cron_spec} | ${c.is_active ? "active" : "inactive"} | last: ${c.last_triggered_at ?? "Never"}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export async function cronjobShow(idOrName) {
|
|
16
|
+
const c = (await request(`cronjobs/${idOrName}`));
|
|
17
|
+
console.log(`Name: ${c.name}`);
|
|
18
|
+
console.log(`ID: ${c.id}`);
|
|
19
|
+
console.log(`Schedule: ${c.cron_spec} (UTC)`);
|
|
20
|
+
console.log(`Active: ${c.is_active}`);
|
|
21
|
+
console.log(`Image: ${c.job_definition?.image ?? "—"}`);
|
|
22
|
+
console.log(`Last run: ${c.last_triggered_at ?? "Never"}`);
|
|
23
|
+
}
|
|
24
|
+
export async function cronjobCreate(options) {
|
|
25
|
+
// Two forms per tools/cli.md: inline (--image [+ --command/--args]) or a
|
|
26
|
+
// workload YAML/JSON file reused as the job_definition template (--file).
|
|
27
|
+
let jobDefinition;
|
|
28
|
+
if (options.file !== undefined) {
|
|
29
|
+
if (options.image !== undefined || options.command !== undefined || options.args !== undefined) {
|
|
30
|
+
throw new Error("--file cannot be combined with --image/--command/--args");
|
|
31
|
+
}
|
|
32
|
+
const raw = fs.readFileSync(options.file, "utf8");
|
|
33
|
+
let template;
|
|
34
|
+
try {
|
|
35
|
+
template = JSON.parse(raw);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
const { parse } = await import("yaml");
|
|
39
|
+
template = parse(raw);
|
|
40
|
+
}
|
|
41
|
+
// JobDefinition forbids extra fields — notably a fixed `name` and the
|
|
42
|
+
// submit-only `namePrefix` (cronjobs/dtos.py).
|
|
43
|
+
delete template.name;
|
|
44
|
+
delete template.namePrefix;
|
|
45
|
+
jobDefinition = template;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (options.image === undefined) {
|
|
49
|
+
throw new Error("--image is required (or pass --file <job.yaml>)");
|
|
50
|
+
}
|
|
51
|
+
jobDefinition = {
|
|
52
|
+
image: options.image,
|
|
53
|
+
command: options.command?.split(" ").filter(Boolean),
|
|
54
|
+
args: options.args?.split(" ").filter(Boolean),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const payload = {
|
|
58
|
+
name: options.name,
|
|
59
|
+
cron_spec: options.schedule,
|
|
60
|
+
is_active: options.active !== "false",
|
|
61
|
+
job_definition: jobDefinition,
|
|
62
|
+
};
|
|
63
|
+
// Trailing slash: collection routes live at /1/cronjobs/ (see key.ts).
|
|
64
|
+
const c = (await request("cronjobs/", { method: "POST", body: payload }));
|
|
65
|
+
console.log(`Cronjob created: ${c.name} (${c.id})`);
|
|
66
|
+
}
|
|
67
|
+
export async function cronjobUpdate(idOrName, options) {
|
|
68
|
+
const body = {};
|
|
69
|
+
if (options.schedule !== undefined)
|
|
70
|
+
body.cron_spec = options.schedule;
|
|
71
|
+
if (options.active !== undefined)
|
|
72
|
+
body.is_active = options.active !== "false";
|
|
73
|
+
if (options.image !== undefined)
|
|
74
|
+
body.job_definition = { image: options.image };
|
|
75
|
+
const c = (await request(`cronjobs/${idOrName}`, { method: "PATCH", body }));
|
|
76
|
+
console.log(`Cronjob ${c.name} updated.`);
|
|
77
|
+
}
|
|
78
|
+
export async function cronjobDelete(idOrName) {
|
|
79
|
+
await request(`cronjobs/${idOrName}`, { method: "DELETE" });
|
|
80
|
+
console.log(`Cronjob ${idOrName} deleted.`);
|
|
81
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { request } from "../api/client.js";
|
|
3
|
+
import { parseEnvFile, parseResources, parseStringList, parseVolumeShorthand } from "../lib/parse.js";
|
|
4
|
+
export async function jobList(options = {}) {
|
|
5
|
+
// API filters (jobs/dtos.py ListJobsRequest): repeatable `tag=` and
|
|
6
|
+
// `label=` query params; labels are "key:value" ("key=value" accepted).
|
|
7
|
+
// Commander keeps a non-collect option as a scalar when used once, so
|
|
8
|
+
// coerce to arrays here (a bare string would otherwise iterate chars).
|
|
9
|
+
const tags = parseStringList(options.tag);
|
|
10
|
+
const labels = parseStringList(options.label);
|
|
11
|
+
const query = new URLSearchParams();
|
|
12
|
+
for (const tag of tags)
|
|
13
|
+
query.append("tag", tag);
|
|
14
|
+
for (const label of labels) {
|
|
15
|
+
if (!label.includes(":") && !label.includes("=")) {
|
|
16
|
+
throw new Error(`Invalid --label '${label}' — expected key:value (or key=value)`);
|
|
17
|
+
}
|
|
18
|
+
query.append("label", label);
|
|
19
|
+
}
|
|
20
|
+
const qs = query.toString();
|
|
21
|
+
// Trailing slash: collection routes live at /1/jobs/ (a bare /1/jobs 307-redirects
|
|
22
|
+
// to an http:// Location behind the TLS-terminating LB, mangling requests).
|
|
23
|
+
const data = (await request(`jobs/${qs ? `?${qs}` : ""}`));
|
|
24
|
+
if (data.jobs.length === 0) {
|
|
25
|
+
console.log("No jobs. Submit one with `solti job run --image <image> -- <cmd>`.");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
console.log("NAME | STATUS | IMAGE | COST (EST/ACT)");
|
|
29
|
+
for (const job of data.jobs) {
|
|
30
|
+
console.log(`${job.name} | ${job.status} | ${job.image ?? "—"} | ${job.estimated_cost ?? "—"}/${job.actual_cost ?? "—"}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export async function jobStatus(jobIdOrName, options = {}) {
|
|
34
|
+
const terminalStates = new Set(["succeeded", "failed", "cancelled"]);
|
|
35
|
+
const render = (job) => {
|
|
36
|
+
console.log(`Name: ${job.name}`);
|
|
37
|
+
console.log(`ID: ${job.id}`);
|
|
38
|
+
console.log(`Status: ${job.status}`);
|
|
39
|
+
console.log(`VM Status: ${job.vm_status ?? "—"}`);
|
|
40
|
+
console.log(`Cost (EST): ${job.estimated_cost ?? "—"}`);
|
|
41
|
+
console.log(`Cost (ACT): ${job.actual_cost ?? "—"}`);
|
|
42
|
+
console.log(`Retries: ${job.retry_count ?? 0}/${job.max_retries ?? 0}`);
|
|
43
|
+
if (job.error_summary)
|
|
44
|
+
console.log(`Error: ${job.error_summary}`);
|
|
45
|
+
};
|
|
46
|
+
if (!options.watch) {
|
|
47
|
+
render((await request(`jobs/${jobIdOrName}`)));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// §3.9: stream status updates in place until a terminal state — the CLI
|
|
51
|
+
// counterpart of the SDKs' `wait_until_complete`.
|
|
52
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
53
|
+
let lastStatus = null;
|
|
54
|
+
for (;;) {
|
|
55
|
+
const job = (await request(`jobs/${jobIdOrName}`));
|
|
56
|
+
if (job.status !== lastStatus) {
|
|
57
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
58
|
+
render(job);
|
|
59
|
+
lastStatus = job.status;
|
|
60
|
+
}
|
|
61
|
+
if (terminalStates.has(job.status)) {
|
|
62
|
+
console.log(`\nJob reached terminal state: ${job.status}`);
|
|
63
|
+
process.exitCode = job.status === "succeeded" ? 0 : 1;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export async function jobCancel(jobIdOrName) {
|
|
70
|
+
await request(`jobs/${jobIdOrName}/cancel`, { method: "POST" });
|
|
71
|
+
console.log(`Cancellation requested for ${jobIdOrName}.`);
|
|
72
|
+
}
|
|
73
|
+
export async function jobRun(options) {
|
|
74
|
+
let payload;
|
|
75
|
+
if (options.file !== undefined) {
|
|
76
|
+
const raw = fs.readFileSync(options.file, "utf8");
|
|
77
|
+
// Minimal YAML/JSON support: JSON parses directly; YAML requires the yaml
|
|
78
|
+
// package. Try JSON first, then fall back to a lazy import of yaml.
|
|
79
|
+
try {
|
|
80
|
+
payload = JSON.parse(raw);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
const { parse } = await import("yaml");
|
|
84
|
+
payload = parse(raw);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
if (options.image === undefined) {
|
|
89
|
+
console.error("--image is required (or pass -f <workload.yaml>).");
|
|
90
|
+
process.exitCode = 1;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
payload = {
|
|
94
|
+
image: options.image,
|
|
95
|
+
command: options.command,
|
|
96
|
+
args: options.args ?? (options.positional.length > 0 ? options.positional : undefined),
|
|
97
|
+
name: options.name,
|
|
98
|
+
namePrefix: options.namePrefix,
|
|
99
|
+
maxRetries: options.maxRetries !== undefined ? Number(options.maxRetries) : undefined,
|
|
100
|
+
env: {
|
|
101
|
+
// --env-from-file provides the base set; explicit --env flags win.
|
|
102
|
+
...(options.envFromFile !== undefined ? parseEnvFile(readEnvFile(options.envFromFile)) : {}),
|
|
103
|
+
...Object.fromEntries((options.env ?? []).map((e) => e.split("=", 2))),
|
|
104
|
+
},
|
|
105
|
+
volumes: Object.fromEntries((options.volume ?? []).map((v) => {
|
|
106
|
+
// Three formats per tools/cli.md: shorthand `<bucket>[/<prefix>]:<mountPath>`
|
|
107
|
+
// or the structured `bucket=<b>,mountPath=<p>[,connection=...][,prefix=...]`.
|
|
108
|
+
// The volume name (map key) defaults to the bucket name unless
|
|
109
|
+
// `--volume-name <name>` is given.
|
|
110
|
+
let volume;
|
|
111
|
+
if (v.includes("=")) {
|
|
112
|
+
const parts = Object.fromEntries(v.split(",").map((pair) => pair.split("=", 2)));
|
|
113
|
+
if (!parts.bucket || !parts.mountPath) {
|
|
114
|
+
throw new Error(`Invalid --volume spec '${v}' — expected bucket=<b>,mountPath=<p>[,connection=...][,prefix=...]`);
|
|
115
|
+
}
|
|
116
|
+
volume = { bucket: parts.bucket, mountPath: parts.mountPath };
|
|
117
|
+
if (parts.connection)
|
|
118
|
+
volume.connection = parts.connection;
|
|
119
|
+
if (parts.prefix)
|
|
120
|
+
volume.prefix = parts.prefix;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
volume = parseVolumeShorthand(v);
|
|
124
|
+
}
|
|
125
|
+
return [options.volumeName ?? volume.bucket, volume];
|
|
126
|
+
})),
|
|
127
|
+
labels: Object.fromEntries((options.label ?? []).map((l) => l.split("=", 2))),
|
|
128
|
+
tags: options.tag,
|
|
129
|
+
// Arrays only — Change 048: the API accepts the documented scalar form
|
|
130
|
+
// (`cloud: aws`) but a scalar JSON string in jobs.cloud 500s every
|
|
131
|
+
// /1/jobs* read, so the CLI always sends arrays.
|
|
132
|
+
cloud: parseStringList(options.cloud).length > 0 ? parseStringList(options.cloud) : undefined,
|
|
133
|
+
region: parseStringList(options.region).length > 0 ? parseStringList(options.region) : undefined,
|
|
134
|
+
};
|
|
135
|
+
if (options.resources !== undefined) {
|
|
136
|
+
const parsed = parseResources(options.resources);
|
|
137
|
+
payload.resources = {
|
|
138
|
+
...(parsed.cpu !== undefined ? { cpu: parsed.cpu } : {}),
|
|
139
|
+
...(parsed.memory !== undefined ? { memory: parsed.memory } : {}),
|
|
140
|
+
...(parsed.gpu !== undefined ? { gpu: parsed.gpu } : {}),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// Trailing slash: collection routes live at /1/jobs/ (see jobList).
|
|
145
|
+
const job = (await request("jobs/", { method: "POST", body: payload }));
|
|
146
|
+
console.log(`Job submitted: ${job.name} (${job.job_id ?? job.id}) — ${job.status}`);
|
|
147
|
+
}
|
|
148
|
+
function readEnvFile(path) {
|
|
149
|
+
try {
|
|
150
|
+
return fs.readFileSync(path, "utf8");
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
throw new Error(`--env-from-file: cannot read '${path}' — check that the file exists`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { request } from "../api/client.js";
|
|
2
|
+
export async function keyList() {
|
|
3
|
+
// Trailing slash: collection routes live at /1/keys/ (a bare /1/keys
|
|
4
|
+
// 307-redirects to an http:// Location behind the TLS-terminating LB).
|
|
5
|
+
const data = (await request("keys/"));
|
|
6
|
+
const items = data.keys ?? [];
|
|
7
|
+
if (items.length === 0) {
|
|
8
|
+
console.log("No API keys. Create one with `solti key create --name <n> --role <role>`.");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
for (const k of items) {
|
|
12
|
+
// The id is needed for `solti key delete <id>` (Zitadel user id).
|
|
13
|
+
console.log(`${k.name} (${k.role ?? "no role"}) id: ${k.id}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export async function keyCreate(options) {
|
|
17
|
+
const body = { name: options.name, role: options.role };
|
|
18
|
+
if (options.expiresAt !== undefined)
|
|
19
|
+
body.expires_at = options.expiresAt;
|
|
20
|
+
// CreateKeyResponse fields: id, name, value (the one-time token), role.
|
|
21
|
+
const created = (await request("keys/", { method: "POST", body }));
|
|
22
|
+
const token = created.value ?? created.api_key ?? created.token;
|
|
23
|
+
console.log(`API key created: ${created.name}`);
|
|
24
|
+
if (token !== undefined) {
|
|
25
|
+
console.log(`\n${token}\n`);
|
|
26
|
+
console.log("(store it now — it is shown only once)");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export async function keyDelete(idOrName) {
|
|
30
|
+
await request(`keys/${idOrName}`, { method: "DELETE" });
|
|
31
|
+
console.log(`API key ${idOrName} deleted.`);
|
|
32
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { request } from "../api/client.js";
|
|
2
|
+
export async function organizationList() {
|
|
3
|
+
const { organizations } = (await request("organizations", { global: true }));
|
|
4
|
+
if (organizations.length === 0) {
|
|
5
|
+
console.log("No organizations. Create one with `solti organization create <name>`.");
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
for (const org of organizations) {
|
|
9
|
+
console.log(`${org.id} ${org.displayName ?? "(unnamed)"}${org.role ? ` (${org.role})` : ""}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export async function organizationCreate(name) {
|
|
13
|
+
const org = (await request("organizations", {
|
|
14
|
+
method: "POST",
|
|
15
|
+
global: true,
|
|
16
|
+
body: { name, category: "personal" },
|
|
17
|
+
}));
|
|
18
|
+
console.log(`Organization created: ${org.displayName} (${org.id})`);
|
|
19
|
+
}
|
|
20
|
+
export async function organizationShow(orgIdOrName) {
|
|
21
|
+
// Resolve by id directly, otherwise by display name.
|
|
22
|
+
const { organizations } = (await request("organizations", { global: true }));
|
|
23
|
+
const org = organizations.find((o) => o.id === orgIdOrName) ??
|
|
24
|
+
organizations.find((o) => o.displayName === orgIdOrName);
|
|
25
|
+
if (org === undefined) {
|
|
26
|
+
console.error(`Organization ${orgIdOrName} not found.`);
|
|
27
|
+
process.exitCode = 1;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const detail = (await request(`organizations/${org.id}`, { global: true }));
|
|
31
|
+
const members = (await request(`organizations/${org.id}/members`, { global: true }).catch(() => null));
|
|
32
|
+
console.log(`Organization: ${detail.displayName ?? "(unnamed)"}`);
|
|
33
|
+
console.log(`ID: ${detail.id}`);
|
|
34
|
+
console.log(`Category: ${detail.category ?? "—"}`);
|
|
35
|
+
console.log(`Your role: ${detail.role ?? "—"}`);
|
|
36
|
+
if (members?.members?.length) {
|
|
37
|
+
console.log("Members:");
|
|
38
|
+
for (const member of members.members) {
|
|
39
|
+
console.log(` - ${member.email ?? "(unknown)"}${member.role ? ` (${member.role})` : ""}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export async function organizationDelete(orgIdOrName, options = {}) {
|
|
44
|
+
const { organizations } = (await request("organizations", { global: true }));
|
|
45
|
+
const org = organizations.find((o) => o.id === orgIdOrName) ?? organizations.find((o) => o.displayName === orgIdOrName);
|
|
46
|
+
if (org === undefined) {
|
|
47
|
+
console.error(`Organization ${orgIdOrName} not found.`);
|
|
48
|
+
process.exitCode = 1;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const confirmed = options.yes === true;
|
|
52
|
+
if (!confirmed) {
|
|
53
|
+
console.error(`This permanently deletes ${org.displayName ?? org.id} and all its projects. Re-run with --yes to confirm.`);
|
|
54
|
+
process.exitCode = 1;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
await request(`organizations/${org.id}`, { method: "DELETE", global: true });
|
|
58
|
+
console.log(`Organization ${org.displayName ?? org.id} deleted.`);
|
|
59
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { deleteProfile, getActiveAccount, listProfiles, readActive, readProfile, writeActive, writeProfile, } from "../config/store.js";
|
|
2
|
+
async function requireAccount() {
|
|
3
|
+
const account = await getActiveAccount();
|
|
4
|
+
if (account === null) {
|
|
5
|
+
console.error("No logged-in account. Profiles live under accounts — run `solti account login` first, or use SOLTI_API_KEY + SOLTI_PROJECT_ID for headless usage.");
|
|
6
|
+
process.exit(1);
|
|
7
|
+
}
|
|
8
|
+
return account;
|
|
9
|
+
}
|
|
10
|
+
export async function profileList() {
|
|
11
|
+
const account = await requireAccount();
|
|
12
|
+
const profiles = await listProfiles(account);
|
|
13
|
+
const active = (await readActive()).active_profile;
|
|
14
|
+
if (profiles.length === 0) {
|
|
15
|
+
console.log("No profiles. Create one with `solti profile set <name> --project-id <id>`.");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (const profile of profiles) {
|
|
19
|
+
const config = await readProfile(account, profile);
|
|
20
|
+
const marker = profile === active ? "*" : " ";
|
|
21
|
+
console.log(`${marker} ${profile} (project: ${config?.project_id ?? "?"})`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export async function profileSet(profileName, options) {
|
|
25
|
+
const account = await requireAccount();
|
|
26
|
+
if (options.projectId === undefined || options.projectId === "") {
|
|
27
|
+
console.error("--project-id is required.");
|
|
28
|
+
process.exitCode = 1;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await writeProfile(account, profileName, {
|
|
32
|
+
project_id: options.projectId,
|
|
33
|
+
api_key: options.apiKey ?? null,
|
|
34
|
+
api_url: options.apiUrl ?? null,
|
|
35
|
+
});
|
|
36
|
+
const active = await readActive();
|
|
37
|
+
if (active.active_profile === undefined || active.active_profile === null) {
|
|
38
|
+
await writeActive({ ...active, active_profile: profileName });
|
|
39
|
+
}
|
|
40
|
+
console.log(`Profile ${profileName} saved (project: ${options.projectId}).`);
|
|
41
|
+
}
|
|
42
|
+
export async function profileUse(profileName) {
|
|
43
|
+
const account = await requireAccount();
|
|
44
|
+
const config = await readProfile(account, profileName);
|
|
45
|
+
if (config === null) {
|
|
46
|
+
console.error(`Profile ${profileName} not found. Create it with \`solti profile set ${profileName} --project-id <id>\`.`);
|
|
47
|
+
process.exitCode = 1;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const active = await readActive();
|
|
51
|
+
await writeActive({ ...active, active_profile: profileName });
|
|
52
|
+
// Shell integration: the printed export can be eval'd to sync the session.
|
|
53
|
+
console.log(`export SOLTI_PROFILE=${profileName}`);
|
|
54
|
+
}
|
|
55
|
+
export async function profileShow(profileName) {
|
|
56
|
+
const account = await requireAccount();
|
|
57
|
+
const name = profileName ?? (await readActive()).active_profile;
|
|
58
|
+
if (name === null || name === undefined) {
|
|
59
|
+
console.error("No active profile. Pass a profile name or run `solti profile use <name>`.");
|
|
60
|
+
process.exitCode = 1;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const config = await readProfile(account, name);
|
|
64
|
+
if (config === null) {
|
|
65
|
+
console.error(`Profile ${name} not found.`);
|
|
66
|
+
process.exitCode = 1;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
console.log(`Profile: ${name}`);
|
|
70
|
+
console.log(`Account: ${account}`);
|
|
71
|
+
console.log(`Project ID: ${config.project_id}`);
|
|
72
|
+
console.log(`API Key: ${config.api_key ? `${config.api_key.slice(0, 8)}…${config.api_key.slice(-4)}` : "(OAuth — uses the account's stored tokens)"}`);
|
|
73
|
+
console.log(`API URL: ${config.api_url ?? "(default)"}`);
|
|
74
|
+
}
|
|
75
|
+
export async function profileDelete(profileName) {
|
|
76
|
+
const account = await requireAccount();
|
|
77
|
+
await deleteProfile(account, profileName);
|
|
78
|
+
const active = await readActive();
|
|
79
|
+
if (active.active_profile === profileName) {
|
|
80
|
+
await writeActive({ ...active, active_profile: undefined });
|
|
81
|
+
}
|
|
82
|
+
console.log(`Profile ${profileName} deleted.`);
|
|
83
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { request } from "../api/client.js";
|
|
2
|
+
import { getActiveAccount, readActive, writeActive } from "../config/store.js";
|
|
3
|
+
async function listAllProjects() {
|
|
4
|
+
const { organizations } = (await request("organizations", { global: true }));
|
|
5
|
+
const all = [];
|
|
6
|
+
for (const org of organizations) {
|
|
7
|
+
const { projects } = (await request(`organizations/${org.id}/projects`, {
|
|
8
|
+
global: true,
|
|
9
|
+
}));
|
|
10
|
+
all.push(...projects);
|
|
11
|
+
}
|
|
12
|
+
return all;
|
|
13
|
+
}
|
|
14
|
+
export async function projectList(options) {
|
|
15
|
+
if (options.org !== undefined) {
|
|
16
|
+
const { organizations } = (await request("organizations", { global: true }));
|
|
17
|
+
const org = organizations.find((o) => o.id === options.org) ??
|
|
18
|
+
organizations.find((o) => o.displayName === options.org);
|
|
19
|
+
if (org === undefined) {
|
|
20
|
+
console.error(`Organization ${options.org} not found.`);
|
|
21
|
+
process.exitCode = 1;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const { projects } = (await request(`organizations/${org.id}/projects`, {
|
|
25
|
+
global: true,
|
|
26
|
+
}));
|
|
27
|
+
printProjects(projects);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
printProjects(await listAllProjects());
|
|
31
|
+
}
|
|
32
|
+
function printProjects(projects) {
|
|
33
|
+
if (projects.length === 0) {
|
|
34
|
+
console.log("No projects. Create one with `solti project create <name> --org <org>`.");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
for (const project of projects) {
|
|
38
|
+
console.log(`${project.id} ${project.displayName ?? "(unnamed)"}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export async function projectCreate(name, options) {
|
|
42
|
+
const { organizations } = (await request("organizations", { global: true }));
|
|
43
|
+
const org = organizations.find((o) => o.id === options.org) ??
|
|
44
|
+
organizations.find((o) => o.displayName === options.org);
|
|
45
|
+
if (org === undefined) {
|
|
46
|
+
console.error(`Organization ${options.org} not found.`);
|
|
47
|
+
process.exitCode = 1;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const project = (await request(`organizations/${org.id}/projects`, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
global: true,
|
|
53
|
+
body: { name },
|
|
54
|
+
}));
|
|
55
|
+
console.log(`Project created: ${project.displayName} (${project.id})`);
|
|
56
|
+
}
|
|
57
|
+
export async function projectShow(projectId) {
|
|
58
|
+
const project = (await request(`projects/${projectId}`, { global: true }));
|
|
59
|
+
console.log(`Project: ${project.displayName ?? "(unnamed)"}`);
|
|
60
|
+
console.log(`ID: ${project.id}`);
|
|
61
|
+
console.log(`Organization ID: ${project.organizationId}`);
|
|
62
|
+
}
|
|
63
|
+
export async function projectUse(projectId) {
|
|
64
|
+
const account = await getActiveAccount();
|
|
65
|
+
if (account === null) {
|
|
66
|
+
console.error("No logged-in account. Run `solti account login` first.");
|
|
67
|
+
process.exitCode = 1;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// Validate access before switching.
|
|
71
|
+
await request(`projects/${projectId}`, { global: true });
|
|
72
|
+
const active = await readActive();
|
|
73
|
+
const profileName = process.env.SOLTI_PROFILE ?? active.active_profile;
|
|
74
|
+
if (profileName !== null && profileName !== undefined) {
|
|
75
|
+
const { readProfile, writeProfile } = await import("../config/store.js");
|
|
76
|
+
const config = await readProfile(account, profileName);
|
|
77
|
+
await writeProfile(account, profileName, {
|
|
78
|
+
project_id: projectId,
|
|
79
|
+
api_key: config?.api_key ?? null,
|
|
80
|
+
api_url: config?.api_url ?? null,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
await writeActive({ ...active, active_profile: "default" });
|
|
85
|
+
const { writeProfile } = await import("../config/store.js");
|
|
86
|
+
await writeProfile(account, "default", { project_id: projectId, api_key: null, api_url: null });
|
|
87
|
+
}
|
|
88
|
+
console.log(`Active project set to ${projectId}.`);
|
|
89
|
+
// Change 033 D-1: the shared global host scopes requests via the
|
|
90
|
+
// X-Solti-Project-Id header; per-project wildcard hosts are not deployed.
|
|
91
|
+
console.log(`API base URL: https://api.solti.run/1`);
|
|
92
|
+
}
|
|
93
|
+
export async function projectDelete(projectId, options = {}) {
|
|
94
|
+
if (!options.yes) {
|
|
95
|
+
console.error("This permanently deletes the project. Re-run with --yes to confirm.");
|
|
96
|
+
process.exitCode = 1;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await request(`projects/${projectId}`, { method: "DELETE", global: true });
|
|
100
|
+
console.log(`Project ${projectId} deleted.`);
|
|
101
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { request } from "../api/client.js";
|
|
2
|
+
export async function resourceList(options) {
|
|
3
|
+
// API route for compute pools is /1/resources/virtual-machine-pools.
|
|
4
|
+
const routeFor = {
|
|
5
|
+
storages: "storages",
|
|
6
|
+
repositories: "repositories",
|
|
7
|
+
"vm-pools": "virtual-machine-pools",
|
|
8
|
+
};
|
|
9
|
+
const types = options.type !== undefined ? [options.type] : ["storages", "repositories", "vm-pools"];
|
|
10
|
+
for (const type of types) {
|
|
11
|
+
const query = new URLSearchParams();
|
|
12
|
+
if (options.connection !== undefined)
|
|
13
|
+
query.set("connection_id", options.connection);
|
|
14
|
+
const qs = query.toString();
|
|
15
|
+
const path = `resources/${routeFor[type] ?? type}${qs ? `?${qs}` : ""}`;
|
|
16
|
+
const data = (await request(path));
|
|
17
|
+
const key = Object.keys(data).find((k) => Array.isArray(data[k])) ?? "";
|
|
18
|
+
const items = data[key] ?? [];
|
|
19
|
+
console.log(`\n${type} (${items.length}):`);
|
|
20
|
+
for (const item of items) {
|
|
21
|
+
const metadata = (item.metadata ?? {});
|
|
22
|
+
const region = (metadata.region ?? metadata.location ?? "—");
|
|
23
|
+
console.log(` - ${item.name} [${item.platform}${region !== "—" ? ` · ${region}` : ""}]`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/** Root of the local Solti CLI state (overridable for tests). */
|
|
4
|
+
export const SOLTI_HOME = process.env.SOLTI_HOME ?? path.join(os.homedir(), ".solti");
|
|
5
|
+
export const activeStorePath = path.join(SOLTI_HOME, "active.json");
|
|
6
|
+
export const accountsDir = path.join(SOLTI_HOME, "accounts");
|
|
7
|
+
/** Emails are used as directory names; sanitize path-hostile characters. */
|
|
8
|
+
export function sanitizeEmail(email) {
|
|
9
|
+
return email.replace(/[^A-Za-z0-9._@+-]/g, "_");
|
|
10
|
+
}
|
|
11
|
+
export function accountDir(email) {
|
|
12
|
+
return path.join(accountsDir, sanitizeEmail(email));
|
|
13
|
+
}
|
|
14
|
+
export function authFilePath(email) {
|
|
15
|
+
return path.join(accountDir(email), "auth.json");
|
|
16
|
+
}
|
|
17
|
+
export function profilesDir(email) {
|
|
18
|
+
return path.join(accountDir(email), "profiles");
|
|
19
|
+
}
|
|
20
|
+
export function profileDir(email, profileName) {
|
|
21
|
+
return path.join(profilesDir(email), sanitizeEmail(profileName));
|
|
22
|
+
}
|
|
23
|
+
export function profileConfigPath(email, profileName) {
|
|
24
|
+
return path.join(profileDir(email, profileName), "config.json");
|
|
25
|
+
}
|