Skip to content

Commit

Permalink
pool ok
Browse files Browse the repository at this point in the history
  • Loading branch information
sean committed Oct 14, 2024
1 parent 3506e26 commit 82f821a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
40 changes: 40 additions & 0 deletions dev/create-vm-pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
import os
from time import sleep

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'helpers')))

import cudo_api

from cudo_compute import Disk, CreateVMBody
from cudo_compute.rest import ApiException


vm_name = 'launch-lot'
id = 1
ids = []
api = cudo_api.virtual_machines()
try:
for i in range(12):
vm_id = vm_name + str(id)
disk = Disk(storage_class="STORAGE_CLASS_NETWORK", size_gib=100,
id="my-disk-id-" + vm_id)

request = CreateVMBody(vm_id=vm_id, machine_type="intel-broadwell",
data_center_id="gb-bournemouth-1", boot_disk_image_id='ubuntu-nvidia-docker',
memory_gib=4, vcpus=2, gpus=0, gpu_model="", boot_disk=disk)

vm = api.create_vm(cudo_api.project_id_throwable(), request)
print(vm)
ids.append(vm_id)
id += 1

except ApiException as e:
print(e)

sleep(80)
for del_id in ids:
res = api.terminate_vm(cudo_api.project_id_throwable(), del_id)
print(res)

print("done")
18 changes: 14 additions & 4 deletions helpers/cudo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def project_id_throwable():
if err:
raise Exception(err)


def api_keys():
return cudo.APIKeysApi(c)

Expand Down Expand Up @@ -116,10 +117,14 @@ def user():
return cudo.UserApi(c)


def legacy_virtual_machines():
return cudo.VirtualMachinesApi(c)


class PooledVirtualMachinesApi(cudo.VirtualMachinesApi):
def __init__(self, api_client=None):
self.task_queue = None
self.max_workers = 2
self.max_workers = 5
self.shutdown_event = threading.Event()
self.workers_active = False
self.executor = None
Expand All @@ -137,12 +142,19 @@ def worker(self):
if not self.task_queue:
break
req = self.task_queue.get(timeout=1)
print(req)
create_vm_body = None
try:
project, create_vm_body = req
vm = super().create_vm(project, create_vm_body)
print(f"Created VM: {vm.to_dict()}")
wait = True
while wait:
res = self.get_vm(project, create_vm_body.vm_id)
if (res.vm.state == 'ACTIVE' or res.vm.state == 'FAILED' or res.vm.state == 'STOPPED'
or res.vm.state == 'SUSPENDED' or res.vm.state == 'DELETED'):
wait = False
else:
sleep(5)
except Exception as e:
if create_vm_body:
print(f"Error creating VM: {create_vm_body.vm_id} {e}")
Expand Down Expand Up @@ -174,8 +186,6 @@ def stop_workers(self):
except Exception as e:
print(f"Error shutting down: {e}")



pool = PooledVirtualMachinesApi(c)

def virtual_machines():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "cudo-compute"
version = "0.2.0"
version = "0.3.0"
authors = [
{ name = "Cudo Ventures", email = "dev@cudoventures.com" },
]
Expand Down

0 comments on commit 82f821a

Please sign in to comment.