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/gongji_state.py
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from cloudgpu.lock import FileLock
|
|
9
|
+
|
|
10
|
+
DEFAULT_PENDING = Path.home() / ".cloudgpu" / "gongji_pending.json"
|
|
11
|
+
DEFAULT_TTL = Path.home() / ".cloudgpu" / "gongji_ttl.json"
|
|
12
|
+
|
|
13
|
+
_BLOCKING = {"submitting", "unknown"}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class GongjiStateError(RuntimeError):
|
|
17
|
+
"""pending/TTL 台账损坏或结构非法。"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class GongjiPendingStore:
|
|
21
|
+
def __init__(self, path=None, clock=None) -> None:
|
|
22
|
+
self.path = (
|
|
23
|
+
Path(path).expanduser() if path else DEFAULT_PENDING
|
|
24
|
+
)
|
|
25
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
26
|
+
self._clock = clock or (
|
|
27
|
+
lambda: datetime.now(timezone.utc)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def all(self) -> list:
|
|
31
|
+
return list(self._load()["entries"])
|
|
32
|
+
|
|
33
|
+
def blocking(self) -> list:
|
|
34
|
+
return [
|
|
35
|
+
entry
|
|
36
|
+
for entry in self._load()["entries"]
|
|
37
|
+
if entry.get("status") in _BLOCKING
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
def has_name(self, name: str) -> bool:
|
|
41
|
+
return any(
|
|
42
|
+
entry.get("name") == name
|
|
43
|
+
for entry in self._load()["entries"]
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def add_submitting(
|
|
47
|
+
self,
|
|
48
|
+
*,
|
|
49
|
+
name: str,
|
|
50
|
+
image: str,
|
|
51
|
+
mark: str,
|
|
52
|
+
resource: dict,
|
|
53
|
+
ttl_seconds=None,
|
|
54
|
+
) -> None:
|
|
55
|
+
if not isinstance(name, str) or not name:
|
|
56
|
+
raise ValueError("name 不能为空")
|
|
57
|
+
with self._lock():
|
|
58
|
+
data = self._load()
|
|
59
|
+
if any(
|
|
60
|
+
entry.get("status") in _BLOCKING
|
|
61
|
+
for entry in data["entries"]
|
|
62
|
+
):
|
|
63
|
+
raise ValueError(
|
|
64
|
+
"存在未解决的共绩任务,请先 reconcile"
|
|
65
|
+
)
|
|
66
|
+
if any(
|
|
67
|
+
entry.get("name") == name
|
|
68
|
+
for entry in data["entries"]
|
|
69
|
+
):
|
|
70
|
+
raise ValueError(f"任务名已存在: {name}")
|
|
71
|
+
now = self._now()
|
|
72
|
+
data["entries"].append(
|
|
73
|
+
{
|
|
74
|
+
"name": name,
|
|
75
|
+
"status": "submitting",
|
|
76
|
+
"task_id": None,
|
|
77
|
+
"image": image,
|
|
78
|
+
"mark": mark,
|
|
79
|
+
"resource": dict(resource),
|
|
80
|
+
"ttl_seconds": ttl_seconds,
|
|
81
|
+
"created_at": now,
|
|
82
|
+
"updated_at": now,
|
|
83
|
+
"error": None,
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
self._save(data)
|
|
87
|
+
|
|
88
|
+
def mark_created(self, name: str, task_id: int) -> None:
|
|
89
|
+
self._update(
|
|
90
|
+
name,
|
|
91
|
+
{"submitting", "unknown"},
|
|
92
|
+
"created",
|
|
93
|
+
task_id=task_id,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
def mark_rejected(self, name: str, error: str) -> None:
|
|
97
|
+
self._update(
|
|
98
|
+
name, {"submitting"}, "rejected", error=error
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
def mark_unknown(self, name: str, error: str) -> None:
|
|
102
|
+
self._update(
|
|
103
|
+
name, {"submitting"}, "unknown", error=error
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def resolve_not_created(self, name: str) -> None:
|
|
107
|
+
self._update(
|
|
108
|
+
name, {"submitting", "unknown"}, "not_created"
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
def _update(
|
|
112
|
+
self,
|
|
113
|
+
name: str,
|
|
114
|
+
allowed,
|
|
115
|
+
status: str,
|
|
116
|
+
*,
|
|
117
|
+
task_id=None,
|
|
118
|
+
error=None,
|
|
119
|
+
) -> None:
|
|
120
|
+
with self._lock():
|
|
121
|
+
data = self._load()
|
|
122
|
+
for entry in data["entries"]:
|
|
123
|
+
if entry.get("name") != name:
|
|
124
|
+
continue
|
|
125
|
+
if entry.get("status") not in allowed:
|
|
126
|
+
raise ValueError(
|
|
127
|
+
f"任务 {name} 不能从 "
|
|
128
|
+
f"{entry.get('status')} 转为 {status}"
|
|
129
|
+
)
|
|
130
|
+
entry["status"] = status
|
|
131
|
+
if task_id is not None:
|
|
132
|
+
entry["task_id"] = task_id
|
|
133
|
+
if error is not None:
|
|
134
|
+
entry["error"] = error
|
|
135
|
+
entry["updated_at"] = self._now()
|
|
136
|
+
self._save(data)
|
|
137
|
+
return
|
|
138
|
+
raise ValueError(f"任务不存在: {name}")
|
|
139
|
+
|
|
140
|
+
def _lock(self):
|
|
141
|
+
return FileLock(str(self.path) + ".lock")
|
|
142
|
+
|
|
143
|
+
def _load(self) -> dict:
|
|
144
|
+
if not self.path.exists():
|
|
145
|
+
return {"entries": []}
|
|
146
|
+
try:
|
|
147
|
+
data = json.loads(
|
|
148
|
+
self.path.read_text(encoding="utf-8")
|
|
149
|
+
)
|
|
150
|
+
except (OSError, json.JSONDecodeError) as exc:
|
|
151
|
+
raise GongjiStateError(
|
|
152
|
+
f"共绩 pending 台账损坏: {self.path}: {exc}"
|
|
153
|
+
) from None
|
|
154
|
+
if not isinstance(data, dict) or not isinstance(
|
|
155
|
+
data.get("entries"), list
|
|
156
|
+
):
|
|
157
|
+
raise GongjiStateError(
|
|
158
|
+
f"共绩 pending 台账结构非法: {self.path}"
|
|
159
|
+
)
|
|
160
|
+
names = set()
|
|
161
|
+
for entry in data["entries"]:
|
|
162
|
+
if not isinstance(entry, dict):
|
|
163
|
+
raise GongjiStateError(
|
|
164
|
+
"共绩 pending 台账条目不是对象"
|
|
165
|
+
)
|
|
166
|
+
name = entry.get("name")
|
|
167
|
+
if not isinstance(name, str) or not name:
|
|
168
|
+
raise GongjiStateError(
|
|
169
|
+
"共绩 pending 台账任务名非法"
|
|
170
|
+
)
|
|
171
|
+
if name in names:
|
|
172
|
+
raise GongjiStateError(
|
|
173
|
+
f"共绩 pending 台账任务名重复: {name}"
|
|
174
|
+
)
|
|
175
|
+
names.add(name)
|
|
176
|
+
return data
|
|
177
|
+
|
|
178
|
+
def _save(self, data: dict) -> None:
|
|
179
|
+
tmp = self.path.with_name(self.path.name + ".tmp")
|
|
180
|
+
tmp.write_text(
|
|
181
|
+
json.dumps(data, ensure_ascii=False, indent=2),
|
|
182
|
+
encoding="utf-8",
|
|
183
|
+
)
|
|
184
|
+
try:
|
|
185
|
+
tmp.chmod(0o600)
|
|
186
|
+
except OSError:
|
|
187
|
+
pass
|
|
188
|
+
os.replace(tmp, self.path)
|
|
189
|
+
try:
|
|
190
|
+
self.path.chmod(0o600)
|
|
191
|
+
except OSError:
|
|
192
|
+
pass
|
|
193
|
+
|
|
194
|
+
def _now(self) -> str:
|
|
195
|
+
value = self._clock()
|
|
196
|
+
if value.tzinfo is None:
|
|
197
|
+
value = value.replace(tzinfo=timezone.utc)
|
|
198
|
+
return value.astimezone(timezone.utc).strftime(
|
|
199
|
+
"%Y-%m-%dT%H:%M:%SZ"
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class GongjiTtlStore:
|
|
204
|
+
def __init__(self, path=None, clock=None) -> None:
|
|
205
|
+
self.path = (
|
|
206
|
+
Path(path).expanduser() if path else DEFAULT_TTL
|
|
207
|
+
)
|
|
208
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
209
|
+
self._clock = clock or (
|
|
210
|
+
lambda: datetime.now(timezone.utc)
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
def all(self) -> list:
|
|
214
|
+
data = self._load()
|
|
215
|
+
rows = []
|
|
216
|
+
for key, entry in data["entries"].items():
|
|
217
|
+
item = dict(entry)
|
|
218
|
+
item["task_id"] = int(key)
|
|
219
|
+
rows.append(item)
|
|
220
|
+
rows.sort(key=lambda item: item["expire_at"])
|
|
221
|
+
return rows
|
|
222
|
+
|
|
223
|
+
def register(
|
|
224
|
+
self,
|
|
225
|
+
task_id: int,
|
|
226
|
+
ttl_seconds: int,
|
|
227
|
+
name: str,
|
|
228
|
+
auto_release: bool = True,
|
|
229
|
+
) -> None:
|
|
230
|
+
if ttl_seconds <= 0:
|
|
231
|
+
raise ValueError("ttl_seconds 必须大于 0")
|
|
232
|
+
with self._lock():
|
|
233
|
+
data = self._load()
|
|
234
|
+
data["entries"][str(task_id)] = {
|
|
235
|
+
"expire_at": self._now_epoch()
|
|
236
|
+
+ int(ttl_seconds),
|
|
237
|
+
"ttl_seconds": int(ttl_seconds),
|
|
238
|
+
"name": name,
|
|
239
|
+
"auto_release": bool(auto_release),
|
|
240
|
+
}
|
|
241
|
+
self._save(data)
|
|
242
|
+
|
|
243
|
+
def unregister(self, task_id: int) -> None:
|
|
244
|
+
with self._lock():
|
|
245
|
+
data = self._load()
|
|
246
|
+
data["entries"].pop(str(task_id), None)
|
|
247
|
+
self._save(data)
|
|
248
|
+
|
|
249
|
+
def list_expiring(self) -> list:
|
|
250
|
+
now = self._now_epoch()
|
|
251
|
+
rows = self.all()
|
|
252
|
+
for row in rows:
|
|
253
|
+
row["expires_in"] = round(
|
|
254
|
+
row["expire_at"] - now, 1
|
|
255
|
+
)
|
|
256
|
+
row["expired"] = row["expire_at"] <= now
|
|
257
|
+
return rows
|
|
258
|
+
|
|
259
|
+
def _lock(self):
|
|
260
|
+
return FileLock(str(self.path) + ".lock")
|
|
261
|
+
|
|
262
|
+
def _load(self) -> dict:
|
|
263
|
+
if not self.path.exists():
|
|
264
|
+
return {"entries": {}}
|
|
265
|
+
try:
|
|
266
|
+
data = json.loads(
|
|
267
|
+
self.path.read_text(encoding="utf-8")
|
|
268
|
+
)
|
|
269
|
+
except (OSError, json.JSONDecodeError) as exc:
|
|
270
|
+
raise GongjiStateError(
|
|
271
|
+
f"共绩 TTL 台账损坏: {self.path}: {exc}"
|
|
272
|
+
) from None
|
|
273
|
+
if not isinstance(data, dict) or not isinstance(
|
|
274
|
+
data.get("entries"), dict
|
|
275
|
+
):
|
|
276
|
+
raise GongjiStateError(
|
|
277
|
+
f"共绩 TTL 台账结构非法: {self.path}"
|
|
278
|
+
)
|
|
279
|
+
clean = {}
|
|
280
|
+
for key, entry in data["entries"].items():
|
|
281
|
+
if not str(key).isdigit():
|
|
282
|
+
raise GongjiStateError(
|
|
283
|
+
f"共绩 TTL 台账 task_id 非法: {key}"
|
|
284
|
+
)
|
|
285
|
+
if (
|
|
286
|
+
not isinstance(entry, dict)
|
|
287
|
+
or not isinstance(
|
|
288
|
+
entry.get("expire_at"), (int, float)
|
|
289
|
+
)
|
|
290
|
+
):
|
|
291
|
+
raise GongjiStateError(
|
|
292
|
+
f"共绩 TTL 台账条目非法: {key}"
|
|
293
|
+
)
|
|
294
|
+
clean[str(key)] = entry
|
|
295
|
+
return {"entries": clean}
|
|
296
|
+
|
|
297
|
+
def _save(self, data: dict) -> None:
|
|
298
|
+
tmp = self.path.with_name(self.path.name + ".tmp")
|
|
299
|
+
tmp.write_text(
|
|
300
|
+
json.dumps(data, ensure_ascii=False, indent=2),
|
|
301
|
+
encoding="utf-8",
|
|
302
|
+
)
|
|
303
|
+
try:
|
|
304
|
+
tmp.chmod(0o600)
|
|
305
|
+
except OSError:
|
|
306
|
+
pass
|
|
307
|
+
os.replace(tmp, self.path)
|
|
308
|
+
try:
|
|
309
|
+
self.path.chmod(0o600)
|
|
310
|
+
except OSError:
|
|
311
|
+
pass
|
|
312
|
+
|
|
313
|
+
def _now(self) -> datetime:
|
|
314
|
+
value = self._clock()
|
|
315
|
+
if value.tzinfo is None:
|
|
316
|
+
value = value.replace(tzinfo=timezone.utc)
|
|
317
|
+
return value
|
|
318
|
+
|
|
319
|
+
def _now_epoch(self) -> float:
|
|
320
|
+
return self._now().timestamp()
|