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/autodl_buy.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import secrets
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
|
|
7
|
+
from cloudgpu.errors import (
|
|
8
|
+
AuthFailedError,
|
|
9
|
+
OrderRejectedError,
|
|
10
|
+
OrderUncertainError,
|
|
11
|
+
)
|
|
12
|
+
from cloudgpu.orders import OrderConflictError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class BuyPlan:
|
|
17
|
+
gpu_model: str
|
|
18
|
+
count: int
|
|
19
|
+
image: object
|
|
20
|
+
offer: object
|
|
21
|
+
quote: object
|
|
22
|
+
instance_name: str
|
|
23
|
+
region: object = None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class BuyResult:
|
|
28
|
+
plan: BuyPlan
|
|
29
|
+
quote: object
|
|
30
|
+
order_id: int
|
|
31
|
+
status: str
|
|
32
|
+
instance_uuid: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class BuyBlockedError(RuntimeError):
|
|
36
|
+
"""存在结果不明的订单,必须先对账。"""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class NoCandidateError(RuntimeError):
|
|
40
|
+
"""没有满足条件的候选机器。"""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def prepare_buy(
|
|
44
|
+
adapter,
|
|
45
|
+
store,
|
|
46
|
+
*,
|
|
47
|
+
gpu_model,
|
|
48
|
+
count,
|
|
49
|
+
image_id,
|
|
50
|
+
region=None,
|
|
51
|
+
max_hourly=None,
|
|
52
|
+
name_prefix="cloudgpu",
|
|
53
|
+
clock=None,
|
|
54
|
+
):
|
|
55
|
+
if not isinstance(gpu_model, str) or not gpu_model:
|
|
56
|
+
raise ValueError("gpu_model 不能为空")
|
|
57
|
+
if (
|
|
58
|
+
isinstance(count, bool)
|
|
59
|
+
or not isinstance(count, int)
|
|
60
|
+
or count < 1
|
|
61
|
+
):
|
|
62
|
+
raise ValueError("count 必须是 >=1 的整数")
|
|
63
|
+
if max_hourly is not None and max_hourly <= 0:
|
|
64
|
+
raise ValueError("max_hourly 必须大于 0")
|
|
65
|
+
if not isinstance(image_id, str) or not image_id:
|
|
66
|
+
raise ValueError("image_id 不能为空")
|
|
67
|
+
|
|
68
|
+
blocking = store.blocking()
|
|
69
|
+
if blocking:
|
|
70
|
+
ids = ", ".join(str(row["id"]) for row in blocking)
|
|
71
|
+
raise BuyBlockedError(
|
|
72
|
+
f"存在未解决的订单 {ids},请先运行 "
|
|
73
|
+
"autodl orders --reconcile"
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
images = adapter.list_images(gpu_model=gpu_model)
|
|
77
|
+
image = next(
|
|
78
|
+
(item for item in images if item.id == image_id), None
|
|
79
|
+
)
|
|
80
|
+
if image is None:
|
|
81
|
+
raise ValueError(
|
|
82
|
+
f"镜像不存在或不兼容: {image_id}"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
candidates = adapter.search_candidates(
|
|
86
|
+
gpu_model=gpu_model,
|
|
87
|
+
count=count,
|
|
88
|
+
region=region,
|
|
89
|
+
)
|
|
90
|
+
if not candidates:
|
|
91
|
+
raise NoCandidateError("没有满足条件的候选机器")
|
|
92
|
+
|
|
93
|
+
for candidate in candidates[:5]:
|
|
94
|
+
if not _cuda_ok(image, candidate):
|
|
95
|
+
continue
|
|
96
|
+
quote = adapter.preview_order(
|
|
97
|
+
machine_id=candidate.machine_id,
|
|
98
|
+
count=count,
|
|
99
|
+
)
|
|
100
|
+
if (
|
|
101
|
+
max_hourly is not None
|
|
102
|
+
and quote.hourly_total > max_hourly
|
|
103
|
+
):
|
|
104
|
+
continue
|
|
105
|
+
name = _unique_name(store, name_prefix, clock)
|
|
106
|
+
return BuyPlan(
|
|
107
|
+
gpu_model=gpu_model,
|
|
108
|
+
count=count,
|
|
109
|
+
image=image,
|
|
110
|
+
offer=candidate,
|
|
111
|
+
quote=quote,
|
|
112
|
+
instance_name=name,
|
|
113
|
+
region=region,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
raise NoCandidateError(
|
|
117
|
+
"没有满足价格、镜像 CUDA 和库存条件的候选机器"
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def execute_buy(
|
|
122
|
+
adapter, store, plan, *, max_hourly, clock=None
|
|
123
|
+
):
|
|
124
|
+
if max_hourly is None or max_hourly <= 0:
|
|
125
|
+
raise ValueError("--execute 必须提供 --max-hourly")
|
|
126
|
+
blocking = store.blocking()
|
|
127
|
+
if blocking:
|
|
128
|
+
ids = ", ".join(str(row["id"]) for row in blocking)
|
|
129
|
+
raise BuyBlockedError(
|
|
130
|
+
f"存在未解决的订单 {ids},请先运行 "
|
|
131
|
+
"autodl orders --reconcile"
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
candidates = adapter.search_candidates(
|
|
135
|
+
gpu_model=plan.gpu_model,
|
|
136
|
+
count=plan.count,
|
|
137
|
+
region=plan.region,
|
|
138
|
+
)
|
|
139
|
+
offer = next(
|
|
140
|
+
(
|
|
141
|
+
item
|
|
142
|
+
for item in candidates
|
|
143
|
+
if item.machine_id == plan.offer.machine_id
|
|
144
|
+
and item.free >= plan.count
|
|
145
|
+
),
|
|
146
|
+
None,
|
|
147
|
+
)
|
|
148
|
+
if offer is None:
|
|
149
|
+
raise NoCandidateError(
|
|
150
|
+
"提交前复核失败: 机器已无足够空闲"
|
|
151
|
+
)
|
|
152
|
+
if not _cuda_ok(plan.image, offer):
|
|
153
|
+
raise NoCandidateError(
|
|
154
|
+
"提交前复核失败: 镜像与主机 CUDA 不兼容"
|
|
155
|
+
)
|
|
156
|
+
quote = adapter.preview_order(
|
|
157
|
+
machine_id=offer.machine_id,
|
|
158
|
+
count=plan.count,
|
|
159
|
+
)
|
|
160
|
+
if quote.hourly_total > max_hourly:
|
|
161
|
+
raise NoCandidateError(
|
|
162
|
+
"提交前复核失败: 报价超过 --max-hourly"
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
order_id = store.create_intent_if_clear(
|
|
167
|
+
instance_name=plan.instance_name,
|
|
168
|
+
machine_id=offer.machine_id,
|
|
169
|
+
gpu_model=plan.gpu_model,
|
|
170
|
+
gpu_count=plan.count,
|
|
171
|
+
image_id=plan.image.id,
|
|
172
|
+
hourly_total=quote.hourly_total,
|
|
173
|
+
)
|
|
174
|
+
except OrderConflictError as exc:
|
|
175
|
+
raise BuyBlockedError(str(exc)) from None
|
|
176
|
+
|
|
177
|
+
try:
|
|
178
|
+
instance_uuid = adapter.create_payg_order(
|
|
179
|
+
machine_id=offer.machine_id,
|
|
180
|
+
count=plan.count,
|
|
181
|
+
image=plan.image,
|
|
182
|
+
instance_name=plan.instance_name,
|
|
183
|
+
price_info=quote.price_info,
|
|
184
|
+
)
|
|
185
|
+
except OrderRejectedError as exc:
|
|
186
|
+
status = (
|
|
187
|
+
"sold_out" if exc.kind == "sold_out" else "rejected"
|
|
188
|
+
)
|
|
189
|
+
store.mark_status(order_id, status, str(exc))
|
|
190
|
+
raise
|
|
191
|
+
except AuthFailedError as exc:
|
|
192
|
+
store.mark_status(order_id, "rejected", str(exc))
|
|
193
|
+
raise
|
|
194
|
+
except OrderUncertainError as exc:
|
|
195
|
+
store.mark_status(order_id, "unknown", str(exc))
|
|
196
|
+
raise
|
|
197
|
+
|
|
198
|
+
store.mark_created(order_id, instance_uuid)
|
|
199
|
+
return BuyResult(
|
|
200
|
+
plan=plan,
|
|
201
|
+
quote=quote,
|
|
202
|
+
order_id=order_id,
|
|
203
|
+
status="created",
|
|
204
|
+
instance_uuid=instance_uuid,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def reconcile_orders(adapter, store) -> list:
|
|
209
|
+
for order in store.blocking():
|
|
210
|
+
rows = adapter.list_instances(
|
|
211
|
+
name=order["instance_name"]
|
|
212
|
+
)
|
|
213
|
+
match = next(
|
|
214
|
+
(
|
|
215
|
+
row
|
|
216
|
+
for row in rows
|
|
217
|
+
if getattr(row, "name", None)
|
|
218
|
+
== order["instance_name"]
|
|
219
|
+
and getattr(row, "uuid", None)
|
|
220
|
+
),
|
|
221
|
+
None,
|
|
222
|
+
)
|
|
223
|
+
if match is not None:
|
|
224
|
+
store.mark_created(order["id"], match.uuid)
|
|
225
|
+
return store.all()
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _cuda_ok(image, offer) -> bool:
|
|
229
|
+
if not image.cuda:
|
|
230
|
+
return True
|
|
231
|
+
host = (offer.extra or {}).get("highest_cuda_version")
|
|
232
|
+
image_version = _cuda_version(image.cuda)
|
|
233
|
+
host_version = _cuda_version(host)
|
|
234
|
+
if image_version is None or host_version is None:
|
|
235
|
+
return False
|
|
236
|
+
return image_version <= host_version
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _cuda_version(value):
|
|
240
|
+
if value is None:
|
|
241
|
+
return None
|
|
242
|
+
parts = str(value).strip().split(".")
|
|
243
|
+
try:
|
|
244
|
+
major = int(parts[0])
|
|
245
|
+
minor = int(parts[1]) if len(parts) > 1 else 0
|
|
246
|
+
except (ValueError, IndexError):
|
|
247
|
+
return None
|
|
248
|
+
return major, minor
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _unique_name(store, prefix: str, clock) -> str:
|
|
252
|
+
now = clock() if clock is not None else datetime.now(
|
|
253
|
+
timezone.utc
|
|
254
|
+
)
|
|
255
|
+
if now.tzinfo is None:
|
|
256
|
+
now = now.replace(tzinfo=timezone.utc)
|
|
257
|
+
stamp = now.astimezone(timezone.utc).strftime(
|
|
258
|
+
"%Y%m%d%H%M%S"
|
|
259
|
+
)
|
|
260
|
+
for _ in range(50):
|
|
261
|
+
name = (
|
|
262
|
+
f"{prefix}-{stamp}-{secrets.token_hex(2)}"
|
|
263
|
+
)
|
|
264
|
+
if not store.has_name(name):
|
|
265
|
+
return name
|
|
266
|
+
raise RuntimeError("无法生成唯一实例名")
|