-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path_containers.py
More file actions
1134 lines (887 loc) · 35.9 KB
/
_containers.py
File metadata and controls
1134 lines (887 loc) · 35.9 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Container deployment and management service for Verda.
This module provides functionality for managing container deployments, including
creation, updates, deletion, and monitoring of containerized applications.
"""
import base64
import os
from dataclasses import dataclass, field
from enum import Enum
from typing import Any
from dataclasses_json import Undefined, config, dataclass_json # type: ignore
from verda.http_client import HTTPClient
from verda.inference_client import InferenceClient, InferenceResponse
# API endpoints
CONTAINER_DEPLOYMENTS_ENDPOINT = '/container-deployments'
SERVERLESS_COMPUTE_RESOURCES_ENDPOINT = '/serverless-compute-resources'
CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT = '/container-registry-credentials'
SECRETS_ENDPOINT = '/secrets'
FILESET_SECRETS_ENDPOINT = '/file-secrets'
class EnvVarType(str, Enum):
"""Types of environment variables that can be set in containers."""
PLAIN = 'plain'
SECRET = 'secret'
class SecretType(str, Enum):
"""Types of secrets that can be set in containers."""
GENERIC = 'generic' # Regular secret, can be used in env vars
FILESET = 'file-secret' # A file secret that can be mounted into the container
class VolumeMountType(str, Enum):
"""Types of volume mounts that can be configured for containers."""
SCRATCH = 'scratch'
SECRET = 'secret'
MEMORY = 'memory'
SHARED = 'shared'
class ContainerRegistryType(str, Enum):
"""Supported container registry types."""
GCR = 'gcr'
DOCKERHUB = 'dockerhub'
GITHUB = 'ghcr'
AWS_ECR = 'aws-ecr'
CUSTOM = 'custom'
class ContainerDeploymentStatus(str, Enum):
"""Possible states of a container deployment."""
INITIALIZING = 'initializing'
HEALTHY = 'healthy'
DEGRADED = 'degraded'
UNHEALTHY = 'unhealthy'
PAUSED = 'paused'
QUOTA_REACHED = 'quota_reached'
IMAGE_PULLING = 'image_pulling'
VERSION_UPDATING = 'version_updating'
@dataclass_json
@dataclass
class HealthcheckSettings:
"""Configuration for container health checking.
Attributes:
enabled: Whether health checking is enabled.
port: Port number to perform health check on.
path: HTTP path to perform health check on.
"""
enabled: bool = True
port: int | None = None
path: str | None = None
@dataclass_json
@dataclass
class EntrypointOverridesSettings:
"""Configuration for overriding container entrypoint and command.
Attributes:
enabled: Whether entrypoint overrides are enabled.
entrypoint: List of strings forming the entrypoint command.
cmd: List of strings forming the command arguments.
"""
enabled: bool = True
entrypoint: list[str] | None = None
cmd: list[str] | None = None
@dataclass_json
@dataclass
class EnvVar:
"""Environment variable configuration for containers.
Attributes:
name: Name of the environment variable.
value_or_reference_to_secret: Direct value or reference to a secret.
type: Type of the environment variable.
"""
name: str
value_or_reference_to_secret: str
type: EnvVarType
@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class VolumeMount:
"""Base class for volume mount configurations.
Attributes:
type: Type of volume mount.
mount_path: Path where the volume should be mounted in the container.
size_in_mb: Size of the volume in megabytes. Deprecated: use MemoryMount for memory volumes instead.
"""
type: VolumeMountType
mount_path: str
# Deprecated: use MemoryMount for memory volumes instead.
size_in_mb: int | None = field(default=None, kw_only=True)
@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class GeneralStorageMount(VolumeMount):
"""General storage volume mount configuration."""
def __init__(self, mount_path: str):
"""Initialize a general scratch volume mount.
Args:
mount_path: Path where the volume should be mounted in the container.
"""
super().__init__(type=VolumeMountType.SCRATCH, mount_path=mount_path)
@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class SecretMount(VolumeMount):
"""Secret volume mount configuration.
A secret volume mount allows mounting secret files into the container.
Attributes:
secret_name: The name of the fileset secret to mount. This secret must be created in advance, for example using `create_fileset_secret_from_file_paths`
file_names: List of file names that are part of the fileset secret.
"""
secret_name: str
file_names: list[str] | None = None
def __init__(self, mount_path: str, secret_name: str, file_names: list[str] | None = None):
self.secret_name = secret_name
self.file_names = file_names
super().__init__(type=VolumeMountType.SECRET, mount_path=mount_path)
@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class MemoryMount(VolumeMount):
"""Memory volume mount configuration.
A memory volume mount provides high-speed, ephemeral in-memory storage inside your container.
The mount path is currently hardcoded to /dev/shm and cannot be changed.
Attributes:
size_in_mb: Size of the memory volume in megabytes.
"""
size_in_mb: int
def __init__(self, size_in_mb: int):
super().__init__(type=VolumeMountType.MEMORY, mount_path='/dev/shm')
self.size_in_mb = size_in_mb
@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class SharedFileSystemMount(VolumeMount):
"""Shared filesystem volume mount configuration.
A shared filesystem volume mount allows mounting a shared filesystem into the container.
"""
volume_id: str # The ID of the shared filesystem volume to mount, needs to be created first
def __init__(self, mount_path: str, volume_id: str):
super().__init__(type=VolumeMountType.SHARED, mount_path=mount_path)
self.volume_id = volume_id
def _decode_volume_mount(data: dict) -> VolumeMount:
"""Decode a volume mount dict into the correct VolumeMount subclass based on type."""
mount_type = data.get('type')
if mount_type == VolumeMountType.SHARED or mount_type == 'shared':
return SharedFileSystemMount(mount_path=data['mount_path'], volume_id=data['volume_id'])
if mount_type == VolumeMountType.SECRET or mount_type == 'secret':
return SecretMount(
mount_path=data['mount_path'],
secret_name=data['secret_name'],
file_names=data.get('file_names'),
)
if mount_type == VolumeMountType.MEMORY or mount_type == 'memory':
return MemoryMount(size_in_mb=data['size_in_mb'])
return GeneralStorageMount(mount_path=data['mount_path'])
def _decode_volume_mounts(data: list[dict] | None) -> list[VolumeMount] | None:
"""Decode a list of volume mount dicts into the correct VolumeMount subclasses."""
if not data:
return None
return [_decode_volume_mount(v) for v in data]
@dataclass_json
@dataclass
class Container:
"""Container configuration for deployment creation and updates.
Attributes:
image: Container image to use.
exposed_port: Port to expose from the container.
name: Name of the container (system-managed, read-only).
healthcheck: Optional health check configuration.
entrypoint_overrides: Optional entrypoint override settings.
env: Optional list of environment variables.
volume_mounts: Optional list of volume mounts.
"""
image: str | dict
exposed_port: int
name: str | None = None
healthcheck: HealthcheckSettings | None = None
entrypoint_overrides: EntrypointOverridesSettings | None = None
env: list[EnvVar] | None = None
volume_mounts: list[VolumeMount] | None = field(
default=None, metadata=config(decoder=_decode_volume_mounts)
)
@dataclass_json
@dataclass
class ContainerRegistryCredentials:
"""Credentials for accessing a container registry.
Attributes:
name: Name of the credentials.
"""
name: str
@dataclass_json
@dataclass
class ContainerRegistrySettings:
"""Settings for container registry access.
Attributes:
is_private: Whether the registry is private.
credentials: Optional credentials for accessing private registry.
"""
is_private: bool
credentials: ContainerRegistryCredentials | None = None
@dataclass_json
@dataclass
class ComputeResource:
"""Compute resource configuration.
Attributes:
name: Name of the compute resource.
size: Size of the compute resource.
is_available: Whether the compute resource is currently available.
"""
name: str
size: int
# Made optional since it's only used in API responses
is_available: bool | None = None
@dataclass_json
@dataclass
class ScalingPolicy:
"""Policy for controlling scaling behavior.
Attributes:
delay_seconds: Number of seconds to wait before applying scaling action.
"""
delay_seconds: int
@dataclass_json
@dataclass
class QueueLoadScalingTrigger:
"""Trigger for scaling based on queue load.
Attributes:
threshold: Queue load threshold that triggers scaling.
"""
threshold: float
@dataclass_json
@dataclass
class UtilizationScalingTrigger:
"""Trigger for scaling based on resource utilization.
Attributes:
enabled: Whether this trigger is enabled.
threshold: Utilization threshold that triggers scaling.
"""
enabled: bool
threshold: float | None = None
@dataclass_json
@dataclass
class ScalingTriggers:
"""Collection of triggers that can cause scaling actions.
Attributes:
queue_load: Optional trigger based on queue load.
cpu_utilization: Optional trigger based on CPU utilization.
gpu_utilization: Optional trigger based on GPU utilization.
"""
queue_load: QueueLoadScalingTrigger | None = None
cpu_utilization: UtilizationScalingTrigger | None = None
gpu_utilization: UtilizationScalingTrigger | None = None
@dataclass_json
@dataclass
class ScalingOptions:
"""Configuration for automatic scaling behavior.
Attributes:
min_replica_count: Minimum number of replicas to maintain.
max_replica_count: Maximum number of replicas allowed.
scale_down_policy: Policy for scaling down replicas.
scale_up_policy: Policy for scaling up replicas.
queue_message_ttl_seconds: Time-to-live for queue messages in seconds.
concurrent_requests_per_replica: Number of concurrent requests each replica can handle.
scaling_triggers: Configuration for various scaling triggers.
"""
min_replica_count: int
max_replica_count: int
scale_down_policy: ScalingPolicy
scale_up_policy: ScalingPolicy
queue_message_ttl_seconds: int
concurrent_requests_per_replica: int
scaling_triggers: ScalingTriggers
@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class Deployment:
"""Configuration for creating or updating a container deployment.
Attributes:
name: Name of the deployment.
container_registry_settings: Settings for accessing container registry.
containers: List of container specifications in the deployment.
compute: Compute resource configuration.
is_spot: Whether is spot deployment.
endpoint_base_url: Optional base URL for the deployment endpoint.
scaling: Optional scaling configuration.
created_at: Optional timestamp when the deployment was created.
"""
name: str
containers: list[Container]
compute: ComputeResource
container_registry_settings: ContainerRegistrySettings = field(
default_factory=lambda: ContainerRegistrySettings(is_private=False)
)
is_spot: bool = False
endpoint_base_url: str | None = None
scaling: ScalingOptions | None = None
created_at: str | None = None
_inference_client: InferenceClient | None = None
def __str__(self):
"""Returns a string representation of the deployment, excluding sensitive information.
Returns:
str: A formatted string representation of the deployment.
"""
# Get all attributes except _inference_client
attrs = {k: v for k, v in self.__dict__.items() if k != '_inference_client'}
# Format each attribute
attr_strs = [f'{k}={v!r}' for k, v in attrs.items()]
return f'Deployment({", ".join(attr_strs)})'
def __repr__(self):
"""Returns a repr representation of the deployment, excluding sensitive information.
Returns:
str: A formatted string representation of the deployment.
"""
return self.__str__()
@classmethod
def from_dict_with_inference_key(
cls, data: dict[str, Any], inference_key: str | None = None
) -> 'Deployment':
"""Creates a Deployment instance from a dictionary with an inference key.
Args:
data: Dictionary containing deployment data.
inference_key: Inference key to set on the deployment.
Returns:
Deployment: A new Deployment instance with the inference client initialized.
"""
deployment = Deployment.from_dict(data, infer_missing=True)
if inference_key and deployment.endpoint_base_url:
deployment._inference_client = InferenceClient(
inference_key=inference_key,
endpoint_base_url=deployment.endpoint_base_url,
)
return deployment
def set_inference_client(self, inference_key: str) -> None:
"""Sets the inference client for this deployment.
Args:
inference_key: The inference key to use for authentication.
Raises:
ValueError: If endpoint_base_url is not set.
"""
if self.endpoint_base_url is None:
raise ValueError('Endpoint base URL must be set to use inference client')
self._inference_client = InferenceClient(
inference_key=inference_key, endpoint_base_url=self.endpoint_base_url
)
def _validate_inference_client(self) -> None:
"""Validates that the inference client is initialized.
Raises:
ValueError: If inference client is not initialized.
"""
if self._inference_client is None:
raise ValueError(
'Inference client not initialized. Use from_dict_with_inference_key or set_inference_client to initialize inference capabilities.'
)
def run_sync(
self,
data: dict[str, Any],
path: str = '',
timeout_seconds: int = 60 * 5,
headers: dict[str, str] | None = None,
http_method: str = 'POST',
stream: bool = False,
) -> InferenceResponse:
"""Runs a synchronous inference request.
Args:
data: The data to send in the request.
path: The endpoint path to send the request to.
timeout_seconds: Maximum time to wait for the response.
headers: Optional headers to include in the request.
http_method: The HTTP method to use for the request.
stream: Whether to stream the response.
Returns:
InferenceResponse: The response from the inference request.
Raises:
ValueError: If the inference client is not initialized.
"""
self._validate_inference_client()
return self._inference_client.run_sync(
data, path, timeout_seconds, headers, http_method, stream
)
def run(
self,
data: dict[str, Any],
path: str = '',
timeout_seconds: int = 60 * 5,
headers: dict[str, str] | None = None,
http_method: str = 'POST',
stream: bool = False,
):
"""Runs an asynchronous inference request.
Args:
data: The data to send in the request.
path: The endpoint path to send the request to.
timeout_seconds: Maximum time to wait for the response.
headers: Optional headers to include in the request.
http_method: The HTTP method to use for the request.
stream: Whether to stream the response.
Returns:
The response from the inference request.
Raises:
ValueError: If the inference client is not initialized.
"""
self._validate_inference_client()
return self._inference_client.run(data, path, timeout_seconds, headers, http_method, stream)
def health(self):
"""Checks the health of the deployed application.
Returns:
The health check response.
Raises:
ValueError: If the inference client is not initialized.
"""
self._validate_inference_client()
# build healthcheck path
healthcheck_path = '/health'
if (
self.containers
and self.containers[0].healthcheck
and self.containers[0].healthcheck.path
):
healthcheck_path = self.containers[0].healthcheck.path
return self._inference_client.health(healthcheck_path)
# Function alias
healthcheck = health
@dataclass_json
@dataclass
class ReplicaInfo:
"""Information about a deployment replica.
Attributes:
id: Unique identifier of the replica.
status: Current status of the replica.
started_at: Timestamp when the replica was started.
"""
id: str
status: str
started_at: str
@dataclass_json
@dataclass
class Secret:
"""A secret model class.
Attributes:
name: Name of the secret.
created_at: Timestamp when the secret was created.
secret_type: Type of the secret.
"""
name: str
created_at: str
secret_type: SecretType
@dataclass_json
@dataclass
class RegistryCredential:
"""A container registry credential model class.
Attributes:
name: Name of the registry credential.
created_at: Timestamp when the credential was created.
"""
name: str
created_at: str
@dataclass_json
@dataclass
class BaseRegistryCredentials:
"""Base class for registry credentials.
Attributes:
name: Name of the registry credential.
type: Type of the container registry.
"""
name: str
type: ContainerRegistryType
@dataclass_json
@dataclass
class DockerHubCredentials(BaseRegistryCredentials):
"""Credentials for DockerHub registry.
Attributes:
username: DockerHub username.
access_token: DockerHub access token.
"""
username: str
access_token: str
def __init__(self, name: str, username: str, access_token: str):
"""Initializes DockerHub credentials.
Args:
name: Name of the credentials.
username: DockerHub username.
access_token: DockerHub access token.
"""
super().__init__(name=name, type=ContainerRegistryType.DOCKERHUB)
self.username = username
self.access_token = access_token
@dataclass_json
@dataclass
class GithubCredentials(BaseRegistryCredentials):
"""Credentials for GitHub Container Registry.
Attributes:
username: GitHub username.
access_token: GitHub access token.
"""
username: str
access_token: str
def __init__(self, name: str, username: str, access_token: str):
"""Initializes GitHub credentials.
Args:
name: Name of the credentials.
username: GitHub username.
access_token: GitHub access token.
"""
super().__init__(name=name, type=ContainerRegistryType.GITHUB)
self.username = username
self.access_token = access_token
@dataclass_json
@dataclass
class GCRCredentials(BaseRegistryCredentials):
"""Credentials for Google Container Registry.
Attributes:
service_account_key: GCP service account key JSON.
"""
service_account_key: str
def __init__(self, name: str, service_account_key: str):
"""Initializes GCR credentials.
Args:
name: Name of the credentials.
service_account_key: GCP service account key JSON.
"""
super().__init__(name=name, type=ContainerRegistryType.GCR)
self.service_account_key = service_account_key
@dataclass_json
@dataclass
class AWSECRCredentials(BaseRegistryCredentials):
"""Credentials for AWS Elastic Container Registry.
Attributes:
access_key_id: AWS access key ID.
secret_access_key: AWS secret access key.
region: AWS region.
ecr_repo: ECR repository name.
"""
access_key_id: str
secret_access_key: str
region: str
ecr_repo: str
def __init__(
self,
name: str,
access_key_id: str,
secret_access_key: str,
region: str,
ecr_repo: str,
):
"""Initializes AWS ECR credentials.
Args:
name: Name of the credentials.
access_key_id: AWS access key ID.
secret_access_key: AWS secret access key.
region: AWS region.
ecr_repo: ECR repository name.
"""
super().__init__(name=name, type=ContainerRegistryType.AWS_ECR)
self.access_key_id = access_key_id
self.secret_access_key = secret_access_key
self.region = region
self.ecr_repo = ecr_repo
@dataclass_json
@dataclass
class CustomRegistryCredentials(BaseRegistryCredentials):
"""Credentials for custom container registries.
Attributes:
docker_config_json: Docker config JSON containing registry credentials.
"""
docker_config_json: str
def __init__(self, name: str, docker_config_json: str):
"""Initializes custom registry credentials.
Args:
name: Name of the credentials.
docker_config_json: Docker config JSON containing registry credentials.
"""
super().__init__(name=name, type=ContainerRegistryType.CUSTOM)
self.docker_config_json = docker_config_json
class ContainersService:
"""Service for managing container deployments.
This class provides methods for interacting with container deployment API,
including CRUD operations for deployments and related resources.
"""
def __init__(self, http_client: HTTPClient, inference_key: str | None = None) -> None:
"""Initializes the containers service.
Args:
http_client: HTTP client for making API requests.
inference_key: Optional inference key for authenticating inference requests.
"""
self.client = http_client
self._inference_key = inference_key
def get_deployments(self) -> list[Deployment]:
"""Retrieves all container deployments.
Returns:
list[Deployment]: List of all deployments.
"""
response = self.client.get(CONTAINER_DEPLOYMENTS_ENDPOINT)
return [
Deployment.from_dict_with_inference_key(deployment, self._inference_key)
for deployment in response.json()
]
def get_deployment_by_name(self, deployment_name: str) -> Deployment:
"""Retrieves a specific deployment by name.
Args:
deployment_name: Name of the deployment to retrieve.
Returns:
Deployment: The requested deployment.
"""
response = self.client.get(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}')
return Deployment.from_dict_with_inference_key(response.json(), self._inference_key)
# Function alias
get_deployment = get_deployment_by_name
def create_deployment(self, deployment: Deployment) -> Deployment:
"""Creates a new container deployment.
Args:
deployment: Deployment configuration to create.
Returns:
Deployment: The created deployment.
"""
response = self.client.post(CONTAINER_DEPLOYMENTS_ENDPOINT, deployment.to_dict())
return Deployment.from_dict_with_inference_key(response.json(), self._inference_key)
def update_deployment(self, deployment_name: str, deployment: Deployment) -> Deployment:
"""Updates an existing deployment.
Args:
deployment_name: Name of the deployment to update.
deployment: Updated deployment configuration.
Returns:
Deployment: The updated deployment.
"""
response = self.client.patch(
f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}', deployment.to_dict()
)
return Deployment.from_dict_with_inference_key(response.json(), self._inference_key)
def delete_deployment(self, deployment_name: str) -> None:
"""Deletes a deployment.
Args:
deployment_name: Name of the deployment to delete.
"""
self.client.delete(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}')
def get_deployment_status(self, deployment_name: str) -> ContainerDeploymentStatus:
"""Retrieves the current status of a deployment.
Args:
deployment_name: Name of the deployment.
Returns:
ContainerDeploymentStatus: Current status of the deployment.
"""
response = self.client.get(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/status')
return ContainerDeploymentStatus(response.json()['status'])
def restart_deployment(self, deployment_name: str) -> None:
"""Restarts a deployment.
Args:
deployment_name: Name of the deployment to restart.
"""
self.client.post(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/restart')
def get_deployment_scaling_options(self, deployment_name: str) -> ScalingOptions:
"""Retrieves the scaling options for a deployment.
Args:
deployment_name: Name of the deployment.
Returns:
ScalingOptions: Current scaling options for the deployment.
"""
response = self.client.get(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/scaling')
return ScalingOptions.from_dict(response.json())
def update_deployment_scaling_options(
self, deployment_name: str, scaling_options: ScalingOptions
) -> ScalingOptions:
"""Updates the scaling options for a deployment.
Args:
deployment_name: Name of the deployment.
scaling_options: New scaling options to apply.
Returns:
ScalingOptions: Updated scaling options for the deployment.
"""
response = self.client.patch(
f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/scaling',
scaling_options.to_dict(),
)
return ScalingOptions.from_dict(response.json())
def get_deployment_replicas(self, deployment_name: str) -> list[ReplicaInfo]:
"""Retrieves information about deployment replicas.
Args:
deployment_name: Name of the deployment.
Returns:
list[ReplicaInfo]: List of replica information.
"""
response = self.client.get(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/replicas')
return [ReplicaInfo.from_dict(replica) for replica in response.json()['list']]
def purge_deployment_queue(self, deployment_name: str) -> None:
"""Purges the deployment queue.
Args:
deployment_name: Name of the deployment.
"""
self.client.post(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/purge-queue')
def pause_deployment(self, deployment_name: str) -> None:
"""Pauses a deployment.
Args:
deployment_name: Name of the deployment to pause.
"""
self.client.post(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/pause')
def resume_deployment(self, deployment_name: str) -> None:
"""Resumes a paused deployment.
Args:
deployment_name: Name of the deployment to resume.
"""
self.client.post(f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/resume')
def get_deployment_environment_variables(self, deployment_name: str) -> dict[str, list[EnvVar]]:
"""Retrieves environment variables for a deployment.
Args:
deployment_name: Name of the deployment.
Returns:
dict[str, list[EnvVar]]: Dictionary mapping container names to their environment variables.
"""
response = self.client.get(
f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/environment-variables'
)
result = {}
for item in response.json():
container_name = item['container_name']
env_vars = item['env']
result[container_name] = [EnvVar.from_dict(env_var) for env_var in env_vars]
return result
def add_deployment_environment_variables(
self, deployment_name: str, container_name: str, env_vars: list[EnvVar]
) -> dict[str, list[EnvVar]]:
"""Adds environment variables to a container in a deployment.
Args:
deployment_name: Name of the deployment.
container_name: Name of the container.
env_vars: List of environment variables to add.
Returns:
dict[str, list[EnvVar]]: Updated environment variables for all containers.
"""
response = self.client.post(
f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/environment-variables',
{
'container_name': container_name,
'env': [env_var.to_dict() for env_var in env_vars],
},
)
result = {}
for item in response.json():
container_name = item['container_name']
env_vars = item['env']
result[container_name] = [EnvVar.from_dict(env_var) for env_var in env_vars]
return result
def update_deployment_environment_variables(
self, deployment_name: str, container_name: str, env_vars: list[EnvVar]
) -> dict[str, list[EnvVar]]:
"""Updates environment variables for a container in a deployment.
Args:
deployment_name: Name of the deployment.
container_name: Name of the container.
env_vars: List of updated environment variables.
Returns:
dict[str, list[EnvVar]]: Updated environment variables for all containers.
"""
response = self.client.patch(
f'{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}/environment-variables',
{
'container_name': container_name,
'env': [env_var.to_dict() for env_var in env_vars],
},
)
result = {}
item = response.json()
container_name = item['container_name']
env_vars = item['env']
result[container_name] = [EnvVar.from_dict(env_var) for env_var in env_vars]
return result
def delete_deployment_environment_variables(
self, deployment_name: str, container_name: str, env_var_names: list[str]
) -> dict[str, list[EnvVar]]:
"""Deletes environment variables from a container in a deployment.
Args:
deployment_name: Name of the deployment.
container_name: Name of the container.
env_var_names: List of environment variable names to delete.
Returns: