Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

* Added environment variables to container deployment example
* Updated examples image from 'fastai' to 'ubuntu-24.04-cuda-12.8-open-docker'
* Consistent naming and load of credentials from env variables in examples

v1.8.4 (2025-03-25)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DataCrunch's Public API documentation [is available here](https://api.datacrunch

# Create a new instance
instance = datacrunch.instances.create(instance_type='1V100.6V',
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys,
hostname='example',
description='example instance')
Expand Down
5 changes: 5 additions & 0 deletions datacrunch/datacrunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def __init__(self, client_id: str, client_secret: str, base_url: str = "https://
:type base_url: str, optional
"""

# Validate that client_id and client_secret are not empty
if not client_id or not client_secret:
raise ValueError(
"client_id and client_secret must be provided")

# Constants
self.constants: Constants = Constants(base_url, VERSION)
"""Constants"""
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/advanced_create_instance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Advanced Create Instance
if price_per_hour * DURATION < balance.amount:
# Deploy a new 8V instance
instance = datacrunch.instances.create(instance_type=INSTANCE_TYPE_8V,
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='large instance'
Expand All @@ -67,7 +67,7 @@ Advanced Create Instance
else:
# Deploy a new 4V instance
instance = datacrunch.instances.create(instance_type=INSTANCE_TYPE_4V,
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='medium instance')
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/instance_actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Instance Actions

# Create a new 1V100.6V instance
instance = datacrunch.instances.create(instance_type='1V100.6V',
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='example instance')
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/instances_and_volumes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Instances and Volumes

# Create instance with extra attached volumes
instance_with_extra_volumes = datacrunch.instances.create(instance_type='1V100.6V',
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys,
hostname='example',
description='example instance',
Expand All @@ -38,7 +38,7 @@ Instances and Volumes

# Create instance with custom OS volume size and name
instance_with_custom_os_volume = datacrunch.instances.create(instance_type='1V100.6V',
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys,
hostname='example',
description='example instance',
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/simple_create_instance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Simple Create Instance

# Create a new instance
instance = datacrunch.instances.create(instance_type='1V100.6V',
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='example instance')
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Deploy a new instance:

# Create a new instance
instance = datacrunch.instances.create(instance_type='1V100.6V',
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='example instance')
Expand Down
19 changes: 10 additions & 9 deletions examples/advanced_create_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
# Arbitrary duration for the example
DURATION = 24 * 7 # one week

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

try:
# Create datcrunch client
datacrunch = DataCrunchClient(CLIENT_ID, CLIENT_SECRET)
datacrunch = DataCrunchClient(
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)

# Create new SSH key
public_key = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI0qq2Qjt5GPi7DKdcnBHOkvk8xNsG9dA607tnWagOkHC test_key'
Expand All @@ -51,18 +52,18 @@
if price_per_hour * DURATION < balance.amount:
# Deploy a new 8V instance
instance = datacrunch.instances.create(instance_type=INSTANCE_TYPE_8V,
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='large instance',
os_volume={
"name": "Large OS volume",
"size": 95
})
"name": "Large OS volume",
"size": 95
})
else:
# Deploy a new 4V instance
instance = datacrunch.instances.create(instance_type=INSTANCE_TYPE_4V,
image='fastai',
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='medium instance')
Expand Down
23 changes: 13 additions & 10 deletions examples/containers/compute_resources_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os
from datacrunch import DataCrunchClient
from typing import List
from datacrunch.containers.containers import ComputeResource

# Get client secret and id from environment variables
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')


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

def main():
# Initialize the client with your credentials
client = DataCrunchClient(
client_id="your_client_id",
client_secret="your_client_secret"
)
datacrunch = DataCrunchClient(
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)

# Example 1: List all compute resources
print("\nAll compute resources:")
all_resources = list_all_compute_resources(client)
print("All compute resources:")
all_resources = list_all_compute_resources(datacrunch)
for resource in all_resources:
print(
f"Name: {resource.name}, Size: {resource.size}, Available: {resource.is_available}")

# Example 2: List available compute resources
print("\nAvailable compute resources:")
available_resources = list_available_compute_resources(client)
print("Available compute resources:")
available_resources = list_available_compute_resources(datacrunch)
for resource in available_resources:
print(f"Name: {resource.name}, Size: {resource.size}")

# Example 3: List compute resources of size 8
print("\nCompute resources with size 8:")
size_8_resources = list_compute_resources_by_size(client, 8)
print("Compute resources with size 8:")
size_8_resources = list_compute_resources_by_size(datacrunch, 8)
for resource in size_8_resources:
print(f"Name: {resource.name}, Available: {resource.is_available}")

Expand Down
53 changes: 32 additions & 21 deletions examples/containers/container_deployments_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from datacrunch.containers.containers import (
Container,
ComputeResource,
EnvVar,
EnvVarType,
ScalingOptions,
ScalingPolicy,
ScalingTriggers,
Expand All @@ -29,12 +31,12 @@
DEPLOYMENT_NAME = "my-deployment"
IMAGE_NAME = "your-image-name:version"

# Environment variables
# Get client secret and id from environment variables
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')

# DataCrunch client instance
datacrunch_client = None
datacrunch = None


def wait_for_deployment_health(client: DataCrunchClient, deployment_name: str, max_attempts: int = 10, delay: int = 30) -> bool:
Expand Down Expand Up @@ -79,15 +81,9 @@ def cleanup_resources(client: DataCrunchClient) -> None:
def main() -> None:
"""Main function demonstrating deployment lifecycle management."""
try:
# Check required environment variables
if not DATACRUNCH_CLIENT_ID or not DATACRUNCH_CLIENT_SECRET:
print(
"Please set DATACRUNCH_CLIENT_ID and DATACRUNCH_CLIENT_SECRET environment variables")
return

# Initialize client
global datacrunch_client
datacrunch_client = DataCrunchClient(
global datacrunch
datacrunch = DataCrunchClient(
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)

# Create container configuration
Expand All @@ -104,6 +100,21 @@ def main() -> None:
type=VolumeMountType.SCRATCH,
mount_path="/data"
)
],
env=[
# Secret environment variables needed to be added beforehand
EnvVar(
name="HF_TOKEN",
# This is a reference to a secret already created
value_or_reference_to_secret="hf_token",
type=EnvVarType.SECRET
),
# Plain environment variables can be added directly
EnvVar(
name="VERSION",
value_or_reference_to_secret="1.5.2",
type=EnvVarType.PLAIN
)
]
)

Expand Down Expand Up @@ -143,19 +154,19 @@ def main() -> None:
)

# Create the deployment
created_deployment = datacrunch_client.containers.create_deployment(
created_deployment = datacrunch.containers.create_deployment(
deployment)
print(f"Created deployment: {created_deployment.name}")

# Wait for deployment to be healthy
if not wait_for_deployment_health(datacrunch_client, DEPLOYMENT_NAME):
if not wait_for_deployment_health(datacrunch, DEPLOYMENT_NAME):
print("Deployment health check failed")
cleanup_resources(datacrunch_client)
cleanup_resources(datacrunch)
return

# Update scaling configuration
try:
deployment = datacrunch_client.containers.get_deployment_by_name(
deployment = datacrunch.containers.get_deployment_by_name(
DEPLOYMENT_NAME)
# Create new scaling options with increased replica counts
deployment.scaling = ScalingOptions(
Expand All @@ -177,7 +188,7 @@ def main() -> None:
)
)
)
updated_deployment = datacrunch_client.containers.update_deployment(
updated_deployment = datacrunch.containers.update_deployment(
DEPLOYMENT_NAME, deployment)
print(f"Updated deployment scaling: {updated_deployment.name}")
except APIException as e:
Expand All @@ -186,33 +197,33 @@ def main() -> None:
# Demonstrate deployment operations
try:
# Pause deployment
datacrunch_client.containers.pause_deployment(DEPLOYMENT_NAME)
datacrunch.containers.pause_deployment(DEPLOYMENT_NAME)
print("Deployment paused")
time.sleep(60)

# Resume deployment
datacrunch_client.containers.resume_deployment(DEPLOYMENT_NAME)
datacrunch.containers.resume_deployment(DEPLOYMENT_NAME)
print("Deployment resumed")

# Restart deployment
datacrunch_client.containers.restart_deployment(DEPLOYMENT_NAME)
datacrunch.containers.restart_deployment(DEPLOYMENT_NAME)
print("Deployment restarted")

# Purge queue
datacrunch_client.containers.purge_deployment_queue(
datacrunch.containers.purge_deployment_queue(
DEPLOYMENT_NAME)
print("Queue purged")
except APIException as e:
print(f"Error in deployment operations: {e}")

# Clean up
cleanup_resources(datacrunch_client)
cleanup_resources(datacrunch)

except Exception as e:
print(f"Unexpected error: {e}")
# Attempt cleanup even if there was an error
try:
cleanup_resources(datacrunch_client)
cleanup_resources(datacrunch)
except Exception as cleanup_error:
print(f"Error during cleanup after failure: {cleanup_error}")

Expand Down
14 changes: 7 additions & 7 deletions examples/containers/environment_variables_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from datacrunch import DataCrunchClient
from typing import Dict, List

# Get client secret and id from environment variables
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')

# Initialize DataCrunch client
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
client_secret=DATACRUNCH_CLIENT_SECRET)
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)

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

# Create a new secret
secret_name = "my-secret-key"
datacrunch_client.containers.create_secret(
datacrunch.containers.create_secret(
secret_name,
"my-secret-value"
)
Expand All @@ -62,7 +62,7 @@ def main():
)
]

env_vars = datacrunch_client.containers.add_deployment_environment_variables(
env_vars = datacrunch.containers.add_deployment_environment_variables(
deployment_name=DEPLOYMENT_NAME,
container_name=CONTAINER_NAME,
env_vars=new_env_vars
Expand All @@ -79,7 +79,7 @@ def main():
),
]

env_vars = datacrunch_client.containers.update_deployment_environment_variables(
env_vars = datacrunch.containers.update_deployment_environment_variables(
deployment_name=DEPLOYMENT_NAME,
container_name=CONTAINER_NAME,
env_vars=updated_env_vars
Expand All @@ -88,7 +88,7 @@ def main():

# Delete environment variables
print("\nDeleting environment variables...")
env_vars = datacrunch_client.containers.delete_deployment_environment_variables(
env_vars = datacrunch.containers.delete_deployment_environment_variables(
deployment_name=DEPLOYMENT_NAME,
container_name=CONTAINER_NAME,
env_var_names=["DEBUG"]
Expand Down
Loading