Skip to content

Commit 876be7a

Browse files
committed
Merge branch 'master' into feature/inference_functionality
2 parents 887b834 + 93c94d0 commit 876be7a

16 files changed

+128
-118
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
* Added environment variables to container deployment example
5+
* Updated examples image from 'fastai' to 'ubuntu-24.04-cuda-12.8-open-docker'
6+
* Consistent naming and load of credentials from env variables in examples
7+
48
v1.8.4 (2025-03-25)
59
-------------------
610

datacrunch/datacrunch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def __init__(self, client_id: str, client_secret: str, base_url: str = "https://
3030
:type inference_key: str, optional
3131
"""
3232

33+
# Validate that client_id and client_secret are not empty
34+
if not client_id or not client_secret:
35+
raise ValueError(
36+
"client_id and client_secret must be provided")
37+
3338
# Constants
3439
self.constants: Constants = Constants(base_url, VERSION)
3540
"""Constants"""

examples/advanced_create_instance.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
# Arbitrary duration for the example
2121
DURATION = 24 * 7 # one week
2222

23-
# Get client secret from environment variable
24-
CLIENT_SECRET = os.environ['DATACRUNCH_CLIENT_SECRET']
25-
CLIENT_ID = 'Ibk5bdxV64lKAWOqYnvSi' # Replace with your client ID
23+
# Get client secret and id from environment variables
24+
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
25+
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
2626

2727
try:
2828
# Create datcrunch client
29-
datacrunch = DataCrunchClient(CLIENT_ID, CLIENT_SECRET)
29+
datacrunch = DataCrunchClient(
30+
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
3031

3132
# Create new SSH key
3233
public_key = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI0qq2Qjt5GPi7DKdcnBHOkvk8xNsG9dA607tnWagOkHC test_key'

examples/containers/compute_resources_example.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import os
12
from datacrunch import DataCrunchClient
23
from typing import List
34
from datacrunch.containers.containers import ComputeResource
45

6+
# Get client secret and id from environment variables
7+
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
8+
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
9+
510

611
def list_all_compute_resources(client: DataCrunchClient) -> List[ComputeResource]:
712
"""List all available compute resources.
@@ -44,27 +49,25 @@ def list_compute_resources_by_size(client: DataCrunchClient, size: int) -> List[
4449

4550
def main():
4651
# Initialize the client with your credentials
47-
client = DataCrunchClient(
48-
client_id="your_client_id",
49-
client_secret="your_client_secret"
50-
)
52+
datacrunch = DataCrunchClient(
53+
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
5154

5255
# Example 1: List all compute resources
53-
print("\nAll compute resources:")
54-
all_resources = list_all_compute_resources(client)
56+
print("All compute resources:")
57+
all_resources = list_all_compute_resources(datacrunch)
5558
for resource in all_resources:
5659
print(
5760
f"Name: {resource.name}, Size: {resource.size}, Available: {resource.is_available}")
5861

5962
# Example 2: List available compute resources
60-
print("\nAvailable compute resources:")
61-
available_resources = list_available_compute_resources(client)
63+
print("Available compute resources:")
64+
available_resources = list_available_compute_resources(datacrunch)
6265
for resource in available_resources:
6366
print(f"Name: {resource.name}, Size: {resource.size}")
6467

6568
# Example 3: List compute resources of size 8
66-
print("\nCompute resources with size 8:")
67-
size_8_resources = list_compute_resources_by_size(client, 8)
69+
print("Compute resources with size 8:")
70+
size_8_resources = list_compute_resources_by_size(datacrunch, 8)
6871
for resource in size_8_resources:
6972
print(f"Name: {resource.name}, Available: {resource.is_available}")
7073

examples/containers/container_deployments_example.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from datacrunch.containers.containers import (
1313
Container,
1414
ComputeResource,
15+
EnvVar,
16+
EnvVarType,
1517
ScalingOptions,
1618
ScalingPolicy,
1719
ScalingTriggers,
@@ -29,12 +31,12 @@
2931
DEPLOYMENT_NAME = "my-deployment"
3032
IMAGE_NAME = "your-image-name:version"
3133

32-
# Environment variables
34+
# Get client secret and id from environment variables
3335
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
3436
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
3537

3638
# DataCrunch client instance
37-
datacrunch_client = None
39+
datacrunch = None
3840

3941

4042
def wait_for_deployment_health(client: DataCrunchClient, deployment_name: str, max_attempts: int = 10, delay: int = 30) -> bool:
@@ -79,15 +81,9 @@ def cleanup_resources(client: DataCrunchClient) -> None:
7981
def main() -> None:
8082
"""Main function demonstrating deployment lifecycle management."""
8183
try:
82-
# Check required environment variables
83-
if not DATACRUNCH_CLIENT_ID or not DATACRUNCH_CLIENT_SECRET:
84-
print(
85-
"Please set DATACRUNCH_CLIENT_ID and DATACRUNCH_CLIENT_SECRET environment variables")
86-
return
87-
8884
# Initialize client
89-
global datacrunch_client
90-
datacrunch_client = DataCrunchClient(
85+
global datacrunch
86+
datacrunch = DataCrunchClient(
9187
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
9288

9389
# Create container configuration
@@ -104,6 +100,21 @@ def main() -> None:
104100
type=VolumeMountType.SCRATCH,
105101
mount_path="/data"
106102
)
103+
],
104+
env=[
105+
# Secret environment variables needed to be added beforehand
106+
EnvVar(
107+
name="HF_TOKEN",
108+
# This is a reference to a secret already created
109+
value_or_reference_to_secret="hf_token",
110+
type=EnvVarType.SECRET
111+
),
112+
# Plain environment variables can be added directly
113+
EnvVar(
114+
name="VERSION",
115+
value_or_reference_to_secret="1.5.2",
116+
type=EnvVarType.PLAIN
117+
)
107118
]
108119
)
109120

@@ -143,19 +154,19 @@ def main() -> None:
143154
)
144155

145156
# Create the deployment
146-
created_deployment = datacrunch_client.containers.create_deployment(
157+
created_deployment = datacrunch.containers.create_deployment(
147158
deployment)
148159
print(f"Created deployment: {created_deployment.name}")
149160

150161
# Wait for deployment to be healthy
151-
if not wait_for_deployment_health(datacrunch_client, DEPLOYMENT_NAME):
162+
if not wait_for_deployment_health(datacrunch, DEPLOYMENT_NAME):
152163
print("Deployment health check failed")
153-
cleanup_resources(datacrunch_client)
164+
cleanup_resources(datacrunch)
154165
return
155166

156167
# Update scaling configuration
157168
try:
158-
deployment = datacrunch_client.containers.get_deployment_by_name(
169+
deployment = datacrunch.containers.get_deployment_by_name(
159170
DEPLOYMENT_NAME)
160171
# Create new scaling options with increased replica counts
161172
deployment.scaling = ScalingOptions(
@@ -177,7 +188,7 @@ def main() -> None:
177188
)
178189
)
179190
)
180-
updated_deployment = datacrunch_client.containers.update_deployment(
191+
updated_deployment = datacrunch.containers.update_deployment(
181192
DEPLOYMENT_NAME, deployment)
182193
print(f"Updated deployment scaling: {updated_deployment.name}")
183194
except APIException as e:
@@ -186,33 +197,33 @@ def main() -> None:
186197
# Demonstrate deployment operations
187198
try:
188199
# Pause deployment
189-
datacrunch_client.containers.pause_deployment(DEPLOYMENT_NAME)
200+
datacrunch.containers.pause_deployment(DEPLOYMENT_NAME)
190201
print("Deployment paused")
191202
time.sleep(60)
192203

193204
# Resume deployment
194-
datacrunch_client.containers.resume_deployment(DEPLOYMENT_NAME)
205+
datacrunch.containers.resume_deployment(DEPLOYMENT_NAME)
195206
print("Deployment resumed")
196207

197208
# Restart deployment
198-
datacrunch_client.containers.restart_deployment(DEPLOYMENT_NAME)
209+
datacrunch.containers.restart_deployment(DEPLOYMENT_NAME)
199210
print("Deployment restarted")
200211

201212
# Purge queue
202-
datacrunch_client.containers.purge_deployment_queue(
213+
datacrunch.containers.purge_deployment_queue(
203214
DEPLOYMENT_NAME)
204215
print("Queue purged")
205216
except APIException as e:
206217
print(f"Error in deployment operations: {e}")
207218

208219
# Clean up
209-
cleanup_resources(datacrunch_client)
220+
cleanup_resources(datacrunch)
210221

211222
except Exception as e:
212223
print(f"Unexpected error: {e}")
213224
# Attempt cleanup even if there was an error
214225
try:
215-
cleanup_resources(datacrunch_client)
226+
cleanup_resources(datacrunch)
216227
except Exception as cleanup_error:
217228
print(f"Error during cleanup after failure: {cleanup_error}")
218229

examples/containers/environment_variables_example.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
from datacrunch import DataCrunchClient
1313
from typing import Dict, List
1414

15+
# Get client secret and id from environment variables
1516
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
1617
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
1718

1819
# Initialize DataCrunch client
19-
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
20-
client_secret=DATACRUNCH_CLIENT_SECRET)
20+
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
2121

2222
# Example deployment and container names
2323
DEPLOYMENT_NAME = "my-deployment"
@@ -36,13 +36,13 @@ def print_env_vars(env_vars: Dict[str, List[EnvVar]]) -> None:
3636
def main():
3737
# First, let's get the current environment variables
3838
print("Getting current environment variables...")
39-
env_vars = datacrunch_client.containers.get_deployment_environment_variables(
39+
env_vars = datacrunch.containers.get_deployment_environment_variables(
4040
DEPLOYMENT_NAME)
4141
print_env_vars(env_vars)
4242

4343
# Create a new secret
4444
secret_name = "my-secret-key"
45-
datacrunch_client.containers.create_secret(
45+
datacrunch.containers.create_secret(
4646
secret_name,
4747
"my-secret-value"
4848
)
@@ -62,7 +62,7 @@ def main():
6262
)
6363
]
6464

65-
env_vars = datacrunch_client.containers.add_deployment_environment_variables(
65+
env_vars = datacrunch.containers.add_deployment_environment_variables(
6666
deployment_name=DEPLOYMENT_NAME,
6767
container_name=CONTAINER_NAME,
6868
env_vars=new_env_vars
@@ -79,7 +79,7 @@ def main():
7979
),
8080
]
8181

82-
env_vars = datacrunch_client.containers.update_deployment_environment_variables(
82+
env_vars = datacrunch.containers.update_deployment_environment_variables(
8383
deployment_name=DEPLOYMENT_NAME,
8484
container_name=CONTAINER_NAME,
8585
env_vars=updated_env_vars
@@ -88,7 +88,7 @@ def main():
8888

8989
# Delete environment variables
9090
print("\nDeleting environment variables...")
91-
env_vars = datacrunch_client.containers.delete_deployment_environment_variables(
91+
env_vars = datacrunch.containers.delete_deployment_environment_variables(
9292
deployment_name=DEPLOYMENT_NAME,
9393
container_name=CONTAINER_NAME,
9494
env_var_names=["DEBUG"]

examples/containers/registry_credentials_example.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
CustomRegistryCredentials
99
)
1010

11-
# Environment variables
11+
# Get client secret and id from environment variables
1212
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
1313
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
1414

1515
# Initialize DataCrunch client
16-
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
17-
client_secret=DATACRUNCH_CLIENT_SECRET)
16+
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
1817

1918
# Example 1: DockerHub Credentials
2019
dockerhub_creds = DockerHubCredentials(
2120
name="my-dockerhub-creds",
2221
username="your-dockerhub-username",
2322
access_token="your-dockerhub-access-token"
2423
)
25-
datacrunch_client.containers.add_registry_credentials(dockerhub_creds)
24+
datacrunch.containers.add_registry_credentials(dockerhub_creds)
2625
print("Created DockerHub credentials")
2726

2827
# Example 2: GitHub Container Registry Credentials
@@ -31,7 +30,7 @@
3130
username="your-github-username",
3231
access_token="your-github-token"
3332
)
34-
datacrunch_client.containers.add_registry_credentials(github_creds)
33+
datacrunch.containers.add_registry_credentials(github_creds)
3534
print("Created GitHub credentials")
3635

3736
# Example 3: Google Container Registry (GCR) Credentials
@@ -53,7 +52,7 @@
5352
name="my-gcr-creds",
5453
service_account_key=gcr_service_account_key
5554
)
56-
datacrunch_client.containers.add_registry_credentials(gcr_creds)
55+
datacrunch.containers.add_registry_credentials(gcr_creds)
5756
print("Created GCR credentials")
5857

5958
# Example 4: AWS ECR Credentials
@@ -64,7 +63,7 @@
6463
region="eu-north-1",
6564
ecr_repo="887841266746.dkr.ecr.eu-north-1.amazonaws.com"
6665
)
67-
datacrunch_client.containers.add_registry_credentials(aws_creds)
66+
datacrunch.containers.add_registry_credentials(aws_creds)
6867
print("Created AWS ECR credentials")
6968

7069
# Example 5: Custom Registry Credentials
@@ -80,13 +79,13 @@
8079
name="my-custom-registry-creds",
8180
docker_config_json=custom_docker_config
8281
)
83-
datacrunch_client.containers.add_registry_credentials(custom_creds)
82+
datacrunch.containers.add_registry_credentials(custom_creds)
8483
print("Created Custom registry credentials")
8584

8685
# Delete all registry credentials
87-
datacrunch_client.containers.delete_registry_credentials('my-dockerhub-creds')
88-
datacrunch_client.containers.delete_registry_credentials('my-github-creds')
89-
datacrunch_client.containers.delete_registry_credentials('my-gcr-creds')
90-
datacrunch_client.containers.delete_registry_credentials('my-aws-ecr-creds')
91-
datacrunch_client.containers.delete_registry_credentials(
86+
datacrunch.containers.delete_registry_credentials('my-dockerhub-creds')
87+
datacrunch.containers.delete_registry_credentials('my-github-creds')
88+
datacrunch.containers.delete_registry_credentials('my-gcr-creds')
89+
datacrunch.containers.delete_registry_credentials('my-aws-ecr-creds')
90+
datacrunch.containers.delete_registry_credentials(
9291
'my-custom-registry-creds')

0 commit comments

Comments
 (0)