cloudgpu 0.1.0__py3-none-any.whl
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.
- cloudgpu/__init__.py +1 -0
- cloudgpu/__main__.py +4 -0
- cloudgpu/adapters/__init__.py +0 -0
- cloudgpu/adapters/autodl.py +1240 -0
- cloudgpu/adapters/base.py +15 -0
- cloudgpu/adapters/gongji.py +810 -0
- cloudgpu/adapters/gpugeek.py +924 -0
- cloudgpu/adapters/gpushare.py +653 -0
- cloudgpu/adapters/matpool.py +521 -0
- cloudgpu/adapters/mornai.py +155 -0
- cloudgpu/autodl_buy.py +266 -0
- cloudgpu/cli.py +3007 -0
- cloudgpu/config.py +340 -0
- cloudgpu/dashboard.py +267 -0
- cloudgpu/desktop.py +249 -0
- cloudgpu/doctor.py +295 -0
- cloudgpu/errors.py +39 -0
- cloudgpu/gongji_auth.py +179 -0
- cloudgpu/gongji_deploy.py +314 -0
- cloudgpu/gongji_state.py +320 -0
- cloudgpu/jobs.py +566 -0
- cloudgpu/lock.py +83 -0
- cloudgpu/models.py +65 -0
- cloudgpu/notify.py +295 -0
- cloudgpu/orders.py +319 -0
- cloudgpu/snapshot.py +136 -0
- cloudgpu/tui.py +513 -0
- cloudgpu/watch.py +669 -0
- cloudgpu/web.py +110 -0
- cloudgpu/webui.html +428 -0
- cloudgpu-0.1.0.dist-info/METADATA +303 -0
- cloudgpu-0.1.0.dist-info/RECORD +35 -0
- cloudgpu-0.1.0.dist-info/WHEEL +5 -0
- cloudgpu-0.1.0.dist-info/entry_points.txt +3 -0
- cloudgpu-0.1.0.dist-info/top_level.txt +1 -0
cloudgpu/cli.py
ADDED
|
@@ -0,0 +1,3007 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from dataclasses import asdict
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Optional, Sequence
|
|
10
|
+
|
|
11
|
+
from cloudgpu.adapters.autodl import AutoDLAdapter
|
|
12
|
+
from cloudgpu.adapters.gongji import GongjiAdapter
|
|
13
|
+
from cloudgpu.adapters.gpugeek import GPUGeekAdapter
|
|
14
|
+
from cloudgpu.adapters.gpushare import (
|
|
15
|
+
GpuShareAdapter,
|
|
16
|
+
is_valid_cuda_version,
|
|
17
|
+
)
|
|
18
|
+
from cloudgpu.adapters.matpool import MatpoolAdapter
|
|
19
|
+
from cloudgpu.adapters.mornai import MornAIAdapter
|
|
20
|
+
from cloudgpu.autodl_buy import (
|
|
21
|
+
BuyBlockedError,
|
|
22
|
+
NoCandidateError,
|
|
23
|
+
execute_buy,
|
|
24
|
+
prepare_buy,
|
|
25
|
+
reconcile_orders,
|
|
26
|
+
)
|
|
27
|
+
from cloudgpu.config import (
|
|
28
|
+
ConfigError,
|
|
29
|
+
KNOWN_PLATFORMS,
|
|
30
|
+
load_config,
|
|
31
|
+
)
|
|
32
|
+
from cloudgpu.errors import (
|
|
33
|
+
AdapterError,
|
|
34
|
+
AuthFailedError,
|
|
35
|
+
AuthRequiredError,
|
|
36
|
+
OrderRejectedError,
|
|
37
|
+
OrderUncertainError,
|
|
38
|
+
TaskRejectedError,
|
|
39
|
+
TaskUncertainError,
|
|
40
|
+
)
|
|
41
|
+
from cloudgpu.gongji_deploy import (
|
|
42
|
+
DeployBlockedError,
|
|
43
|
+
NoResourceError,
|
|
44
|
+
execute_deploy,
|
|
45
|
+
lazy_sweep,
|
|
46
|
+
prepare_deploy,
|
|
47
|
+
reconcile_pending,
|
|
48
|
+
sweep_ttl,
|
|
49
|
+
)
|
|
50
|
+
from cloudgpu.gongji_state import (
|
|
51
|
+
GongjiPendingStore,
|
|
52
|
+
GongjiStateError,
|
|
53
|
+
GongjiTtlStore,
|
|
54
|
+
)
|
|
55
|
+
from cloudgpu.jobs import build_jobs
|
|
56
|
+
from cloudgpu.lock import FileLock, LockError
|
|
57
|
+
from cloudgpu.models import GpuOffer, filter_offers
|
|
58
|
+
from cloudgpu.notify import (
|
|
59
|
+
CompositeNotifier,
|
|
60
|
+
NotifierConfigError,
|
|
61
|
+
build_notifiers,
|
|
62
|
+
)
|
|
63
|
+
from cloudgpu.orders import OrderStore
|
|
64
|
+
from cloudgpu.snapshot import collect_snapshot
|
|
65
|
+
from cloudgpu.watch import (
|
|
66
|
+
SchedulerOptions,
|
|
67
|
+
WatchOptions,
|
|
68
|
+
WatchState,
|
|
69
|
+
_credential_hint,
|
|
70
|
+
run_watch,
|
|
71
|
+
run_watch_jobs,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
76
|
+
parser = argparse.ArgumentParser(
|
|
77
|
+
prog="cloudgpu",
|
|
78
|
+
description="国内云 GPU 算力平台空闲监控(只读)",
|
|
79
|
+
)
|
|
80
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
81
|
+
|
|
82
|
+
mornai = subparsers.add_parser("mornai", help="晨涧云只读库存快照")
|
|
83
|
+
mornai.add_argument(
|
|
84
|
+
"--kind",
|
|
85
|
+
choices=("cloud", "container", "both"),
|
|
86
|
+
default="cloud",
|
|
87
|
+
help="云主机、云容器或两者(默认 cloud)",
|
|
88
|
+
)
|
|
89
|
+
mornai.add_argument(
|
|
90
|
+
"--gpu",
|
|
91
|
+
action="append",
|
|
92
|
+
default=[],
|
|
93
|
+
metavar="TEXT",
|
|
94
|
+
help="型号子串,可重复;如 4090、A100",
|
|
95
|
+
)
|
|
96
|
+
mornai.add_argument(
|
|
97
|
+
"--min-free",
|
|
98
|
+
type=int,
|
|
99
|
+
default=None,
|
|
100
|
+
metavar="N",
|
|
101
|
+
help="最少空闲卡数(默认 1)",
|
|
102
|
+
)
|
|
103
|
+
mornai.add_argument(
|
|
104
|
+
"--all",
|
|
105
|
+
action="store_true",
|
|
106
|
+
help="包含 0 空闲的型号,等价于 --min-free 0",
|
|
107
|
+
)
|
|
108
|
+
mornai.add_argument(
|
|
109
|
+
"--json",
|
|
110
|
+
action="store_true",
|
|
111
|
+
help="输出 JSON 而不是文本表格",
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
watch = subparsers.add_parser(
|
|
115
|
+
"watch",
|
|
116
|
+
help="持续轮询并在有卡时提醒",
|
|
117
|
+
)
|
|
118
|
+
watch.add_argument(
|
|
119
|
+
"--config",
|
|
120
|
+
default=None,
|
|
121
|
+
metavar="PATH",
|
|
122
|
+
help="JSON 配置文件,多平台模式",
|
|
123
|
+
)
|
|
124
|
+
watch.add_argument(
|
|
125
|
+
"--platform",
|
|
126
|
+
choices=("mornai", "autodl", "gpushare"),
|
|
127
|
+
default=None,
|
|
128
|
+
help="单平台模式:mornai、autodl 或 gpushare",
|
|
129
|
+
)
|
|
130
|
+
watch.add_argument(
|
|
131
|
+
"--kind",
|
|
132
|
+
choices=("cloud", "container", "both"),
|
|
133
|
+
default=None,
|
|
134
|
+
help="MornAI:云主机、云容器或两者(默认 cloud)",
|
|
135
|
+
)
|
|
136
|
+
watch.add_argument(
|
|
137
|
+
"--scope",
|
|
138
|
+
choices=("region", "machine"),
|
|
139
|
+
default=None,
|
|
140
|
+
help="AutoDL:地区级或机器级(默认 region)",
|
|
141
|
+
)
|
|
142
|
+
watch.add_argument(
|
|
143
|
+
"--region",
|
|
144
|
+
action="append",
|
|
145
|
+
default=[],
|
|
146
|
+
metavar="TEXT",
|
|
147
|
+
help="AutoDL 地区名、机房名或 region_sign,可重复",
|
|
148
|
+
)
|
|
149
|
+
watch.add_argument(
|
|
150
|
+
"--gpu",
|
|
151
|
+
action="append",
|
|
152
|
+
default=[],
|
|
153
|
+
metavar="TEXT",
|
|
154
|
+
help="型号;地区级用子串,机器级必须是完整型号",
|
|
155
|
+
)
|
|
156
|
+
watch.add_argument(
|
|
157
|
+
"--token-file",
|
|
158
|
+
default=None,
|
|
159
|
+
metavar="PATH",
|
|
160
|
+
help="AutoDL token 文件(默认 ~/.cloudgpu/autodl.token)",
|
|
161
|
+
)
|
|
162
|
+
watch.add_argument(
|
|
163
|
+
"--min-free",
|
|
164
|
+
type=int,
|
|
165
|
+
default=None,
|
|
166
|
+
metavar="N",
|
|
167
|
+
help="最少空闲卡数(默认 1,必须 >= 1)",
|
|
168
|
+
)
|
|
169
|
+
watch.add_argument(
|
|
170
|
+
"--max-price",
|
|
171
|
+
type=float,
|
|
172
|
+
default=None,
|
|
173
|
+
metavar="N",
|
|
174
|
+
help="gpushare:最高单价(元/小时)",
|
|
175
|
+
)
|
|
176
|
+
watch.add_argument(
|
|
177
|
+
"--min-vram",
|
|
178
|
+
type=float,
|
|
179
|
+
default=None,
|
|
180
|
+
metavar="N",
|
|
181
|
+
help="gpushare:最低显存 GiB",
|
|
182
|
+
)
|
|
183
|
+
watch.add_argument(
|
|
184
|
+
"--min-ram",
|
|
185
|
+
type=float,
|
|
186
|
+
default=None,
|
|
187
|
+
metavar="N",
|
|
188
|
+
help="gpushare:最低每卡内存 GiB",
|
|
189
|
+
)
|
|
190
|
+
watch.add_argument(
|
|
191
|
+
"--min-cuda",
|
|
192
|
+
default=None,
|
|
193
|
+
help="gpushare:最低 CUDA 版本,如 12.4",
|
|
194
|
+
)
|
|
195
|
+
watch.add_argument(
|
|
196
|
+
"--interval",
|
|
197
|
+
type=float,
|
|
198
|
+
default=None,
|
|
199
|
+
metavar="SECONDS",
|
|
200
|
+
help="轮询间隔秒数(默认 30,最低 5)",
|
|
201
|
+
)
|
|
202
|
+
watch.add_argument(
|
|
203
|
+
"--jitter",
|
|
204
|
+
type=float,
|
|
205
|
+
default=None,
|
|
206
|
+
metavar="SECONDS",
|
|
207
|
+
help="每轮额外随机抖动上限(默认 5)",
|
|
208
|
+
)
|
|
209
|
+
watch.add_argument(
|
|
210
|
+
"--notify",
|
|
211
|
+
default=None,
|
|
212
|
+
metavar="CHANNELS",
|
|
213
|
+
help="逗号分隔:terminal,desktop,serverchan,pushplus,wxpusher,wecom",
|
|
214
|
+
)
|
|
215
|
+
watch.add_argument(
|
|
216
|
+
"--state-file",
|
|
217
|
+
default=None,
|
|
218
|
+
metavar="PATH",
|
|
219
|
+
help="状态文件(默认 ~/.cloudgpu/state.json)",
|
|
220
|
+
)
|
|
221
|
+
watch.add_argument(
|
|
222
|
+
"--once",
|
|
223
|
+
action="store_true",
|
|
224
|
+
help="只检查一轮,适合 cron",
|
|
225
|
+
)
|
|
226
|
+
watch.add_argument(
|
|
227
|
+
"--quiet",
|
|
228
|
+
action="store_true",
|
|
229
|
+
help="不打印每轮摘要",
|
|
230
|
+
)
|
|
231
|
+
watch.add_argument(
|
|
232
|
+
"--json",
|
|
233
|
+
action="store_true",
|
|
234
|
+
help="与 --once 一起使用,输出本轮事件和状态",
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
autodl = subparsers.add_parser("autodl", help="AutoDL 只读查询")
|
|
238
|
+
autodl_sub = autodl.add_subparsers(
|
|
239
|
+
dest="autodl_command",
|
|
240
|
+
required=True,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
autodl_models = autodl_sub.add_parser("models", help="型号目录")
|
|
244
|
+
autodl_models.add_argument("--json", action="store_true")
|
|
245
|
+
autodl_models.add_argument("--token-file", default=None)
|
|
246
|
+
|
|
247
|
+
autodl_regions = autodl_sub.add_parser("regions", help="地区列表")
|
|
248
|
+
autodl_regions.add_argument("--json", action="store_true")
|
|
249
|
+
autodl_regions.add_argument("--token-file", default=None)
|
|
250
|
+
|
|
251
|
+
autodl_offers = autodl_sub.add_parser(
|
|
252
|
+
"offers", help="地区级空闲数"
|
|
253
|
+
)
|
|
254
|
+
autodl_offers.add_argument(
|
|
255
|
+
"--region", action="append", default=[], metavar="TEXT"
|
|
256
|
+
)
|
|
257
|
+
autodl_offers.add_argument(
|
|
258
|
+
"--gpu", action="append", default=[], metavar="TEXT"
|
|
259
|
+
)
|
|
260
|
+
autodl_offers.add_argument(
|
|
261
|
+
"--min-free", type=int, default=1, metavar="N"
|
|
262
|
+
)
|
|
263
|
+
autodl_offers.add_argument(
|
|
264
|
+
"--all", action="store_true", help="包含 0 空闲的型号"
|
|
265
|
+
)
|
|
266
|
+
autodl_offers.add_argument("--json", action="store_true")
|
|
267
|
+
autodl_offers.add_argument("--token-file", default=None)
|
|
268
|
+
|
|
269
|
+
autodl_machines = autodl_sub.add_parser(
|
|
270
|
+
"machines", help="机器明细"
|
|
271
|
+
)
|
|
272
|
+
autodl_machines.add_argument("--region", required=True)
|
|
273
|
+
autodl_machines.add_argument("--gpu", required=True)
|
|
274
|
+
autodl_machines.add_argument(
|
|
275
|
+
"--min-idle", type=int, default=1, metavar="N"
|
|
276
|
+
)
|
|
277
|
+
autodl_machines.add_argument("--json", action="store_true")
|
|
278
|
+
autodl_machines.add_argument("--token-file", default=None)
|
|
279
|
+
|
|
280
|
+
autodl_images = autodl_sub.add_parser(
|
|
281
|
+
"images", help="可用镜像"
|
|
282
|
+
)
|
|
283
|
+
autodl_images.add_argument("--gpu", required=True)
|
|
284
|
+
autodl_images.add_argument("--json", action="store_true")
|
|
285
|
+
autodl_images.add_argument("--token-file", default=None)
|
|
286
|
+
|
|
287
|
+
autodl_buy = autodl_sub.add_parser(
|
|
288
|
+
"buy", help="准备/执行 payg 下单"
|
|
289
|
+
)
|
|
290
|
+
autodl_buy.add_argument("--gpu", required=True)
|
|
291
|
+
autodl_buy.add_argument("--image", required=True)
|
|
292
|
+
autodl_buy.add_argument(
|
|
293
|
+
"--count", type=int, default=1, metavar="N"
|
|
294
|
+
)
|
|
295
|
+
autodl_buy.add_argument("--region", default=None)
|
|
296
|
+
autodl_buy.add_argument(
|
|
297
|
+
"--max-hourly", type=float, default=None, metavar="N"
|
|
298
|
+
)
|
|
299
|
+
autodl_buy.add_argument(
|
|
300
|
+
"--name-prefix", default="cloudgpu"
|
|
301
|
+
)
|
|
302
|
+
autodl_buy.add_argument(
|
|
303
|
+
"--execute", action="store_true"
|
|
304
|
+
)
|
|
305
|
+
autodl_buy.add_argument("--json", action="store_true")
|
|
306
|
+
autodl_buy.add_argument("--token-file", default=None)
|
|
307
|
+
|
|
308
|
+
autodl_instances = autodl_sub.add_parser(
|
|
309
|
+
"instances", help="当前实例"
|
|
310
|
+
)
|
|
311
|
+
autodl_instances.add_argument("--json", action="store_true")
|
|
312
|
+
autodl_instances.add_argument("--token-file", default=None)
|
|
313
|
+
|
|
314
|
+
autodl_orders = autodl_sub.add_parser(
|
|
315
|
+
"orders", help="本地订单台账"
|
|
316
|
+
)
|
|
317
|
+
autodl_orders.add_argument("--json", action="store_true")
|
|
318
|
+
autodl_orders.add_argument(
|
|
319
|
+
"--reconcile", action="store_true"
|
|
320
|
+
)
|
|
321
|
+
autodl_orders.add_argument(
|
|
322
|
+
"--resolve", type=int, default=None, metavar="ID"
|
|
323
|
+
)
|
|
324
|
+
autodl_orders.add_argument(
|
|
325
|
+
"--not-created", action="store_true"
|
|
326
|
+
)
|
|
327
|
+
autodl_orders.add_argument("--token-file", default=None)
|
|
328
|
+
|
|
329
|
+
gpushare = subparsers.add_parser(
|
|
330
|
+
"gpushare", help="恒源云只读查询"
|
|
331
|
+
)
|
|
332
|
+
gpushare_sub = gpushare.add_subparsers(
|
|
333
|
+
dest="gpushare_command",
|
|
334
|
+
required=True,
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
gpushare_machines = gpushare_sub.add_parser(
|
|
338
|
+
"machines", help="机器明细"
|
|
339
|
+
)
|
|
340
|
+
gpushare_machines.add_argument("--gpu", default=None)
|
|
341
|
+
gpushare_machines.add_argument("--region", default=None)
|
|
342
|
+
gpushare_machines.add_argument(
|
|
343
|
+
"--min-free", type=int, default=1, metavar="N"
|
|
344
|
+
)
|
|
345
|
+
gpushare_machines.add_argument(
|
|
346
|
+
"--max-price", type=float, default=None, metavar="N"
|
|
347
|
+
)
|
|
348
|
+
gpushare_machines.add_argument(
|
|
349
|
+
"--min-vram", type=float, default=None, metavar="N"
|
|
350
|
+
)
|
|
351
|
+
gpushare_machines.add_argument(
|
|
352
|
+
"--min-ram", type=float, default=None, metavar="N"
|
|
353
|
+
)
|
|
354
|
+
gpushare_machines.add_argument("--min-cuda", default=None)
|
|
355
|
+
gpushare_machines.add_argument("--json", action="store_true")
|
|
356
|
+
gpushare_machines.add_argument("--token-file", default=None)
|
|
357
|
+
|
|
358
|
+
gpushare_models = gpushare_sub.add_parser(
|
|
359
|
+
"models", help="型号目录"
|
|
360
|
+
)
|
|
361
|
+
gpushare_models.add_argument("--json", action="store_true")
|
|
362
|
+
gpushare_models.add_argument("--token-file", default=None)
|
|
363
|
+
|
|
364
|
+
gpushare_regions = gpushare_sub.add_parser(
|
|
365
|
+
"regions", help="地区目录"
|
|
366
|
+
)
|
|
367
|
+
gpushare_regions.add_argument("--json", action="store_true")
|
|
368
|
+
gpushare_regions.add_argument("--token-file", default=None)
|
|
369
|
+
|
|
370
|
+
gongji = subparsers.add_parser(
|
|
371
|
+
"gongji", help="共绩算力只读查询"
|
|
372
|
+
)
|
|
373
|
+
gongji_sub = gongji.add_subparsers(
|
|
374
|
+
dest="gongji_command",
|
|
375
|
+
required=True,
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
gongji_resources = gongji_sub.add_parser(
|
|
379
|
+
"resources", help="GPU 资源"
|
|
380
|
+
)
|
|
381
|
+
gongji_resources.add_argument("--gpu", default=None)
|
|
382
|
+
gongji_resources.add_argument("--region", default=None)
|
|
383
|
+
gongji_resources.add_argument(
|
|
384
|
+
"--min-free", type=int, default=1, metavar="N"
|
|
385
|
+
)
|
|
386
|
+
gongji_resources.add_argument(
|
|
387
|
+
"--max-price", type=float, default=None, metavar="N"
|
|
388
|
+
)
|
|
389
|
+
gongji_resources.add_argument(
|
|
390
|
+
"--min-vram", type=float, default=None, metavar="N"
|
|
391
|
+
)
|
|
392
|
+
gongji_resources.add_argument(
|
|
393
|
+
"--min-ram", type=float, default=None, metavar="N"
|
|
394
|
+
)
|
|
395
|
+
gongji_resources.add_argument("--json", action="store_true")
|
|
396
|
+
gongji_resources.add_argument("--token-file", default=None)
|
|
397
|
+
|
|
398
|
+
gongji_models = gongji_sub.add_parser("models", help="型号目录")
|
|
399
|
+
gongji_models.add_argument("--json", action="store_true")
|
|
400
|
+
gongji_models.add_argument("--token-file", default=None)
|
|
401
|
+
|
|
402
|
+
gongji_regions = gongji_sub.add_parser("regions", help="地区目录")
|
|
403
|
+
gongji_regions.add_argument("--json", action="store_true")
|
|
404
|
+
gongji_regions.add_argument("--token-file", default=None)
|
|
405
|
+
|
|
406
|
+
gongji_tasks = gongji_sub.add_parser(
|
|
407
|
+
"tasks", help="任务列表"
|
|
408
|
+
)
|
|
409
|
+
gongji_tasks.add_argument(
|
|
410
|
+
"--status", default="Running,Pending,Paused"
|
|
411
|
+
)
|
|
412
|
+
gongji_tasks.add_argument(
|
|
413
|
+
"--reconcile", action="store_true"
|
|
414
|
+
)
|
|
415
|
+
gongji_tasks.add_argument(
|
|
416
|
+
"--resolve", default=None, metavar="NAME"
|
|
417
|
+
)
|
|
418
|
+
gongji_tasks.add_argument(
|
|
419
|
+
"--not-created", action="store_true"
|
|
420
|
+
)
|
|
421
|
+
gongji_tasks.add_argument("--json", action="store_true")
|
|
422
|
+
gongji_tasks.add_argument("--token-file", default=None)
|
|
423
|
+
|
|
424
|
+
gongji_task = gongji_sub.add_parser("task", help="任务详情")
|
|
425
|
+
gongji_task.add_argument("task_id", type=int)
|
|
426
|
+
gongji_task.add_argument("--json", action="store_true")
|
|
427
|
+
gongji_task.add_argument("--token-file", default=None)
|
|
428
|
+
|
|
429
|
+
gongji_deploy = gongji_sub.add_parser(
|
|
430
|
+
"deploy", help="准备/执行弹性部署"
|
|
431
|
+
)
|
|
432
|
+
gongji_deploy.add_argument("--gpu", required=True)
|
|
433
|
+
gongji_deploy.add_argument(
|
|
434
|
+
"--count", type=int, default=1, metavar="N"
|
|
435
|
+
)
|
|
436
|
+
gongji_deploy.add_argument("--image", required=True)
|
|
437
|
+
gongji_deploy.add_argument(
|
|
438
|
+
"--port", type=int, action="append", default=None
|
|
439
|
+
)
|
|
440
|
+
gongji_deploy.add_argument("--region", default=None)
|
|
441
|
+
gongji_deploy.add_argument(
|
|
442
|
+
"--ttl", type=int, default=None, metavar="SECONDS"
|
|
443
|
+
)
|
|
444
|
+
gongji_deploy.add_argument(
|
|
445
|
+
"--max-hourly", type=float, default=None, metavar="N"
|
|
446
|
+
)
|
|
447
|
+
gongji_deploy.add_argument(
|
|
448
|
+
"--name-prefix", default="cloudgpu"
|
|
449
|
+
)
|
|
450
|
+
gongji_deploy.add_argument(
|
|
451
|
+
"--execute", action="store_true"
|
|
452
|
+
)
|
|
453
|
+
gongji_deploy.add_argument("--json", action="store_true")
|
|
454
|
+
gongji_deploy.add_argument("--token-file", default=None)
|
|
455
|
+
|
|
456
|
+
for action in ("pause", "resume", "stop"):
|
|
457
|
+
action_parser = gongji_sub.add_parser(
|
|
458
|
+
action, help=f"{action} 任务"
|
|
459
|
+
)
|
|
460
|
+
action_parser.add_argument("task_id", type=int)
|
|
461
|
+
if action == "stop":
|
|
462
|
+
action_parser.add_argument(
|
|
463
|
+
"--force", action="store_true"
|
|
464
|
+
)
|
|
465
|
+
action_parser.add_argument(
|
|
466
|
+
"--json", action="store_true"
|
|
467
|
+
)
|
|
468
|
+
action_parser.add_argument(
|
|
469
|
+
"--token-file", default=None
|
|
470
|
+
)
|
|
471
|
+
|
|
472
|
+
gongji_ttl = gongji_sub.add_parser("ttl", help="TTL 管理")
|
|
473
|
+
gongji_ttl_sub = gongji_ttl.add_subparsers(
|
|
474
|
+
dest="gongji_ttl_command", required=True
|
|
475
|
+
)
|
|
476
|
+
gongji_ttl_list = gongji_ttl_sub.add_parser(
|
|
477
|
+
"list", help="TTL 列表"
|
|
478
|
+
)
|
|
479
|
+
gongji_ttl_list.add_argument("--json", action="store_true")
|
|
480
|
+
gongji_ttl_list.add_argument("--token-file", default=None)
|
|
481
|
+
gongji_ttl_sweep = gongji_ttl_sub.add_parser(
|
|
482
|
+
"sweep", help="扫描过期任务"
|
|
483
|
+
)
|
|
484
|
+
gongji_ttl_sweep.add_argument(
|
|
485
|
+
"--execute", action="store_true"
|
|
486
|
+
)
|
|
487
|
+
gongji_ttl_sweep.add_argument("--json", action="store_true")
|
|
488
|
+
gongji_ttl_sweep.add_argument("--token-file", default=None)
|
|
489
|
+
gongji_ttl_rm = gongji_ttl_sub.add_parser(
|
|
490
|
+
"rm", help="移除 TTL 条目"
|
|
491
|
+
)
|
|
492
|
+
gongji_ttl_rm.add_argument("task_id", type=int)
|
|
493
|
+
gongji_ttl_rm.add_argument("--json", action="store_true")
|
|
494
|
+
gongji_ttl_rm.add_argument("--token-file", default=None)
|
|
495
|
+
|
|
496
|
+
matpool = subparsers.add_parser(
|
|
497
|
+
"matpool", help="矩池云只读查询"
|
|
498
|
+
)
|
|
499
|
+
matpool_sub = matpool.add_subparsers(
|
|
500
|
+
dest="matpool_command",
|
|
501
|
+
required=True,
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
matpool_machines = matpool_sub.add_parser(
|
|
505
|
+
"machines", help="机器明细"
|
|
506
|
+
)
|
|
507
|
+
matpool_machines.add_argument("--gpu", default=None)
|
|
508
|
+
matpool_machines.add_argument("--region", default=None)
|
|
509
|
+
matpool_machines.add_argument(
|
|
510
|
+
"--min-free", type=int, default=1, metavar="N"
|
|
511
|
+
)
|
|
512
|
+
matpool_machines.add_argument(
|
|
513
|
+
"--max-price", type=float, default=None, metavar="N"
|
|
514
|
+
)
|
|
515
|
+
matpool_machines.add_argument(
|
|
516
|
+
"--min-vram", type=float, default=None, metavar="N"
|
|
517
|
+
)
|
|
518
|
+
matpool_machines.add_argument(
|
|
519
|
+
"--min-ram", type=float, default=None, metavar="N"
|
|
520
|
+
)
|
|
521
|
+
matpool_machines.add_argument("--json", action="store_true")
|
|
522
|
+
|
|
523
|
+
matpool_models = matpool_sub.add_parser(
|
|
524
|
+
"models", help="型号目录"
|
|
525
|
+
)
|
|
526
|
+
matpool_models.add_argument("--json", action="store_true")
|
|
527
|
+
|
|
528
|
+
matpool_regions = matpool_sub.add_parser(
|
|
529
|
+
"regions", help="地区目录"
|
|
530
|
+
)
|
|
531
|
+
matpool_regions.add_argument("--json", action="store_true")
|
|
532
|
+
|
|
533
|
+
gpugeek = subparsers.add_parser(
|
|
534
|
+
"gpugeek", help="GPUGeek 只读查询"
|
|
535
|
+
)
|
|
536
|
+
gpugeek_sub = gpugeek.add_subparsers(
|
|
537
|
+
dest="gpugeek_command",
|
|
538
|
+
required=True,
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
gpugeek_offers = gpugeek_sub.add_parser(
|
|
542
|
+
"offers", help="云端市场"
|
|
543
|
+
)
|
|
544
|
+
gpugeek_offers.add_argument("--gpu", default=None)
|
|
545
|
+
gpugeek_offers.add_argument("--region", default=None)
|
|
546
|
+
gpugeek_offers.add_argument(
|
|
547
|
+
"--min-free", type=int, default=1, metavar="N"
|
|
548
|
+
)
|
|
549
|
+
gpugeek_offers.add_argument(
|
|
550
|
+
"--max-price", type=float, default=None, metavar="N"
|
|
551
|
+
)
|
|
552
|
+
gpugeek_offers.add_argument(
|
|
553
|
+
"--min-vram", type=float, default=None, metavar="N"
|
|
554
|
+
)
|
|
555
|
+
gpugeek_offers.add_argument(
|
|
556
|
+
"--min-ram", type=float, default=None, metavar="N"
|
|
557
|
+
)
|
|
558
|
+
gpugeek_offers.add_argument("--json", action="store_true")
|
|
559
|
+
|
|
560
|
+
gpugeek_bms = gpugeek_sub.add_parser(
|
|
561
|
+
"bms", help="BMS 裸金属市场"
|
|
562
|
+
)
|
|
563
|
+
gpugeek_bms.add_argument("--gpu", default=None)
|
|
564
|
+
gpugeek_bms.add_argument("--region", default=None)
|
|
565
|
+
gpugeek_bms.add_argument(
|
|
566
|
+
"--min-available", type=int, default=1, metavar="N"
|
|
567
|
+
)
|
|
568
|
+
gpugeek_bms.add_argument(
|
|
569
|
+
"--max-price", type=float, default=None, metavar="N"
|
|
570
|
+
)
|
|
571
|
+
gpugeek_bms.add_argument(
|
|
572
|
+
"--min-vram", type=float, default=None, metavar="N"
|
|
573
|
+
)
|
|
574
|
+
gpugeek_bms.add_argument(
|
|
575
|
+
"--min-ram", type=float, default=None, metavar="N"
|
|
576
|
+
)
|
|
577
|
+
gpugeek_bms.add_argument("--json", action="store_true")
|
|
578
|
+
|
|
579
|
+
gpugeek_models = gpugeek_sub.add_parser(
|
|
580
|
+
"models", help="型号目录"
|
|
581
|
+
)
|
|
582
|
+
gpugeek_models.add_argument(
|
|
583
|
+
"--kind", choices=("cloud", "bms"), default="cloud"
|
|
584
|
+
)
|
|
585
|
+
gpugeek_models.add_argument("--json", action="store_true")
|
|
586
|
+
|
|
587
|
+
gpugeek_regions = gpugeek_sub.add_parser(
|
|
588
|
+
"regions", help="地区目录"
|
|
589
|
+
)
|
|
590
|
+
gpugeek_regions.add_argument(
|
|
591
|
+
"--kind", choices=("cloud", "bms"), default="cloud"
|
|
592
|
+
)
|
|
593
|
+
gpugeek_regions.add_argument("--json", action="store_true")
|
|
594
|
+
|
|
595
|
+
find = subparsers.add_parser(
|
|
596
|
+
"find", help="跨平台空闲快照"
|
|
597
|
+
)
|
|
598
|
+
find.add_argument("--config", required=True)
|
|
599
|
+
find.add_argument(
|
|
600
|
+
"--platform",
|
|
601
|
+
action="append",
|
|
602
|
+
choices=tuple(sorted(KNOWN_PLATFORMS)),
|
|
603
|
+
default=[],
|
|
604
|
+
)
|
|
605
|
+
find.add_argument(
|
|
606
|
+
"--gpu",
|
|
607
|
+
action="append",
|
|
608
|
+
default=[],
|
|
609
|
+
metavar="TEXT",
|
|
610
|
+
)
|
|
611
|
+
find.add_argument("--json", action="store_true")
|
|
612
|
+
|
|
613
|
+
tui = subparsers.add_parser(
|
|
614
|
+
"tui", help="Linux 终端监控面板"
|
|
615
|
+
)
|
|
616
|
+
tui.add_argument("--config", default=None)
|
|
617
|
+
tui.add_argument("--platform", default=None)
|
|
618
|
+
tui.add_argument("--target", default=None)
|
|
619
|
+
tui.add_argument("--gpu", default="")
|
|
620
|
+
tui.add_argument(
|
|
621
|
+
"--min-free", type=int, default=0, metavar="N"
|
|
622
|
+
)
|
|
623
|
+
tui.add_argument(
|
|
624
|
+
"--max-price", type=float, default=None, metavar="N"
|
|
625
|
+
)
|
|
626
|
+
tui.add_argument(
|
|
627
|
+
"--sort",
|
|
628
|
+
choices=(
|
|
629
|
+
"price-asc", "price-desc", "free-desc", "target"
|
|
630
|
+
),
|
|
631
|
+
default="price-asc",
|
|
632
|
+
)
|
|
633
|
+
|
|
634
|
+
web = subparsers.add_parser(
|
|
635
|
+
"web", help="Linux Web 监控面板(本机/局域网)"
|
|
636
|
+
)
|
|
637
|
+
web.add_argument("--config", default=None)
|
|
638
|
+
web.add_argument("--host", default="127.0.0.1")
|
|
639
|
+
web.add_argument("--port", type=int, default=8787)
|
|
640
|
+
web.add_argument("--no-open", action="store_true")
|
|
641
|
+
web.add_argument("--allow-lan", action="store_true")
|
|
642
|
+
|
|
643
|
+
desktop = subparsers.add_parser(
|
|
644
|
+
"desktop", help="Windows GUI(Linux 请用 tui/web)"
|
|
645
|
+
)
|
|
646
|
+
desktop.add_argument("--config", default=None)
|
|
647
|
+
|
|
648
|
+
doctor = subparsers.add_parser(
|
|
649
|
+
"doctor", help="配置、凭据与监控安全检查"
|
|
650
|
+
)
|
|
651
|
+
doctor.add_argument("--config", default=None)
|
|
652
|
+
doctor.add_argument("--check-api", action="store_true")
|
|
653
|
+
doctor.add_argument("--json", action="store_true")
|
|
654
|
+
return parser
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
def main(
|
|
658
|
+
argv: Optional[list[str]] = None,
|
|
659
|
+
adapter=None,
|
|
660
|
+
adapters=None,
|
|
661
|
+
notifiers=None,
|
|
662
|
+
state=None,
|
|
663
|
+
sleeper=None,
|
|
664
|
+
clock=None,
|
|
665
|
+
random_func=None,
|
|
666
|
+
env=None,
|
|
667
|
+
) -> int:
|
|
668
|
+
args = build_parser().parse_args(argv)
|
|
669
|
+
if args.command == "mornai":
|
|
670
|
+
return _run_mornai(args, adapter)
|
|
671
|
+
if args.command == "watch":
|
|
672
|
+
return _run_watch_command(
|
|
673
|
+
args,
|
|
674
|
+
adapter=adapter,
|
|
675
|
+
adapters=adapters,
|
|
676
|
+
notifiers=notifiers,
|
|
677
|
+
state=state,
|
|
678
|
+
sleeper=sleeper,
|
|
679
|
+
clock=clock,
|
|
680
|
+
random_func=random_func,
|
|
681
|
+
env=env,
|
|
682
|
+
)
|
|
683
|
+
if args.command == "autodl":
|
|
684
|
+
return _run_autodl(args, adapter=adapter, env=env)
|
|
685
|
+
if args.command == "gpushare":
|
|
686
|
+
return _run_gpushare(args, adapter=adapter, env=env)
|
|
687
|
+
if args.command == "gongji":
|
|
688
|
+
return _run_gongji(args, adapter=adapter, env=env)
|
|
689
|
+
if args.command == "matpool":
|
|
690
|
+
return _run_matpool(args, adapter=adapter)
|
|
691
|
+
if args.command == "gpugeek":
|
|
692
|
+
return _run_gpugeek(args, adapter=adapter)
|
|
693
|
+
if args.command == "find":
|
|
694
|
+
return _run_find(args, adapters=adapters, env=env)
|
|
695
|
+
if args.command == "tui":
|
|
696
|
+
return _run_tui(args, adapters=adapters, env=env)
|
|
697
|
+
if args.command == "web":
|
|
698
|
+
return _run_web(args, adapters=adapters, env=env)
|
|
699
|
+
if args.command == "desktop":
|
|
700
|
+
return _run_desktop(args)
|
|
701
|
+
if args.command == "doctor":
|
|
702
|
+
return _run_doctor(args, adapters=adapters, env=env)
|
|
703
|
+
return 2
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
def _run_mornai(args, adapter=None) -> int:
|
|
707
|
+
active = adapter or MornAIAdapter()
|
|
708
|
+
min_free = (
|
|
709
|
+
0
|
|
710
|
+
if args.all
|
|
711
|
+
else (args.min_free if args.min_free is not None else 1)
|
|
712
|
+
)
|
|
713
|
+
kinds = ("cloud", "container") if args.kind == "both" else (args.kind,)
|
|
714
|
+
|
|
715
|
+
try:
|
|
716
|
+
raw_offers = []
|
|
717
|
+
for kind in kinds:
|
|
718
|
+
raw_offers.extend(active.list_offers(kind=kind))
|
|
719
|
+
checked_at = raw_offers[0].checked_at if raw_offers else ""
|
|
720
|
+
offers = filter_offers(
|
|
721
|
+
raw_offers,
|
|
722
|
+
gpu_patterns=args.gpu,
|
|
723
|
+
min_free=min_free,
|
|
724
|
+
)
|
|
725
|
+
except AdapterError as exc:
|
|
726
|
+
if args.json:
|
|
727
|
+
print(json.dumps({"error": str(exc)}, ensure_ascii=False))
|
|
728
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
729
|
+
return 3
|
|
730
|
+
|
|
731
|
+
if args.json:
|
|
732
|
+
print(
|
|
733
|
+
json.dumps(
|
|
734
|
+
{
|
|
735
|
+
"platform": active.platform,
|
|
736
|
+
"kind": args.kind,
|
|
737
|
+
"checked_at": checked_at,
|
|
738
|
+
"offers": [offer.to_dict() for offer in offers],
|
|
739
|
+
},
|
|
740
|
+
ensure_ascii=False,
|
|
741
|
+
)
|
|
742
|
+
)
|
|
743
|
+
else:
|
|
744
|
+
_print_offers(offers)
|
|
745
|
+
return 0
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
def _run_config_watch(
|
|
749
|
+
args,
|
|
750
|
+
*,
|
|
751
|
+
adapters=None,
|
|
752
|
+
state=None,
|
|
753
|
+
sleeper=None,
|
|
754
|
+
clock=None,
|
|
755
|
+
random_func=None,
|
|
756
|
+
env=None,
|
|
757
|
+
) -> int:
|
|
758
|
+
if (
|
|
759
|
+
args.platform is not None
|
|
760
|
+
or args.kind is not None
|
|
761
|
+
or args.scope is not None
|
|
762
|
+
or args.region
|
|
763
|
+
or args.gpu
|
|
764
|
+
or args.token_file is not None
|
|
765
|
+
or args.min_free is not None
|
|
766
|
+
or args.interval is not None
|
|
767
|
+
or args.jitter is not None
|
|
768
|
+
or args.max_price is not None
|
|
769
|
+
or args.min_vram is not None
|
|
770
|
+
or args.min_ram is not None
|
|
771
|
+
or args.min_cuda is not None
|
|
772
|
+
):
|
|
773
|
+
print(
|
|
774
|
+
"错误: --config 不能与单平台目标参数混用",
|
|
775
|
+
file=sys.stderr,
|
|
776
|
+
)
|
|
777
|
+
return 2
|
|
778
|
+
|
|
779
|
+
try:
|
|
780
|
+
config = load_config(args.config)
|
|
781
|
+
except ConfigError as exc:
|
|
782
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
783
|
+
return 2
|
|
784
|
+
|
|
785
|
+
state_path = (
|
|
786
|
+
Path(args.state_file).expanduser()
|
|
787
|
+
if args.state_file
|
|
788
|
+
else Path(config.state_file).expanduser()
|
|
789
|
+
)
|
|
790
|
+
lock = FileLock(Path(str(state_path) + ".lock"))
|
|
791
|
+
try:
|
|
792
|
+
try:
|
|
793
|
+
lock.acquire()
|
|
794
|
+
except LockError as exc:
|
|
795
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
796
|
+
return 3
|
|
797
|
+
|
|
798
|
+
active_state = (
|
|
799
|
+
state if state is not None else WatchState.load(state_path)
|
|
800
|
+
)
|
|
801
|
+
override = None
|
|
802
|
+
if args.notify is not None:
|
|
803
|
+
override = tuple(
|
|
804
|
+
name.strip()
|
|
805
|
+
for name in args.notify.split(",")
|
|
806
|
+
if name.strip()
|
|
807
|
+
)
|
|
808
|
+
elif args.json:
|
|
809
|
+
override = ()
|
|
810
|
+
|
|
811
|
+
resolved_env = os.environ if env is None else env
|
|
812
|
+
terminal_stream = sys.stderr if args.json else sys.stdout
|
|
813
|
+
|
|
814
|
+
def notifier_builder(names):
|
|
815
|
+
return CompositeNotifier(
|
|
816
|
+
build_notifiers(
|
|
817
|
+
names,
|
|
818
|
+
env=resolved_env,
|
|
819
|
+
stream=terminal_stream,
|
|
820
|
+
)
|
|
821
|
+
)
|
|
822
|
+
|
|
823
|
+
try:
|
|
824
|
+
jobs = build_jobs(
|
|
825
|
+
config,
|
|
826
|
+
adapters=adapters,
|
|
827
|
+
env=resolved_env,
|
|
828
|
+
notify_override=override,
|
|
829
|
+
notifier_builder=notifier_builder,
|
|
830
|
+
)
|
|
831
|
+
except AuthRequiredError as exc:
|
|
832
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
833
|
+
return 2
|
|
834
|
+
except NotifierConfigError as exc:
|
|
835
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
836
|
+
return 2
|
|
837
|
+
except AuthFailedError as exc:
|
|
838
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
839
|
+
return 3
|
|
840
|
+
except AdapterError as exc:
|
|
841
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
842
|
+
return 3
|
|
843
|
+
except ValueError as exc:
|
|
844
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
845
|
+
return 2
|
|
846
|
+
|
|
847
|
+
options = SchedulerOptions(
|
|
848
|
+
interval=config.interval,
|
|
849
|
+
jitter=config.jitter,
|
|
850
|
+
once=args.once,
|
|
851
|
+
quiet=args.quiet or args.json,
|
|
852
|
+
state_path=state_path,
|
|
853
|
+
json_output=args.json,
|
|
854
|
+
)
|
|
855
|
+
return run_watch_jobs(
|
|
856
|
+
jobs,
|
|
857
|
+
active_state,
|
|
858
|
+
options,
|
|
859
|
+
sleeper=sleeper,
|
|
860
|
+
clock=clock,
|
|
861
|
+
random_func=random_func,
|
|
862
|
+
)
|
|
863
|
+
finally:
|
|
864
|
+
lock.release()
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
def _run_watch_command(
|
|
868
|
+
args,
|
|
869
|
+
*,
|
|
870
|
+
adapter=None,
|
|
871
|
+
adapters=None,
|
|
872
|
+
notifiers=None,
|
|
873
|
+
state=None,
|
|
874
|
+
sleeper=None,
|
|
875
|
+
clock=None,
|
|
876
|
+
random_func=None,
|
|
877
|
+
env=None,
|
|
878
|
+
) -> int:
|
|
879
|
+
if args.json and not args.once:
|
|
880
|
+
print("错误: --json 只能与 --once 一起使用", file=sys.stderr)
|
|
881
|
+
return 2
|
|
882
|
+
|
|
883
|
+
if args.config:
|
|
884
|
+
return _run_config_watch(
|
|
885
|
+
args,
|
|
886
|
+
adapters=adapters,
|
|
887
|
+
state=state,
|
|
888
|
+
sleeper=sleeper,
|
|
889
|
+
clock=clock,
|
|
890
|
+
random_func=random_func,
|
|
891
|
+
env=env,
|
|
892
|
+
)
|
|
893
|
+
|
|
894
|
+
args.interval = 30.0 if args.interval is None else args.interval
|
|
895
|
+
args.jitter = 5.0 if args.jitter is None else args.jitter
|
|
896
|
+
args.min_free = 1 if args.min_free is None else args.min_free
|
|
897
|
+
|
|
898
|
+
if args.interval < 5:
|
|
899
|
+
print("错误: --interval 不能小于 5 秒", file=sys.stderr)
|
|
900
|
+
return 2
|
|
901
|
+
if args.jitter < 0:
|
|
902
|
+
print("错误: --jitter 不能为负数", file=sys.stderr)
|
|
903
|
+
return 2
|
|
904
|
+
if args.min_free < 1:
|
|
905
|
+
print("错误: --min-free 不能小于 1", file=sys.stderr)
|
|
906
|
+
return 2
|
|
907
|
+
|
|
908
|
+
args.platform = args.platform or "mornai"
|
|
909
|
+
|
|
910
|
+
if args.notify is None:
|
|
911
|
+
names = [] if args.json else ["terminal"]
|
|
912
|
+
else:
|
|
913
|
+
names = [
|
|
914
|
+
name.strip()
|
|
915
|
+
for name in args.notify.split(",")
|
|
916
|
+
if name.strip()
|
|
917
|
+
]
|
|
918
|
+
|
|
919
|
+
resolved_env = os.environ if env is None else env
|
|
920
|
+
terminal_stream = sys.stderr if args.json else sys.stdout
|
|
921
|
+
try:
|
|
922
|
+
if notifiers is not None:
|
|
923
|
+
active_notifier = CompositeNotifier(notifiers)
|
|
924
|
+
else:
|
|
925
|
+
active_notifier = CompositeNotifier(
|
|
926
|
+
build_notifiers(
|
|
927
|
+
names,
|
|
928
|
+
env=resolved_env,
|
|
929
|
+
stream=terminal_stream,
|
|
930
|
+
)
|
|
931
|
+
)
|
|
932
|
+
except NotifierConfigError as exc:
|
|
933
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
934
|
+
return 2
|
|
935
|
+
|
|
936
|
+
state_path = (
|
|
937
|
+
Path(args.state_file).expanduser()
|
|
938
|
+
if args.state_file
|
|
939
|
+
else default_state_path()
|
|
940
|
+
)
|
|
941
|
+
lock = FileLock(Path(str(state_path) + ".lock"))
|
|
942
|
+
try:
|
|
943
|
+
lock.acquire()
|
|
944
|
+
except LockError as exc:
|
|
945
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
946
|
+
return 3
|
|
947
|
+
try:
|
|
948
|
+
return _run_single_watch(
|
|
949
|
+
args,
|
|
950
|
+
adapter=adapter,
|
|
951
|
+
active_notifier=active_notifier,
|
|
952
|
+
state=state,
|
|
953
|
+
state_path=state_path,
|
|
954
|
+
sleeper=sleeper,
|
|
955
|
+
clock=clock,
|
|
956
|
+
random_func=random_func,
|
|
957
|
+
resolved_env=resolved_env,
|
|
958
|
+
)
|
|
959
|
+
finally:
|
|
960
|
+
lock.release()
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
def _run_single_watch(
|
|
964
|
+
args,
|
|
965
|
+
*,
|
|
966
|
+
adapter=None,
|
|
967
|
+
active_notifier=None,
|
|
968
|
+
state=None,
|
|
969
|
+
state_path=None,
|
|
970
|
+
sleeper=None,
|
|
971
|
+
clock=None,
|
|
972
|
+
random_func=None,
|
|
973
|
+
resolved_env=None,
|
|
974
|
+
) -> int:
|
|
975
|
+
active_state = state if state is not None else WatchState.load(state_path)
|
|
976
|
+
fetch_offers = None
|
|
977
|
+
reset_prefixes = ()
|
|
978
|
+
credential_hint = ""
|
|
979
|
+
|
|
980
|
+
try:
|
|
981
|
+
if args.platform != "gpushare" and any(
|
|
982
|
+
value is not None
|
|
983
|
+
for value in (
|
|
984
|
+
args.max_price,
|
|
985
|
+
args.min_vram,
|
|
986
|
+
args.min_ram,
|
|
987
|
+
args.min_cuda,
|
|
988
|
+
)
|
|
989
|
+
):
|
|
990
|
+
print(
|
|
991
|
+
"错误: --max-price/--min-vram/--min-ram/--min-cuda "
|
|
992
|
+
"仅 gpushare 支持",
|
|
993
|
+
file=sys.stderr,
|
|
994
|
+
)
|
|
995
|
+
return 2
|
|
996
|
+
|
|
997
|
+
if args.platform == "mornai":
|
|
998
|
+
if args.scope is not None:
|
|
999
|
+
print(
|
|
1000
|
+
"错误: MornAI 不支持 --scope",
|
|
1001
|
+
file=sys.stderr,
|
|
1002
|
+
)
|
|
1003
|
+
return 2
|
|
1004
|
+
if args.region:
|
|
1005
|
+
print(
|
|
1006
|
+
"错误: MornAI 不支持 --region",
|
|
1007
|
+
file=sys.stderr,
|
|
1008
|
+
)
|
|
1009
|
+
return 2
|
|
1010
|
+
if args.token_file:
|
|
1011
|
+
print(
|
|
1012
|
+
"错误: MornAI 不支持 --token-file",
|
|
1013
|
+
file=sys.stderr,
|
|
1014
|
+
)
|
|
1015
|
+
return 2
|
|
1016
|
+
active = adapter or MornAIAdapter()
|
|
1017
|
+
kinds = (
|
|
1018
|
+
("cloud", "container")
|
|
1019
|
+
if args.kind == "both"
|
|
1020
|
+
else (args.kind or "cloud",)
|
|
1021
|
+
)
|
|
1022
|
+
elif args.platform == "autodl":
|
|
1023
|
+
if args.kind is not None:
|
|
1024
|
+
print(
|
|
1025
|
+
"错误: AutoDL 不支持 --kind,请用 "
|
|
1026
|
+
"--scope/--region/--gpu",
|
|
1027
|
+
file=sys.stderr,
|
|
1028
|
+
)
|
|
1029
|
+
return 2
|
|
1030
|
+
token_file = (
|
|
1031
|
+
Path(args.token_file).expanduser()
|
|
1032
|
+
if args.token_file
|
|
1033
|
+
else None
|
|
1034
|
+
)
|
|
1035
|
+
credential_hint = _credential_hint(
|
|
1036
|
+
"autodl", token_file=token_file
|
|
1037
|
+
)
|
|
1038
|
+
active = adapter or AutoDLAdapter(
|
|
1039
|
+
token_file=token_file,
|
|
1040
|
+
env=resolved_env,
|
|
1041
|
+
)
|
|
1042
|
+
if hasattr(active, "require_token"):
|
|
1043
|
+
active.require_token()
|
|
1044
|
+
|
|
1045
|
+
scope = args.scope or "region"
|
|
1046
|
+
if scope == "region":
|
|
1047
|
+
kinds = ("region",)
|
|
1048
|
+
if args.region:
|
|
1049
|
+
known_regions = active.list_regions()
|
|
1050
|
+
names = {
|
|
1051
|
+
region.region_name
|
|
1052
|
+
for region in known_regions
|
|
1053
|
+
if any(
|
|
1054
|
+
pattern
|
|
1055
|
+
in (
|
|
1056
|
+
region.region_name,
|
|
1057
|
+
region.data_center,
|
|
1058
|
+
region.region_sign,
|
|
1059
|
+
)
|
|
1060
|
+
for pattern in args.region
|
|
1061
|
+
)
|
|
1062
|
+
}
|
|
1063
|
+
if not names:
|
|
1064
|
+
print(
|
|
1065
|
+
"错误: 未找到匹配地区: "
|
|
1066
|
+
+ ", ".join(args.region),
|
|
1067
|
+
file=sys.stderr,
|
|
1068
|
+
)
|
|
1069
|
+
return 2
|
|
1070
|
+
reset_prefixes = tuple(
|
|
1071
|
+
f"autodl|region|{name}|" for name in names
|
|
1072
|
+
)
|
|
1073
|
+
else:
|
|
1074
|
+
reset_prefixes = ("autodl|region|",)
|
|
1075
|
+
fetch_offers = lambda kind: active.list_offers(
|
|
1076
|
+
kind="region",
|
|
1077
|
+
regions=tuple(args.region),
|
|
1078
|
+
models=tuple(args.gpu),
|
|
1079
|
+
)
|
|
1080
|
+
else:
|
|
1081
|
+
if len(args.region) != 1:
|
|
1082
|
+
print(
|
|
1083
|
+
"错误: 机器级监控需要恰好一个 --region",
|
|
1084
|
+
file=sys.stderr,
|
|
1085
|
+
)
|
|
1086
|
+
return 2
|
|
1087
|
+
if len(args.gpu) != 1:
|
|
1088
|
+
print(
|
|
1089
|
+
"错误: 机器级监控需要恰好一个 --gpu",
|
|
1090
|
+
file=sys.stderr,
|
|
1091
|
+
)
|
|
1092
|
+
return 2
|
|
1093
|
+
models = active.list_models()
|
|
1094
|
+
if args.gpu[0] not in {item.name for item in models}:
|
|
1095
|
+
print(
|
|
1096
|
+
f"错误: AutoDL 型号不存在: {args.gpu[0]},"
|
|
1097
|
+
"请用 autodl models 查看",
|
|
1098
|
+
file=sys.stderr,
|
|
1099
|
+
)
|
|
1100
|
+
return 2
|
|
1101
|
+
known_regions = active.list_regions()
|
|
1102
|
+
matched_regions = [
|
|
1103
|
+
region
|
|
1104
|
+
for region in known_regions
|
|
1105
|
+
if args.region[0]
|
|
1106
|
+
in (
|
|
1107
|
+
region.region_name,
|
|
1108
|
+
region.data_center,
|
|
1109
|
+
region.region_sign,
|
|
1110
|
+
)
|
|
1111
|
+
]
|
|
1112
|
+
if not matched_regions:
|
|
1113
|
+
print(
|
|
1114
|
+
f"错误: 未找到匹配地区: {args.region[0]}",
|
|
1115
|
+
file=sys.stderr,
|
|
1116
|
+
)
|
|
1117
|
+
return 2
|
|
1118
|
+
region_name = matched_regions[0].region_name
|
|
1119
|
+
reset_prefixes = (
|
|
1120
|
+
f"autodl|machine|{region_name}|{args.gpu[0]}|",
|
|
1121
|
+
)
|
|
1122
|
+
kinds = ("machine",)
|
|
1123
|
+
fetch_offers = lambda kind: active.list_machines(
|
|
1124
|
+
region=args.region[0],
|
|
1125
|
+
gpu_model=args.gpu[0],
|
|
1126
|
+
min_idle=args.min_free,
|
|
1127
|
+
)
|
|
1128
|
+
elif args.platform == "gpushare":
|
|
1129
|
+
if args.kind is not None:
|
|
1130
|
+
print(
|
|
1131
|
+
"错误: gpushare 不支持 --kind",
|
|
1132
|
+
file=sys.stderr,
|
|
1133
|
+
)
|
|
1134
|
+
return 2
|
|
1135
|
+
if args.scope is not None and args.scope != "machine":
|
|
1136
|
+
print(
|
|
1137
|
+
"错误: gpushare 只支持 --scope machine",
|
|
1138
|
+
file=sys.stderr,
|
|
1139
|
+
)
|
|
1140
|
+
return 2
|
|
1141
|
+
if len(args.gpu) > 1:
|
|
1142
|
+
print(
|
|
1143
|
+
"错误: gpushare 最多一个 --gpu",
|
|
1144
|
+
file=sys.stderr,
|
|
1145
|
+
)
|
|
1146
|
+
return 2
|
|
1147
|
+
if len(args.region) > 1:
|
|
1148
|
+
print(
|
|
1149
|
+
"错误: gpushare 最多一个 --region",
|
|
1150
|
+
file=sys.stderr,
|
|
1151
|
+
)
|
|
1152
|
+
return 2
|
|
1153
|
+
for name, value in (
|
|
1154
|
+
("max-price", args.max_price),
|
|
1155
|
+
("min-vram", args.min_vram),
|
|
1156
|
+
("min-ram", args.min_ram),
|
|
1157
|
+
):
|
|
1158
|
+
if value is not None and value < 0:
|
|
1159
|
+
print(
|
|
1160
|
+
f"错误: --{name} 不能为负数",
|
|
1161
|
+
file=sys.stderr,
|
|
1162
|
+
)
|
|
1163
|
+
return 2
|
|
1164
|
+
if args.min_cuda is not None and not is_valid_cuda_version(
|
|
1165
|
+
args.min_cuda
|
|
1166
|
+
):
|
|
1167
|
+
print(
|
|
1168
|
+
"错误: --min-cuda 不是合法版本号",
|
|
1169
|
+
file=sys.stderr,
|
|
1170
|
+
)
|
|
1171
|
+
return 2
|
|
1172
|
+
|
|
1173
|
+
token_file = (
|
|
1174
|
+
Path(args.token_file).expanduser()
|
|
1175
|
+
if args.token_file
|
|
1176
|
+
else None
|
|
1177
|
+
)
|
|
1178
|
+
credential_hint = _credential_hint(
|
|
1179
|
+
"gpushare", token_file=token_file
|
|
1180
|
+
)
|
|
1181
|
+
active = adapter or GpuShareAdapter(
|
|
1182
|
+
token_file=token_file,
|
|
1183
|
+
env=resolved_env,
|
|
1184
|
+
)
|
|
1185
|
+
if hasattr(active, "require_token"):
|
|
1186
|
+
active.require_token()
|
|
1187
|
+
|
|
1188
|
+
known_models = active.list_models()
|
|
1189
|
+
known_regions = active.list_regions()
|
|
1190
|
+
model_names = [
|
|
1191
|
+
item["name"]
|
|
1192
|
+
for item in known_models
|
|
1193
|
+
if not args.gpu
|
|
1194
|
+
or args.gpu[0].casefold() in item["name"].casefold()
|
|
1195
|
+
]
|
|
1196
|
+
region_names = [
|
|
1197
|
+
item["name"]
|
|
1198
|
+
for item in known_regions
|
|
1199
|
+
if not args.region
|
|
1200
|
+
or args.region[0].casefold() in item["name"].casefold()
|
|
1201
|
+
]
|
|
1202
|
+
if args.gpu and not model_names:
|
|
1203
|
+
print(
|
|
1204
|
+
f"错误: 恒源云型号不存在: {args.gpu[0]}",
|
|
1205
|
+
file=sys.stderr,
|
|
1206
|
+
)
|
|
1207
|
+
return 2
|
|
1208
|
+
if args.region and not region_names:
|
|
1209
|
+
print(
|
|
1210
|
+
f"错误: 恒源云地区不存在: {args.region[0]}",
|
|
1211
|
+
file=sys.stderr,
|
|
1212
|
+
)
|
|
1213
|
+
return 2
|
|
1214
|
+
|
|
1215
|
+
if not args.gpu and not args.region:
|
|
1216
|
+
reset_prefixes = ("gpushare|machine|",)
|
|
1217
|
+
elif args.region and not args.gpu:
|
|
1218
|
+
reset_prefixes = tuple(
|
|
1219
|
+
f"gpushare|machine|{name}|"
|
|
1220
|
+
for name in region_names
|
|
1221
|
+
)
|
|
1222
|
+
else:
|
|
1223
|
+
reset_prefixes = tuple(
|
|
1224
|
+
f"gpushare|machine|{region}|{model}|"
|
|
1225
|
+
for region in region_names
|
|
1226
|
+
for model in model_names
|
|
1227
|
+
)
|
|
1228
|
+
|
|
1229
|
+
kinds = ("machine",)
|
|
1230
|
+
fetch_offers = lambda kind: active.list_machines(
|
|
1231
|
+
gpu_model=args.gpu[0] if args.gpu else None,
|
|
1232
|
+
region=args.region[0] if args.region else None,
|
|
1233
|
+
min_free=args.min_free,
|
|
1234
|
+
max_price=args.max_price,
|
|
1235
|
+
min_vram_gb=args.min_vram,
|
|
1236
|
+
min_ram_gb=args.min_ram,
|
|
1237
|
+
min_cuda=args.min_cuda,
|
|
1238
|
+
)
|
|
1239
|
+
else:
|
|
1240
|
+
return 2
|
|
1241
|
+
except AuthRequiredError as exc:
|
|
1242
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1243
|
+
print(
|
|
1244
|
+
"提醒: "
|
|
1245
|
+
+ (
|
|
1246
|
+
credential_hint
|
|
1247
|
+
or _credential_hint(
|
|
1248
|
+
args.platform, token_file=args.token_file
|
|
1249
|
+
)
|
|
1250
|
+
),
|
|
1251
|
+
file=sys.stderr,
|
|
1252
|
+
)
|
|
1253
|
+
return 2
|
|
1254
|
+
except AuthFailedError as exc:
|
|
1255
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1256
|
+
print(
|
|
1257
|
+
"提醒: "
|
|
1258
|
+
+ (
|
|
1259
|
+
credential_hint
|
|
1260
|
+
or _credential_hint(
|
|
1261
|
+
args.platform, token_file=args.token_file
|
|
1262
|
+
)
|
|
1263
|
+
),
|
|
1264
|
+
file=sys.stderr,
|
|
1265
|
+
)
|
|
1266
|
+
return 3
|
|
1267
|
+
except AdapterError as exc:
|
|
1268
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1269
|
+
return 3
|
|
1270
|
+
|
|
1271
|
+
options = WatchOptions(
|
|
1272
|
+
platform=args.platform,
|
|
1273
|
+
kinds=kinds,
|
|
1274
|
+
gpu_patterns=tuple(args.gpu),
|
|
1275
|
+
min_free=args.min_free,
|
|
1276
|
+
interval=args.interval,
|
|
1277
|
+
jitter=args.jitter,
|
|
1278
|
+
once=args.once,
|
|
1279
|
+
quiet=args.quiet or args.json,
|
|
1280
|
+
state_path=state_path,
|
|
1281
|
+
json_output=args.json,
|
|
1282
|
+
reset_prefixes=reset_prefixes,
|
|
1283
|
+
credential_hint=credential_hint,
|
|
1284
|
+
)
|
|
1285
|
+
|
|
1286
|
+
try:
|
|
1287
|
+
return run_watch(
|
|
1288
|
+
active,
|
|
1289
|
+
active_notifier,
|
|
1290
|
+
active_state,
|
|
1291
|
+
options,
|
|
1292
|
+
fetch_offers=fetch_offers,
|
|
1293
|
+
sleeper=sleeper,
|
|
1294
|
+
clock=clock,
|
|
1295
|
+
random_func=random_func,
|
|
1296
|
+
)
|
|
1297
|
+
except ValueError as exc:
|
|
1298
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1299
|
+
return 2
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
def _run_autodl(args, *, adapter=None, env=None) -> int:
|
|
1303
|
+
resolved_env = os.environ if env is None else env
|
|
1304
|
+
token_file = (
|
|
1305
|
+
Path(args.token_file).expanduser()
|
|
1306
|
+
if getattr(args, "token_file", None)
|
|
1307
|
+
else None
|
|
1308
|
+
)
|
|
1309
|
+
active = adapter or AutoDLAdapter(
|
|
1310
|
+
token_file=token_file,
|
|
1311
|
+
env=resolved_env,
|
|
1312
|
+
)
|
|
1313
|
+
command = args.autodl_command
|
|
1314
|
+
|
|
1315
|
+
if command in ("images", "buy", "instances", "orders"):
|
|
1316
|
+
return _run_autodl_order(
|
|
1317
|
+
args, active=active, env=resolved_env
|
|
1318
|
+
)
|
|
1319
|
+
|
|
1320
|
+
try:
|
|
1321
|
+
if command == "models":
|
|
1322
|
+
models = active.list_models()
|
|
1323
|
+
if args.json:
|
|
1324
|
+
print(
|
|
1325
|
+
json.dumps(
|
|
1326
|
+
{
|
|
1327
|
+
"platform": "autodl",
|
|
1328
|
+
"models": [asdict(item) for item in models],
|
|
1329
|
+
},
|
|
1330
|
+
ensure_ascii=False,
|
|
1331
|
+
)
|
|
1332
|
+
)
|
|
1333
|
+
else:
|
|
1334
|
+
_print_autodl_models(models)
|
|
1335
|
+
elif command == "regions":
|
|
1336
|
+
regions = active.list_regions()
|
|
1337
|
+
if args.json:
|
|
1338
|
+
print(
|
|
1339
|
+
json.dumps(
|
|
1340
|
+
{
|
|
1341
|
+
"platform": "autodl",
|
|
1342
|
+
"regions": [
|
|
1343
|
+
asdict(item) for item in regions
|
|
1344
|
+
],
|
|
1345
|
+
},
|
|
1346
|
+
ensure_ascii=False,
|
|
1347
|
+
)
|
|
1348
|
+
)
|
|
1349
|
+
else:
|
|
1350
|
+
_print_autodl_regions(regions)
|
|
1351
|
+
elif command == "offers":
|
|
1352
|
+
if args.min_free < 0:
|
|
1353
|
+
print(
|
|
1354
|
+
"错误: --min-free 不能为负数",
|
|
1355
|
+
file=sys.stderr,
|
|
1356
|
+
)
|
|
1357
|
+
return 2
|
|
1358
|
+
min_free = 0 if args.all else args.min_free
|
|
1359
|
+
offers = active.list_offers(
|
|
1360
|
+
kind="region",
|
|
1361
|
+
regions=args.region,
|
|
1362
|
+
models=args.gpu,
|
|
1363
|
+
)
|
|
1364
|
+
offers = filter_offers(offers, args.gpu, min_free)
|
|
1365
|
+
if args.json:
|
|
1366
|
+
print(
|
|
1367
|
+
json.dumps(
|
|
1368
|
+
{
|
|
1369
|
+
"platform": "autodl",
|
|
1370
|
+
"kind": "region",
|
|
1371
|
+
"offers": [
|
|
1372
|
+
offer.to_dict() for offer in offers
|
|
1373
|
+
],
|
|
1374
|
+
},
|
|
1375
|
+
ensure_ascii=False,
|
|
1376
|
+
)
|
|
1377
|
+
)
|
|
1378
|
+
else:
|
|
1379
|
+
_print_autodl_offers(offers)
|
|
1380
|
+
elif command == "machines":
|
|
1381
|
+
if args.min_idle < 1:
|
|
1382
|
+
print(
|
|
1383
|
+
"错误: --min-idle 不能小于 1",
|
|
1384
|
+
file=sys.stderr,
|
|
1385
|
+
)
|
|
1386
|
+
return 2
|
|
1387
|
+
offers = active.list_machines(
|
|
1388
|
+
region=args.region,
|
|
1389
|
+
gpu_model=args.gpu,
|
|
1390
|
+
min_idle=args.min_idle,
|
|
1391
|
+
)
|
|
1392
|
+
if args.json:
|
|
1393
|
+
print(
|
|
1394
|
+
json.dumps(
|
|
1395
|
+
{
|
|
1396
|
+
"platform": "autodl",
|
|
1397
|
+
"kind": "machine",
|
|
1398
|
+
"offers": [
|
|
1399
|
+
offer.to_dict() for offer in offers
|
|
1400
|
+
],
|
|
1401
|
+
},
|
|
1402
|
+
ensure_ascii=False,
|
|
1403
|
+
)
|
|
1404
|
+
)
|
|
1405
|
+
else:
|
|
1406
|
+
_print_autodl_machines(offers)
|
|
1407
|
+
else:
|
|
1408
|
+
return 2
|
|
1409
|
+
except AuthRequiredError as exc:
|
|
1410
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1411
|
+
print(
|
|
1412
|
+
f"提醒: {_credential_hint('autodl', token_file=token_file)}",
|
|
1413
|
+
file=sys.stderr,
|
|
1414
|
+
)
|
|
1415
|
+
return 2
|
|
1416
|
+
except AuthFailedError as exc:
|
|
1417
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1418
|
+
print(
|
|
1419
|
+
f"提醒: {_credential_hint('autodl', token_file=token_file)}",
|
|
1420
|
+
file=sys.stderr,
|
|
1421
|
+
)
|
|
1422
|
+
return 3
|
|
1423
|
+
except AdapterError as exc:
|
|
1424
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1425
|
+
return 3
|
|
1426
|
+
except ValueError as exc:
|
|
1427
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1428
|
+
return 2
|
|
1429
|
+
return 0
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
def _run_matpool(args, *, adapter=None) -> int:
|
|
1433
|
+
active = adapter or MatpoolAdapter()
|
|
1434
|
+
command = args.matpool_command
|
|
1435
|
+
|
|
1436
|
+
try:
|
|
1437
|
+
if command == "machines":
|
|
1438
|
+
if args.min_free < 0:
|
|
1439
|
+
print(
|
|
1440
|
+
"错误: --min-free 不能为负数",
|
|
1441
|
+
file=sys.stderr,
|
|
1442
|
+
)
|
|
1443
|
+
return 2
|
|
1444
|
+
for name, value in (
|
|
1445
|
+
("max-price", args.max_price),
|
|
1446
|
+
("min-vram", args.min_vram),
|
|
1447
|
+
("min-ram", args.min_ram),
|
|
1448
|
+
):
|
|
1449
|
+
if value is not None and value < 0:
|
|
1450
|
+
print(
|
|
1451
|
+
f"错误: --{name} 不能为负数",
|
|
1452
|
+
file=sys.stderr,
|
|
1453
|
+
)
|
|
1454
|
+
return 2
|
|
1455
|
+
offers = active.list_machines(
|
|
1456
|
+
gpu_model=args.gpu,
|
|
1457
|
+
region=args.region,
|
|
1458
|
+
min_free=args.min_free,
|
|
1459
|
+
max_price=args.max_price,
|
|
1460
|
+
min_vram_gb=args.min_vram,
|
|
1461
|
+
min_ram_gb=args.min_ram,
|
|
1462
|
+
)
|
|
1463
|
+
if args.json:
|
|
1464
|
+
print(
|
|
1465
|
+
json.dumps(
|
|
1466
|
+
{
|
|
1467
|
+
"platform": "matpool",
|
|
1468
|
+
"kind": "machine",
|
|
1469
|
+
"offers": [
|
|
1470
|
+
offer.to_dict() for offer in offers
|
|
1471
|
+
],
|
|
1472
|
+
},
|
|
1473
|
+
ensure_ascii=False,
|
|
1474
|
+
)
|
|
1475
|
+
)
|
|
1476
|
+
else:
|
|
1477
|
+
_print_matpool_machines(offers)
|
|
1478
|
+
elif command == "models":
|
|
1479
|
+
models = active.list_models()
|
|
1480
|
+
if args.json:
|
|
1481
|
+
print(
|
|
1482
|
+
json.dumps(
|
|
1483
|
+
{"platform": "matpool", "models": models},
|
|
1484
|
+
ensure_ascii=False,
|
|
1485
|
+
)
|
|
1486
|
+
)
|
|
1487
|
+
else:
|
|
1488
|
+
_print_matpool_models(models)
|
|
1489
|
+
elif command == "regions":
|
|
1490
|
+
regions = active.list_regions()
|
|
1491
|
+
if args.json:
|
|
1492
|
+
print(
|
|
1493
|
+
json.dumps(
|
|
1494
|
+
{"platform": "matpool", "regions": regions},
|
|
1495
|
+
ensure_ascii=False,
|
|
1496
|
+
)
|
|
1497
|
+
)
|
|
1498
|
+
else:
|
|
1499
|
+
_print_matpool_regions(regions)
|
|
1500
|
+
else:
|
|
1501
|
+
return 2
|
|
1502
|
+
except AdapterError as exc:
|
|
1503
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1504
|
+
return 3
|
|
1505
|
+
except ValueError as exc:
|
|
1506
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1507
|
+
return 2
|
|
1508
|
+
return 0
|
|
1509
|
+
|
|
1510
|
+
|
|
1511
|
+
def _print_matpool_machines(offers) -> None:
|
|
1512
|
+
print(
|
|
1513
|
+
f"{'MACHINE':<10} {'GPU':<20} {'REGION':<10} "
|
|
1514
|
+
f"{'FREE/TOTAL':>10} {'PRICE':>8} {'VRAM':>6} "
|
|
1515
|
+
f"{'RAM/GPU':>8} {'CPU':>4}"
|
|
1516
|
+
)
|
|
1517
|
+
print(
|
|
1518
|
+
f"{'-' * 10} {'-' * 20} {'-' * 10} {'-' * 10} "
|
|
1519
|
+
f"{'-' * 8} {'-' * 6} {'-' * 8} {'-' * 4}"
|
|
1520
|
+
)
|
|
1521
|
+
for offer in offers:
|
|
1522
|
+
extra = offer.extra or {}
|
|
1523
|
+
price = (
|
|
1524
|
+
f"¥{offer.price_per_hour:.2f}"
|
|
1525
|
+
if offer.price_per_hour is not None
|
|
1526
|
+
else "-"
|
|
1527
|
+
)
|
|
1528
|
+
total = offer.total if offer.total is not None else "?"
|
|
1529
|
+
vram = (
|
|
1530
|
+
f"{offer.gpu_memory_gb}G"
|
|
1531
|
+
if offer.gpu_memory_gb is not None
|
|
1532
|
+
else "-"
|
|
1533
|
+
)
|
|
1534
|
+
ram = extra.get("ram_per_gpu_gb")
|
|
1535
|
+
ram_text = f"{ram}G" if ram is not None else "-"
|
|
1536
|
+
print(
|
|
1537
|
+
f"{(offer.machine_id or '-'):<10} {offer.gpu_model:<20} "
|
|
1538
|
+
f"{(offer.region or '-'):<10} "
|
|
1539
|
+
f"{f'{offer.free}/{total}':>10} {price:>8} "
|
|
1540
|
+
f"{vram:>6} {ram_text:>8} "
|
|
1541
|
+
f"{str(extra.get('cpu_cores', '-')):>4}"
|
|
1542
|
+
)
|
|
1543
|
+
|
|
1544
|
+
|
|
1545
|
+
def _print_matpool_models(models) -> None:
|
|
1546
|
+
print(f"{'MODEL':<24} {'COUNT':>6} {'MIN_PRICE':>10}")
|
|
1547
|
+
print(f"{'-' * 24} {'-' * 6} {'-' * 10}")
|
|
1548
|
+
for item in models:
|
|
1549
|
+
price = (
|
|
1550
|
+
f"¥{item['min_price']:.2f}"
|
|
1551
|
+
if item.get("min_price") is not None
|
|
1552
|
+
else "-"
|
|
1553
|
+
)
|
|
1554
|
+
print(f"{item['name']:<24} {item['count']:>6} {price:>10}")
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
def _print_matpool_regions(regions) -> None:
|
|
1558
|
+
print(f"{'REGION':<16} {'COUNT':>6}")
|
|
1559
|
+
print(f"{'-' * 16} {'-' * 6}")
|
|
1560
|
+
for item in regions:
|
|
1561
|
+
print(f"{item['name']:<16} {item['count']:>6}")
|
|
1562
|
+
|
|
1563
|
+
|
|
1564
|
+
def _run_gpugeek(args, *, adapter=None) -> int:
|
|
1565
|
+
active = adapter or GPUGeekAdapter()
|
|
1566
|
+
command = args.gpugeek_command
|
|
1567
|
+
|
|
1568
|
+
try:
|
|
1569
|
+
if command == "offers":
|
|
1570
|
+
if args.min_free < 0:
|
|
1571
|
+
print(
|
|
1572
|
+
"错误: --min-free 不能为负数",
|
|
1573
|
+
file=sys.stderr,
|
|
1574
|
+
)
|
|
1575
|
+
return 2
|
|
1576
|
+
for name, value in (
|
|
1577
|
+
("max-price", args.max_price),
|
|
1578
|
+
("min-vram", args.min_vram),
|
|
1579
|
+
("min-ram", args.min_ram),
|
|
1580
|
+
):
|
|
1581
|
+
if value is not None and value < 0:
|
|
1582
|
+
print(
|
|
1583
|
+
f"错误: --{name} 不能为负数",
|
|
1584
|
+
file=sys.stderr,
|
|
1585
|
+
)
|
|
1586
|
+
return 2
|
|
1587
|
+
offers = active.list_offers(
|
|
1588
|
+
gpu_model=args.gpu,
|
|
1589
|
+
region=args.region,
|
|
1590
|
+
min_free=args.min_free,
|
|
1591
|
+
max_price=args.max_price,
|
|
1592
|
+
min_vram_gb=args.min_vram,
|
|
1593
|
+
min_ram_gb=args.min_ram,
|
|
1594
|
+
)
|
|
1595
|
+
if args.json:
|
|
1596
|
+
print(
|
|
1597
|
+
json.dumps(
|
|
1598
|
+
{
|
|
1599
|
+
"platform": "gpugeek",
|
|
1600
|
+
"kind": "cloud",
|
|
1601
|
+
"offers": [
|
|
1602
|
+
offer.to_dict() for offer in offers
|
|
1603
|
+
],
|
|
1604
|
+
},
|
|
1605
|
+
ensure_ascii=False,
|
|
1606
|
+
)
|
|
1607
|
+
)
|
|
1608
|
+
else:
|
|
1609
|
+
_print_gpugeek_offers(offers)
|
|
1610
|
+
elif command == "bms":
|
|
1611
|
+
if args.min_available < 0:
|
|
1612
|
+
print(
|
|
1613
|
+
"错误: --min-available 不能为负数",
|
|
1614
|
+
file=sys.stderr,
|
|
1615
|
+
)
|
|
1616
|
+
return 2
|
|
1617
|
+
for name, value in (
|
|
1618
|
+
("max-price", args.max_price),
|
|
1619
|
+
("min-vram", args.min_vram),
|
|
1620
|
+
("min-ram", args.min_ram),
|
|
1621
|
+
):
|
|
1622
|
+
if value is not None and value < 0:
|
|
1623
|
+
print(
|
|
1624
|
+
f"错误: --{name} 不能为负数",
|
|
1625
|
+
file=sys.stderr,
|
|
1626
|
+
)
|
|
1627
|
+
return 2
|
|
1628
|
+
rows = active.list_bms(
|
|
1629
|
+
gpu_model=args.gpu,
|
|
1630
|
+
region=args.region,
|
|
1631
|
+
min_available=args.min_available,
|
|
1632
|
+
max_price=args.max_price,
|
|
1633
|
+
min_vram_gb=args.min_vram,
|
|
1634
|
+
min_ram_gb=args.min_ram,
|
|
1635
|
+
)
|
|
1636
|
+
if args.json:
|
|
1637
|
+
print(
|
|
1638
|
+
json.dumps(
|
|
1639
|
+
{
|
|
1640
|
+
"platform": "gpugeek",
|
|
1641
|
+
"kind": "bms",
|
|
1642
|
+
"machines": rows,
|
|
1643
|
+
},
|
|
1644
|
+
ensure_ascii=False,
|
|
1645
|
+
)
|
|
1646
|
+
)
|
|
1647
|
+
else:
|
|
1648
|
+
_print_gpugeek_bms(rows)
|
|
1649
|
+
elif command == "models":
|
|
1650
|
+
models = active.list_models(kind=args.kind)
|
|
1651
|
+
if args.json:
|
|
1652
|
+
print(
|
|
1653
|
+
json.dumps(
|
|
1654
|
+
{
|
|
1655
|
+
"platform": "gpugeek",
|
|
1656
|
+
"kind": args.kind,
|
|
1657
|
+
"models": models,
|
|
1658
|
+
},
|
|
1659
|
+
ensure_ascii=False,
|
|
1660
|
+
)
|
|
1661
|
+
)
|
|
1662
|
+
else:
|
|
1663
|
+
_print_gpugeek_models(models)
|
|
1664
|
+
elif command == "regions":
|
|
1665
|
+
regions = active.list_regions(kind=args.kind)
|
|
1666
|
+
if args.json:
|
|
1667
|
+
print(
|
|
1668
|
+
json.dumps(
|
|
1669
|
+
{
|
|
1670
|
+
"platform": "gpugeek",
|
|
1671
|
+
"kind": args.kind,
|
|
1672
|
+
"regions": regions,
|
|
1673
|
+
},
|
|
1674
|
+
ensure_ascii=False,
|
|
1675
|
+
)
|
|
1676
|
+
)
|
|
1677
|
+
else:
|
|
1678
|
+
_print_gpugeek_regions(regions)
|
|
1679
|
+
else:
|
|
1680
|
+
return 2
|
|
1681
|
+
except AdapterError as exc:
|
|
1682
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1683
|
+
return 3
|
|
1684
|
+
except ValueError as exc:
|
|
1685
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1686
|
+
return 2
|
|
1687
|
+
return 0
|
|
1688
|
+
|
|
1689
|
+
|
|
1690
|
+
def _print_gpugeek_offers(offers) -> None:
|
|
1691
|
+
print(
|
|
1692
|
+
f"{'GPU':<24} {'REGION':<12} {'FREE/TOTAL':>10} "
|
|
1693
|
+
f"{'PRICE/H':>9} {'VRAM':>6} {'RAM/GPU':>9} {'CPU':>4}"
|
|
1694
|
+
)
|
|
1695
|
+
print(
|
|
1696
|
+
f"{'-' * 24} {'-' * 12} {'-' * 10} {'-' * 9} "
|
|
1697
|
+
f"{'-' * 6} {'-' * 9} {'-' * 4}"
|
|
1698
|
+
)
|
|
1699
|
+
for offer in offers:
|
|
1700
|
+
extra = offer.extra or {}
|
|
1701
|
+
price = (
|
|
1702
|
+
f"¥{offer.price_per_hour:.2f}"
|
|
1703
|
+
if offer.price_per_hour is not None
|
|
1704
|
+
else "-"
|
|
1705
|
+
)
|
|
1706
|
+
total = offer.total if offer.total is not None else "?"
|
|
1707
|
+
vram = (
|
|
1708
|
+
f"{offer.gpu_memory_gb}G"
|
|
1709
|
+
if offer.gpu_memory_gb is not None
|
|
1710
|
+
else "-"
|
|
1711
|
+
)
|
|
1712
|
+
memory_gb = extra.get("memory_gb")
|
|
1713
|
+
ram = (
|
|
1714
|
+
f"{memory_gb / offer.total:.1f}G"
|
|
1715
|
+
if memory_gb is not None
|
|
1716
|
+
and offer.total is not None
|
|
1717
|
+
and offer.total > 0
|
|
1718
|
+
else "-"
|
|
1719
|
+
)
|
|
1720
|
+
print(
|
|
1721
|
+
f"{offer.gpu_model:<24} {(offer.region or '-'):<12} "
|
|
1722
|
+
f"{f'{offer.free}/{total}':>10} {price:>9} "
|
|
1723
|
+
f"{vram:>6} {ram:>9} "
|
|
1724
|
+
f"{str(extra.get('cpu_core_num', '-')):>4}"
|
|
1725
|
+
)
|
|
1726
|
+
|
|
1727
|
+
|
|
1728
|
+
def _print_gpugeek_bms(rows) -> None:
|
|
1729
|
+
print(
|
|
1730
|
+
f"{'GPU':<24} {'REGION':<12} {'AVAILABLE':>9} "
|
|
1731
|
+
f"{'PRICE/MONTH':>12} {'VRAM':>6} {'RAM':>7} {'CPU':>4}"
|
|
1732
|
+
)
|
|
1733
|
+
print(
|
|
1734
|
+
f"{'-' * 24} {'-' * 12} {'-' * 9} {'-' * 12} "
|
|
1735
|
+
f"{'-' * 6} {'-' * 7} {'-' * 4}"
|
|
1736
|
+
)
|
|
1737
|
+
for row in rows:
|
|
1738
|
+
price = (
|
|
1739
|
+
f"¥{row['price_per_month']:.0f}"
|
|
1740
|
+
if row["price_per_month"] is not None
|
|
1741
|
+
else "-"
|
|
1742
|
+
)
|
|
1743
|
+
vram = (
|
|
1744
|
+
f"{row['vram_gb']:.0f}G"
|
|
1745
|
+
if row["vram_gb"] is not None
|
|
1746
|
+
else "-"
|
|
1747
|
+
)
|
|
1748
|
+
ram = (
|
|
1749
|
+
f"{row['memory_gb']:.0f}G"
|
|
1750
|
+
if row["memory_gb"] is not None
|
|
1751
|
+
else "-"
|
|
1752
|
+
)
|
|
1753
|
+
cpu = row["cpu_cores"] if row["cpu_cores"] is not None else "-"
|
|
1754
|
+
print(
|
|
1755
|
+
f"{row['gpu_name'] + ' x' + str(row['gpu_num']):<24} "
|
|
1756
|
+
f"{row['region']:<12} {row['available']:>9} "
|
|
1757
|
+
f"{price:>12} {vram:>6} {ram:>7} {str(cpu):>4}"
|
|
1758
|
+
)
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
def _print_gpugeek_models(models) -> None:
|
|
1762
|
+
print(f"{'MODEL':<24} {'COUNT':>6} {'MIN_PRICE':>10}")
|
|
1763
|
+
print(f"{'-' * 24} {'-' * 6} {'-' * 10}")
|
|
1764
|
+
for item in models:
|
|
1765
|
+
price = (
|
|
1766
|
+
f"¥{item['min_price']:.2f}"
|
|
1767
|
+
if item.get("min_price") is not None
|
|
1768
|
+
else "-"
|
|
1769
|
+
)
|
|
1770
|
+
print(f"{item['name']:<24} {item['count']:>6} {price:>10}")
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
def _print_gpugeek_regions(regions) -> None:
|
|
1774
|
+
print(f"{'REGION':<16} {'COUNT':>6}")
|
|
1775
|
+
print(f"{'-' * 16} {'-' * 6}")
|
|
1776
|
+
for item in regions:
|
|
1777
|
+
print(f"{item['name']:<16} {item['count']:>6}")
|
|
1778
|
+
|
|
1779
|
+
|
|
1780
|
+
def _run_tui(args, *, adapters=None, env=None) -> int:
|
|
1781
|
+
path = _display_config_path(args.config)
|
|
1782
|
+
try:
|
|
1783
|
+
config = load_config(path)
|
|
1784
|
+
except ConfigError as exc:
|
|
1785
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1786
|
+
return 2
|
|
1787
|
+
from cloudgpu.tui import TuiView, run_tui
|
|
1788
|
+
|
|
1789
|
+
if args.min_free < 0:
|
|
1790
|
+
print(
|
|
1791
|
+
"错误: --min-free 不能为负数",
|
|
1792
|
+
file=sys.stderr,
|
|
1793
|
+
)
|
|
1794
|
+
return 2
|
|
1795
|
+
if args.max_price is not None and args.max_price < 0:
|
|
1796
|
+
print(
|
|
1797
|
+
"错误: --max-price 不能为负数",
|
|
1798
|
+
file=sys.stderr,
|
|
1799
|
+
)
|
|
1800
|
+
return 2
|
|
1801
|
+
view = TuiView(
|
|
1802
|
+
platform=args.platform,
|
|
1803
|
+
target=args.target,
|
|
1804
|
+
search=args.gpu,
|
|
1805
|
+
min_free=args.min_free,
|
|
1806
|
+
max_price=args.max_price,
|
|
1807
|
+
sort=args.sort,
|
|
1808
|
+
)
|
|
1809
|
+
try:
|
|
1810
|
+
return run_tui(
|
|
1811
|
+
config,
|
|
1812
|
+
config_path=str(path),
|
|
1813
|
+
adapters=adapters,
|
|
1814
|
+
env=env,
|
|
1815
|
+
view=view,
|
|
1816
|
+
)
|
|
1817
|
+
except KeyboardInterrupt:
|
|
1818
|
+
return 0
|
|
1819
|
+
|
|
1820
|
+
|
|
1821
|
+
def _run_web(args, *, adapters=None, env=None) -> int:
|
|
1822
|
+
from cloudgpu.web import _is_loopback, run_web
|
|
1823
|
+
|
|
1824
|
+
if not args.allow_lan and not _is_loopback(args.host):
|
|
1825
|
+
print(
|
|
1826
|
+
"错误: 非 loopback 地址需要 --allow-lan",
|
|
1827
|
+
file=sys.stderr,
|
|
1828
|
+
)
|
|
1829
|
+
return 2
|
|
1830
|
+
path = _display_config_path(args.config)
|
|
1831
|
+
try:
|
|
1832
|
+
config = load_config(path)
|
|
1833
|
+
except ConfigError as exc:
|
|
1834
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1835
|
+
return 2
|
|
1836
|
+
try:
|
|
1837
|
+
return run_web(
|
|
1838
|
+
config,
|
|
1839
|
+
host=args.host,
|
|
1840
|
+
port=args.port,
|
|
1841
|
+
open_browser=not args.no_open,
|
|
1842
|
+
allow_lan=args.allow_lan,
|
|
1843
|
+
adapters=adapters,
|
|
1844
|
+
env=env,
|
|
1845
|
+
)
|
|
1846
|
+
except (OSError, ValueError) as exc:
|
|
1847
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1848
|
+
return 2
|
|
1849
|
+
except KeyboardInterrupt:
|
|
1850
|
+
return 0
|
|
1851
|
+
|
|
1852
|
+
|
|
1853
|
+
def _run_doctor(args, *, adapters=None, env=None) -> int:
|
|
1854
|
+
from cloudgpu.doctor import checks_payload, run_doctor
|
|
1855
|
+
|
|
1856
|
+
path = _display_config_path(args.config)
|
|
1857
|
+
try:
|
|
1858
|
+
config = load_config(path)
|
|
1859
|
+
except ConfigError as exc:
|
|
1860
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1861
|
+
return 2
|
|
1862
|
+
checks = run_doctor(
|
|
1863
|
+
config,
|
|
1864
|
+
env=env,
|
|
1865
|
+
check_api=args.check_api,
|
|
1866
|
+
adapters=adapters,
|
|
1867
|
+
)
|
|
1868
|
+
payload = checks_payload(checks)
|
|
1869
|
+
if args.json:
|
|
1870
|
+
print(json.dumps(payload, ensure_ascii=False))
|
|
1871
|
+
else:
|
|
1872
|
+
for check in checks:
|
|
1873
|
+
label = {
|
|
1874
|
+
"ok": "OK",
|
|
1875
|
+
"warn": "WARN",
|
|
1876
|
+
"fail": "FAIL",
|
|
1877
|
+
}.get(check.status, check.status.upper())
|
|
1878
|
+
print(f"[{label}] {check.name}: {check.detail}")
|
|
1879
|
+
print(
|
|
1880
|
+
f"检查完成: {payload['failures']} 个失败, "
|
|
1881
|
+
f"{payload['warnings']} 个警告"
|
|
1882
|
+
)
|
|
1883
|
+
return 3 if payload["failures"] else 0
|
|
1884
|
+
|
|
1885
|
+
|
|
1886
|
+
def _run_desktop(args) -> int:
|
|
1887
|
+
from cloudgpu.desktop import main as desktop_main
|
|
1888
|
+
|
|
1889
|
+
argv = (
|
|
1890
|
+
["--config", args.config]
|
|
1891
|
+
if args.config
|
|
1892
|
+
else []
|
|
1893
|
+
)
|
|
1894
|
+
try:
|
|
1895
|
+
return desktop_main(argv)
|
|
1896
|
+
except KeyboardInterrupt:
|
|
1897
|
+
return 0
|
|
1898
|
+
|
|
1899
|
+
|
|
1900
|
+
def _display_config_path(value):
|
|
1901
|
+
if value:
|
|
1902
|
+
return Path(value).expanduser()
|
|
1903
|
+
return Path.home() / ".cloudgpu" / "config.json"
|
|
1904
|
+
|
|
1905
|
+
|
|
1906
|
+
def _run_find(args, *, adapters=None, env=None) -> int:
|
|
1907
|
+
try:
|
|
1908
|
+
config = load_config(args.config)
|
|
1909
|
+
except ConfigError as exc:
|
|
1910
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
1911
|
+
return 2
|
|
1912
|
+
|
|
1913
|
+
snapshot = collect_snapshot(
|
|
1914
|
+
config,
|
|
1915
|
+
adapters=adapters,
|
|
1916
|
+
env=env,
|
|
1917
|
+
platforms=tuple(args.platform) or None,
|
|
1918
|
+
)
|
|
1919
|
+
if snapshot.targets_total == 0:
|
|
1920
|
+
print("错误: 没有匹配的 target", file=sys.stderr)
|
|
1921
|
+
return 2
|
|
1922
|
+
|
|
1923
|
+
patterns = [item.casefold() for item in args.gpu if item]
|
|
1924
|
+
if patterns:
|
|
1925
|
+
items = [
|
|
1926
|
+
item
|
|
1927
|
+
for item in snapshot.items
|
|
1928
|
+
if any(
|
|
1929
|
+
pattern
|
|
1930
|
+
in (
|
|
1931
|
+
f"{item.offer.gpu_model} "
|
|
1932
|
+
f"{item.offer.display_name} "
|
|
1933
|
+
f"{item.offer.description}"
|
|
1934
|
+
).casefold()
|
|
1935
|
+
for pattern in patterns
|
|
1936
|
+
)
|
|
1937
|
+
]
|
|
1938
|
+
else:
|
|
1939
|
+
items = list(snapshot.items)
|
|
1940
|
+
|
|
1941
|
+
if args.json:
|
|
1942
|
+
print(
|
|
1943
|
+
json.dumps(
|
|
1944
|
+
{
|
|
1945
|
+
"checked_at": snapshot.checked_at,
|
|
1946
|
+
"targets_ok": snapshot.targets_ok,
|
|
1947
|
+
"targets_total": snapshot.targets_total,
|
|
1948
|
+
"offers": [
|
|
1949
|
+
{
|
|
1950
|
+
**item.offer.to_dict(),
|
|
1951
|
+
"target": item.target,
|
|
1952
|
+
}
|
|
1953
|
+
for item in items
|
|
1954
|
+
],
|
|
1955
|
+
"errors": [
|
|
1956
|
+
{
|
|
1957
|
+
"target": error.target,
|
|
1958
|
+
"platform": error.platform,
|
|
1959
|
+
"error": error.error,
|
|
1960
|
+
}
|
|
1961
|
+
for error in snapshot.errors
|
|
1962
|
+
],
|
|
1963
|
+
},
|
|
1964
|
+
ensure_ascii=False,
|
|
1965
|
+
)
|
|
1966
|
+
)
|
|
1967
|
+
else:
|
|
1968
|
+
for error in snapshot.errors:
|
|
1969
|
+
print(
|
|
1970
|
+
f"错误: {error.target} "
|
|
1971
|
+
f"({error.platform}): {error.error}",
|
|
1972
|
+
file=sys.stderr,
|
|
1973
|
+
)
|
|
1974
|
+
if items:
|
|
1975
|
+
_print_find_table(items)
|
|
1976
|
+
else:
|
|
1977
|
+
_print_find_table([])
|
|
1978
|
+
print("没有符合条件的空闲资源")
|
|
1979
|
+
print(
|
|
1980
|
+
f"共 {len(items)} 条空闲,"
|
|
1981
|
+
f"{snapshot.targets_ok}/{snapshot.targets_total} "
|
|
1982
|
+
"个 target 成功",
|
|
1983
|
+
file=sys.stderr,
|
|
1984
|
+
)
|
|
1985
|
+
|
|
1986
|
+
return 0 if snapshot.targets_ok > 0 else 3
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
def _print_find_table(items) -> None:
|
|
1990
|
+
print(
|
|
1991
|
+
f"{'TARGET':<16} {'PLATFORM':<10} {'KIND':<10} "
|
|
1992
|
+
f"{'GPU':<22} {'REGION':<12} {'FREE/TOTAL':>10} "
|
|
1993
|
+
f"{'PRICE/H':>9} {'VRAM':>6}"
|
|
1994
|
+
)
|
|
1995
|
+
print(
|
|
1996
|
+
f"{'-' * 16} {'-' * 10} {'-' * 10} {'-' * 22} "
|
|
1997
|
+
f"{'-' * 12} {'-' * 10} {'-' * 9} {'-' * 6}"
|
|
1998
|
+
)
|
|
1999
|
+
for item in items:
|
|
2000
|
+
offer = item.offer
|
|
2001
|
+
price = (
|
|
2002
|
+
f"¥{offer.price_per_hour:.2f}"
|
|
2003
|
+
if offer.price_per_hour is not None
|
|
2004
|
+
else "-"
|
|
2005
|
+
)
|
|
2006
|
+
total = offer.total if offer.total is not None else "?"
|
|
2007
|
+
vram = (
|
|
2008
|
+
f"{offer.gpu_memory_gb}G"
|
|
2009
|
+
if offer.gpu_memory_gb is not None
|
|
2010
|
+
else "-"
|
|
2011
|
+
)
|
|
2012
|
+
print(
|
|
2013
|
+
f"{item.target:<16} {offer.platform:<10} "
|
|
2014
|
+
f"{(offer.service_kind or '-'):<10} "
|
|
2015
|
+
f"{offer.gpu_model:<22} {(offer.region or '-'):<12} "
|
|
2016
|
+
f"{f'{offer.free}/{total}':>10} {price:>9} "
|
|
2017
|
+
f"{vram:>6}"
|
|
2018
|
+
)
|
|
2019
|
+
|
|
2020
|
+
|
|
2021
|
+
def _run_gongji(args, *, adapter=None, env=None) -> int:
|
|
2022
|
+
resolved_env = os.environ if env is None else env
|
|
2023
|
+
token_file = (
|
|
2024
|
+
Path(args.token_file).expanduser()
|
|
2025
|
+
if getattr(args, "token_file", None)
|
|
2026
|
+
else None
|
|
2027
|
+
)
|
|
2028
|
+
active = adapter or GongjiAdapter(
|
|
2029
|
+
token_file=token_file,
|
|
2030
|
+
env=resolved_env,
|
|
2031
|
+
)
|
|
2032
|
+
command = args.gongji_command
|
|
2033
|
+
|
|
2034
|
+
if command in (
|
|
2035
|
+
"tasks", "task", "deploy", "pause", "resume",
|
|
2036
|
+
"stop", "ttl",
|
|
2037
|
+
):
|
|
2038
|
+
return _run_gongji_instance(
|
|
2039
|
+
args, active=active, env=resolved_env
|
|
2040
|
+
)
|
|
2041
|
+
|
|
2042
|
+
try:
|
|
2043
|
+
if hasattr(active, "require_token"):
|
|
2044
|
+
active.require_token()
|
|
2045
|
+
|
|
2046
|
+
if command == "resources":
|
|
2047
|
+
if args.min_free < 0:
|
|
2048
|
+
print(
|
|
2049
|
+
"错误: --min-free 不能为负数",
|
|
2050
|
+
file=sys.stderr,
|
|
2051
|
+
)
|
|
2052
|
+
return 2
|
|
2053
|
+
for name, value in (
|
|
2054
|
+
("max-price", args.max_price),
|
|
2055
|
+
("min-vram", args.min_vram),
|
|
2056
|
+
("min-ram", args.min_ram),
|
|
2057
|
+
):
|
|
2058
|
+
if value is not None and value < 0:
|
|
2059
|
+
print(
|
|
2060
|
+
f"错误: --{name} 不能为负数",
|
|
2061
|
+
file=sys.stderr,
|
|
2062
|
+
)
|
|
2063
|
+
return 2
|
|
2064
|
+
offers = active.list_resources(
|
|
2065
|
+
gpu_model=args.gpu,
|
|
2066
|
+
region=args.region,
|
|
2067
|
+
min_free=args.min_free,
|
|
2068
|
+
max_price=args.max_price,
|
|
2069
|
+
min_vram_gb=args.min_vram,
|
|
2070
|
+
min_ram_gb=args.min_ram,
|
|
2071
|
+
)
|
|
2072
|
+
if args.json:
|
|
2073
|
+
print(
|
|
2074
|
+
json.dumps(
|
|
2075
|
+
{
|
|
2076
|
+
"platform": "gongji",
|
|
2077
|
+
"kind": "region",
|
|
2078
|
+
"offers": [
|
|
2079
|
+
offer.to_dict() for offer in offers
|
|
2080
|
+
],
|
|
2081
|
+
},
|
|
2082
|
+
ensure_ascii=False,
|
|
2083
|
+
)
|
|
2084
|
+
)
|
|
2085
|
+
else:
|
|
2086
|
+
_print_gongji_resources(offers)
|
|
2087
|
+
elif command == "models":
|
|
2088
|
+
models = active.list_models()
|
|
2089
|
+
if args.json:
|
|
2090
|
+
print(
|
|
2091
|
+
json.dumps(
|
|
2092
|
+
{"platform": "gongji", "models": models},
|
|
2093
|
+
ensure_ascii=False,
|
|
2094
|
+
)
|
|
2095
|
+
)
|
|
2096
|
+
else:
|
|
2097
|
+
_print_gongji_models(models)
|
|
2098
|
+
elif command == "regions":
|
|
2099
|
+
regions = active.list_regions()
|
|
2100
|
+
if args.json:
|
|
2101
|
+
print(
|
|
2102
|
+
json.dumps(
|
|
2103
|
+
{"platform": "gongji", "regions": regions},
|
|
2104
|
+
ensure_ascii=False,
|
|
2105
|
+
)
|
|
2106
|
+
)
|
|
2107
|
+
else:
|
|
2108
|
+
_print_gongji_regions(regions)
|
|
2109
|
+
else:
|
|
2110
|
+
return 2
|
|
2111
|
+
except AuthRequiredError as exc:
|
|
2112
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2113
|
+
print(
|
|
2114
|
+
f"提醒: {_credential_hint('gongji', token_file=token_file)}",
|
|
2115
|
+
file=sys.stderr,
|
|
2116
|
+
)
|
|
2117
|
+
return 2
|
|
2118
|
+
except AuthFailedError as exc:
|
|
2119
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2120
|
+
print(
|
|
2121
|
+
f"提醒: {_credential_hint('gongji', token_file=token_file)}",
|
|
2122
|
+
file=sys.stderr,
|
|
2123
|
+
)
|
|
2124
|
+
return 3
|
|
2125
|
+
except AdapterError as exc:
|
|
2126
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2127
|
+
return 3
|
|
2128
|
+
except ValueError as exc:
|
|
2129
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2130
|
+
return 2
|
|
2131
|
+
return 0
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
def _run_gongji_instance(args, *, active, env) -> int:
|
|
2135
|
+
pending = GongjiPendingStore(
|
|
2136
|
+
env.get("CLOUDGPU_GONGJI_PENDING_DB")
|
|
2137
|
+
)
|
|
2138
|
+
ttl = GongjiTtlStore(
|
|
2139
|
+
env.get("CLOUDGPU_GONGJI_TTL_DB")
|
|
2140
|
+
)
|
|
2141
|
+
command = args.gongji_command
|
|
2142
|
+
|
|
2143
|
+
try:
|
|
2144
|
+
if command != "ttl":
|
|
2145
|
+
lazy_sweep(active, ttl)
|
|
2146
|
+
|
|
2147
|
+
if command == "tasks":
|
|
2148
|
+
if args.resolve is not None:
|
|
2149
|
+
if not args.not_created:
|
|
2150
|
+
print(
|
|
2151
|
+
"错误: --resolve 需要 --not-created",
|
|
2152
|
+
file=sys.stderr,
|
|
2153
|
+
)
|
|
2154
|
+
return 2
|
|
2155
|
+
pending.resolve_not_created(args.resolve)
|
|
2156
|
+
pending_rows = (
|
|
2157
|
+
reconcile_pending(active, pending, ttl)
|
|
2158
|
+
if args.reconcile
|
|
2159
|
+
else (pending.all() if args.resolve else None)
|
|
2160
|
+
)
|
|
2161
|
+
tasks = active.list_tasks(status=args.status)
|
|
2162
|
+
if args.json:
|
|
2163
|
+
payload = {
|
|
2164
|
+
"platform": "gongji",
|
|
2165
|
+
"tasks": tasks,
|
|
2166
|
+
}
|
|
2167
|
+
if pending_rows is not None:
|
|
2168
|
+
payload["pending"] = pending_rows
|
|
2169
|
+
print(
|
|
2170
|
+
json.dumps(
|
|
2171
|
+
payload, ensure_ascii=False
|
|
2172
|
+
)
|
|
2173
|
+
)
|
|
2174
|
+
else:
|
|
2175
|
+
_print_gongji_tasks(tasks)
|
|
2176
|
+
if pending_rows is not None:
|
|
2177
|
+
_print_gongji_pending(pending_rows)
|
|
2178
|
+
elif command == "task":
|
|
2179
|
+
data = active.task_detail(args.task_id)
|
|
2180
|
+
if args.json:
|
|
2181
|
+
print(
|
|
2182
|
+
json.dumps(
|
|
2183
|
+
{
|
|
2184
|
+
"platform": "gongji",
|
|
2185
|
+
"task": data,
|
|
2186
|
+
},
|
|
2187
|
+
ensure_ascii=False,
|
|
2188
|
+
)
|
|
2189
|
+
)
|
|
2190
|
+
else:
|
|
2191
|
+
_print_gongji_task(data)
|
|
2192
|
+
elif command == "deploy":
|
|
2193
|
+
ports = tuple(args.port or [8080])
|
|
2194
|
+
if args.execute and args.max_hourly is None:
|
|
2195
|
+
print(
|
|
2196
|
+
"错误: --execute 必须提供 --max-hourly",
|
|
2197
|
+
file=sys.stderr,
|
|
2198
|
+
)
|
|
2199
|
+
return 2
|
|
2200
|
+
plan = prepare_deploy(
|
|
2201
|
+
active,
|
|
2202
|
+
pending,
|
|
2203
|
+
gpu_model=args.gpu,
|
|
2204
|
+
count=args.count,
|
|
2205
|
+
image=args.image,
|
|
2206
|
+
ports=ports,
|
|
2207
|
+
region=args.region,
|
|
2208
|
+
max_hourly=args.max_hourly,
|
|
2209
|
+
ttl_seconds=args.ttl,
|
|
2210
|
+
name_prefix=args.name_prefix,
|
|
2211
|
+
)
|
|
2212
|
+
if not args.execute:
|
|
2213
|
+
if args.json:
|
|
2214
|
+
print(
|
|
2215
|
+
json.dumps(
|
|
2216
|
+
{
|
|
2217
|
+
"platform": "gongji",
|
|
2218
|
+
"mode": "dry-run",
|
|
2219
|
+
"plan": _gongji_plan_dict(plan),
|
|
2220
|
+
},
|
|
2221
|
+
ensure_ascii=False,
|
|
2222
|
+
)
|
|
2223
|
+
)
|
|
2224
|
+
else:
|
|
2225
|
+
_print_gongji_plan(plan)
|
|
2226
|
+
return 0
|
|
2227
|
+
result = execute_deploy(
|
|
2228
|
+
active,
|
|
2229
|
+
pending,
|
|
2230
|
+
ttl,
|
|
2231
|
+
plan,
|
|
2232
|
+
max_hourly=args.max_hourly,
|
|
2233
|
+
)
|
|
2234
|
+
if args.json:
|
|
2235
|
+
print(
|
|
2236
|
+
json.dumps(
|
|
2237
|
+
{
|
|
2238
|
+
"platform": "gongji",
|
|
2239
|
+
"mode": "execute",
|
|
2240
|
+
**result,
|
|
2241
|
+
"plan": _gongji_plan_dict(plan),
|
|
2242
|
+
},
|
|
2243
|
+
ensure_ascii=False,
|
|
2244
|
+
)
|
|
2245
|
+
)
|
|
2246
|
+
else:
|
|
2247
|
+
print(
|
|
2248
|
+
f"任务 {result['task_id']} 已创建 "
|
|
2249
|
+
f"({result['task_name']})"
|
|
2250
|
+
)
|
|
2251
|
+
print(
|
|
2252
|
+
f"小时价 ¥{result['hourly_total']:.2f}"
|
|
2253
|
+
)
|
|
2254
|
+
elif command in ("pause", "resume", "stop"):
|
|
2255
|
+
if command == "stop" and not args.force:
|
|
2256
|
+
if args.json or not sys.stdin.isatty():
|
|
2257
|
+
print(
|
|
2258
|
+
"错误: 非交互环境 stop 必须 --force",
|
|
2259
|
+
file=sys.stderr,
|
|
2260
|
+
)
|
|
2261
|
+
return 2
|
|
2262
|
+
answer = input(
|
|
2263
|
+
f"确认停止任务 {args.task_id}?"
|
|
2264
|
+
"此操作不可恢复 (y/N): "
|
|
2265
|
+
)
|
|
2266
|
+
if answer.strip().lower() != "y":
|
|
2267
|
+
print("已取消")
|
|
2268
|
+
return 0
|
|
2269
|
+
getattr(
|
|
2270
|
+
active,
|
|
2271
|
+
{
|
|
2272
|
+
"pause": "pause_task",
|
|
2273
|
+
"resume": "recover_task",
|
|
2274
|
+
"stop": "stop_task",
|
|
2275
|
+
}[command],
|
|
2276
|
+
)(args.task_id)
|
|
2277
|
+
if args.json:
|
|
2278
|
+
print(
|
|
2279
|
+
json.dumps(
|
|
2280
|
+
{
|
|
2281
|
+
"platform": "gongji",
|
|
2282
|
+
"task_id": args.task_id,
|
|
2283
|
+
"action": command,
|
|
2284
|
+
},
|
|
2285
|
+
ensure_ascii=False,
|
|
2286
|
+
)
|
|
2287
|
+
)
|
|
2288
|
+
else:
|
|
2289
|
+
print(f"{command} 任务 {args.task_id} 成功")
|
|
2290
|
+
elif command == "ttl":
|
|
2291
|
+
sub = args.gongji_ttl_command
|
|
2292
|
+
if sub == "list":
|
|
2293
|
+
rows = ttl.list_expiring()
|
|
2294
|
+
if args.json:
|
|
2295
|
+
print(
|
|
2296
|
+
json.dumps(
|
|
2297
|
+
{
|
|
2298
|
+
"platform": "gongji",
|
|
2299
|
+
"items": rows,
|
|
2300
|
+
},
|
|
2301
|
+
ensure_ascii=False,
|
|
2302
|
+
)
|
|
2303
|
+
)
|
|
2304
|
+
else:
|
|
2305
|
+
_print_gongji_ttl(rows)
|
|
2306
|
+
elif sub == "sweep":
|
|
2307
|
+
result = sweep_ttl(
|
|
2308
|
+
active, ttl, execute=args.execute
|
|
2309
|
+
)
|
|
2310
|
+
if args.json:
|
|
2311
|
+
print(
|
|
2312
|
+
json.dumps(
|
|
2313
|
+
{
|
|
2314
|
+
"platform": "gongji",
|
|
2315
|
+
"mode": (
|
|
2316
|
+
"execute"
|
|
2317
|
+
if args.execute
|
|
2318
|
+
else "dry-run"
|
|
2319
|
+
),
|
|
2320
|
+
**result,
|
|
2321
|
+
},
|
|
2322
|
+
ensure_ascii=False,
|
|
2323
|
+
)
|
|
2324
|
+
)
|
|
2325
|
+
else:
|
|
2326
|
+
_print_gongji_ttl(result["items"])
|
|
2327
|
+
print(
|
|
2328
|
+
f"过期 {result['expired']},停止 "
|
|
2329
|
+
f"{result['stopped']},已结束 "
|
|
2330
|
+
f"{result['ended']},失败 "
|
|
2331
|
+
f"{result['failed']}"
|
|
2332
|
+
)
|
|
2333
|
+
elif sub == "rm":
|
|
2334
|
+
ttl.unregister(args.task_id)
|
|
2335
|
+
if args.json:
|
|
2336
|
+
print(
|
|
2337
|
+
json.dumps(
|
|
2338
|
+
{
|
|
2339
|
+
"platform": "gongji",
|
|
2340
|
+
"removed": args.task_id,
|
|
2341
|
+
},
|
|
2342
|
+
ensure_ascii=False,
|
|
2343
|
+
)
|
|
2344
|
+
)
|
|
2345
|
+
else:
|
|
2346
|
+
print(f"已移除 TTL {args.task_id}")
|
|
2347
|
+
else:
|
|
2348
|
+
return 2
|
|
2349
|
+
else:
|
|
2350
|
+
return 2
|
|
2351
|
+
except AuthRequiredError as exc:
|
|
2352
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2353
|
+
return 2
|
|
2354
|
+
except AuthFailedError as exc:
|
|
2355
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2356
|
+
return 3
|
|
2357
|
+
except (GongjiStateError, LockError) as exc:
|
|
2358
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2359
|
+
return 3
|
|
2360
|
+
except (DeployBlockedError, ValueError) as exc:
|
|
2361
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2362
|
+
return 2
|
|
2363
|
+
except NoResourceError as exc:
|
|
2364
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2365
|
+
return 3
|
|
2366
|
+
except TaskRejectedError as exc:
|
|
2367
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2368
|
+
return 3
|
|
2369
|
+
except TaskUncertainError as exc:
|
|
2370
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2371
|
+
print(
|
|
2372
|
+
"提醒: 任务结果未知,已写入 pending;请运行 "
|
|
2373
|
+
"gongji tasks --reconcile",
|
|
2374
|
+
file=sys.stderr,
|
|
2375
|
+
)
|
|
2376
|
+
return 4
|
|
2377
|
+
except AdapterError as exc:
|
|
2378
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2379
|
+
return 3
|
|
2380
|
+
return 0
|
|
2381
|
+
|
|
2382
|
+
|
|
2383
|
+
def _gongji_plan_dict(plan) -> dict:
|
|
2384
|
+
return {
|
|
2385
|
+
"task_name": plan.task_name,
|
|
2386
|
+
"gpu_model": plan.gpu_model,
|
|
2387
|
+
"count": plan.count,
|
|
2388
|
+
"image": plan.image,
|
|
2389
|
+
"ports": list(plan.ports),
|
|
2390
|
+
"region": plan.offer.region,
|
|
2391
|
+
"mark": plan.mark,
|
|
2392
|
+
"hourly_total": plan.hourly_total,
|
|
2393
|
+
"ttl_seconds": plan.ttl_seconds,
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
|
|
2397
|
+
def _print_gongji_plan(plan) -> None:
|
|
2398
|
+
print("dry-run: 未提交任务")
|
|
2399
|
+
print(f"任务名 {plan.task_name}")
|
|
2400
|
+
print(f"GPU {plan.gpu_model} × {plan.count}")
|
|
2401
|
+
print(f"地区 {plan.offer.region or '-'}")
|
|
2402
|
+
print(f"镜像 {plan.image}")
|
|
2403
|
+
print(f"端口 {', '.join(str(p) for p in plan.ports)}")
|
|
2404
|
+
print(f"小时价 ¥{plan.hourly_total:.2f}")
|
|
2405
|
+
if plan.ttl_seconds:
|
|
2406
|
+
print(f"TTL {plan.ttl_seconds}s")
|
|
2407
|
+
print("增加 --execute --max-hourly N 才会真实创建")
|
|
2408
|
+
|
|
2409
|
+
|
|
2410
|
+
def _print_gongji_tasks(rows) -> None:
|
|
2411
|
+
print(
|
|
2412
|
+
f"{'ID':>6} {'NAME':<28} {'STATUS':<10} "
|
|
2413
|
+
f"{'GPU':<12} {'POINTS':>6} URL"
|
|
2414
|
+
)
|
|
2415
|
+
print(
|
|
2416
|
+
f"{'-' * 6} {'-' * 28} {'-' * 10} {'-' * 12} "
|
|
2417
|
+
f"{'-' * 6} {'-' * 20}"
|
|
2418
|
+
)
|
|
2419
|
+
for row in rows:
|
|
2420
|
+
print(
|
|
2421
|
+
f"{(row.get('task_id') or '-'):>6} "
|
|
2422
|
+
f"{(row.get('task_name') or ''):<28} "
|
|
2423
|
+
f"{(row.get('status') or ''):<10} "
|
|
2424
|
+
f"{(row.get('gpu_name') or ''):<12} "
|
|
2425
|
+
f"{(row.get('points') or ''):>6} "
|
|
2426
|
+
f"{row.get('url') or ''}"
|
|
2427
|
+
)
|
|
2428
|
+
|
|
2429
|
+
|
|
2430
|
+
def _print_gongji_task(data) -> None:
|
|
2431
|
+
print(f"ID {data.get('task_id')}")
|
|
2432
|
+
print(f"名称 {data.get('task_name')}")
|
|
2433
|
+
print(f"状态 {data.get('status')}")
|
|
2434
|
+
print(
|
|
2435
|
+
f"节点 {data.get('runing_points', 0)} / "
|
|
2436
|
+
f"{data.get('points', 0)}"
|
|
2437
|
+
)
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
def _print_gongji_pending(rows) -> None:
|
|
2441
|
+
print(
|
|
2442
|
+
f"{'NAME':<28} {'STATUS':<12} {'TASK_ID':>8} ERROR"
|
|
2443
|
+
)
|
|
2444
|
+
for row in rows:
|
|
2445
|
+
print(
|
|
2446
|
+
f"{row.get('name', ''):<28} "
|
|
2447
|
+
f"{row.get('status', ''):<12} "
|
|
2448
|
+
f"{row.get('task_id') or '-':>8} "
|
|
2449
|
+
f"{row.get('error') or ''}"
|
|
2450
|
+
)
|
|
2451
|
+
|
|
2452
|
+
|
|
2453
|
+
def _print_gongji_ttl(rows) -> None:
|
|
2454
|
+
print(
|
|
2455
|
+
f"{'TASK_ID':>8} {'NAME':<28} "
|
|
2456
|
+
f"{'EXPIRES_IN':>12} EXPIRED"
|
|
2457
|
+
)
|
|
2458
|
+
for row in rows:
|
|
2459
|
+
print(
|
|
2460
|
+
f"{row['task_id']:>8} "
|
|
2461
|
+
f"{row.get('name', ''):<28} "
|
|
2462
|
+
f"{row.get('expires_in', 0):>12} "
|
|
2463
|
+
f"{row.get('expired', False)}"
|
|
2464
|
+
)
|
|
2465
|
+
|
|
2466
|
+
|
|
2467
|
+
def _print_gongji_resources(offers) -> None:
|
|
2468
|
+
print(
|
|
2469
|
+
f"{'GPU':<20} {'REGION':<10} {'FREE':>5} "
|
|
2470
|
+
f"{'PRICE':>8} {'VRAM':>6} {'CPU':>4} {'RAM/GPU':>8}"
|
|
2471
|
+
)
|
|
2472
|
+
print(
|
|
2473
|
+
f"{'-' * 20} {'-' * 10} {'-' * 5} {'-' * 8} "
|
|
2474
|
+
f"{'-' * 6} {'-' * 4} {'-' * 8}"
|
|
2475
|
+
)
|
|
2476
|
+
for offer in offers:
|
|
2477
|
+
extra = offer.extra or {}
|
|
2478
|
+
price = (
|
|
2479
|
+
f"¥{offer.price_per_hour:.2f}"
|
|
2480
|
+
if offer.price_per_hour is not None
|
|
2481
|
+
else "-"
|
|
2482
|
+
)
|
|
2483
|
+
vram = (
|
|
2484
|
+
f"{offer.gpu_memory_gb}G"
|
|
2485
|
+
if offer.gpu_memory_gb is not None
|
|
2486
|
+
else "-"
|
|
2487
|
+
)
|
|
2488
|
+
cpu = extra.get("cpu_cores", "-")
|
|
2489
|
+
ram = extra.get("ram_per_gpu_gb", "-")
|
|
2490
|
+
ram_text = f"{ram}G" if ram != "-" else "-"
|
|
2491
|
+
print(
|
|
2492
|
+
f"{offer.gpu_model:<20} {(offer.region or '-'):<10} "
|
|
2493
|
+
f"{offer.free:>5} {price:>8} {vram:>6} "
|
|
2494
|
+
f"{str(cpu):>4} {ram_text:>8}"
|
|
2495
|
+
)
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
def _print_gongji_models(models) -> None:
|
|
2499
|
+
print(f"{'MODEL':<24} {'COUNT':>6} {'MIN_PRICE':>10}")
|
|
2500
|
+
print(f"{'-' * 24} {'-' * 6} {'-' * 10}")
|
|
2501
|
+
for item in models:
|
|
2502
|
+
price = (
|
|
2503
|
+
f"¥{item['min_price']:.2f}"
|
|
2504
|
+
if item.get("min_price") is not None
|
|
2505
|
+
else "-"
|
|
2506
|
+
)
|
|
2507
|
+
print(f"{item['name']:<24} {item['count']:>6} {price:>10}")
|
|
2508
|
+
|
|
2509
|
+
|
|
2510
|
+
def _print_gongji_regions(regions) -> None:
|
|
2511
|
+
print(f"{'REGION':<16} {'COUNT':>6}")
|
|
2512
|
+
print(f"{'-' * 16} {'-' * 6}")
|
|
2513
|
+
for item in regions:
|
|
2514
|
+
print(f"{item['name']:<16} {item['count']:>6}")
|
|
2515
|
+
|
|
2516
|
+
|
|
2517
|
+
def _run_gpushare(args, *, adapter=None, env=None) -> int:
|
|
2518
|
+
resolved_env = os.environ if env is None else env
|
|
2519
|
+
token_file = (
|
|
2520
|
+
Path(args.token_file).expanduser()
|
|
2521
|
+
if getattr(args, "token_file", None)
|
|
2522
|
+
else None
|
|
2523
|
+
)
|
|
2524
|
+
active = adapter or GpuShareAdapter(
|
|
2525
|
+
token_file=token_file,
|
|
2526
|
+
env=resolved_env,
|
|
2527
|
+
)
|
|
2528
|
+
command = args.gpushare_command
|
|
2529
|
+
|
|
2530
|
+
try:
|
|
2531
|
+
if hasattr(active, "require_token"):
|
|
2532
|
+
active.require_token()
|
|
2533
|
+
|
|
2534
|
+
if command == "machines":
|
|
2535
|
+
if args.min_free < 0:
|
|
2536
|
+
print(
|
|
2537
|
+
"错误: --min-free 不能为负数",
|
|
2538
|
+
file=sys.stderr,
|
|
2539
|
+
)
|
|
2540
|
+
return 2
|
|
2541
|
+
for name, value in (
|
|
2542
|
+
("max-price", args.max_price),
|
|
2543
|
+
("min-vram", args.min_vram),
|
|
2544
|
+
("min-ram", args.min_ram),
|
|
2545
|
+
):
|
|
2546
|
+
if value is not None and value < 0:
|
|
2547
|
+
print(
|
|
2548
|
+
f"错误: --{name} 不能为负数",
|
|
2549
|
+
file=sys.stderr,
|
|
2550
|
+
)
|
|
2551
|
+
return 2
|
|
2552
|
+
offers = active.list_machines(
|
|
2553
|
+
gpu_model=args.gpu,
|
|
2554
|
+
region=args.region,
|
|
2555
|
+
min_free=args.min_free,
|
|
2556
|
+
max_price=args.max_price,
|
|
2557
|
+
min_vram_gb=args.min_vram,
|
|
2558
|
+
min_ram_gb=args.min_ram,
|
|
2559
|
+
min_cuda=args.min_cuda,
|
|
2560
|
+
)
|
|
2561
|
+
if args.json:
|
|
2562
|
+
print(
|
|
2563
|
+
json.dumps(
|
|
2564
|
+
{
|
|
2565
|
+
"platform": "gpushare",
|
|
2566
|
+
"kind": "machine",
|
|
2567
|
+
"offers": [
|
|
2568
|
+
offer.to_dict() for offer in offers
|
|
2569
|
+
],
|
|
2570
|
+
},
|
|
2571
|
+
ensure_ascii=False,
|
|
2572
|
+
)
|
|
2573
|
+
)
|
|
2574
|
+
else:
|
|
2575
|
+
_print_gpushare_machines(offers)
|
|
2576
|
+
elif command == "models":
|
|
2577
|
+
models = active.list_models()
|
|
2578
|
+
if args.json:
|
|
2579
|
+
print(
|
|
2580
|
+
json.dumps(
|
|
2581
|
+
{
|
|
2582
|
+
"platform": "gpushare",
|
|
2583
|
+
"models": models,
|
|
2584
|
+
},
|
|
2585
|
+
ensure_ascii=False,
|
|
2586
|
+
)
|
|
2587
|
+
)
|
|
2588
|
+
else:
|
|
2589
|
+
_print_gpushare_models(models)
|
|
2590
|
+
elif command == "regions":
|
|
2591
|
+
regions = active.list_regions()
|
|
2592
|
+
if args.json:
|
|
2593
|
+
print(
|
|
2594
|
+
json.dumps(
|
|
2595
|
+
{
|
|
2596
|
+
"platform": "gpushare",
|
|
2597
|
+
"regions": regions,
|
|
2598
|
+
},
|
|
2599
|
+
ensure_ascii=False,
|
|
2600
|
+
)
|
|
2601
|
+
)
|
|
2602
|
+
else:
|
|
2603
|
+
_print_gpushare_regions(regions)
|
|
2604
|
+
else:
|
|
2605
|
+
return 2
|
|
2606
|
+
except AuthRequiredError as exc:
|
|
2607
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2608
|
+
print(
|
|
2609
|
+
f"提醒: {_credential_hint('gpushare', token_file=token_file)}",
|
|
2610
|
+
file=sys.stderr,
|
|
2611
|
+
)
|
|
2612
|
+
return 2
|
|
2613
|
+
except AuthFailedError as exc:
|
|
2614
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2615
|
+
print(
|
|
2616
|
+
f"提醒: {_credential_hint('gpushare', token_file=token_file)}",
|
|
2617
|
+
file=sys.stderr,
|
|
2618
|
+
)
|
|
2619
|
+
return 3
|
|
2620
|
+
except AdapterError as exc:
|
|
2621
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2622
|
+
return 3
|
|
2623
|
+
except ValueError as exc:
|
|
2624
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2625
|
+
return 2
|
|
2626
|
+
return 0
|
|
2627
|
+
|
|
2628
|
+
|
|
2629
|
+
def _print_gpushare_machines(offers) -> None:
|
|
2630
|
+
print(
|
|
2631
|
+
f"{'MACHINE':<14} {'GPU':<14} {'REGION':<10} "
|
|
2632
|
+
f"{'FREE/TOTAL':>10} {'PRICE':>8} {'VRAM':>6} "
|
|
2633
|
+
f"{'RAM/GPU':>8} {'CUDA':<8}"
|
|
2634
|
+
)
|
|
2635
|
+
print(
|
|
2636
|
+
f"{'-' * 14} {'-' * 14} {'-' * 10} {'-' * 10} "
|
|
2637
|
+
f"{'-' * 8} {'-' * 6} {'-' * 8} {'-' * 8}"
|
|
2638
|
+
)
|
|
2639
|
+
for offer in offers:
|
|
2640
|
+
extra = offer.extra or {}
|
|
2641
|
+
price = (
|
|
2642
|
+
f"¥{offer.price_per_hour:.2f}"
|
|
2643
|
+
if offer.price_per_hour is not None
|
|
2644
|
+
else "-"
|
|
2645
|
+
)
|
|
2646
|
+
total = offer.total if offer.total is not None else "?"
|
|
2647
|
+
vram = (
|
|
2648
|
+
f"{offer.gpu_memory_gb}G"
|
|
2649
|
+
if offer.gpu_memory_gb is not None
|
|
2650
|
+
else "-"
|
|
2651
|
+
)
|
|
2652
|
+
ram = extra.get("ram_per_gpu_gb")
|
|
2653
|
+
ram_text = f"{ram}G" if ram is not None else "-"
|
|
2654
|
+
cuda = extra.get("gpu_toolkit_version") or "-"
|
|
2655
|
+
print(
|
|
2656
|
+
f"{(offer.machine_id or '-'):<14} {offer.gpu_model:<14} "
|
|
2657
|
+
f"{(offer.region or '-'):<10} "
|
|
2658
|
+
f"{f'{offer.free}/{total}':>10} {price:>8} "
|
|
2659
|
+
f"{vram:>6} {ram_text:>8} {cuda:<8}"
|
|
2660
|
+
)
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
def _print_gpushare_models(models) -> None:
|
|
2664
|
+
print(f"{'MODEL':<20} {'COUNT':>6} {'MIN_PRICE':>10}")
|
|
2665
|
+
print(f"{'-' * 20} {'-' * 6} {'-' * 10}")
|
|
2666
|
+
for item in models:
|
|
2667
|
+
price = (
|
|
2668
|
+
f"¥{item['min_price']:.2f}"
|
|
2669
|
+
if item.get("min_price") is not None
|
|
2670
|
+
else "-"
|
|
2671
|
+
)
|
|
2672
|
+
print(f"{item['name']:<20} {item['count']:>6} {price:>10}")
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
def _print_gpushare_regions(regions) -> None:
|
|
2676
|
+
print(f"{'REGION':<16} {'COUNT':>6}")
|
|
2677
|
+
print(f"{'-' * 16} {'-' * 6}")
|
|
2678
|
+
for item in regions:
|
|
2679
|
+
print(f"{item['name']:<16} {item['count']:>6}")
|
|
2680
|
+
|
|
2681
|
+
|
|
2682
|
+
def _run_autodl_order(args, *, active, env) -> int:
|
|
2683
|
+
command = args.autodl_command
|
|
2684
|
+
store = OrderStore(env.get("CLOUDGPU_ORDERS_DB"))
|
|
2685
|
+
|
|
2686
|
+
try:
|
|
2687
|
+
if command == "images":
|
|
2688
|
+
images = active.list_images(gpu_model=args.gpu)
|
|
2689
|
+
if args.json:
|
|
2690
|
+
print(
|
|
2691
|
+
json.dumps(
|
|
2692
|
+
{
|
|
2693
|
+
"platform": "autodl",
|
|
2694
|
+
"images": [
|
|
2695
|
+
asdict(image) for image in images
|
|
2696
|
+
],
|
|
2697
|
+
},
|
|
2698
|
+
ensure_ascii=False,
|
|
2699
|
+
)
|
|
2700
|
+
)
|
|
2701
|
+
else:
|
|
2702
|
+
_print_autodl_images(images)
|
|
2703
|
+
elif command == "buy":
|
|
2704
|
+
if args.execute and args.max_hourly is None:
|
|
2705
|
+
print(
|
|
2706
|
+
"错误: --execute 必须提供 --max-hourly",
|
|
2707
|
+
file=sys.stderr,
|
|
2708
|
+
)
|
|
2709
|
+
return 2
|
|
2710
|
+
plan = prepare_buy(
|
|
2711
|
+
active,
|
|
2712
|
+
store,
|
|
2713
|
+
gpu_model=args.gpu,
|
|
2714
|
+
count=args.count,
|
|
2715
|
+
image_id=args.image,
|
|
2716
|
+
region=args.region,
|
|
2717
|
+
max_hourly=args.max_hourly,
|
|
2718
|
+
name_prefix=args.name_prefix,
|
|
2719
|
+
)
|
|
2720
|
+
if not args.execute:
|
|
2721
|
+
if args.json:
|
|
2722
|
+
print(
|
|
2723
|
+
json.dumps(
|
|
2724
|
+
{
|
|
2725
|
+
"platform": "autodl",
|
|
2726
|
+
"mode": "dry-run",
|
|
2727
|
+
"plan": _plan_dict(plan),
|
|
2728
|
+
},
|
|
2729
|
+
ensure_ascii=False,
|
|
2730
|
+
)
|
|
2731
|
+
)
|
|
2732
|
+
else:
|
|
2733
|
+
_print_autodl_plan(plan)
|
|
2734
|
+
return 0
|
|
2735
|
+
|
|
2736
|
+
result = execute_buy(
|
|
2737
|
+
active,
|
|
2738
|
+
store,
|
|
2739
|
+
plan,
|
|
2740
|
+
max_hourly=args.max_hourly,
|
|
2741
|
+
)
|
|
2742
|
+
if args.json:
|
|
2743
|
+
print(
|
|
2744
|
+
json.dumps(
|
|
2745
|
+
{
|
|
2746
|
+
"platform": "autodl",
|
|
2747
|
+
"mode": "execute",
|
|
2748
|
+
"status": result.status,
|
|
2749
|
+
"order_id": result.order_id,
|
|
2750
|
+
"instance_uuid": result.instance_uuid,
|
|
2751
|
+
"plan": _plan_dict(plan),
|
|
2752
|
+
},
|
|
2753
|
+
ensure_ascii=False,
|
|
2754
|
+
)
|
|
2755
|
+
)
|
|
2756
|
+
else:
|
|
2757
|
+
_print_autodl_result(result)
|
|
2758
|
+
elif command == "instances":
|
|
2759
|
+
rows = active.list_instances()
|
|
2760
|
+
if args.json:
|
|
2761
|
+
print(
|
|
2762
|
+
json.dumps(
|
|
2763
|
+
{
|
|
2764
|
+
"platform": "autodl",
|
|
2765
|
+
"instances": [
|
|
2766
|
+
asdict(row) for row in rows
|
|
2767
|
+
],
|
|
2768
|
+
},
|
|
2769
|
+
ensure_ascii=False,
|
|
2770
|
+
)
|
|
2771
|
+
)
|
|
2772
|
+
else:
|
|
2773
|
+
_print_autodl_instances(rows)
|
|
2774
|
+
elif command == "orders":
|
|
2775
|
+
if args.resolve is not None:
|
|
2776
|
+
if not args.not_created:
|
|
2777
|
+
print(
|
|
2778
|
+
"错误: --resolve 需要 --not-created",
|
|
2779
|
+
file=sys.stderr,
|
|
2780
|
+
)
|
|
2781
|
+
return 2
|
|
2782
|
+
store.resolve_not_created(args.resolve)
|
|
2783
|
+
rows = (
|
|
2784
|
+
reconcile_orders(active, store)
|
|
2785
|
+
if args.reconcile
|
|
2786
|
+
else store.all()
|
|
2787
|
+
)
|
|
2788
|
+
if args.json:
|
|
2789
|
+
print(
|
|
2790
|
+
json.dumps(
|
|
2791
|
+
{
|
|
2792
|
+
"platform": "autodl",
|
|
2793
|
+
"orders": rows,
|
|
2794
|
+
},
|
|
2795
|
+
ensure_ascii=False,
|
|
2796
|
+
)
|
|
2797
|
+
)
|
|
2798
|
+
else:
|
|
2799
|
+
_print_autodl_orders(rows)
|
|
2800
|
+
else:
|
|
2801
|
+
return 2
|
|
2802
|
+
except AuthRequiredError as exc:
|
|
2803
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2804
|
+
return 2
|
|
2805
|
+
except AuthFailedError as exc:
|
|
2806
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2807
|
+
return 3
|
|
2808
|
+
except (BuyBlockedError, ValueError) as exc:
|
|
2809
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2810
|
+
return 2
|
|
2811
|
+
except NoCandidateError as exc:
|
|
2812
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2813
|
+
return 3
|
|
2814
|
+
except OrderRejectedError as exc:
|
|
2815
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2816
|
+
return 3
|
|
2817
|
+
except OrderUncertainError as exc:
|
|
2818
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2819
|
+
print(
|
|
2820
|
+
"提醒: 订单结果未知,已写入台账;请运行 "
|
|
2821
|
+
"autodl orders --reconcile",
|
|
2822
|
+
file=sys.stderr,
|
|
2823
|
+
)
|
|
2824
|
+
return 4
|
|
2825
|
+
except AdapterError as exc:
|
|
2826
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
2827
|
+
return 3
|
|
2828
|
+
return 0
|
|
2829
|
+
|
|
2830
|
+
|
|
2831
|
+
def _plan_dict(plan) -> dict:
|
|
2832
|
+
return {
|
|
2833
|
+
"gpu_model": plan.gpu_model,
|
|
2834
|
+
"count": plan.count,
|
|
2835
|
+
"machine_id": plan.offer.machine_id,
|
|
2836
|
+
"region": plan.offer.region,
|
|
2837
|
+
"image": plan.image.id,
|
|
2838
|
+
"hourly_total": plan.quote.hourly_total,
|
|
2839
|
+
"instance_name": plan.instance_name,
|
|
2840
|
+
}
|
|
2841
|
+
|
|
2842
|
+
|
|
2843
|
+
def _print_autodl_images(images) -> None:
|
|
2844
|
+
print(f"{'ID':<28} {'KIND':<8} {'CUDA':<8} LABEL")
|
|
2845
|
+
print(f"{'-' * 28} {'-' * 8} {'-' * 8} {'-' * 30}")
|
|
2846
|
+
for image in images:
|
|
2847
|
+
print(
|
|
2848
|
+
f"{image.id:<28} {image.kind:<8} "
|
|
2849
|
+
f"{(image.cuda or '-'):<8} {image.label}"
|
|
2850
|
+
)
|
|
2851
|
+
|
|
2852
|
+
|
|
2853
|
+
def _print_autodl_plan(plan) -> None:
|
|
2854
|
+
print("dry-run: 未提交订单")
|
|
2855
|
+
print(f"机器 {plan.offer.machine_id}")
|
|
2856
|
+
print(f"地区 {plan.offer.region or '-'}")
|
|
2857
|
+
print(f"GPU {plan.gpu_model} × {plan.count}")
|
|
2858
|
+
print(f"镜像 {plan.image.id}")
|
|
2859
|
+
print(f"小时价 ¥{plan.quote.hourly_total:.2f}")
|
|
2860
|
+
print(f"实例名 {plan.instance_name}")
|
|
2861
|
+
print("增加 --execute --max-hourly N 才会真实下单")
|
|
2862
|
+
|
|
2863
|
+
|
|
2864
|
+
def _print_autodl_result(result) -> None:
|
|
2865
|
+
print(f"订单 {result.order_id} 已创建")
|
|
2866
|
+
print(f"状态 {result.status}")
|
|
2867
|
+
print(f"实例 ID {result.instance_uuid}")
|
|
2868
|
+
print(f"实例名 {result.plan.instance_name}")
|
|
2869
|
+
print(f"小时价 ¥{result.quote.hourly_total:.2f}")
|
|
2870
|
+
|
|
2871
|
+
|
|
2872
|
+
def _print_autodl_instances(rows) -> None:
|
|
2873
|
+
print(
|
|
2874
|
+
f"{'UUID':<24} {'NAME':<20} {'STATUS':<12} "
|
|
2875
|
+
f"{'GPU':<16} {'COUNT':>5} {'REGION':<12} MACHINE"
|
|
2876
|
+
)
|
|
2877
|
+
print(
|
|
2878
|
+
f"{'-' * 24} {'-' * 20} {'-' * 12} {'-' * 16} "
|
|
2879
|
+
f"{'-' * 5} {'-' * 12} {'-' * 12}"
|
|
2880
|
+
)
|
|
2881
|
+
for row in rows:
|
|
2882
|
+
print(
|
|
2883
|
+
f"{row.uuid:<24} {row.name:<20} {row.status:<12} "
|
|
2884
|
+
f"{(row.gpu_name or '-'):<16} "
|
|
2885
|
+
f"{(row.gpu_count if row.gpu_count is not None else '-'):>5} "
|
|
2886
|
+
f"{(row.region or '-'):<12} "
|
|
2887
|
+
f"{row.machine_id or '-'}"
|
|
2888
|
+
)
|
|
2889
|
+
|
|
2890
|
+
|
|
2891
|
+
def _print_autodl_orders(rows) -> None:
|
|
2892
|
+
print(
|
|
2893
|
+
f"{'ID':>4} {'STATUS':<12} {'INSTANCE_NAME':<28} "
|
|
2894
|
+
f"{'GPU':<12} {'COUNT':>5} {'PRICE':>8} "
|
|
2895
|
+
f"{'UUID':<24} ERROR"
|
|
2896
|
+
)
|
|
2897
|
+
print(
|
|
2898
|
+
f"{'-' * 4} {'-' * 12} {'-' * 28} {'-' * 12} "
|
|
2899
|
+
f"{'-' * 5} {'-' * 8} {'-' * 24} {'-' * 20}"
|
|
2900
|
+
)
|
|
2901
|
+
for row in rows:
|
|
2902
|
+
price = (
|
|
2903
|
+
f"¥{row['hourly_total']:.2f}"
|
|
2904
|
+
if row["hourly_total"] is not None
|
|
2905
|
+
else "-"
|
|
2906
|
+
)
|
|
2907
|
+
print(
|
|
2908
|
+
f"{row['id']:>4} {row['status']:<12} "
|
|
2909
|
+
f"{row['instance_name']:<28} "
|
|
2910
|
+
f"{row['gpu_model']:<12} {row['gpu_count']:>5} "
|
|
2911
|
+
f"{price:>8} {(row['instance_uuid'] or '-'):<24} "
|
|
2912
|
+
f"{row['error'] or ''}"
|
|
2913
|
+
)
|
|
2914
|
+
|
|
2915
|
+
|
|
2916
|
+
def _print_autodl_models(models) -> None:
|
|
2917
|
+
print(f"{'GPU':<28} {'MEM':<6} {'SERIES':<18} {'ARCH':<6}")
|
|
2918
|
+
print(f"{'-' * 28} {'-' * 6} {'-' * 18} {'-' * 6}")
|
|
2919
|
+
for item in models:
|
|
2920
|
+
memory = (
|
|
2921
|
+
f"{item.memory_gb}G"
|
|
2922
|
+
if item.memory_gb is not None
|
|
2923
|
+
else "-"
|
|
2924
|
+
)
|
|
2925
|
+
print(
|
|
2926
|
+
f"{item.name:<28} {memory:<6} "
|
|
2927
|
+
f"{(item.series or '-'):<18} {(item.cpu_arch or '-'):<6}"
|
|
2928
|
+
)
|
|
2929
|
+
|
|
2930
|
+
|
|
2931
|
+
def _print_autodl_regions(regions) -> None:
|
|
2932
|
+
print(
|
|
2933
|
+
f"{'REGION':<12} {'DATA_CENTER':<14} "
|
|
2934
|
+
f"{'SIGN':<12} {'VISIBLE':<10}"
|
|
2935
|
+
)
|
|
2936
|
+
print(f"{'-' * 12} {'-' * 14} {'-' * 12} {'-' * 10}")
|
|
2937
|
+
for item in regions:
|
|
2938
|
+
print(
|
|
2939
|
+
f"{item.region_name:<12} {item.data_center:<14} "
|
|
2940
|
+
f"{item.region_sign:<12} {item.visible:<10}"
|
|
2941
|
+
)
|
|
2942
|
+
|
|
2943
|
+
|
|
2944
|
+
def _print_autodl_offers(offers) -> None:
|
|
2945
|
+
print(f"{'REGION':<12} {'GPU':<28} {'FREE/TOTAL':>12}")
|
|
2946
|
+
print(f"{'-' * 12} {'-' * 28} {'-' * 12}")
|
|
2947
|
+
for offer in offers:
|
|
2948
|
+
total = offer.total if offer.total is not None else "?"
|
|
2949
|
+
print(
|
|
2950
|
+
f"{(offer.region or '-'):<12} {offer.display_name:<28} "
|
|
2951
|
+
f"{f'{offer.free}/{total}':>12}"
|
|
2952
|
+
)
|
|
2953
|
+
|
|
2954
|
+
|
|
2955
|
+
def _print_autodl_machines(offers) -> None:
|
|
2956
|
+
print(
|
|
2957
|
+
f"{'MACHINE':<16} {'GPU':<28} {'IDLE/TOTAL':>12} "
|
|
2958
|
+
f"{'PRICE':>8} {'CUDA':<8} {'CPU':<28}"
|
|
2959
|
+
)
|
|
2960
|
+
print(
|
|
2961
|
+
f"{'-' * 16} {'-' * 28} {'-' * 12} "
|
|
2962
|
+
f"{'-' * 8} {'-' * 8} {'-' * 28}"
|
|
2963
|
+
)
|
|
2964
|
+
for offer in offers:
|
|
2965
|
+
extra = offer.extra or {}
|
|
2966
|
+
price = (
|
|
2967
|
+
f"¥{offer.price_per_hour:.2f}"
|
|
2968
|
+
if offer.price_per_hour is not None
|
|
2969
|
+
else "-"
|
|
2970
|
+
)
|
|
2971
|
+
total = offer.total if offer.total is not None else "?"
|
|
2972
|
+
cuda = extra.get("highest_cuda_version") or "-"
|
|
2973
|
+
cpu = extra.get("cpu_name") or "-"
|
|
2974
|
+
print(
|
|
2975
|
+
f"{(offer.machine_id or '-'):<16} {offer.display_name:<28} "
|
|
2976
|
+
f"{f'{offer.free}/{total}':>12} {price:>8} "
|
|
2977
|
+
f"{cuda:<8} {cpu:<28}"
|
|
2978
|
+
)
|
|
2979
|
+
|
|
2980
|
+
|
|
2981
|
+
def default_state_path() -> Path:
|
|
2982
|
+
override = os.environ.get("CLOUDGPU_STATE_FILE")
|
|
2983
|
+
if override:
|
|
2984
|
+
return Path(override).expanduser()
|
|
2985
|
+
return Path.home() / ".cloudgpu" / "state.json"
|
|
2986
|
+
|
|
2987
|
+
|
|
2988
|
+
def _print_offers(offers: Sequence[GpuOffer]) -> None:
|
|
2989
|
+
print(f"{'KIND':<9} {'GPU':<24} {'MEM':<5} {'FREE':>4}")
|
|
2990
|
+
print(f"{'-' * 9} {'-' * 24} {'-' * 5} {'-' * 4}")
|
|
2991
|
+
for offer in offers:
|
|
2992
|
+
memory = (
|
|
2993
|
+
f"{offer.gpu_memory_gb}G"
|
|
2994
|
+
if offer.gpu_memory_gb is not None
|
|
2995
|
+
else "-"
|
|
2996
|
+
)
|
|
2997
|
+
kind = offer.service_kind or "-"
|
|
2998
|
+
print(
|
|
2999
|
+
f"{kind:<9} {offer.display_name:<24} "
|
|
3000
|
+
f"{memory:<5} {offer.free:>4}"
|
|
3001
|
+
)
|
|
3002
|
+
print()
|
|
3003
|
+
print("提示:晨涧云仅提供全平台空闲卡总数(无地区/价格/同机信息)。")
|
|
3004
|
+
|
|
3005
|
+
|
|
3006
|
+
if __name__ == "__main__":
|
|
3007
|
+
raise SystemExit(main())
|