dotop-gpu-cli 0.0.0-stage → 0.1.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 +148 -2
- package/bin/dotop-gpu.mjs +597 -0
- package/package.json +33 -4
- package/src/api.mjs +214 -0
- package/src/config.mjs +91 -0
- package/src/prompts.mjs +529 -0
- package/src/render.mjs +185 -0
- package/src/rentargs.mjs +232 -0
- package/src/rentflow.mjs +186 -0
- package/src/sshkeys.mjs +69 -0
- package/src/wizard.mjs +1056 -0
package/src/api.mjs
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Open API 客户端:零依赖 fetch 封装 + 21 个操作的薄包装。
|
|
2
|
+
//
|
|
3
|
+
// 约定:
|
|
4
|
+
// - 成功返回解析后的 JSON;失败抛 ApiError(带 httpStatus / errorCode / message);
|
|
5
|
+
// - 所有路径都拼在 baseUrl 下,baseUrl 已去掉尾部斜杠。
|
|
6
|
+
import { readConfig } from './config.mjs';
|
|
7
|
+
|
|
8
|
+
export class ApiError extends Error {
|
|
9
|
+
constructor(httpStatus, errorCode, message, body) {
|
|
10
|
+
super(message || `HTTP ${httpStatus}`);
|
|
11
|
+
this.httpStatus = httpStatus;
|
|
12
|
+
this.errorCode = errorCode || '';
|
|
13
|
+
this.body = body;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class Client {
|
|
18
|
+
constructor({ apiKey, baseUrl, timeoutMs = 30000 }) {
|
|
19
|
+
this.apiKey = apiKey;
|
|
20
|
+
this.baseUrl = String(baseUrl || '').replace(/\/+$/, '');
|
|
21
|
+
this.timeoutMs = timeoutMs;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async request(method, path, { query, body, timeoutMs } = {}) {
|
|
25
|
+
const url = new URL(this.baseUrl + path);
|
|
26
|
+
if (query) {
|
|
27
|
+
for (const [k, v] of Object.entries(query)) {
|
|
28
|
+
if (v === undefined || v === null || v === '') continue;
|
|
29
|
+
url.searchParams.set(k, String(v));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const headers = { Accept: 'application/json' };
|
|
34
|
+
if (this.apiKey) headers.Authorization = `Bearer ${this.apiKey}`;
|
|
35
|
+
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
|
36
|
+
|
|
37
|
+
const ac = new AbortController();
|
|
38
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs || this.timeoutMs);
|
|
39
|
+
let res;
|
|
40
|
+
try {
|
|
41
|
+
res = await fetch(url, {
|
|
42
|
+
method,
|
|
43
|
+
headers,
|
|
44
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
45
|
+
signal: ac.signal,
|
|
46
|
+
});
|
|
47
|
+
} catch (err) {
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
if (err?.name === 'AbortError') {
|
|
50
|
+
throw new ApiError(0, 'timeout', `请求超时(${timeoutMs || this.timeoutMs}ms):${method} ${path}`);
|
|
51
|
+
}
|
|
52
|
+
throw new ApiError(0, 'network_error', `连不上服务 ${this.baseUrl}:${err?.message || err}`);
|
|
53
|
+
}
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
|
|
56
|
+
const text = await res.text();
|
|
57
|
+
let payload = null;
|
|
58
|
+
if (text) {
|
|
59
|
+
try {
|
|
60
|
+
payload = JSON.parse(text);
|
|
61
|
+
} catch {
|
|
62
|
+
payload = null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!res.ok) {
|
|
67
|
+
// 两种错误体:扁平 {"error","message"} 与中间件嵌套 {"error":{"code","message"}}
|
|
68
|
+
let code = '';
|
|
69
|
+
let msg = '';
|
|
70
|
+
if (payload && typeof payload.error === 'string') {
|
|
71
|
+
code = payload.error;
|
|
72
|
+
msg = payload.message || '';
|
|
73
|
+
} else if (payload && payload.error && typeof payload.error === 'object') {
|
|
74
|
+
code = payload.error.code || '';
|
|
75
|
+
msg = payload.error.message || '';
|
|
76
|
+
}
|
|
77
|
+
if (!msg) msg = text || `HTTP ${res.status}`;
|
|
78
|
+
throw new ApiError(res.status, code, msg, payload);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return payload;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ── 目录 ────────────────────────────────────────────────
|
|
85
|
+
listZones() {
|
|
86
|
+
return this.request('GET', '/api/v1/open/gpu/zones');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
listImages(zoneKey) {
|
|
90
|
+
return this.request('GET', `/api/v1/open/gpu/zones/${encodeURIComponent(zoneKey)}/images`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
listGpuTypes(zoneKey) {
|
|
94
|
+
return this.request('GET', `/api/v1/open/gpu/zones/${encodeURIComponent(zoneKey)}/gpu-types`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ── 公钥 ────────────────────────────────────────────────
|
|
98
|
+
listSSHKeys() {
|
|
99
|
+
return this.request('GET', '/api/v1/open/gpu/ssh-keys');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
createSSHKey({ name, description, publicKey }) {
|
|
103
|
+
return this.request('POST', '/api/v1/open/gpu/ssh-keys', {
|
|
104
|
+
body: { name, description, public_key: publicKey },
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
updateSSHKey(id, { name, description }) {
|
|
109
|
+
const body = {};
|
|
110
|
+
if (name !== undefined) body.name = name;
|
|
111
|
+
if (description !== undefined) body.description = description;
|
|
112
|
+
return this.request('PATCH', `/api/v1/open/gpu/ssh-keys/${id}`, { body });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
deleteSSHKey(id) {
|
|
116
|
+
return this.request('DELETE', `/api/v1/open/gpu/ssh-keys/${id}`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ── 报价与下单 ──────────────────────────────────────────
|
|
120
|
+
quoteRental({ zoneKey, planId, billingMode, commitmentCount }) {
|
|
121
|
+
return this.request('POST', '/api/v1/open/gpu/rentals/quote', {
|
|
122
|
+
body: {
|
|
123
|
+
zone_key: zoneKey,
|
|
124
|
+
plan_id: planId,
|
|
125
|
+
billing_mode: billingMode,
|
|
126
|
+
commitment_count: commitmentCount ?? 0,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
createRental(body) {
|
|
132
|
+
return this.request('POST', '/api/v1/open/gpu/rentals', { body });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
listRentals({ zoneKey, state, page, size } = {}) {
|
|
136
|
+
return this.request('GET', '/api/v1/open/gpu/rentals', {
|
|
137
|
+
query: { zone_key: zoneKey, state, page, size },
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
getRental(id) {
|
|
142
|
+
return this.request('GET', `/api/v1/open/gpu/rentals/${id}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── 生命周期 ────────────────────────────────────────────
|
|
146
|
+
stopRental(id, idempotencyKey) {
|
|
147
|
+
return this.change(id, 'stop', idempotencyKey);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
startRental(id, idempotencyKey) {
|
|
151
|
+
return this.change(id, 'start', idempotencyKey);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
rebootRental(id, idempotencyKey) {
|
|
155
|
+
return this.change(id, 'reboot', idempotencyKey);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
destroyRental(id, idempotencyKey) {
|
|
159
|
+
return this.change(id, 'destroy', idempotencyKey);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
change(id, kind, idempotencyKey) {
|
|
163
|
+
const body = {};
|
|
164
|
+
if (idempotencyKey) body.idempotency_key = idempotencyKey;
|
|
165
|
+
return this.request('POST', `/api/v1/open/gpu/rentals/${id}/${kind}`, { body });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ── 观测 ────────────────────────────────────────────────
|
|
169
|
+
listChanges(id, { page, size } = {}) {
|
|
170
|
+
return this.request('GET', `/api/v1/open/gpu/rentals/${id}/change-transactions`, {
|
|
171
|
+
query: { page, size },
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
listSessions(id, { page, size } = {}) {
|
|
176
|
+
return this.request('GET', `/api/v1/open/gpu/rentals/${id}/sessions`, {
|
|
177
|
+
query: { page, size },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getSnapshotState(id) {
|
|
182
|
+
return this.request('GET', `/api/v1/open/gpu/rentals/${id}/snapshot-state`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
listBilling(id, { page, size } = {}) {
|
|
186
|
+
return this.request('GET', `/api/v1/open/gpu/rentals/${id}/billing`, {
|
|
187
|
+
query: { page, size },
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
getFirewall(id) {
|
|
192
|
+
return this.request('GET', `/api/v1/open/gpu/rentals/${id}/firewall`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
updateFirewall(id, firewall) {
|
|
196
|
+
return this.request('PUT', `/api/v1/open/gpu/rentals/${id}/firewall`, {
|
|
197
|
+
body: {
|
|
198
|
+
allowed_ports: firewall.allowed_ports || [],
|
|
199
|
+
port_labels: firewall.port_labels || {},
|
|
200
|
+
whitelist: firewall.whitelist || [],
|
|
201
|
+
blacklist: firewall.blacklist || [],
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 从配置 + 环境变量构造客户端(命令子程序共用)
|
|
208
|
+
export function clientFrom({ apiKey, baseUrl }) {
|
|
209
|
+
return new Client({ apiKey, baseUrl });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function loadStoredKey() {
|
|
213
|
+
return readConfig().apiKey || '';
|
|
214
|
+
}
|
package/src/config.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// 本地配置读写:~/.config/dotop-gpu/config.json(权限 600)
|
|
2
|
+
//
|
|
3
|
+
// API Key 的优先级(高 → 低):
|
|
4
|
+
// 1. 命令行 --api-key
|
|
5
|
+
// 2. 环境变量 DOTOP_GPU_API_KEY
|
|
6
|
+
// 3. 配置文件 apiKey
|
|
7
|
+
//
|
|
8
|
+
// 服务地址同理:--base-url > DOTOP_GPU_BASE_URL > 配置文件 baseUrl > 默认 http://127.0.0.1:8080
|
|
9
|
+
import { readFileSync, writeFileSync, mkdirSync, chmodSync, existsSync } from 'node:fs';
|
|
10
|
+
import { homedir } from 'node:os';
|
|
11
|
+
import { dirname, join } from 'node:path';
|
|
12
|
+
|
|
13
|
+
export const DEFAULT_BASE_URL = 'http://127.0.0.1:8080';
|
|
14
|
+
|
|
15
|
+
export function configPath() {
|
|
16
|
+
if (process.env.DOTOP_GPU_CONFIG) return process.env.DOTOP_GPU_CONFIG;
|
|
17
|
+
const base = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
|
|
18
|
+
return join(base, 'dotop-gpu', 'config.json');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function readConfig() {
|
|
22
|
+
const p = configPath();
|
|
23
|
+
if (!existsSync(p)) return {};
|
|
24
|
+
try {
|
|
25
|
+
const raw = readFileSync(p, 'utf8');
|
|
26
|
+
const parsed = JSON.parse(raw);
|
|
27
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
28
|
+
} catch {
|
|
29
|
+
// 文件坏了就当作空配置,不要因此让整个 CLI 起不来
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function writeConfig(patch) {
|
|
35
|
+
const p = configPath();
|
|
36
|
+
const next = { ...readConfig(), ...patch };
|
|
37
|
+
mkdirSync(dirname(p), { recursive: true });
|
|
38
|
+
writeFileSync(p, JSON.stringify(next, null, 2) + '\n', 'utf8');
|
|
39
|
+
try {
|
|
40
|
+
chmodSync(p, 0o600);
|
|
41
|
+
} catch {
|
|
42
|
+
// Windows 上 chmod 基本是空操作,忽略
|
|
43
|
+
}
|
|
44
|
+
return p;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 解析全局参数,剩下的交给子命令
|
|
48
|
+
export function parseGlobalArgs(argv) {
|
|
49
|
+
const opts = { apiKey: '', baseUrl: '', rest: [] };
|
|
50
|
+
const rest = [];
|
|
51
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
52
|
+
const a = argv[i];
|
|
53
|
+
if (a === '--api-key' || a === '--key') {
|
|
54
|
+
opts.apiKey = argv[i + 1] || '';
|
|
55
|
+
i += 1;
|
|
56
|
+
} else if (a.startsWith('--api-key=')) {
|
|
57
|
+
opts.apiKey = a.slice('--api-key='.length);
|
|
58
|
+
} else if (a === '--base-url' || a === '--url') {
|
|
59
|
+
opts.baseUrl = argv[i + 1] || '';
|
|
60
|
+
i += 1;
|
|
61
|
+
} else if (a.startsWith('--base-url=')) {
|
|
62
|
+
opts.baseUrl = a.slice('--base-url='.length);
|
|
63
|
+
} else {
|
|
64
|
+
rest.push(a);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
opts.rest = rest;
|
|
68
|
+
return opts;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function resolveSettings(globalOpts) {
|
|
72
|
+
const cfg = readConfig();
|
|
73
|
+
const apiKey = globalOpts.apiKey || process.env.DOTOP_GPU_API_KEY || cfg.apiKey || '';
|
|
74
|
+
const baseUrl = trimSlash(
|
|
75
|
+
globalOpts.baseUrl ||
|
|
76
|
+
process.env.DOTOP_GPU_BASE_URL ||
|
|
77
|
+
cfg.baseUrl ||
|
|
78
|
+
DEFAULT_BASE_URL,
|
|
79
|
+
);
|
|
80
|
+
return { apiKey, baseUrl, cfg };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function trimSlash(s) {
|
|
84
|
+
return String(s || '').replace(/\/+$/, '');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function maskKey(key) {
|
|
88
|
+
if (!key) return '(未设置)';
|
|
89
|
+
if (key.length <= 14) return '***';
|
|
90
|
+
return `${key.slice(0, 7)}…${key.slice(-4)}`;
|
|
91
|
+
}
|