-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstances_async.py
More file actions
356 lines (269 loc) · 13.8 KB
/
instances_async.py
File metadata and controls
356 lines (269 loc) · 13.8 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import asyncio
from typing import List
from gcore import AsyncGcore
from gcore.types.cloud.instance import Volume, Instance
from gcore.types.cloud.network_interface import NetworkInterface
from gcore.types.cloud.instance_create_params import (
InterfaceNewInterfaceExternalSerializerPydantic,
VolumeCreateInstanceCreateVolumeFromImageSerializer,
)
from gcore.types.cloud.instances.instance_flavor_detailed import InstanceFlavorDetailed
async 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"]
# TODO set placement group ID before running
placement_group_id = os.environ.get("GCORE_CLOUD_PLACEMENT_GROUP_ID")
gcore = AsyncGcore(
# No need to explicitly pass to AsyncGcore constructor if using environment variables
# api_key=api_key,
# cloud_project_id=cloud_project_id,
# cloud_region_id=cloud_region_id,
)
uploaded_image_id = await upload_image(client=gcore)
instance_id = await create_instance(client=gcore, image_id=uploaded_image_id)
instance = await get_instance(client=gcore, instance_id=instance_id)
await get_console(client=gcore, instance_id=instance_id)
await list_instances(client=gcore)
await update_instance(client=gcore, instance_id=instance_id)
await reboot_instance(client=gcore, instance_id=instance_id)
await resize_instance(client=gcore, instance_id=instance_id)
# Flavors
await list_flavors(client=gcore)
# Images
await list_images(client=gcore)
await get_image(client=gcore, image_id=uploaded_image_id)
await update_image(client=gcore, image_id=uploaded_image_id)
await delete_image(client=gcore, image_id=uploaded_image_id)
# volume_image_id = await create_image_from_volume(client=gcore, volume_id=instance.volumes[0].id)
# await delete_image(client=gcore, image_id=volume_image_id)
# Interfaces
interfaces = await list_interfaces(client=gcore, instance_id=instance_id)
if interfaces:
ip_address = interfaces[0].ip_assignments[0].ip_address
port_id = interfaces[0].port_id
network_id = interfaces[0].network_id
await detach_interface(client=gcore, instance_id=instance_id, ip_address=ip_address, port_id=port_id)
await attach_interface(client=gcore, instance_id=instance_id, network_id=network_id)
# Metrics
await list_metrics(client=gcore, instance_id=instance_id)
# Placement groups
if placement_group_id:
await add_to_placement_group(client=gcore, instance_id=instance_id, placement_group_id=placement_group_id)
await remove_from_placement_group(client=gcore, instance_id=instance_id)
# Security groups
await unassign_security_group(client=gcore, instance_id=instance_id)
await assign_security_group(client=gcore, instance_id=instance_id)
await delete_instance(client=gcore, instance_id=instance_id, volumes=instance.volumes)
async def create_instance(*, client: AsyncGcore, image_id: str) -> str:
print("\n=== CREATE INSTANCE ===")
instance = await client.cloud.instances.create_and_poll(
name="gcore-go-example-instance",
flavor="g1-standard-1-2",
interfaces=[
InterfaceNewInterfaceExternalSerializerPydantic(type="external"),
],
volumes=[
VolumeCreateInstanceCreateVolumeFromImageSerializer(
name="gcore-go-example-volume",
size=10,
type_name="standard",
source="image",
image_id=image_id,
boot_index=0,
),
],
password="Gcore123!",
tags={"name": "gcore-go-example"},
)
print(f"Created instance: ID={instance.id}, name={instance.name}, status={instance.status}")
print("========================")
return instance.id
async def get_instance(*, client: AsyncGcore, instance_id: str) -> Instance:
print("\n=== GET INSTANCE ===")
instance = await client.cloud.instances.get(instance_id=instance_id)
print(f"Instance: ID={instance.id}, name={instance.name}, status={instance.status}")
print("========================")
return instance
async def get_console(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== GET CONSOLE ===")
console = await client.cloud.instances.get_console(instance_id=instance_id)
print(
f"Console: protocol={console.remote_console.protocol}, type={console.remote_console.type}, url={console.remote_console.url}"
)
print("========================")
async def list_instances(*, client: AsyncGcore) -> None:
print("\n=== LIST INSTANCES ===")
instances = await client.cloud.instances.list()
count = 0
async for instance in instances:
count += 1
print(f" {count}. Instance: ID={instance.id}, name={instance.name}, status={instance.status}")
print("========================")
async def update_instance(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== UPDATE INSTANCE ===")
instance = await client.cloud.instances.update(instance_id=instance_id, name="gcore-go-example-updated")
print(f"Instance updated: ID={instance.id}, name changed to {instance.name}")
print("========================")
async def reboot_instance(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== REBOOT INSTANCE ===")
response = await client.cloud.instances.action(instance_id=instance_id, action="reboot")
await client.cloud.tasks.poll(task_id=response.tasks[0])
print(f"Rebooted instance: {instance_id}")
print("========================")
async def resize_instance(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== RESIZE INSTANCE ===")
instance = await client.cloud.instances.resize_and_poll(instance_id=instance_id, flavor_id="g1-standard-2-4")
print(f"Instance resized: ID={instance.id}, new flavor=g1-standard-2-4")
print("========================")
async def list_flavors(*, client: AsyncGcore) -> None:
print("\n=== LIST FLAVORS ===")
flavors = await client.cloud.instances.flavors.list()
await _print_flavor_details(flavors.results)
print("========================")
async def list_interfaces(*, client: AsyncGcore, instance_id: str) -> List[NetworkInterface]:
print("\n=== LIST INTERFACES ===")
interfaces = await client.cloud.instances.interfaces.list(instance_id=instance_id)
for count, interface in enumerate(interfaces.results, 1):
print(f" {count}. Interface: PortID={interface.port_id}, NetworkID={interface.network_id}")
print("========================")
return interfaces.results
async def list_metrics(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== LIST METRICS ===")
metrics = await client.cloud.instances.metrics.list(instance_id=instance_id, time_interval=1, time_unit="hour")
print(f"Metrics for instance: {len(metrics.results)} entries")
# Display first few metrics
display_count = min(3, len(metrics.results))
for i in range(display_count):
metric = metrics.results[i]
cpu = getattr(metric, "cpu_util", "N/A")
memory = getattr(metric, "memory_util", "N/A")
timestamp = getattr(metric, "timestamp", "N/A")
print(f" {i + 1}. Metric: CPU={cpu}%, Memory={memory}%, Time={timestamp}")
if len(metrics.results) > display_count:
print(f" ... and {len(metrics.results) - display_count} more metrics")
print("========================")
async def assign_security_group(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== ASSIGN SECURITY GROUP ===")
await client.cloud.instances.assign_security_group(instance_id=instance_id, name="default")
print("Assigned security group: default")
print("========================")
async def unassign_security_group(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== UNASSIGN SECURITY GROUP ===")
await client.cloud.instances.unassign_security_group(instance_id=instance_id, name="default")
print("Unassigned security group: default")
print("========================")
async def add_to_placement_group(*, client: AsyncGcore, instance_id: str, placement_group_id: str) -> None:
print("\n=== ADD TO PLACEMENT GROUP ===")
await client.cloud.instances.add_to_placement_group_and_poll(
instance_id=instance_id, servergroup_id=placement_group_id
)
print(f"Added instance {instance_id} to placement group: {placement_group_id}")
print("========================")
async def remove_from_placement_group(*, client: AsyncGcore, instance_id: str) -> None:
print("\n=== REMOVE FROM PLACEMENT GROUP ===")
await client.cloud.instances.remove_from_placement_group_and_poll(instance_id=instance_id)
print(f"Removed instance {instance_id} from placement group")
print("========================")
async def detach_interface(*, client: AsyncGcore, instance_id: str, ip_address: str, port_id: str) -> None:
print("\n=== DETACH INTERFACE ===")
interfaces = await client.cloud.instances.interfaces.detach_and_poll(
instance_id=instance_id, ip_address=ip_address, port_id=port_id
)
for count, interface in enumerate(interfaces.results, 1):
print(f" {count}. Interface: PortID={interface.port_id}, NetworkID={interface.network_id}")
print(f"Detached interface (IP: {ip_address}, Port: {port_id}) from instance: {instance_id}")
print("========================")
async def attach_interface(*, client: AsyncGcore, instance_id: str, network_id: str) -> None:
print("\n=== ATTACH INTERFACE ===")
interfaces = await client.cloud.instances.interfaces.attach_and_poll(
instance_id=instance_id, type="any_subnet", network_id=network_id
)
for count, interface in enumerate(interfaces.results, 1):
print(f" {count}. Interface: PortID={interface.port_id}, NetworkID={interface.network_id}")
print(f"Attached interface to any available subnet in network {network_id} (instance: {instance_id})")
print("========================")
async def delete_instance(*, client: AsyncGcore, instance_id: str, volumes: List[Volume]) -> None:
print("\n=== DELETE INSTANCE ===")
volumes_str = ""
if volumes:
volumes_str = ",".join([vol.id for vol in volumes])
await client.cloud.instances.delete_and_poll(
instance_id=instance_id,
delete_floatings=True,
volumes=volumes_str,
)
print(f"Deleted instance and related resources: ID={instance_id}, Volumes={volumes_str}")
print("========================")
async def upload_image(*, client: AsyncGcore) -> str:
print("\n=== UPLOAD IMAGE ===")
image = await client.cloud.instances.images.upload_and_poll(
name="gcore-go-example-uploaded",
url="https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img",
os_type="linux",
architecture="x86_64",
ssh_key="allow",
os_distro="Ubuntu",
os_version="24.04",
)
print(
f"Uploaded image: ID={image.id}, name={image.name}, OS type={image.os_type}, arch={image.architecture}, status={image.status}, size={image.size}"
)
print("========================")
return image.id
async def create_image_from_volume(*, client: AsyncGcore, volume_id: str) -> str:
print("\n=== CREATE IMAGE FROM VOLUME ===")
image = await client.cloud.instances.images.create_from_volume_and_poll(
volume_id=volume_id, name="gcore-go-example", os_type="linux"
)
print(f"Created image ID: {image.id}")
print("========================")
return image.id
async def list_images(*, client: AsyncGcore) -> None:
print("\n=== LIST ALL IMAGES ===")
images = await client.cloud.instances.images.list()
display_count = 3
if len(images.results) < display_count:
display_count = len(images.results)
for i in range(display_count):
img = images.results[i]
print(f" {i + 1}. Image ID: {img.id}, name: {img.name}, OS type: {img.os_type}, status: {img.status}")
if len(images.results) > display_count:
print(f" ... and {len(images.results) - display_count} more images")
print("========================")
async def get_image(*, client: AsyncGcore, image_id: str) -> None:
print("\n=== GET IMAGE BY ID ===")
image = await client.cloud.instances.images.get(image_id=image_id)
print(f"Image ID: {image.id}, name: {image.name}, OS type: {image.os_type}, status: {image.status}")
print("========================")
async def update_image(*, client: AsyncGcore, image_id: str) -> None:
print("\n=== UPDATE IMAGE ===")
updated_image = await client.cloud.instances.images.update(image_id=image_id, name="gcore-go-example-updated")
print(f"Updated image ID: {updated_image.id}, name: {updated_image.name}")
print("========================")
async def delete_image(*, client: AsyncGcore, image_id: str) -> None:
print("\n=== DELETE IMAGE ===")
await client.cloud.instances.images.delete_and_poll(image_id=image_id)
print(f"Image with ID {image_id} successfully deleted")
print("========================")
async def _print_flavor_details(flavors: List[InstanceFlavorDetailed]) -> 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: ID={flavor.flavor_id}, name={flavor.flavor_name}")
print(f" RAM: {flavor.ram} MB, VCPUs: {flavor.vcpus}")
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")
if __name__ == "__main__":
asyncio.run(main())