|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to manage hardware instances via the CentML SDK. |
| 4 | +
|
| 5 | +Demonstrates the hardware instance lifecycle: |
| 6 | +- Listing hardware instances (optionally filtered by cluster) |
| 7 | +- Creating a new hardware instance |
| 8 | +- Deleting a hardware instance |
| 9 | +
|
| 10 | +Note: creating and deleting hardware instances require admin privileges |
| 11 | +(PERM_ADMIN_MANAGE_HARDWARE) on your CentML organization. |
| 12 | +""" |
| 13 | + |
| 14 | +import click |
| 15 | + |
| 16 | +from centml.sdk import CreateHardwareInstanceRequest |
| 17 | +from centml.sdk.api import get_centml_client |
| 18 | + |
| 19 | + |
| 20 | +def display_hardware_instances(instances): |
| 21 | + """Display hardware instance information in a formatted list.""" |
| 22 | + if not instances: |
| 23 | + click.echo("No hardware instances found.") |
| 24 | + return |
| 25 | + |
| 26 | + click.echo(f"\nFound {len(instances)} hardware instance(s)\n") |
| 27 | + |
| 28 | + for hw in sorted(instances, key=lambda x: x.id): |
| 29 | + click.echo(f"ID: {hw.id}") |
| 30 | + click.echo(f"Name: {hw.name}") |
| 31 | + click.echo(f"Cluster ID: {hw.cluster_id}") |
| 32 | + click.echo(f"GPU Type: {hw.gpu_type}") |
| 33 | + click.echo(f"Num GPUs: {hw.num_gpu}") |
| 34 | + click.echo(f"CPU: {hw.cpu}") |
| 35 | + click.echo(f"Memory: {hw.memory}") |
| 36 | + click.echo(f"Cost / hr: {hw.cost_per_hr}") |
| 37 | + click.echo("-" * 40) |
| 38 | + |
| 39 | + |
| 40 | +@click.group() |
| 41 | +def cli(): |
| 42 | + """Manage hardware instances. |
| 43 | +
|
| 44 | + These commands use the centml CLI authentication, so make sure you are |
| 45 | + logged in to the centml CLI before running this script. |
| 46 | + """ |
| 47 | + |
| 48 | + |
| 49 | +@cli.command(name="list") |
| 50 | +@click.option("--cluster-id", type=int, default=None, help="Filter to a specific cluster") |
| 51 | +def list_instances(cluster_id): |
| 52 | + """List hardware instances, optionally filtered by cluster. |
| 53 | +
|
| 54 | + \b |
| 55 | + Examples: |
| 56 | + python manage_hardware_instances.py list |
| 57 | + python manage_hardware_instances.py list --cluster-id 1 |
| 58 | + """ |
| 59 | + with get_centml_client() as client: |
| 60 | + instances = client.get_hardware_instances(cluster_id) |
| 61 | + display_hardware_instances(instances) |
| 62 | + |
| 63 | + |
| 64 | +@cli.command() |
| 65 | +@click.option("--cluster-id", type=int, required=True, help="Cluster the hardware belongs to") |
| 66 | +@click.option("--name", required=True, help="Display name for the hardware instance") |
| 67 | +@click.option("--gpu-type", required=True, help="GPU type identifier (e.g. H100, A100)") |
| 68 | +@click.option("--num-gpu", type=int, required=True, help="Number of GPUs") |
| 69 | +@click.option("--cpu", type=int, required=True, help="CPU in millicores") |
| 70 | +@click.option("--memory", type=int, required=True, help="Memory in MB") |
| 71 | +@click.option("--accelerator-resource-key", required=True, help="Kubernetes accelerator resource key") |
| 72 | +@click.option("--accelerator-memory", type=int, required=True, help="Accelerator memory in MB") |
| 73 | +@click.option( |
| 74 | + "--node-affinity-label", |
| 75 | + "node_affinity_labels", |
| 76 | + type=(str, str), |
| 77 | + multiple=True, |
| 78 | + help="Node affinity label as KEY VALUE (repeatable)", |
| 79 | +) |
| 80 | +def create( |
| 81 | + cluster_id, name, gpu_type, num_gpu, cpu, memory, accelerator_resource_key, accelerator_memory, node_affinity_labels |
| 82 | +): |
| 83 | + """Create a new hardware instance (requires admin privileges). |
| 84 | +
|
| 85 | + \b |
| 86 | + Examples: |
| 87 | + python manage_hardware_instances.py create \\ |
| 88 | + --cluster-id 1 --name h100-8x --gpu-type H100 --num-gpu 8 \\ |
| 89 | + --cpu 64000 --memory 128000 \\ |
| 90 | + --accelerator-resource-key nvidia.com/gpu --accelerator-memory 80000 \\ |
| 91 | + --node-affinity-label gpu h100 |
| 92 | + """ |
| 93 | + request = CreateHardwareInstanceRequest( |
| 94 | + cluster_id=cluster_id, |
| 95 | + name=name, |
| 96 | + gpu_type=gpu_type, |
| 97 | + num_gpu=num_gpu, |
| 98 | + cpu=cpu, |
| 99 | + memory=memory, |
| 100 | + accelerator_resource_key=accelerator_resource_key, |
| 101 | + node_affinity_labels=dict(node_affinity_labels), |
| 102 | + accelerator_memory=accelerator_memory, |
| 103 | + ) |
| 104 | + with get_centml_client() as client: |
| 105 | + instance = client.create_hardware_instance(request) |
| 106 | + click.echo(f"Created hardware instance '{instance.name}' with ID {instance.id}") |
| 107 | + |
| 108 | + |
| 109 | +@cli.command() |
| 110 | +@click.argument("hardware_instance_id", type=int) |
| 111 | +def delete(hardware_instance_id): |
| 112 | + """Delete a hardware instance by ID (requires admin privileges). |
| 113 | +
|
| 114 | + \b |
| 115 | + Examples: |
| 116 | + python manage_hardware_instances.py delete 123 |
| 117 | + """ |
| 118 | + with get_centml_client() as client: |
| 119 | + client.delete_hardware_instance(hardware_instance_id) |
| 120 | + click.echo(f"Deleted hardware instance {hardware_instance_id}") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + cli() |
0 commit comments