-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.py
More file actions
454 lines (405 loc) · 15.4 KB
/
Copy pathinstance.py
File metadata and controls
454 lines (405 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""Stateful handles for individual CreateOS sandboxes."""
from __future__ import annotations
import copy
import re
import threading
import time
from urllib.parse import quote
from ._streams import CommandStream
from .errors import CommandError, OperationTimeout
from .models import (
AttachDiskOptions,
BandwidthView,
DestroyedResponse,
DetachDiskOptions,
DiskAttachment,
EgressView,
ExecOptions,
ForkSandboxRequest,
PaginationOptions,
ResizeSandboxResponse,
RunCommandRequest,
RunCommandResponse,
Sandbox,
SandboxAccessTokenCreateResponse,
SandboxAccessTokenMetadata,
SandboxDisk,
SandboxStatus,
WaitOptions,
)
from .services import (
ComputerService,
FilesService,
ProcessesService,
_fetch_all,
_one,
_request_options,
)
class SandboxInstance:
"""A stateful handle to one sandbox and its nested services."""
def __init__(self, transport, data: Sandbox) -> None:
self._transport = transport
self._data = data
self._lock = threading.RLock()
self.files = FilesService(self)
self.processes = ProcessesService(self)
self.computer = ComputerService(self)
@property
def id(self) -> str:
"""Return the sandbox identifier."""
return self._snapshot().id
@property
def name(self) -> str:
"""Return the user-visible sandbox name."""
return self._snapshot().name or ""
@property
def status(self) -> SandboxStatus:
"""Return the last observed lifecycle status."""
return self._snapshot().status
@property
def ip_address(self) -> str:
"""Return the last observed private IP address."""
return self._snapshot().ip_address or ""
@property
def data(self) -> Sandbox:
"""Return a copy of the last observed server projection."""
return self._snapshot()
def _snapshot(self):
with self._lock:
return copy.deepcopy(self._data)
def _update(self, data):
with self._lock:
self._data = data
def _path(self, suffix: str) -> str:
return f"/v1/sandboxes/{quote(self.id, safe='')}{suffix}"
def with_access_token(self, token: str) -> SandboxInstance:
"""Return a separate handle using a delegated sandbox token."""
token = token.strip()
if not token:
raise ValueError("sandbox access token must not be empty")
return SandboxInstance(
self._transport.with_api_key(token), self._snapshot()
)
def create_access_token(self) -> SandboxAccessTokenCreateResponse:
"""Create a token; its plaintext value is returned only once."""
return _one(
SandboxAccessTokenCreateResponse,
self._transport.request("POST", self._path("/access-token")),
)
def get_access_token(self) -> SandboxAccessTokenMetadata:
"""Read token state and its redacted hint."""
return _one(
SandboxAccessTokenMetadata,
self._transport.request("GET", self._path("/access-token")),
)
def rotate_access_token(self) -> SandboxAccessTokenCreateResponse:
"""Replace an existing token and return its new plaintext value."""
return _one(
SandboxAccessTokenCreateResponse,
self._transport.request("POST", self._path("/access-token/rotate")),
)
def disable_access_token(self) -> SandboxAccessTokenMetadata:
"""Revoke the current token, if present."""
return _one(
SandboxAccessTokenMetadata,
self._transport.request("DELETE", self._path("/access-token")),
)
def refresh(self) -> SandboxInstance:
"""Reload the server projection and return this handle."""
self._update(
_one(Sandbox, self._transport.request("GET", self._path("")))
)
return self
def _lifecycle(self, suffix: str) -> SandboxInstance:
self._update(
_one(Sandbox, self._transport.request("POST", self._path(suffix)))
)
return self
def pause(self) -> SandboxInstance:
"""Snapshot and pause the sandbox."""
return self._lifecycle("/pause")
def resume(self) -> SandboxInstance:
"""Restore the paused sandbox."""
return self._lifecycle("/resume")
def fork(
self, request: ForkSandboxRequest | None = None
) -> SandboxInstance:
"""Create an independent sandbox from this sandbox."""
data = self._transport.request(
"POST", self._path("/fork"), body=request or ForkSandboxRequest()
)
return SandboxInstance(self._transport, _one(Sandbox, data))
def destroy(self) -> None:
"""Start destruction of the sandbox."""
response = _one(
DestroyedResponse,
self._transport.request("DELETE", self._path("")),
)
with self._lock:
self._data.status = response.status
def resize(self, disk_mib: int) -> ResizeSandboxResponse:
"""Grow the sandbox overlay disk to the requested size."""
data = self._transport.request(
"POST", self._path("/resize"), body={"disk_mib": disk_mib}
)
result = _one(ResizeSandboxResponse, data)
with self._lock:
self._data.disk_mib = result.disk_mib
return result
def set_ingress(self, enabled: bool) -> SandboxInstance:
"""Enable or disable public ingress."""
data = self._transport.request(
"PATCH", self._path(""), body={"ingress_enabled": enabled}
)
self._update(_one(Sandbox, data))
return self
def set_auto_pause(self, timeout: float | None) -> SandboxInstance:
"""Set the idle timeout in seconds, or disable it with ``None``."""
body: dict[str, bool | int]
if timeout is None:
body = {"disable_auto_pause": True}
else:
if (
timeout < 60
or timeout > 86400
or not float(timeout).is_integer()
):
raise ValueError(
"auto-pause timeout must be whole seconds between "
"1 minute and 24 hours"
)
body = {"auto_pause_after_seconds": int(timeout)}
self._update(
_one(
Sandbox,
self._transport.request("PATCH", self._path(""), body=body),
)
)
return self
def add_ssh_public_keys(self, keys: list[str]) -> int:
"""Add OpenSSH public keys and return the resulting key count."""
data = self._transport.request(
"POST", self._path("/ssh-pubkeys"), body={"keys": keys}
)
return int(data["count"])
def run_command(
self, request: RunCommandRequest, options: ExecOptions | None = None
) -> RunCommandResponse:
"""Run a command and return its buffered output."""
request = copy.deepcopy(request)
options = options or ExecOptions()
if options.standard_input is not None:
request.standard_input = options.standard_input
if options.environment_variables is not None:
request.environment_variables = options.environment_variables
request.stream = False
data = self._transport.request(
"POST",
self._path("/exec"),
body=request,
options=_request_options(options),
)
return _one(RunCommandResponse, data)
def shell(
self, script: str, options: ExecOptions | None = None
) -> RunCommandResponse:
"""Run a Bash script and raise when it exits unsuccessfully."""
response = self.run_command(
RunCommandRequest(command="bash", arguments=["-lc", script]),
options,
)
result = response.result
if result.exit_code or result.error_message:
message = f"command exited with status {result.exit_code}"
if result.error_message:
message += f"\nerror: {result.error_message}"
if result.standard_error:
message += f"\nstderr: {result.standard_error[-2000:]}"
raise CommandError(message, response)
return response
def stream_command(
self, request: RunCommandRequest, options: ExecOptions | None = None
) -> CommandStream:
"""Run a command and return a context-managed event stream."""
request = copy.deepcopy(request)
options = options or ExecOptions()
request.stream = True
if options.standard_input is not None:
request.standard_input = options.standard_input
if options.environment_variables is not None:
request.environment_variables = options.environment_variables
response = self._transport.stream(
"POST",
self._path("/exec?stream=true"),
body=request,
options=_request_options(options),
)
return CommandStream(response)
def wait_until_running(
self, options: WaitOptions | None = None
) -> SandboxInstance:
"""Wait until the sandbox is running."""
return self._wait_for(
SandboxStatus.RUNNING,
{
SandboxStatus.ERROR,
SandboxStatus.FAILED,
SandboxStatus.DESTROYING,
SandboxStatus.DESTROYED,
},
options,
)
def wait_until_paused(
self, options: WaitOptions | None = None
) -> SandboxInstance:
"""Wait until the sandbox is paused."""
return self._wait_for(
SandboxStatus.PAUSED,
{
SandboxStatus.ERROR,
SandboxStatus.FAILED,
SandboxStatus.DESTROYING,
SandboxStatus.DESTROYED,
},
options,
)
def wait_until_destroyed(
self, options: WaitOptions | None = None
) -> SandboxInstance:
"""Wait until the sandbox is fully destroyed."""
return self._wait_for(
SandboxStatus.DESTROYED,
{SandboxStatus.ERROR, SandboxStatus.FAILED},
options,
)
def _wait_for(self, desired, terminal, options):
options = options or WaitOptions()
deadline = time.monotonic() + options.timeout
while True:
# Refresh the complete projection so cached properties remain
# consistent with the status observed by this poll.
data = _one(
Sandbox,
self._transport.request(
"GET", self._path(""), options=options.request
),
)
self._update(data)
if data.status in terminal:
raise RuntimeError(
f"sandbox {data.id!r} entered terminal state "
f"{data.status!r}"
)
if data.status == desired:
return self
remaining = deadline - time.monotonic()
if remaining <= 0:
raise OperationTimeout(
f"wait for sandbox {self.id!r} to become {desired!s}"
)
time.sleep(min(0.5, remaining))
def preview_url(/sitelet?url=https%3A%2F%2Fgithub.com%2FNodeOps-app%2Fcreateos-python-sdk%2Fblob%2Fmain%2Fsrc%2Fcreateos%2Fself%2C%2520port%3A%2520int) -> str:
"""Return the public ingress URL for a sandbox port."""
if not 1 <= port <= 65535:
raise ValueError(f"port must be between 1 and 65535, got {port}")
data = self._snapshot()
if not data.ingress_enabled or not data.ingress_url_template:
raise ValueError("sandbox ingress is not enabled")
return data.ingress_url_template.replace("<port>", str(port))
def wait_for_port(
self, port: int, host: str = "127.0.0.1", timeout: float = 30.0
) -> None:
"""Wait until a TCP port is listening inside the sandbox."""
if not 1 <= port <= 65535:
raise ValueError(f"port must be between 1 and 65535, got {port}")
if (
not host
or not re.fullmatch(r"[A-Za-z0-9.-]+", host)
or ".." in host
):
raise ValueError(
f"host {host!r} is not a valid IP address or DNS name"
)
seconds = max(1, int(timeout + 0.999))
script = (
f"timeout {seconds} bash -c 'until (echo > /dev/tcp/{host}/{port}) "
"2>/dev/null; do sleep 0.25; done'"
)
response = self.run_command(
RunCommandRequest(command="bash", arguments=["-c", script]),
ExecOptions(timeout=timeout + 5),
)
if response.result.exit_code:
raise OperationTimeout(
f"port {port} did not become ready within {timeout}s"
)
def egress(self) -> EgressView:
"""Return the current outbound network allowlist."""
return _one(
EgressView, self._transport.request("GET", self._path("/egress"))
)
def set_egress(self, rules: list[str]) -> EgressView:
"""Replace and return the outbound network allowlist."""
return _one(
EgressView,
self._transport.request(
"PUT", self._path("/egress"), body={"egress": rules}
),
)
def bandwidth(self) -> BandwidthView:
"""Return sandbox bandwidth quota and usage counters."""
return _one(
BandwidthView,
self._transport.request("GET", self._path("/bandwidth")),
)
def recharge_bandwidth(self, bytes_: int) -> BandwidthView:
"""Add bytes to the sandbox bandwidth quota."""
return _one(
BandwidthView,
self._transport.request(
"POST",
self._path("/bandwidth/recharge"),
body={"add_bytes": bytes_},
),
)
def attach_network(self, network_id: str) -> None:
"""Connect this sandbox to an overlay network."""
self._transport.request(
"POST", self._path("/networks"), body={"id": network_id}
)
def detach_network(self, network_id: str) -> None:
"""Disconnect this sandbox from an overlay network."""
self._transport.request(
"DELETE", self._path(f"/networks/{quote(network_id, safe='')}")
)
def list_disks(
self, options: PaginationOptions | None = None
) -> list[SandboxDisk]:
"""Return persistent disks attached to this sandbox."""
return _fetch_all(
self._transport,
self._path("/disks"),
SandboxDisk,
options or PaginationOptions(),
legacy_key="disks",
)
def attach_disk(self, options: AttachDiskOptions) -> None:
"""Mount a registered persistent disk in this sandbox."""
body = DiskAttachment(
options.disk_id, options.mount_path, options.sub_path
)
data = self._transport.request("POST", self._path("/disks"), body=body)
if data.get("id") != self.id:
raise RuntimeError(
f"server acknowledged sandbox {data.get('id')!r}, "
f"expected {self.id!r}"
)
def detach_disk(self, options: DetachDiskOptions) -> bool:
"""Unmount a persistent disk and report whether it detached."""
data = self._transport.request(
"DELETE",
self._path(f"/disks/{quote(options.disk_id, safe='')}"),
params={"mount_path": options.mount_path},
)
return bool(data.get("detached"))
Instance = SandboxInstance