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
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// dotop-gpu — GPU 分时出租 Open API 的命令行客户端
|
|
3
|
+
//
|
|
4
|
+
// 不带子命令直接运行 = 交互式下单向导。
|
|
5
|
+
//
|
|
6
|
+
// 用法:
|
|
7
|
+
// dotop-gpu 交互式下单
|
|
8
|
+
// dotop-gpu rent 同上(显式)
|
|
9
|
+
// dotop-gpu rent --quick 用默认配置快速下单(自动挑可用区/规格/镜像/公钥/按量)
|
|
10
|
+
// dotop-gpu list 我的租约
|
|
11
|
+
// dotop-gpu show <id> 租约详情
|
|
12
|
+
// dotop-gpu stop|start|reboot|destroy <id> [--idem KEY]
|
|
13
|
+
// dotop-gpu keys 公钥列表
|
|
14
|
+
// dotop-gpu keys add 上传本机公钥(交互)
|
|
15
|
+
// dotop-gpu keys rm <id> 删除公钥
|
|
16
|
+
// dotop-gpu login 保存 / 更新 API Key
|
|
17
|
+
// dotop-gpu whoami 查看当前配置与连通性
|
|
18
|
+
//
|
|
19
|
+
// 全局参数:--api-key <key> --base-url <url>
|
|
20
|
+
import process from 'node:process';
|
|
21
|
+
import { readFileSync } from 'node:fs';
|
|
22
|
+
import { parseGlobalArgs, resolveSettings, maskKey, writeConfig, configPath } from '../src/config.mjs';
|
|
23
|
+
import { Client, ApiError } from '../src/api.mjs';
|
|
24
|
+
import { runWizard } from '../src/wizard.mjs';
|
|
25
|
+
import { parseRentArgs, RentArgError } from '../src/rentargs.mjs';
|
|
26
|
+
import { buildPreset, validateCombo } from '../src/rentflow.mjs';
|
|
27
|
+
import {
|
|
28
|
+
ask,
|
|
29
|
+
askPassword,
|
|
30
|
+
confirm,
|
|
31
|
+
selectOne,
|
|
32
|
+
closeRL,
|
|
33
|
+
} from '../src/prompts.mjs';
|
|
34
|
+
import {
|
|
35
|
+
paint,
|
|
36
|
+
table,
|
|
37
|
+
money,
|
|
38
|
+
duration,
|
|
39
|
+
ts,
|
|
40
|
+
stateText,
|
|
41
|
+
billingText,
|
|
42
|
+
shortId,
|
|
43
|
+
} from '../src/render.mjs';
|
|
44
|
+
import { scanLocalKeys, fingerprintOf } from '../src/sshkeys.mjs';
|
|
45
|
+
|
|
46
|
+
const HELP = `
|
|
47
|
+
${paint.bold('dotop-gpu')} — GPU 分时出租 Open API 客户端
|
|
48
|
+
|
|
49
|
+
${paint.bold('下单')}
|
|
50
|
+
dotop-gpu 交互式向导(推荐)
|
|
51
|
+
dotop-gpu rent 同上
|
|
52
|
+
dotop-gpu rent --quick 用默认配置直接下单,不再逐项询问
|
|
53
|
+
|
|
54
|
+
${paint.bold('查询')}
|
|
55
|
+
dotop-gpu list [--all] 我的租约(默认只列未销毁的)
|
|
56
|
+
dotop-gpu show <id> 租约详情(含 SSH 命令)
|
|
57
|
+
|
|
58
|
+
${paint.bold('生命周期')}
|
|
59
|
+
dotop-gpu stop <id> 停机(停止 GPU 计费)
|
|
60
|
+
dotop-gpu start <id> 开机
|
|
61
|
+
dotop-gpu reboot <id> 重启
|
|
62
|
+
dotop-gpu destroy <id> 销毁
|
|
63
|
+
|
|
64
|
+
${paint.bold('公钥')}
|
|
65
|
+
dotop-gpu keys 列出公钥
|
|
66
|
+
dotop-gpu keys add 从本机 ~/.ssh 选一个上传
|
|
67
|
+
dotop-gpu keys rm <id> 删除公钥
|
|
68
|
+
|
|
69
|
+
${paint.bold('配置')}
|
|
70
|
+
dotop-gpu login 保存 API Key 到本地
|
|
71
|
+
dotop-gpu whoami 显示当前 Key / 服务地址 / 连通性
|
|
72
|
+
|
|
73
|
+
${paint.bold('全局参数')}
|
|
74
|
+
--api-key <key> 临时指定 Key(不落盘)
|
|
75
|
+
--base-url <url> 服务地址(默认 http://127.0.0.1:8080)
|
|
76
|
+
--idem <key> 生命周期操作的幂等键
|
|
77
|
+
-v, --version 显示版本
|
|
78
|
+
|
|
79
|
+
环境变量:DOTOP_GPU_API_KEY、DOTOP_GPU_BASE_URL、DOTOP_GPU_CONFIG
|
|
80
|
+
`;
|
|
81
|
+
|
|
82
|
+
// 版本号取自本包 package.json,避免和文件名/常量两处维护。
|
|
83
|
+
// 相对 bin/ 向上找 package.json;读不到时返回 'unknown',不让 --version 报错退出。
|
|
84
|
+
function readPackageVersion() {
|
|
85
|
+
try {
|
|
86
|
+
const url = new URL('../package.json', import.meta.url);
|
|
87
|
+
const pkg = JSON.parse(readFileSync(url, 'utf8'));
|
|
88
|
+
return `dotop-gpu ${pkg.version || 'unknown'}(${pkg.name || 'dotop-gpu-cli'})`;
|
|
89
|
+
} catch {
|
|
90
|
+
return 'dotop-gpu unknown';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function main() {
|
|
95
|
+
const argv = process.argv.slice(2);
|
|
96
|
+
if (argv[0] === '-h' || argv[0] === '--help' || argv[0] === 'help') {
|
|
97
|
+
console.log(HELP);
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
// --version / -v:只读本包 package.json,不联网、不读配置,必须在任何鉴权检查之前。
|
|
101
|
+
if (argv[0] === '-v' || argv[0] === '--version' || argv[0] === 'version') {
|
|
102
|
+
console.log(readPackageVersion());
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const g = parseGlobalArgs(argv);
|
|
107
|
+
const { apiKey, baseUrl } = resolveSettings(g);
|
|
108
|
+
const cmd = g.rest[0] || '';
|
|
109
|
+
const args = g.rest.slice(1);
|
|
110
|
+
|
|
111
|
+
// 纯本地帮助,不需要 Key(还没配置 Key 的新用户最需要看它)
|
|
112
|
+
if (cmd === 'rent' && (args.includes('-h') || args.includes('--help'))) {
|
|
113
|
+
console.log(RENT_HELP);
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 需要鉴权的命令在进入前统一检查一次
|
|
118
|
+
if (cmd !== 'login' && cmd !== 'whoami' && cmd !== 'help') {
|
|
119
|
+
if (!apiKey) {
|
|
120
|
+
console.log(paint.yellow('还没有配置 API Key。'));
|
|
121
|
+
console.log(paint.dim('用 dotop-gpu login 保存,或设置环境变量 DOTOP_GPU_API_KEY。'));
|
|
122
|
+
const ok = await confirm('现在输入一个 Key 并保存?', {
|
|
123
|
+
defaultValue: true,
|
|
124
|
+
actionYes: '现在输入',
|
|
125
|
+
actionNo: '跳过',
|
|
126
|
+
});
|
|
127
|
+
if (!ok) return 1;
|
|
128
|
+
const key = await askPassword('API Key(gk_ 开头)');
|
|
129
|
+
if (!key) return 1;
|
|
130
|
+
writeConfig({ apiKey: key });
|
|
131
|
+
console.log(paint.green(`✔ 已保存到 ${configPath()}`));
|
|
132
|
+
return await mainWith({ ...g, apiKey: key });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return await dispatch(cmd, args, { apiKey, baseUrl });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function mainWith(g) {
|
|
140
|
+
const { apiKey, baseUrl } = resolveSettings(g);
|
|
141
|
+
return await dispatch(g.rest[0] || '', g.rest.slice(1), { apiKey, baseUrl });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function dispatch(cmd, args, { apiKey, baseUrl }) {
|
|
145
|
+
const client = new Client({ apiKey, baseUrl });
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
switch (cmd) {
|
|
149
|
+
case '':
|
|
150
|
+
case 'rent':
|
|
151
|
+
return await cmdRent(client, args);
|
|
152
|
+
case 'list':
|
|
153
|
+
return await cmdList(client, args);
|
|
154
|
+
case 'show':
|
|
155
|
+
return await cmdShow(client, args);
|
|
156
|
+
case 'stop':
|
|
157
|
+
case 'start':
|
|
158
|
+
case 'reboot':
|
|
159
|
+
case 'destroy':
|
|
160
|
+
return await cmdChange(client, cmd, args);
|
|
161
|
+
case 'keys':
|
|
162
|
+
return await cmdKeys(client, args);
|
|
163
|
+
case 'login':
|
|
164
|
+
return await cmdLogin();
|
|
165
|
+
case 'whoami':
|
|
166
|
+
return await cmdWhoami(client, { apiKey, baseUrl });
|
|
167
|
+
default:
|
|
168
|
+
console.log(paint.red(`未知命令:${cmd}`));
|
|
169
|
+
console.log(HELP);
|
|
170
|
+
return 1;
|
|
171
|
+
}
|
|
172
|
+
} catch (err) {
|
|
173
|
+
reportError(err);
|
|
174
|
+
return 1;
|
|
175
|
+
} finally {
|
|
176
|
+
closeRL();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function reportError(err) {
|
|
181
|
+
if (err instanceof RentArgError) {
|
|
182
|
+
console.log(paint.red(err.message));
|
|
183
|
+
console.log(paint.dim('用法:dotop-gpu rent --help'));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (err instanceof ApiError) {
|
|
187
|
+
console.log(paint.red(`请求失败:${err.message}`));
|
|
188
|
+
if (err.errorCode) console.log(paint.dim(`错误码:${err.errorCode}(HTTP ${err.httpStatus})`));
|
|
189
|
+
if (err.httpStatus === 401) {
|
|
190
|
+
console.log(paint.dim('Key 无效或已停用。用 dotop-gpu login 重新配置。'));
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
console.log(paint.red(`出错了:${err?.message || err}`));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// rent 子命令的详细帮助:参数多,主 HELP 里塞不下。
|
|
198
|
+
const RENT_HELP = `
|
|
199
|
+
${paint.bold('dotop-gpu rent')} — 下单(交互式 / 全参数 / 混合)
|
|
200
|
+
|
|
201
|
+
${paint.bold('三种用法')}
|
|
202
|
+
1. 全交互(什么都不给)
|
|
203
|
+
dotop-gpu rent
|
|
204
|
+
|
|
205
|
+
2. 全参数 + --yes(完全非交互,适合脚本/CI)
|
|
206
|
+
dotop-gpu rent --zone z1 --plan 1 --image 8130 --ssh-key 27 \\
|
|
207
|
+
--billing daily --count 3 --yes
|
|
208
|
+
|
|
209
|
+
3. 混合(给一部分,剩下的照常问)
|
|
210
|
+
dotop-gpu rent --zone z2 --billing daily
|
|
211
|
+
|
|
212
|
+
${paint.bold('参数')}
|
|
213
|
+
--zone <区> 可用区:zone_key(z1)或名字,也可用子串
|
|
214
|
+
--plan <规格> 规格:id(1)或名字(Lab GPU x1)
|
|
215
|
+
--image <镜像> 镜像:id(8130)或名字(Ubuntu 24.04)
|
|
216
|
+
--ssh-key <公钥> SSH 公钥:id、名称或指纹前缀
|
|
217
|
+
--billing <模式> 计费:on-demand / daily / weekly / monthly / yearly
|
|
218
|
+
也可写 按量 / 包日 / 包周 / 包月 / 包年、day/week/month/year
|
|
219
|
+
--count <n> 购买期数(仅周期计费,1-99)
|
|
220
|
+
--user-data <文本> 建机 user_data
|
|
221
|
+
--auto-renew 开启自动续费(--no-auto-renew 关闭)
|
|
222
|
+
--idem <键> 幂等键;重跑同一条命令必须复用它,避免开两台
|
|
223
|
+
|
|
224
|
+
${paint.bold('行为开关')}
|
|
225
|
+
--yes, -y 跳过确认页直接下单(余额不足时仍会拦住)
|
|
226
|
+
--dry-run 只报价、只打印,不下单
|
|
227
|
+
--no-wait 下单后不等建机完成,立刻打印租约号
|
|
228
|
+
--quick, -q 等价于 --yes(用默认值全自动下单)
|
|
229
|
+
|
|
230
|
+
${paint.bold('说明')}
|
|
231
|
+
· 只给 --plan 不给 --zone 时,会反查这个规格属于哪个可用区;多个区都有则要求显式 --zone。
|
|
232
|
+
· 名称匹配到多条时不会猜,会把候选列出来让你改用 id。
|
|
233
|
+
· 未指定的项按向导提问;--yes 时按「第一个有货」自动挑。
|
|
234
|
+
· 无论哪种用法,报价与余额门槛都由服务端返回,客户端不复刻价格计算。
|
|
235
|
+
`;
|
|
236
|
+
|
|
237
|
+
async function cmdRent(client, args) {
|
|
238
|
+
// rent --help 走单独的详细帮助,不跟着主 HELP 一起塞
|
|
239
|
+
if (args.includes('-h') || args.includes('--help')) {
|
|
240
|
+
console.log(RENT_HELP);
|
|
241
|
+
return 0;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const opts = parseRentArgs(args);
|
|
245
|
+
if (opts.unknown.length > 0) {
|
|
246
|
+
console.log(paint.red(`不认识的参数:${opts.unknown.join(' ')}`));
|
|
247
|
+
console.log(paint.dim('用法:dotop-gpu rent --help'));
|
|
248
|
+
return 1;
|
|
249
|
+
}
|
|
250
|
+
if (opts.quick) {
|
|
251
|
+
// 老写法 --quick 等价于 --yes(全默认值下单)
|
|
252
|
+
opts.yes = true;
|
|
253
|
+
}
|
|
254
|
+
validateCombo(opts);
|
|
255
|
+
|
|
256
|
+
// 混合模式:给了任何一项就按「已指定的跳过、剩下的照常问」处理。
|
|
257
|
+
// 一项都没给且没有 --yes = 原样的全交互向导。
|
|
258
|
+
const preset = await buildPreset(client, opts);
|
|
259
|
+
|
|
260
|
+
if (opts.dryRun) {
|
|
261
|
+
// 演练必须一次跑完才能算出完整报价:没给的项按 --yes 的规则自动挑,
|
|
262
|
+
// 但走到报价就停,绝不发下单请求。
|
|
263
|
+
await runWizard(client, { auto: false, preset, yes: true, dryRun: true, noWait: true });
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
await runWizard(client, {
|
|
268
|
+
auto: false,
|
|
269
|
+
preset,
|
|
270
|
+
yes: Boolean(opts.yes),
|
|
271
|
+
dryRun: false,
|
|
272
|
+
noWait: Boolean(opts.noWait),
|
|
273
|
+
});
|
|
274
|
+
return 0;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function cmdList(client, args) {
|
|
278
|
+
const all = args.includes('--all') || args.includes('-a');
|
|
279
|
+
const res = await client.listRentals({ page: 1, size: 200 });
|
|
280
|
+
let items = res.items || [];
|
|
281
|
+
if (!all) items = items.filter((r) => r.state !== 'destroyed');
|
|
282
|
+
|
|
283
|
+
if (items.length === 0) {
|
|
284
|
+
console.log(paint.dim(all ? '还没有任何租约。' : '没有进行中的租约(用 --all 看全部)。'));
|
|
285
|
+
return 0;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const rows = items.map((r) => ({
|
|
289
|
+
...r,
|
|
290
|
+
stateText: stateText(r.state),
|
|
291
|
+
billingText: billingText(r.billing_mode),
|
|
292
|
+
expired: r.expires_at ? ts(r.expires_at) : '-',
|
|
293
|
+
}));
|
|
294
|
+
|
|
295
|
+
console.log(
|
|
296
|
+
table(
|
|
297
|
+
[
|
|
298
|
+
{ title: 'ID', get: (r) => String(r.id) },
|
|
299
|
+
{ title: '租约号', get: (r) => shortId(r.rental_no, 16) },
|
|
300
|
+
{ title: '状态', get: (r) => r.stateText },
|
|
301
|
+
{ title: '可用区', get: (r) => r.zone_name || r.zone_key },
|
|
302
|
+
{ title: '实例', get: (r) => shortId(r.instance_name, 14) },
|
|
303
|
+
{ title: '计费', get: (r) => r.billingText },
|
|
304
|
+
{ title: '到期', get: (r) => r.expired },
|
|
305
|
+
],
|
|
306
|
+
rows,
|
|
307
|
+
),
|
|
308
|
+
);
|
|
309
|
+
console.log(
|
|
310
|
+
paint.dim(
|
|
311
|
+
`\n共 ${items.length} 条${res.total && res.total > items.length ? `(总计 ${res.total})` : ''} · 查看详情:dotop-gpu show <id>`,
|
|
312
|
+
),
|
|
313
|
+
);
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function cmdShow(client, args) {
|
|
318
|
+
const id = Number.parseInt(args[0], 10);
|
|
319
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
320
|
+
console.log(paint.red('用法:dotop-gpu show <id>'));
|
|
321
|
+
return 1;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const d = await client.getRental(id);
|
|
325
|
+
|
|
326
|
+
console.log('');
|
|
327
|
+
console.log(`${paint.bold(d.rental_no)} ${paint.dim(`id=${d.id}`)}`);
|
|
328
|
+
console.log(`${stateText(d.state, d.boot_state)}`);
|
|
329
|
+
console.log('');
|
|
330
|
+
console.log(`可用区 ${d.zone_name} (${d.zone_key})`);
|
|
331
|
+
console.log(`实例 ${d.instance_name}`);
|
|
332
|
+
console.log(`规格 ${d.gpu_model} × ${d.gpu_count} 卡${d.vram_gb ? ` / ${d.vram_gb}G 显存` : ''}`);
|
|
333
|
+
console.log(`镜像 ${d.image_name}`);
|
|
334
|
+
if (d.volume_gb) console.log(`数据盘 ${d.volume_gb} GiB`);
|
|
335
|
+
console.log(`计费 ${billingText(d.billing_mode)} · ${money(d.hourly_cost)}/小时`);
|
|
336
|
+
if (d.expires_at) {
|
|
337
|
+
console.log(`到期 ${ts(d.expires_at)}${d.auto_renew ? paint.dim('(自动续费)') : ''}`);
|
|
338
|
+
}
|
|
339
|
+
console.log(`已运行 ${duration(d.running_seconds)}`);
|
|
340
|
+
console.log(`已花费 ${money(d.cost)}`);
|
|
341
|
+
console.log(`创建 ${ts(d.created_ts)}`);
|
|
342
|
+
if (d.stopped_ts) console.log(`停机 ${ts(d.stopped_ts)}`);
|
|
343
|
+
|
|
344
|
+
console.log('');
|
|
345
|
+
if (d.ssh && d.ssh.command) {
|
|
346
|
+
console.log(`公网 IP ${d.floating_ip || paint.dim('(无)')}`);
|
|
347
|
+
console.log(`公钥 ${d.ssh_key?.name || '-'}`);
|
|
348
|
+
console.log('');
|
|
349
|
+
console.log(paint.bold('登录命令:'));
|
|
350
|
+
console.log(paint.cyan(` ${d.ssh.command}`));
|
|
351
|
+
} else {
|
|
352
|
+
console.log(paint.dim('实例还没就绪,暂无 SSH 命令。稍后再查。'));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (d.last_error) {
|
|
356
|
+
console.log('');
|
|
357
|
+
console.log(paint.red(`最近错误:${d.last_error}`));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return 0;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function cmdChange(client, kind, args) {
|
|
364
|
+
const id = Number.parseInt(args[0], 10);
|
|
365
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
366
|
+
console.log(paint.red(`用法:dotop-gpu ${kind} <id>`));
|
|
367
|
+
return 1;
|
|
368
|
+
}
|
|
369
|
+
const i = args.indexOf('--idem');
|
|
370
|
+
const idem = i >= 0 ? args[i + 1] : undefined;
|
|
371
|
+
|
|
372
|
+
// 销毁是有损操作,先确认
|
|
373
|
+
if (kind === 'destroy') {
|
|
374
|
+
const ok = await confirm(`确认销毁租约 ${id}?销毁后不可恢复。`, {
|
|
375
|
+
defaultValue: false,
|
|
376
|
+
actionYes: '销毁',
|
|
377
|
+
actionNo: '取消(不销毁)',
|
|
378
|
+
});
|
|
379
|
+
if (!ok) {
|
|
380
|
+
console.log(paint.dim('已取消。'));
|
|
381
|
+
return 0;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const fn = {
|
|
386
|
+
stop: () => client.stopRental(id, idem),
|
|
387
|
+
start: () => client.startRental(id, idem),
|
|
388
|
+
reboot: () => client.rebootRental(id, idem),
|
|
389
|
+
destroy: () => client.destroyRental(id, idem),
|
|
390
|
+
}[kind];
|
|
391
|
+
|
|
392
|
+
try {
|
|
393
|
+
const ch = await fn();
|
|
394
|
+
console.log(paint.green(`✔ ${ch.change_type} 已受理(state=${ch.state})`));
|
|
395
|
+
} catch (err) {
|
|
396
|
+
// destroy 在「周期租约未到期」时会被拒,这里是常见路径,给出更明确的提示
|
|
397
|
+
if (kind === 'destroy' && err instanceof ApiError && err.httpStatus === 409) {
|
|
398
|
+
console.log(paint.yellow(`销毁被拒绝:${err.message}`));
|
|
399
|
+
console.log(paint.dim(`周期租约可以先 dotop-gpu stop ${id} 停机,到期后再销毁。`));
|
|
400
|
+
return 1;
|
|
401
|
+
}
|
|
402
|
+
throw err;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// 变更也是异步的,短轮询到终结态
|
|
406
|
+
await watchChange(client, id, kind);
|
|
407
|
+
return 0;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function watchChange(client, id, kind) {
|
|
411
|
+
const deadline = Date.now() + 60_000;
|
|
412
|
+
while (Date.now() < deadline) {
|
|
413
|
+
await new Promise((r) => setTimeout(r, 3000));
|
|
414
|
+
let res;
|
|
415
|
+
try {
|
|
416
|
+
res = await client.listChanges(id, { page: 1, size: 5 });
|
|
417
|
+
} catch {
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
const latest = (res.items || []).find((c) => c.change_type === kind);
|
|
421
|
+
if (!latest) continue;
|
|
422
|
+
if (latest.state === 'SUCCESS') {
|
|
423
|
+
console.log(paint.green(`✔ ${kind} 已完成`));
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (latest.state === 'FAILED') {
|
|
427
|
+
console.log(paint.red(`✘ ${kind} 失败`));
|
|
428
|
+
if (latest.failure_reason) {
|
|
429
|
+
console.log(paint.dim(`原因:${latest.failure_reason}(${failureHint(latest.failure_reason)})`));
|
|
430
|
+
}
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
console.log(paint.dim(`变更还在处理中,用 dotop-gpu show ${id} 查看最新状态。`));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function failureHint(reason) {
|
|
438
|
+
return (
|
|
439
|
+
{
|
|
440
|
+
not_supported: '该操作当前不支持,不要重试',
|
|
441
|
+
timeout: '超时,可重试一次',
|
|
442
|
+
instance_missing: '实例已不存在,重新查询状态',
|
|
443
|
+
center_unavailable: '资源服务不可达,稍后重试',
|
|
444
|
+
conflict: '状态前置不满足,先刷新状态',
|
|
445
|
+
unknown: '未知原因,请联系支持',
|
|
446
|
+
}[reason] || ''
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
async function cmdKeys(client, args) {
|
|
451
|
+
const sub = args[0] || '';
|
|
452
|
+
|
|
453
|
+
if (sub === 'add') {
|
|
454
|
+
const local = scanLocalKeys();
|
|
455
|
+
if (local.length === 0) {
|
|
456
|
+
console.log(paint.yellow('没找到本机公钥(~/.ssh/*.pub)。'));
|
|
457
|
+
console.log(paint.dim('可以先用 ssh-keygen -t ed25519 生成。'));
|
|
458
|
+
return 1;
|
|
459
|
+
}
|
|
460
|
+
const picked = await selectOne('选择要上传的公钥', local, {
|
|
461
|
+
render: (k) => `${k.name} ${paint.dim(fingerprintOf(k.publicKey))}`,
|
|
462
|
+
});
|
|
463
|
+
if (!picked) return 0;
|
|
464
|
+
// 上传前把「要传什么」摆出来再问,和向导里的上传保持一致。
|
|
465
|
+
const description = `dotop-gpu 上传(${picked.file})`;
|
|
466
|
+
const ok = await confirm(
|
|
467
|
+
`将上传这把公钥:\n 名称 ${picked.name}\n 指纹 ${fingerprintOf(picked.publicKey)}\n 描述 ${description}`,
|
|
468
|
+
{ defaultValue: true, actionYes: '上传', actionNo: '不传' },
|
|
469
|
+
);
|
|
470
|
+
if (!ok) {
|
|
471
|
+
console.log(paint.dim('已取消上传,公钥没有添加。'));
|
|
472
|
+
return 0;
|
|
473
|
+
}
|
|
474
|
+
// 成功和失败都回到公钥列表:成功时能看到新公钥确实进了列表,
|
|
475
|
+
// 失败(比如 409 已存在)时也能就地看到那把已有的公钥。
|
|
476
|
+
let freshId = null;
|
|
477
|
+
try {
|
|
478
|
+
const created = await client.createSSHKey({
|
|
479
|
+
name: picked.name,
|
|
480
|
+
description,
|
|
481
|
+
publicKey: picked.publicKey,
|
|
482
|
+
});
|
|
483
|
+
freshId = created.id;
|
|
484
|
+
console.log(paint.green(`✔ 已上传 ${created.name}(id=${created.id})`));
|
|
485
|
+
console.log(paint.dim(`指纹 ${created.fingerprint}`));
|
|
486
|
+
} catch (err) {
|
|
487
|
+
console.log(paint.red(`上传失败:${err.message}`));
|
|
488
|
+
if (err.httpStatus === 409) {
|
|
489
|
+
const again = (await client.listSSHKeys()).items || [];
|
|
490
|
+
const hit = again.find((k) => k.fingerprint === fingerprintOf(picked.publicKey));
|
|
491
|
+
if (hit) {
|
|
492
|
+
freshId = hit.id;
|
|
493
|
+
console.log(
|
|
494
|
+
paint.dim(`平台上有同指纹的公钥「${hit.name}」(id=${hit.id}),直接复用,不用重新上传。`),
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
await printKeyList(client, freshId);
|
|
500
|
+
return 0;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (sub === 'rm' || sub === 'delete') {
|
|
504
|
+
const id = Number.parseInt(args[1], 10);
|
|
505
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
506
|
+
console.log(paint.red('用法:dotop-gpu keys rm <id>'));
|
|
507
|
+
return 1;
|
|
508
|
+
}
|
|
509
|
+
const ok = await confirm(`确认删除公钥 ${id}?已有租约不受影响。`, {
|
|
510
|
+
defaultValue: false,
|
|
511
|
+
actionYes: '删除',
|
|
512
|
+
actionNo: '取消(不删)',
|
|
513
|
+
});
|
|
514
|
+
if (!ok) {
|
|
515
|
+
console.log(paint.dim(`已取消,公钥 ${id} 没有删除。`));
|
|
516
|
+
return 0;
|
|
517
|
+
}
|
|
518
|
+
await client.deleteSSHKey(id);
|
|
519
|
+
console.log(paint.green(`✔ 已删除公钥 ${id}`));
|
|
520
|
+
return 0;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const items = (await client.listSSHKeys()).items || [];
|
|
524
|
+
await printKeyList(client, null, items);
|
|
525
|
+
return 0;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 打印公钥列表。freshId 命中时在那一行标「← 刚上传」——
|
|
529
|
+
// 上传后不管成功还是失败都走这里,让用户在同一个视图里看到结果。
|
|
530
|
+
async function printKeyList(client, freshId = null, preloaded = null) {
|
|
531
|
+
const items = preloaded || (await client.listSSHKeys()).items || [];
|
|
532
|
+
if (items.length === 0) {
|
|
533
|
+
console.log(paint.dim('还没有公钥。用 dotop-gpu keys add 从本机上传一个。'));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
console.log(
|
|
537
|
+
table(
|
|
538
|
+
[
|
|
539
|
+
{ title: 'ID', get: (k) => String(k.id) },
|
|
540
|
+
{ title: '名称', get: (k) => shortId(k.name, 20) },
|
|
541
|
+
{ title: '描述', get: (k) => shortId(k.description || '-', 24) },
|
|
542
|
+
{ title: '指纹', get: (k) => shortId(k.fingerprint, 28) },
|
|
543
|
+
{ title: '创建', get: (k) => ts(Date.parse(k.created_at) / 1000) },
|
|
544
|
+
{
|
|
545
|
+
title: '',
|
|
546
|
+
get: (k) => (freshId && freshId === k.id ? paint.green('← 刚上传') : ''),
|
|
547
|
+
},
|
|
548
|
+
],
|
|
549
|
+
items,
|
|
550
|
+
),
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
async function cmdLogin() {
|
|
555
|
+
console.log(paint.dim(`配置文件:${configPath()}`));
|
|
556
|
+
const key = await askPassword('API Key(gk_ 开头,输入后回车)');
|
|
557
|
+
if (!key) {
|
|
558
|
+
console.log(paint.red('没有输入内容,未保存。'));
|
|
559
|
+
return 1;
|
|
560
|
+
}
|
|
561
|
+
const url = await ask('服务地址', { defaultValue: 'http://127.0.0.1:8080' });
|
|
562
|
+
writeConfig({ apiKey: key, baseUrl: url });
|
|
563
|
+
console.log(paint.green(`✔ 已保存(Key ${maskKey(key)})`));
|
|
564
|
+
return 0;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
async function cmdWhoami(client, { apiKey, baseUrl }) {
|
|
568
|
+
console.log(`服务地址 ${baseUrl}`);
|
|
569
|
+
console.log(`API Key ${maskKey(apiKey)}`);
|
|
570
|
+
console.log(`配置文件 ${configPath()}`);
|
|
571
|
+
|
|
572
|
+
if (!apiKey) {
|
|
573
|
+
console.log(paint.yellow('\n没有配置 Key。用 dotop-gpu login 保存。'));
|
|
574
|
+
return 1;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
try {
|
|
578
|
+
const zones = (await client.listZones()).items || [];
|
|
579
|
+
console.log(paint.green(`\n✔ 连通正常,可见 ${zones.length} 个可用区`));
|
|
580
|
+
for (const z of zones) console.log(` · ${z.name} (${z.zone_key}) ${paint.dim(z.state)}`);
|
|
581
|
+
return 0;
|
|
582
|
+
} catch (err) {
|
|
583
|
+
reportError(err);
|
|
584
|
+
return 1;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
main()
|
|
589
|
+
.then((code) => {
|
|
590
|
+
closeRL();
|
|
591
|
+
process.exitCode = code || 0;
|
|
592
|
+
})
|
|
593
|
+
.catch((err) => {
|
|
594
|
+
reportError(err);
|
|
595
|
+
closeRL();
|
|
596
|
+
process.exitCode = 1;
|
|
597
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotop-gpu-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "GPU 分时出租 Open API 命令行客户端:交互式选型下单、查询租约与生命周期管理",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"dotop-gpu": "bin/dotop-gpu.mjs",
|
|
8
|
+
"dotop-gpu-cli": "bin/dotop-gpu.mjs"
|
|
9
|
+
},
|
|
10
|
+
"main": "src/api.mjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./src/api.mjs",
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"bin",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"start": "node bin/dotop-gpu.mjs"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"gpu",
|
|
28
|
+
"cli",
|
|
29
|
+
"rental",
|
|
30
|
+
"openapi",
|
|
31
|
+
"interactive",
|
|
32
|
+
"dotop"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT"
|
|
35
|
+
}
|