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/jobs.py
ADDED
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from cloudgpu.adapters.autodl import AutoDLAdapter
|
|
4
|
+
from cloudgpu.adapters.gongji import GongjiAdapter
|
|
5
|
+
from cloudgpu.adapters.gpugeek import GPUGeekAdapter
|
|
6
|
+
from cloudgpu.adapters.gpushare import GpuShareAdapter
|
|
7
|
+
from cloudgpu.adapters.matpool import MatpoolAdapter
|
|
8
|
+
from cloudgpu.adapters.mornai import MornAIAdapter
|
|
9
|
+
from cloudgpu.notify import CompositeNotifier, build_notifiers
|
|
10
|
+
from cloudgpu.watch import WatchJob, _credential_hint
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def build_jobs(
|
|
14
|
+
config,
|
|
15
|
+
*,
|
|
16
|
+
adapters=None,
|
|
17
|
+
notifier_builder=None,
|
|
18
|
+
env=None,
|
|
19
|
+
notify_override=None,
|
|
20
|
+
) -> list:
|
|
21
|
+
adapters = dict(adapters or {})
|
|
22
|
+
|
|
23
|
+
def adapter_for(platform):
|
|
24
|
+
if platform not in adapters:
|
|
25
|
+
token_file = config.platforms.get(platform, {}).get(
|
|
26
|
+
"token_file"
|
|
27
|
+
)
|
|
28
|
+
if platform == "mornai":
|
|
29
|
+
adapters[platform] = MornAIAdapter()
|
|
30
|
+
elif platform == "autodl":
|
|
31
|
+
adapters[platform] = AutoDLAdapter(
|
|
32
|
+
token_file=token_file, env=env
|
|
33
|
+
)
|
|
34
|
+
elif platform == "gpushare":
|
|
35
|
+
adapters[platform] = GpuShareAdapter(
|
|
36
|
+
token_file=token_file, env=env
|
|
37
|
+
)
|
|
38
|
+
elif platform == "matpool":
|
|
39
|
+
adapters[platform] = MatpoolAdapter()
|
|
40
|
+
elif platform == "gpugeek":
|
|
41
|
+
adapters[platform] = GPUGeekAdapter()
|
|
42
|
+
else:
|
|
43
|
+
adapters[platform] = GongjiAdapter(
|
|
44
|
+
token_file=token_file, env=env
|
|
45
|
+
)
|
|
46
|
+
return adapters[platform]
|
|
47
|
+
|
|
48
|
+
for platform in {target.platform for target in config.targets}:
|
|
49
|
+
adapter = adapter_for(platform)
|
|
50
|
+
if hasattr(adapter, "require_token"):
|
|
51
|
+
adapter.require_token()
|
|
52
|
+
|
|
53
|
+
if notifier_builder is None:
|
|
54
|
+
|
|
55
|
+
def notifier_builder(names):
|
|
56
|
+
return CompositeNotifier(
|
|
57
|
+
build_notifiers(names, env=env)
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
notifier_cache = {}
|
|
61
|
+
|
|
62
|
+
def notifier_for(names):
|
|
63
|
+
key = tuple(names)
|
|
64
|
+
if key not in notifier_cache:
|
|
65
|
+
notifier_cache[key] = notifier_builder(key)
|
|
66
|
+
return notifier_cache[key]
|
|
67
|
+
|
|
68
|
+
jobs = []
|
|
69
|
+
for target in config.targets:
|
|
70
|
+
names = (
|
|
71
|
+
notify_override
|
|
72
|
+
if notify_override is not None
|
|
73
|
+
else (target.notify or config.notify)
|
|
74
|
+
)
|
|
75
|
+
notifier = notifier_for(names)
|
|
76
|
+
adapter = adapter_for(target.platform)
|
|
77
|
+
token_file = config.platforms.get(
|
|
78
|
+
target.platform, {}
|
|
79
|
+
).get("token_file")
|
|
80
|
+
|
|
81
|
+
if target.platform == "mornai":
|
|
82
|
+
job = _mornai_job(target, adapter, notifier)
|
|
83
|
+
elif target.platform == "autodl":
|
|
84
|
+
job = _autodl_job(
|
|
85
|
+
target, adapter, notifier, token_file
|
|
86
|
+
)
|
|
87
|
+
elif target.platform == "gongji":
|
|
88
|
+
job = _gongji_job(
|
|
89
|
+
target, adapter, notifier, token_file
|
|
90
|
+
)
|
|
91
|
+
elif target.platform == "matpool":
|
|
92
|
+
job = _matpool_job(target, adapter, notifier)
|
|
93
|
+
elif target.platform == "gpugeek":
|
|
94
|
+
job = _gpugeek_job(target, adapter, notifier)
|
|
95
|
+
else:
|
|
96
|
+
job = _gpushare_job(
|
|
97
|
+
target, adapter, notifier, token_file
|
|
98
|
+
)
|
|
99
|
+
jobs.append(job)
|
|
100
|
+
return jobs
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _mornai_job(target, adapter, notifier) -> WatchJob:
|
|
104
|
+
options = target.options
|
|
105
|
+
kind = options.get("kind", "cloud")
|
|
106
|
+
kinds = (
|
|
107
|
+
("cloud", "container") if kind == "both" else (kind,)
|
|
108
|
+
)
|
|
109
|
+
gpu = tuple(options.get("gpu", ()))
|
|
110
|
+
return WatchJob(
|
|
111
|
+
name=target.name,
|
|
112
|
+
platform="mornai",
|
|
113
|
+
kinds=kinds,
|
|
114
|
+
gpu_patterns=gpu,
|
|
115
|
+
min_free=options["min_free"],
|
|
116
|
+
fetch_offers=lambda kind: adapter.list_offers(kind=kind),
|
|
117
|
+
notifier=notifier,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _autodl_job(target, adapter, notifier, token_file) -> WatchJob:
|
|
122
|
+
options = target.options
|
|
123
|
+
scope = options.get("scope", "region")
|
|
124
|
+
min_free = options["min_free"]
|
|
125
|
+
hint = _credential_hint("autodl", token_file=token_file)
|
|
126
|
+
|
|
127
|
+
if scope == "region":
|
|
128
|
+
regions = tuple(options.get("regions", ()))
|
|
129
|
+
gpu = tuple(options.get("gpu", ()))
|
|
130
|
+
reset_prefixes = _autodl_region_prefixes(adapter, regions)
|
|
131
|
+
return WatchJob(
|
|
132
|
+
name=target.name,
|
|
133
|
+
platform="autodl",
|
|
134
|
+
kinds=("region",),
|
|
135
|
+
gpu_patterns=gpu,
|
|
136
|
+
min_free=min_free,
|
|
137
|
+
fetch_offers=lambda kind: adapter.list_offers(
|
|
138
|
+
kind="region",
|
|
139
|
+
regions=regions,
|
|
140
|
+
models=gpu,
|
|
141
|
+
),
|
|
142
|
+
reset_prefixes=reset_prefixes,
|
|
143
|
+
credential_hint=hint,
|
|
144
|
+
notifier=notifier,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
regions = tuple(options.get("regions", ()))
|
|
148
|
+
gpu = tuple(options.get("gpu", ()))
|
|
149
|
+
if len(regions) != 1 or len(gpu) != 1:
|
|
150
|
+
raise ValueError(
|
|
151
|
+
f"{target.name} 机器级需要恰好一个地区和一个型号"
|
|
152
|
+
)
|
|
153
|
+
region = regions[0]
|
|
154
|
+
model = gpu[0]
|
|
155
|
+
|
|
156
|
+
models = adapter.list_models()
|
|
157
|
+
if model not in {item.name for item in models}:
|
|
158
|
+
raise ValueError(f"AutoDL 型号不存在: {model}")
|
|
159
|
+
|
|
160
|
+
known_regions = adapter.list_regions()
|
|
161
|
+
matches = [
|
|
162
|
+
item
|
|
163
|
+
for item in known_regions
|
|
164
|
+
if region
|
|
165
|
+
in (item.region_name, item.data_center, item.region_sign)
|
|
166
|
+
]
|
|
167
|
+
if not matches:
|
|
168
|
+
raise ValueError(f"未找到匹配地区: {region}")
|
|
169
|
+
|
|
170
|
+
return WatchJob(
|
|
171
|
+
name=target.name,
|
|
172
|
+
platform="autodl",
|
|
173
|
+
kinds=("machine",),
|
|
174
|
+
gpu_patterns=(model,),
|
|
175
|
+
min_free=min_free,
|
|
176
|
+
fetch_offers=lambda kind: adapter.list_machines(
|
|
177
|
+
region=region,
|
|
178
|
+
gpu_model=model,
|
|
179
|
+
min_idle=min_free,
|
|
180
|
+
),
|
|
181
|
+
reset_prefixes=(
|
|
182
|
+
f"autodl|machine|{matches[0].region_name}|{model}|",
|
|
183
|
+
),
|
|
184
|
+
credential_hint=hint,
|
|
185
|
+
notifier=notifier,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _autodl_region_prefixes(adapter, regions) -> tuple:
|
|
190
|
+
if not regions:
|
|
191
|
+
return ("autodl|region|",)
|
|
192
|
+
known = adapter.list_regions()
|
|
193
|
+
names = {
|
|
194
|
+
item.region_name
|
|
195
|
+
for item in known
|
|
196
|
+
if any(
|
|
197
|
+
pattern
|
|
198
|
+
in (
|
|
199
|
+
item.region_name,
|
|
200
|
+
item.data_center,
|
|
201
|
+
item.region_sign,
|
|
202
|
+
)
|
|
203
|
+
for pattern in regions
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
if not names:
|
|
207
|
+
raise ValueError(
|
|
208
|
+
"未找到匹配地区: " + ", ".join(regions)
|
|
209
|
+
)
|
|
210
|
+
return tuple(
|
|
211
|
+
f"autodl|region|{name}|" for name in names
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _gongji_job(target, adapter, notifier, token_file) -> WatchJob:
|
|
216
|
+
options = target.options
|
|
217
|
+
gpu = options.get("gpu")
|
|
218
|
+
region = options.get("region")
|
|
219
|
+
min_free = options["min_free"]
|
|
220
|
+
reset_prefixes = _gongji_prefixes(adapter, gpu, region)
|
|
221
|
+
|
|
222
|
+
return WatchJob(
|
|
223
|
+
name=target.name,
|
|
224
|
+
platform="gongji",
|
|
225
|
+
kinds=("region",),
|
|
226
|
+
gpu_patterns=(gpu,) if gpu else (),
|
|
227
|
+
min_free=min_free,
|
|
228
|
+
fetch_offers=lambda kind: adapter.list_resources(
|
|
229
|
+
gpu_model=gpu,
|
|
230
|
+
region=region,
|
|
231
|
+
min_free=min_free,
|
|
232
|
+
max_price=options.get("max_price"),
|
|
233
|
+
min_vram_gb=options.get("min_vram"),
|
|
234
|
+
min_ram_gb=options.get("min_ram"),
|
|
235
|
+
),
|
|
236
|
+
reset_prefixes=reset_prefixes,
|
|
237
|
+
credential_hint=_credential_hint(
|
|
238
|
+
"gongji", token_file=token_file
|
|
239
|
+
),
|
|
240
|
+
notifier=notifier,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _gongji_prefixes(adapter, gpu, region) -> tuple:
|
|
245
|
+
if not gpu and not region:
|
|
246
|
+
return ("gongji|region|",)
|
|
247
|
+
|
|
248
|
+
region_names = None
|
|
249
|
+
if region:
|
|
250
|
+
regions = adapter.list_regions()
|
|
251
|
+
region_names = [
|
|
252
|
+
item["name"]
|
|
253
|
+
for item in regions
|
|
254
|
+
if region.casefold() in item["name"].casefold()
|
|
255
|
+
]
|
|
256
|
+
if not region_names:
|
|
257
|
+
raise ValueError(f"共绩地区不存在: {region}")
|
|
258
|
+
|
|
259
|
+
model_names = None
|
|
260
|
+
if gpu:
|
|
261
|
+
models = adapter.list_models()
|
|
262
|
+
model_names = [
|
|
263
|
+
item["name"]
|
|
264
|
+
for item in models
|
|
265
|
+
if gpu.casefold() in item["name"].casefold()
|
|
266
|
+
]
|
|
267
|
+
if not model_names:
|
|
268
|
+
raise ValueError(f"共绩型号不存在: {gpu}")
|
|
269
|
+
|
|
270
|
+
if region_names is not None and model_names is None:
|
|
271
|
+
return tuple(
|
|
272
|
+
f"gongji|region|{name}|"
|
|
273
|
+
for name in region_names
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
if region_names is None:
|
|
277
|
+
regions = adapter.list_regions()
|
|
278
|
+
region_names = [item["name"] for item in regions]
|
|
279
|
+
if not region_names:
|
|
280
|
+
raise ValueError("共绩没有可用地区")
|
|
281
|
+
|
|
282
|
+
return tuple(
|
|
283
|
+
f"gongji|region|{region_name}|{model}|"
|
|
284
|
+
for region_name in region_names
|
|
285
|
+
for model in model_names
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _matpool_job(target, adapter, notifier) -> WatchJob:
|
|
290
|
+
options = target.options
|
|
291
|
+
gpu = options.get("gpu")
|
|
292
|
+
region = options.get("region")
|
|
293
|
+
min_free = options["min_free"]
|
|
294
|
+
reset_prefixes = _matpool_prefixes(adapter, gpu, region)
|
|
295
|
+
|
|
296
|
+
return WatchJob(
|
|
297
|
+
name=target.name,
|
|
298
|
+
platform="matpool",
|
|
299
|
+
kinds=("machine",),
|
|
300
|
+
gpu_patterns=(gpu,) if gpu else (),
|
|
301
|
+
min_free=min_free,
|
|
302
|
+
fetch_offers=lambda kind: adapter.list_machines(
|
|
303
|
+
gpu_model=gpu,
|
|
304
|
+
region=region,
|
|
305
|
+
min_free=min_free,
|
|
306
|
+
max_price=options.get("max_price"),
|
|
307
|
+
min_vram_gb=options.get("min_vram"),
|
|
308
|
+
min_ram_gb=options.get("min_ram"),
|
|
309
|
+
),
|
|
310
|
+
reset_prefixes=reset_prefixes,
|
|
311
|
+
notifier=notifier,
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _matpool_prefixes(adapter, gpu, region) -> tuple:
|
|
316
|
+
if not gpu and not region:
|
|
317
|
+
return ("matpool|machine|",)
|
|
318
|
+
|
|
319
|
+
region_names = None
|
|
320
|
+
if region:
|
|
321
|
+
regions = adapter.list_regions()
|
|
322
|
+
region_names = [
|
|
323
|
+
item["name"]
|
|
324
|
+
for item in regions
|
|
325
|
+
if region.casefold() in item["name"].casefold()
|
|
326
|
+
]
|
|
327
|
+
if not region_names:
|
|
328
|
+
raise ValueError(f"矩池云地区不存在: {region}")
|
|
329
|
+
|
|
330
|
+
model_names = None
|
|
331
|
+
if gpu:
|
|
332
|
+
models = adapter.list_models()
|
|
333
|
+
model_names = [
|
|
334
|
+
item["name"]
|
|
335
|
+
for item in models
|
|
336
|
+
if gpu.casefold() in item["name"].casefold()
|
|
337
|
+
]
|
|
338
|
+
if not model_names:
|
|
339
|
+
raise ValueError(f"矩池云型号不存在: {gpu}")
|
|
340
|
+
|
|
341
|
+
if region_names is not None and model_names is None:
|
|
342
|
+
return tuple(
|
|
343
|
+
f"matpool|machine|{name}|"
|
|
344
|
+
for name in region_names
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
if region_names is None:
|
|
348
|
+
regions = adapter.list_regions()
|
|
349
|
+
region_names = [item["name"] for item in regions]
|
|
350
|
+
if not region_names:
|
|
351
|
+
raise ValueError("矩池云没有可用地区")
|
|
352
|
+
|
|
353
|
+
return tuple(
|
|
354
|
+
f"matpool|machine|{region_name}|{model}|"
|
|
355
|
+
for region_name in region_names
|
|
356
|
+
for model in model_names
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def _gpugeek_job(target, adapter, notifier) -> WatchJob:
|
|
361
|
+
options = target.options
|
|
362
|
+
gpu = options.get("gpu")
|
|
363
|
+
region = options.get("region")
|
|
364
|
+
min_free = options["min_free"]
|
|
365
|
+
reset_prefixes = _gpugeek_prefixes(adapter, gpu, region)
|
|
366
|
+
|
|
367
|
+
return WatchJob(
|
|
368
|
+
name=target.name,
|
|
369
|
+
platform="gpugeek",
|
|
370
|
+
kinds=("cloud",),
|
|
371
|
+
gpu_patterns=(gpu,) if gpu else (),
|
|
372
|
+
min_free=min_free,
|
|
373
|
+
fetch_offers=lambda kind: adapter.list_offers(
|
|
374
|
+
gpu_model=gpu,
|
|
375
|
+
region=region,
|
|
376
|
+
min_free=min_free,
|
|
377
|
+
max_price=options.get("max_price"),
|
|
378
|
+
min_vram_gb=options.get("min_vram"),
|
|
379
|
+
min_ram_gb=options.get("min_ram"),
|
|
380
|
+
),
|
|
381
|
+
reset_prefixes=reset_prefixes,
|
|
382
|
+
notifier=notifier,
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def _gpugeek_prefixes(adapter, gpu, region) -> tuple:
|
|
387
|
+
if not gpu and not region:
|
|
388
|
+
return ("gpugeek|cloud|",)
|
|
389
|
+
|
|
390
|
+
offers = adapter.list_offers(min_free=0)
|
|
391
|
+
if not offers:
|
|
392
|
+
return _gpugeek_catalog_prefixes(adapter, gpu, region)
|
|
393
|
+
|
|
394
|
+
region_names = None
|
|
395
|
+
if region:
|
|
396
|
+
region_names = sorted(
|
|
397
|
+
{
|
|
398
|
+
offer.region
|
|
399
|
+
for offer in offers
|
|
400
|
+
if _gpugeek_region_matches(offer, region)
|
|
401
|
+
}
|
|
402
|
+
)
|
|
403
|
+
if not region_names:
|
|
404
|
+
raise ValueError(f"GPUGeek 地区不存在: {region}")
|
|
405
|
+
|
|
406
|
+
model_names = None
|
|
407
|
+
if gpu:
|
|
408
|
+
model_names = sorted(
|
|
409
|
+
{
|
|
410
|
+
offer.gpu_model
|
|
411
|
+
for offer in offers
|
|
412
|
+
if gpu.casefold() in offer.gpu_model.casefold()
|
|
413
|
+
}
|
|
414
|
+
)
|
|
415
|
+
if not model_names:
|
|
416
|
+
raise ValueError(f"GPUGeek 型号不存在: {gpu}")
|
|
417
|
+
|
|
418
|
+
if region_names is not None and model_names is None:
|
|
419
|
+
return tuple(
|
|
420
|
+
f"gpugeek|cloud|{name}|"
|
|
421
|
+
for name in region_names
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
if region_names is None:
|
|
425
|
+
region_names = sorted(
|
|
426
|
+
{
|
|
427
|
+
offer.region
|
|
428
|
+
for offer in offers
|
|
429
|
+
if offer.region
|
|
430
|
+
}
|
|
431
|
+
)
|
|
432
|
+
if not region_names:
|
|
433
|
+
raise ValueError("GPUGeek 没有可用地区")
|
|
434
|
+
|
|
435
|
+
return tuple(
|
|
436
|
+
f"gpugeek|cloud|{region_name}|{model}|"
|
|
437
|
+
for region_name in region_names
|
|
438
|
+
for model in model_names
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _gpugeek_region_matches(offer, pattern: str) -> bool:
|
|
443
|
+
extra = offer.extra or {}
|
|
444
|
+
haystack = (
|
|
445
|
+
f"{offer.region or ''} "
|
|
446
|
+
f"{extra.get('region_name') or ''} "
|
|
447
|
+
f"{extra.get('dc_id') or ''}"
|
|
448
|
+
).casefold()
|
|
449
|
+
return pattern.casefold() in haystack
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def _gpugeek_catalog_prefixes(adapter, gpu, region) -> tuple:
|
|
453
|
+
region_names = None
|
|
454
|
+
if region:
|
|
455
|
+
regions = adapter.list_regions()
|
|
456
|
+
region_names = [
|
|
457
|
+
item["name"]
|
|
458
|
+
for item in regions
|
|
459
|
+
if region.casefold() in item["name"].casefold()
|
|
460
|
+
]
|
|
461
|
+
if not region_names:
|
|
462
|
+
raise ValueError(f"GPUGeek 地区不存在: {region}")
|
|
463
|
+
|
|
464
|
+
model_names = None
|
|
465
|
+
if gpu:
|
|
466
|
+
models = adapter.list_models()
|
|
467
|
+
model_names = [
|
|
468
|
+
item["name"]
|
|
469
|
+
for item in models
|
|
470
|
+
if gpu.casefold() in item["name"].casefold()
|
|
471
|
+
]
|
|
472
|
+
if not model_names:
|
|
473
|
+
raise ValueError(f"GPUGeek 型号不存在: {gpu}")
|
|
474
|
+
|
|
475
|
+
if region_names is not None and model_names is None:
|
|
476
|
+
return tuple(
|
|
477
|
+
f"gpugeek|cloud|{name}|"
|
|
478
|
+
for name in region_names
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
if region_names is None:
|
|
482
|
+
regions = adapter.list_regions()
|
|
483
|
+
region_names = [item["name"] for item in regions]
|
|
484
|
+
if not region_names:
|
|
485
|
+
raise ValueError("GPUGeek 没有可用地区")
|
|
486
|
+
|
|
487
|
+
return tuple(
|
|
488
|
+
f"gpugeek|cloud|{region_name}|{model}|"
|
|
489
|
+
for region_name in region_names
|
|
490
|
+
for model in model_names
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
def _gpushare_job(target, adapter, notifier, token_file) -> WatchJob:
|
|
495
|
+
options = target.options
|
|
496
|
+
gpu = options.get("gpu")
|
|
497
|
+
region = options.get("region")
|
|
498
|
+
min_free = options["min_free"]
|
|
499
|
+
reset_prefixes = _gpushare_prefixes(adapter, gpu, region)
|
|
500
|
+
|
|
501
|
+
return WatchJob(
|
|
502
|
+
name=target.name,
|
|
503
|
+
platform="gpushare",
|
|
504
|
+
kinds=("machine",),
|
|
505
|
+
gpu_patterns=(gpu,) if gpu else (),
|
|
506
|
+
min_free=min_free,
|
|
507
|
+
fetch_offers=lambda kind: adapter.list_machines(
|
|
508
|
+
gpu_model=gpu,
|
|
509
|
+
region=region,
|
|
510
|
+
min_free=min_free,
|
|
511
|
+
max_price=options.get("max_price"),
|
|
512
|
+
min_vram_gb=options.get("min_vram"),
|
|
513
|
+
min_ram_gb=options.get("min_ram"),
|
|
514
|
+
min_cuda=options.get("min_cuda"),
|
|
515
|
+
),
|
|
516
|
+
reset_prefixes=reset_prefixes,
|
|
517
|
+
credential_hint=_credential_hint(
|
|
518
|
+
"gpushare", token_file=token_file
|
|
519
|
+
),
|
|
520
|
+
notifier=notifier,
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
def _gpushare_prefixes(adapter, gpu, region) -> tuple:
|
|
525
|
+
if not gpu and not region:
|
|
526
|
+
return ("gpushare|machine|",)
|
|
527
|
+
|
|
528
|
+
region_names = None
|
|
529
|
+
if region:
|
|
530
|
+
regions = adapter.list_regions()
|
|
531
|
+
region_names = [
|
|
532
|
+
item["name"]
|
|
533
|
+
for item in regions
|
|
534
|
+
if region.casefold() in item["name"].casefold()
|
|
535
|
+
]
|
|
536
|
+
if not region_names:
|
|
537
|
+
raise ValueError(f"恒源云地区不存在: {region}")
|
|
538
|
+
|
|
539
|
+
model_names = None
|
|
540
|
+
if gpu:
|
|
541
|
+
models = adapter.list_models()
|
|
542
|
+
model_names = [
|
|
543
|
+
item["name"]
|
|
544
|
+
for item in models
|
|
545
|
+
if gpu.casefold() in item["name"].casefold()
|
|
546
|
+
]
|
|
547
|
+
if not model_names:
|
|
548
|
+
raise ValueError(f"恒源云型号不存在: {gpu}")
|
|
549
|
+
|
|
550
|
+
if region_names is not None and model_names is None:
|
|
551
|
+
return tuple(
|
|
552
|
+
f"gpushare|machine|{name}|"
|
|
553
|
+
for name in region_names
|
|
554
|
+
)
|
|
555
|
+
|
|
556
|
+
if region_names is None:
|
|
557
|
+
regions = adapter.list_regions()
|
|
558
|
+
region_names = [item["name"] for item in regions]
|
|
559
|
+
if not region_names:
|
|
560
|
+
raise ValueError("恒源云没有可用地区")
|
|
561
|
+
|
|
562
|
+
return tuple(
|
|
563
|
+
f"gpushare|machine|{region_name}|{model}|"
|
|
564
|
+
for region_name in region_names
|
|
565
|
+
for model in model_names
|
|
566
|
+
)
|
cloudgpu/lock.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LockError(RuntimeError):
|
|
8
|
+
"""状态文件已被另一个监控进程占用。"""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FileLock:
|
|
12
|
+
def __init__(self, path) -> None:
|
|
13
|
+
self.path = Path(path)
|
|
14
|
+
self._fd = None
|
|
15
|
+
|
|
16
|
+
def acquire(self) -> None:
|
|
17
|
+
if self._fd is not None:
|
|
18
|
+
return
|
|
19
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
fd = os.open(
|
|
21
|
+
str(self.path),
|
|
22
|
+
os.O_RDWR | os.O_CREAT,
|
|
23
|
+
0o600,
|
|
24
|
+
)
|
|
25
|
+
try:
|
|
26
|
+
if os.name == "nt":
|
|
27
|
+
import msvcrt
|
|
28
|
+
|
|
29
|
+
if os.fstat(fd).st_size == 0:
|
|
30
|
+
os.write(fd, b"\0")
|
|
31
|
+
os.lseek(fd, 0, os.SEEK_SET)
|
|
32
|
+
try:
|
|
33
|
+
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
|
|
34
|
+
except OSError:
|
|
35
|
+
raise LockError(
|
|
36
|
+
"另一个监控进程正在使用状态文件"
|
|
37
|
+
) from None
|
|
38
|
+
else:
|
|
39
|
+
import fcntl
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
fcntl.flock(
|
|
43
|
+
fd, fcntl.LOCK_EX | fcntl.LOCK_NB
|
|
44
|
+
)
|
|
45
|
+
except OSError:
|
|
46
|
+
raise LockError(
|
|
47
|
+
"另一个监控进程正在使用状态文件"
|
|
48
|
+
) from None
|
|
49
|
+
except Exception:
|
|
50
|
+
os.close(fd)
|
|
51
|
+
raise
|
|
52
|
+
self._fd = fd
|
|
53
|
+
|
|
54
|
+
def release(self) -> None:
|
|
55
|
+
if self._fd is None:
|
|
56
|
+
return
|
|
57
|
+
fd = self._fd
|
|
58
|
+
self._fd = None
|
|
59
|
+
try:
|
|
60
|
+
if os.name == "nt":
|
|
61
|
+
import msvcrt
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
|
|
65
|
+
except OSError:
|
|
66
|
+
pass
|
|
67
|
+
else:
|
|
68
|
+
import fcntl
|
|
69
|
+
|
|
70
|
+
try:
|
|
71
|
+
fcntl.flock(fd, fcntl.LOCK_UN)
|
|
72
|
+
except OSError:
|
|
73
|
+
pass
|
|
74
|
+
finally:
|
|
75
|
+
os.close(fd)
|
|
76
|
+
|
|
77
|
+
def __enter__(self):
|
|
78
|
+
self.acquire()
|
|
79
|
+
return self
|
|
80
|
+
|
|
81
|
+
def __exit__(self, exc_type, exc, tb):
|
|
82
|
+
self.release()
|
|
83
|
+
return False
|
cloudgpu/models.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict, dataclass
|
|
4
|
+
from typing import Any, Optional, Sequence
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class AdapterCapabilities:
|
|
9
|
+
"""平台适配器显式声明的能力,调度层据此决定可调用的操作。"""
|
|
10
|
+
|
|
11
|
+
read_only: bool = True
|
|
12
|
+
purchase: bool = False
|
|
13
|
+
machine_level: bool = False
|
|
14
|
+
region: bool = False
|
|
15
|
+
price: bool = False
|
|
16
|
+
total: bool = False
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class GpuOffer:
|
|
21
|
+
"""调度层统一视图。平台特有字段放 extra,不要求各平台同构。"""
|
|
22
|
+
|
|
23
|
+
platform: str
|
|
24
|
+
service_kind: Optional[str]
|
|
25
|
+
gpu_model: str
|
|
26
|
+
display_name: str
|
|
27
|
+
description: str
|
|
28
|
+
gpu_memory_gb: Optional[int]
|
|
29
|
+
free: int
|
|
30
|
+
total: Optional[int] = None
|
|
31
|
+
region: Optional[str] = None
|
|
32
|
+
machine_id: Optional[str] = None
|
|
33
|
+
price_per_hour: Optional[float] = None
|
|
34
|
+
charge_type: Optional[str] = None
|
|
35
|
+
url: Optional[str] = None
|
|
36
|
+
checked_at: str = ""
|
|
37
|
+
extra: Optional[dict] = None
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def available(self) -> bool:
|
|
41
|
+
return self.free > 0
|
|
42
|
+
|
|
43
|
+
def to_dict(self) -> dict[str, Any]:
|
|
44
|
+
return asdict(self)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def filter_offers(
|
|
48
|
+
offers: Sequence[GpuOffer],
|
|
49
|
+
gpu_patterns: Sequence[str] = (),
|
|
50
|
+
min_free: int = 1,
|
|
51
|
+
) -> list[GpuOffer]:
|
|
52
|
+
"""按型号子串和最少空闲数筛选,保持输入顺序。"""
|
|
53
|
+
patterns = [pattern.lower() for pattern in gpu_patterns if pattern]
|
|
54
|
+
result = []
|
|
55
|
+
for offer in offers:
|
|
56
|
+
if offer.free < min_free:
|
|
57
|
+
continue
|
|
58
|
+
if patterns:
|
|
59
|
+
haystack = (
|
|
60
|
+
f"{offer.gpu_model} {offer.display_name} {offer.description}".lower()
|
|
61
|
+
)
|
|
62
|
+
if not any(pattern in haystack for pattern in patterns):
|
|
63
|
+
continue
|
|
64
|
+
result.append(offer)
|
|
65
|
+
return result
|