|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example showing how to manage hardware instances with the CentML SDK. |
| 4 | +
|
| 5 | +Covers listing, creating and deleting hardware instances. Running this script |
| 6 | +lists the hardware instances you have access to; the create/delete helpers show |
| 7 | +the call pattern and are not invoked automatically. |
| 8 | +
|
| 9 | +This uses the centml CLI authentication, so make sure you are logged in to the |
| 10 | +centml CLI before running it. Creating and deleting hardware instances requires |
| 11 | +admin privileges (PERM_ADMIN_MANAGE_HARDWARE) on your CentML organization. |
| 12 | +""" |
| 13 | + |
| 14 | +from centml.sdk import CreateHardwareInstanceRequest |
| 15 | +from centml.sdk.api import get_centml_client |
| 16 | + |
| 17 | + |
| 18 | +def list_hardware_instances(): |
| 19 | + """List hardware instances, showing the cluster they belong to by name.""" |
| 20 | + with get_centml_client() as client: |
| 21 | + clusters = {c.id: c for c in client.get_clusters().results} |
| 22 | + instances = client.get_hardware_instances() |
| 23 | + |
| 24 | + if not instances: |
| 25 | + print("No hardware instances found.") |
| 26 | + return |
| 27 | + |
| 28 | + print(f"\nFound {len(instances)} hardware instance(s)\n") |
| 29 | + for hw in sorted(instances, key=lambda x: x.id): |
| 30 | + cluster = clusters.get(hw.cluster_id) |
| 31 | + cluster_name = cluster.display_name if cluster else f"cluster {hw.cluster_id}" |
| 32 | + print(f"Name: {hw.name}") |
| 33 | + print(f"Cluster: {cluster_name}") |
| 34 | + print(f"GPU Type: {hw.gpu_type}") |
| 35 | + print(f"Num GPUs: {hw.num_gpu}") |
| 36 | + print(f"CPU: {hw.cpu}") |
| 37 | + print(f"Memory: {hw.memory}") |
| 38 | + print("-" * 40) |
| 39 | + |
| 40 | + |
| 41 | +def create_hardware_instance(): |
| 42 | + """Create a hardware instance (requires admin privileges).""" |
| 43 | + request = CreateHardwareInstanceRequest( |
| 44 | + cluster_id=1, |
| 45 | + name="h100-8x", |
| 46 | + gpu_type="H100", |
| 47 | + num_gpu=8, |
| 48 | + cpu=64000, |
| 49 | + memory=128000, |
| 50 | + accelerator_resource_key="nvidia.com/gpu", |
| 51 | + node_affinity_labels={"gpu": "h100"}, |
| 52 | + accelerator_memory=80000, |
| 53 | + ) |
| 54 | + with get_centml_client() as client: |
| 55 | + instance = client.create_hardware_instance(request) |
| 56 | + print(f"Created hardware instance '{instance.name}' with ID {instance.id}") |
| 57 | + return instance.id |
| 58 | + |
| 59 | + |
| 60 | +def delete_hardware_instance(hardware_instance_id): |
| 61 | + """Delete a hardware instance by ID (requires admin privileges).""" |
| 62 | + with get_centml_client() as client: |
| 63 | + client.delete_hardware_instance(hardware_instance_id) |
| 64 | + print(f"Deleted hardware instance {hardware_instance_id}") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + list_hardware_instances() |
0 commit comments