Repository navigation
Expand file tree
/
Copy pathgpu_virtual_clusters.py
More file actions
188 lines (149 loc) · 6.51 KB
/
Copy pathgpu_virtual_clusters.py
File metadata and controls
188 lines (149 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from typing import List
from gcore import Gcore
from gcore.types.cloud.gpu_image import GPUImage
from gcore.types.cloud.gpu_virtual.gpu_virtual_cluster import GPUVirtualCluster
from gcore.types.cloud.gpu_virtual.cluster_create_params import (
ServersSettings,
ServersSettingsVolumeImageVolumeInputSerializer,
ServersSettingsInterfaceExternalInterfaceInputSerializer,
)
from gcore.types.cloud.gpu_virtual.clusters.gpu_virtual_flavor import GPUVirtualFlavor
def main() -> None:
# TODO set API key before running
# api_key = os.environ["GCORE_API_KEY"]
# TODO set cloud project ID before running
# cloud_project_id = os.environ["GCORE_CLOUD_PROJECT_ID"]
# TODO set cloud region ID before running
# cloud_region_id = os.environ["GCORE_CLOUD_REGION_ID"]
gcore = Gcore(
# No need to explicitly pass to Gcore constructor if using environment variables
# api_key=api_key,
# cloud_project_id=cloud_project_id,
# cloud_region_id=cloud_region_id,
)
# Flavors
flavors = list_flavors(client=gcore)
# Images
images = list_images(client=gcore)
# Clusters
flavor_name = _get_first_available_flavor(flavors)
image_id = _get_first_image(images)
cluster = create_cluster(client=gcore, flavor=flavor_name, image_id=image_id)
get_cluster(client=gcore, cluster_id=cluster.id)
list_clusters(client=gcore)
update_cluster(client=gcore, cluster_id=cluster.id)
# Servers
list_servers(client=gcore, cluster_id=cluster.id)
# Delete
delete_cluster(client=gcore, cluster_id=cluster.id)
def create_cluster(*, client: Gcore, flavor: str, image_id: str) -> GPUVirtualCluster:
print("\n=== CREATE GPU VIRTUAL CLUSTER ===")
cluster = client.cloud.gpu_virtual.clusters.create_and_poll(
name="gcore-python-example-gpu-virtual",
flavor=flavor,
servers_count=1,
servers_settings=ServersSettings(
interfaces=[
ServersSettingsInterfaceExternalInterfaceInputSerializer(type="external"),
],
volumes=[
ServersSettingsVolumeImageVolumeInputSerializer(
name="gcore-python-example-volume",
size=50,
type="standard",
source="image",
image_id=image_id,
boot_index=0,
),
],
),
tags={"name": "gcore-python-example"},
)
print(f"Created cluster: ID={cluster.id}, name={cluster.name}, status={cluster.status}")
print("========================")
return cluster
def get_cluster(*, client: Gcore, cluster_id: str) -> GPUVirtualCluster:
print("\n=== GET GPU VIRTUAL CLUSTER ===")
cluster = client.cloud.gpu_virtual.clusters.get(cluster_id=cluster_id)
print(f"Cluster: ID={cluster.id}, name={cluster.name}, status={cluster.status}")
print("========================")
return cluster
def list_clusters(*, client: Gcore) -> None:
print("\n=== LIST GPU VIRTUAL CLUSTERS ===")
clusters = client.cloud.gpu_virtual.clusters.list()
for count, cluster in enumerate(clusters, 1):
print(f" {count}. Cluster: ID={cluster.id}, name={cluster.name}, status={cluster.status}")
print("========================")
def update_cluster(*, client: Gcore, cluster_id: str) -> None:
print("\n=== UPDATE GPU VIRTUAL CLUSTER ===")
cluster = client.cloud.gpu_virtual.clusters.update(
cluster_id=cluster_id, name="gcore-python-example-gpu-virtual-updated"
)
print(f"Updated cluster: ID={cluster.id}, name={cluster.name}")
print("========================")
def delete_cluster(*, client: Gcore, cluster_id: str) -> None:
print("\n=== DELETE GPU VIRTUAL CLUSTER ===")
client.cloud.gpu_virtual.clusters.delete_and_poll(
cluster_id=cluster_id,
all_volumes=True,
all_floating_ips=True,
)
print(f"Deleted cluster: ID={cluster_id}")
print("========================")
def list_servers(*, client: Gcore, cluster_id: str) -> None:
print("\n=== LIST GPU VIRTUAL CLUSTER SERVERS ===")
servers = client.cloud.gpu_virtual.clusters.servers.list(cluster_id=cluster_id)
for count, server in enumerate(servers.results, 1):
print(f" {count}. Server: ID={server.id}, name={server.name}, status={server.status}")
print("========================")
def list_flavors(*, client: Gcore) -> List[GPUVirtualFlavor]:
print("\n=== LIST GPU VIRTUAL FLAVORS ===")
flavors = client.cloud.gpu_virtual.clusters.flavors.list()
_print_flavor_details(flavors.results)
print("========================")
return flavors.results
def list_images(*, client: Gcore) -> List[GPUImage]:
print("\n=== LIST GPU VIRTUAL IMAGES ===")
images = client.cloud.gpu_virtual.clusters.images.list()
_print_image_details(images.results)
print("========================")
return images.results
def _print_flavor_details(flavors: List[GPUVirtualFlavor]) -> None:
display_count = 3
if len(flavors) < display_count:
display_count = len(flavors)
for i in range(display_count):
flavor = flavors[i]
print(f" {i + 1}. Flavor: name={flavor.name}")
print(f" Capacity: {flavor.capacity}")
status = "AVAILABLE"
if flavor.disabled:
status = "DISABLED"
print(f" Status: {status}")
print()
if len(flavors) > display_count:
print(f" ... and {len(flavors) - display_count} more flavors")
def _print_image_details(images: List[GPUImage]) -> None:
display_count = 3
if len(images) < display_count:
display_count = len(images)
for i in range(display_count):
img = images[i]
print(f" {i + 1}. Image ID: {img.id}, name: {img.name}, OS type: {img.os_type}, status: {img.status}")
if len(images) > display_count:
print(f" ... and {len(images) - display_count} more images")
def _get_first_available_flavor(flavors: List[GPUVirtualFlavor]) -> str:
available_flavors = [f for f in flavors if not f.disabled and f.capacity > 0]
if not available_flavors:
raise ValueError("No available flavors with capacity found")
selected = available_flavors[0]
print(f"Selected flavor: {selected.name} (Capacity: {selected.capacity})")
return selected.name
def _get_first_image(images: List[GPUImage]) -> str:
if not images:
raise ValueError("No images found")
selected = images[0]
print(f"Selected image: {selected.name} (ID: {selected.id})")
return selected.id
if __name__ == "__main__":
main()