Przejdź do głównej zawartości

Przewodnik zaawansowany

Ta część skupia się szczególnie na podkreśleniu bardziej skryptowego podejścia.

Kompletne parametry tworzenia zasobów

Poniższy przykład pokazuje dostępne parametry, które są używane do tworzenia zasobów:

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)
)

Sprawdzanie statusu zasobów

Sprawdź czy zasób jest gotowy

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")

Sprawdź konkretne typy zasobów

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
)

Zaawansowane wzorce

Używanie funkcji pętli

SDK udostępnia funkcje pomocnicze dla operacji wymagających powtórzeń:

# 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"
)