Skip to main content

Advanced guide

This part specifically focus on highlighting more scripting approach.

Complete resource creation parameters

Below example shows available parameters, that are used for the resource creation:

response = resource.resource_create(
# Required parameters
name="advanced-app", # Unique identifier
image_name="myregistry/myapp:v1.0", # Docker image

# Resource specifications
entity="custom", # Entity type (default: "custom")
cpu=4, # CPU cores
memory=16, # Memory in GB
shm_size=2, # Shared memory in GB
gpu=1, # Number of GPUs
gpu_type="A5000", # GPU model

# Storage
volumes=["data-vol"], # Volume names to mount
volume_full_path="/mnt/data", # Mount path for volumes (if defined, only 1 volume is allowed during resource creation)

# Configuration
startup_command="python app.py", # Override entrypoint
repository_secret="my-registry-secret", # For private registries

# Environment and configuration
cgc_data=["KEY1=value1"], # CGC-specific data
environment_data=["ENV=prod"], # Environment variables
arguments_data=["--verbose"], # Command arguments

# Networking
node_port_enabled=True # Enable NodePort service (not available on every CGC instance)
)

Checking resource status

Check if resource is ready

import cgc.sdk.resource as resource
import time

def wait_for_resource(name, max_wait=300):
"""Wait for a resource to be ready"""
waited = 0
while waited < max_wait:
if resource.resource_ready(name):
print(f"{name} is ready!")
return True
print(f"Waiting for {name}... ({waited}s)")
time.sleep(10)
waited += 10
return False

# Use it
if wait_for_resource("my-app"):
print("Application started successfully")
else:
print("Application failed to start in time")

Check specific resource types

from cgc.sdk.resource import ResourceTypes

# Check if a database is ready
is_ready = resource.resource_ready(
"my-postgres",
resource_type=ResourceTypes.db
)

# Check if a job is ready
is_ready = resource.resource_ready(
"data-job",
resource_type=ResourceTypes.job
)

Advanced patterns

Using loop functions

The SDK provides helper functions for operations that need retries:

# Start a resource with automatic retries
resource.start_function_loop(
resource.resource_create,
infinite=False, # Try 5 times then stop
name="my-app",
image_name="nginx:latest",
cpu=1,
memory=2
)

# Stop a resource with retries
resource.stop_function_loop(
resource.resource_delete,
infinite=False,
name="my-app"
)