Skip to content

Commit 7279536

Browse files
feat(cloud): add examples for GPU baremetal cluster images
1 parent 6ac2648 commit 7279536

2 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from typing import List
2+
3+
from gcore import Gcore
4+
from gcore.types.cloud.gpu_image import GPUImage
5+
6+
7+
def main() -> None:
8+
# TODO set API key before running
9+
# api_key = os.environ["GCORE_API_KEY"]
10+
# TODO set cloud project ID before running
11+
# cloud_project_id = os.environ["GCORE_CLOUD_PROJECT_ID"]
12+
# TODO set cloud region ID before running
13+
# cloud_region_id = os.environ["GCORE_CLOUD_REGION_ID"]
14+
15+
gcore = Gcore(
16+
# No need to explicitly pass to Gcore constructor if using environment variables
17+
# api_key=api_key,
18+
# cloud_project_id=cloud_project_id,
19+
# cloud_region_id=cloud_region_id,
20+
)
21+
22+
# List existing images
23+
list_images(client=gcore)
24+
25+
# Upload a new image
26+
image = upload_image(client=gcore)
27+
28+
# Get the newly uploaded image
29+
get_image(client=gcore, image_id=image.id)
30+
31+
# Delete the image
32+
delete_image(client=gcore, image_id=image.id)
33+
34+
35+
def list_images(*, client: Gcore) -> List[GPUImage]:
36+
print("\n=== LIST GPU BAREMETAL CLUSTER IMAGES ===")
37+
images = client.cloud.gpu_baremetal.clusters.images.list()
38+
_print_image_details(images.results)
39+
print(f"Total GPU baremetal cluster images: {len(images.results)}")
40+
print("========================")
41+
return images.results
42+
43+
44+
def upload_image(*, client: Gcore) -> GPUImage:
45+
print("\n=== UPLOAD GPU BAREMETAL CLUSTER IMAGE ===")
46+
image = client.cloud.gpu_baremetal.clusters.images.upload_and_poll(
47+
name="gcore-python-example-gpu-baremetal-image",
48+
url="http://mirror.noris.net/cirros/0.4.0/cirros-0.4.0-x86_64-disk.img",
49+
architecture="x86_64",
50+
os_type="linux",
51+
ssh_key="allow",
52+
tags={"name": "gcore-python-example"},
53+
)
54+
print(f"Uploaded image: ID={image.id}, name={image.name}, status={image.status}")
55+
print("========================")
56+
return image
57+
58+
59+
def get_image(*, client: Gcore, image_id: str) -> GPUImage:
60+
print("\n=== GET GPU BAREMETAL CLUSTER IMAGE ===")
61+
image = client.cloud.gpu_baremetal.clusters.images.get(image_id=image_id)
62+
print(f"Image: ID={image.id}, name={image.name}, status={image.status}")
63+
print(f"OS type: {image.os_type}, architecture: {image.architecture}")
64+
print(f"Min RAM: {image.min_ram} MB, Min Disk: {image.min_disk} GB")
65+
print(f"Visibility: {image.visibility}")
66+
print("========================")
67+
return image
68+
69+
70+
def delete_image(*, client: Gcore, image_id: str) -> None:
71+
print("\n=== DELETE GPU BAREMETAL CLUSTER IMAGE ===")
72+
client.cloud.gpu_baremetal.clusters.images.delete_and_poll(image_id=image_id)
73+
print(f"Deleted image: ID={image_id}")
74+
print("========================")
75+
76+
77+
def _print_image_details(images: List[GPUImage]) -> None:
78+
display_count = 3
79+
if len(images) < display_count:
80+
display_count = len(images)
81+
82+
for i in range(display_count):
83+
img = images[i]
84+
print(f" {i + 1}. Image ID: {img.id}, name: {img.name}, OS type: {img.os_type}, status: {img.status}")
85+
86+
if len(images) > display_count:
87+
print(f" ... and {len(images) - display_count} more images")
88+
89+
90+
if __name__ == "__main__":
91+
main()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import asyncio
2+
from typing import List
3+
4+
from gcore import AsyncGcore
5+
from gcore.types.cloud.gpu_image import GPUImage
6+
7+
8+
async def main() -> None:
9+
# TODO set API key before running
10+
# api_key = os.environ["GCORE_API_KEY"]
11+
# TODO set cloud project ID before running
12+
# cloud_project_id = os.environ["GCORE_CLOUD_PROJECT_ID"]
13+
# TODO set cloud region ID before running
14+
# cloud_region_id = os.environ["GCORE_CLOUD_REGION_ID"]
15+
16+
gcore = AsyncGcore(
17+
# No need to explicitly pass to AsyncGcore constructor if using environment variables
18+
# api_key=api_key,
19+
# cloud_project_id=cloud_project_id,
20+
# cloud_region_id=cloud_region_id,
21+
)
22+
23+
# List existing images
24+
await list_images(client=gcore)
25+
26+
# Upload a new image
27+
image = await upload_image(client=gcore)
28+
29+
# Get the newly uploaded image
30+
await get_image(client=gcore, image_id=image.id)
31+
32+
# Delete the image
33+
await delete_image(client=gcore, image_id=image.id)
34+
35+
36+
async def list_images(*, client: AsyncGcore) -> List[GPUImage]:
37+
print("\n=== LIST GPU BAREMETAL CLUSTER IMAGES ===")
38+
images = await client.cloud.gpu_baremetal.clusters.images.list()
39+
_print_image_details(images.results)
40+
print(f"Total GPU baremetal cluster images: {len(images.results)}")
41+
print("========================")
42+
return images.results
43+
44+
45+
async def upload_image(*, client: AsyncGcore) -> GPUImage:
46+
print("\n=== UPLOAD GPU BAREMETAL CLUSTER IMAGE ===")
47+
image = await client.cloud.gpu_baremetal.clusters.images.upload_and_poll(
48+
name="gcore-python-example-gpu-baremetal-image",
49+
url="http://mirror.noris.net/cirros/0.4.0/cirros-0.4.0-x86_64-disk.img",
50+
architecture="x86_64",
51+
os_type="linux",
52+
ssh_key="allow",
53+
tags={"name": "gcore-python-example"},
54+
)
55+
print(f"Uploaded image: ID={image.id}, name={image.name}, status={image.status}")
56+
print("========================")
57+
return image
58+
59+
60+
async def get_image(*, client: AsyncGcore, image_id: str) -> GPUImage:
61+
print("\n=== GET GPU BAREMETAL CLUSTER IMAGE ===")
62+
image = await client.cloud.gpu_baremetal.clusters.images.get(image_id=image_id)
63+
print(f"Image: ID={image.id}, name={image.name}, status={image.status}")
64+
print(f"OS type: {image.os_type}, architecture: {image.architecture}")
65+
print(f"Min RAM: {image.min_ram} MB, Min Disk: {image.min_disk} GB")
66+
print(f"Visibility: {image.visibility}")
67+
print("========================")
68+
return image
69+
70+
71+
async def delete_image(*, client: AsyncGcore, image_id: str) -> None:
72+
print("\n=== DELETE GPU BAREMETAL CLUSTER IMAGE ===")
73+
await client.cloud.gpu_baremetal.clusters.images.delete_and_poll(image_id=image_id)
74+
print(f"Deleted image: ID={image_id}")
75+
print("========================")
76+
77+
78+
def _print_image_details(images: List[GPUImage]) -> None:
79+
display_count = 3
80+
if len(images) < display_count:
81+
display_count = len(images)
82+
83+
for i in range(display_count):
84+
img = images[i]
85+
print(f" {i + 1}. Image ID: {img.id}, name: {img.name}, OS type: {img.os_type}, status: {img.status}")
86+
87+
if len(images) > display_count:
88+
print(f" ... and {len(images) - display_count} more images")
89+
90+
91+
if __name__ == "__main__":
92+
asyncio.run(main())

0 commit comments

Comments
 (0)