-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathbuild_api.py
More file actions
379 lines (309 loc) · 10.6 KB
/
build_api.py
File metadata and controls
379 lines (309 loc) · 10.6 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
import time
from types import TracebackType
from typing import Callable, Optional, List, Union
import httpx
from e2b.api import handle_api_exception
from e2b.api.client.api.templates import (
post_v3_templates,
get_templates_template_id_files_hash,
post_v_2_templates_template_id_builds_build_id,
get_templates_template_id_builds_build_id_status,
get_templates_aliases_alias,
)
from e2b.api.client.api.tags import (
post_templates_tags,
delete_templates_tags,
get_templates_template_id_tags,
)
from e2b.api.client.client import AuthenticatedClient
from e2b.api.client.models import (
TemplateBuildRequestV3,
TemplateBuildStartV2,
TemplateBuildFileUpload,
Error,
AssignTemplateTagsRequest,
DeleteTemplateTagsRequest,
)
from e2b.api.client.types import UNSET, Unset
from e2b.exceptions import BuildException, FileUploadException, TemplateException
from e2b.template.logger import LogEntry
from e2b.template.types import (
TemplateType,
BuildStatusReason,
TemplateBuildStatus,
TemplateBuildStatusResponse,
TemplateTag,
TemplateTagInfo,
)
from e2b.template.utils import get_build_step_index, tar_file_stream
def request_build(
client: AuthenticatedClient,
name: str,
tags: Optional[List[str]],
cpu_count: int,
memory_mb: int,
):
res = post_v3_templates.sync_detailed(
client=client,
body=TemplateBuildRequestV3(
name=name,
tags=tags if tags else UNSET,
cpu_count=cpu_count,
memory_mb=memory_mb,
),
)
if res.status_code >= 300:
raise handle_api_exception(res, BuildException)
if isinstance(res.parsed, Error):
raise BuildException(f"API error: {res.parsed.message}")
if res.parsed is None:
raise BuildException("Failed to request build")
return res.parsed
def get_file_upload_link(
client: AuthenticatedClient,
template_id: str,
files_hash: str,
stack_trace: Optional[TracebackType] = None,
) -> TemplateBuildFileUpload:
res = get_templates_template_id_files_hash.sync_detailed(
template_id=template_id,
hash_=files_hash,
client=client,
)
if res.status_code >= 300:
raise handle_api_exception(res, FileUploadException, stack_trace)
if isinstance(res.parsed, Error):
raise FileUploadException(f"API error: {res.parsed.message}").with_traceback(
stack_trace
)
if res.parsed is None:
raise FileUploadException("Failed to get file upload link").with_traceback(
stack_trace
)
return res.parsed
def upload_file(
api_client: AuthenticatedClient,
file_name: str,
context_path: str,
url: str,
ignore_patterns: List[str],
resolve_symlinks: bool,
stack_trace: Optional[TracebackType],
):
try:
tar_buffer = tar_file_stream(
file_name, context_path, ignore_patterns, resolve_symlinks
)
# Use bytes with explicit Content-Length.
# S3 presigned PUT URLs do not support Transfer-Encoding: chunked.
body = tar_buffer.getvalue()
client = api_client.get_httpx_client()
response = client.put(
url,
content=body,
headers={"Content-Length": str(len(body))},
)
response.raise_for_status()
except httpx.HTTPStatusError as e:
raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
stack_trace
)
except Exception as e:
raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
stack_trace
)
def trigger_build(
client: AuthenticatedClient,
template_id: str,
build_id: str,
template: TemplateType,
) -> None:
# Convert template dict to TemplateBuildStartV2 model using from_dict
template_data = TemplateBuildStartV2.from_dict(template)
res = post_v_2_templates_template_id_builds_build_id.sync_detailed(
template_id=template_id,
build_id=build_id,
client=client,
body=template_data,
)
if res.status_code >= 300:
raise handle_api_exception(res, BuildException)
def _map_log_entry(entry) -> LogEntry:
"""Map API log entry to LogEntry type."""
return LogEntry(
timestamp=entry.timestamp,
level=entry.level.value,
message=entry.message,
)
def _map_build_status_reason(reason) -> Optional[BuildStatusReason]:
"""Map API build status reason to custom BuildStatusReason type."""
if reason is None or isinstance(reason, Unset):
return None
return BuildStatusReason(
message=reason.message,
step=reason.step if not isinstance(reason.step, Unset) else None,
log_entries=[
_map_log_entry(e)
for e in (
reason.log_entries
if not isinstance(reason.log_entries, Unset) and reason.log_entries
else []
)
],
)
def get_build_status(
client: AuthenticatedClient, template_id: str, build_id: str, logs_offset: int
) -> TemplateBuildStatusResponse:
res = get_templates_template_id_builds_build_id_status.sync_detailed(
template_id=template_id,
build_id=build_id,
client=client,
logs_offset=logs_offset,
)
if res.status_code >= 300:
raise handle_api_exception(res, BuildException)
if isinstance(res.parsed, Error):
raise BuildException(f"API error: {res.parsed.message}")
if res.parsed is None:
raise BuildException("Failed to get build status")
return TemplateBuildStatusResponse(
build_id=res.parsed.build_id,
template_id=res.parsed.template_id,
status=TemplateBuildStatus(res.parsed.status.value),
log_entries=[_map_log_entry(e) for e in res.parsed.log_entries],
logs=res.parsed.logs,
reason=_map_build_status_reason(res.parsed.reason),
)
def wait_for_build_finish(
client: AuthenticatedClient,
template_id: str,
build_id: str,
on_build_logs: Optional[Callable[[LogEntry], None]] = None,
logs_refresh_frequency: float = 0.2,
stack_traces: List[Union[TracebackType, None]] = [],
):
logs_offset = 0
status = TemplateBuildStatus.BUILDING
while status in [TemplateBuildStatus.BUILDING, TemplateBuildStatus.WAITING]:
build_status = get_build_status(client, template_id, build_id, logs_offset)
logs_offset += len(build_status.log_entries)
for log_entry in build_status.log_entries:
if on_build_logs:
on_build_logs(log_entry)
status = build_status.status
if status == TemplateBuildStatus.READY:
return
elif status == TemplateBuildStatus.WAITING:
pass
elif status == TemplateBuildStatus.ERROR:
traceback = None
if build_status.reason and build_status.reason.step:
# Find the corresponding stack trace for the failed step
step_index = get_build_step_index(
build_status.reason.step, len(stack_traces)
)
if step_index < len(stack_traces):
traceback = stack_traces[step_index]
raise BuildException(
build_status.reason.message if build_status.reason else "Build failed"
).with_traceback(traceback)
# Wait for a short period before checking the status again
time.sleep(logs_refresh_frequency)
raise BuildException("Unknown build error occurred.")
def check_alias_exists(client: AuthenticatedClient, alias: str) -> bool:
"""
Check if a template with the given alias exists.
Args:
client: Authenticated API client
alias: Template alias to check
Returns:
True if the alias exists, False otherwise
"""
res = get_templates_aliases_alias.sync_detailed(
alias=alias,
client=client,
)
# If we get a NotFound, the alias doesn't exist
if res.status_code == 404:
return False
# If we get a Forbidden, alias exists, but you are not owner
if res.status_code == 403:
return True
# Handle other errors
if res.status_code >= 300:
raise handle_api_exception(res, TemplateException)
# If we get Ok with data, you are owner and the alias exists
return res.parsed is not None
def assign_tags(
client: AuthenticatedClient, target_name: str, tags: List[str]
) -> TemplateTagInfo:
"""
Assign tag(s) to an existing template build.
Args:
client: Authenticated API client
target_name: Template name in 'name:tag' format (the source build to tag from)
tags: Tags to assign
Returns:
TemplateTagInfo with build_id and assigned tags
"""
res = post_templates_tags.sync_detailed(
client=client,
body=AssignTemplateTagsRequest(
target=target_name,
tags=tags,
),
)
if res.status_code >= 300:
raise handle_api_exception(res, TemplateException)
if isinstance(res.parsed, Error):
raise TemplateException(f"API error: {res.parsed.message}")
if res.parsed is None:
raise TemplateException("Failed to assign tags")
return TemplateTagInfo(
build_id=str(res.parsed.build_id),
tags=res.parsed.tags,
)
def remove_tags(client: AuthenticatedClient, name: str, tags: List[str]) -> None:
"""
Remove tag(s) from a template.
Args:
client: Authenticated API client
name: Template name
tags: List of tags to remove
"""
res = delete_templates_tags.sync_detailed(
client=client,
body=DeleteTemplateTagsRequest(
name=name,
tags=tags,
),
)
if res.status_code >= 300:
raise handle_api_exception(res, TemplateException)
def get_template_tags(
client: AuthenticatedClient, template_id: str
) -> List[TemplateTag]:
"""
Get all tags for a template.
Args:
client: Authenticated API client
template_id: Template ID or name
"""
res = get_templates_template_id_tags.sync_detailed(
template_id=template_id,
client=client,
)
if res.status_code >= 300:
raise handle_api_exception(res, TemplateException)
if isinstance(res.parsed, Error):
raise TemplateException(f"API error: {res.parsed.message}")
if res.parsed is None:
raise TemplateException("Failed to get template tags")
return [
TemplateTag(
tag=item.tag,
build_id=str(item.build_id),
created_at=item.created_at,
)
for item in res.parsed
]