|
| 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