virtpool/client.py

53 lines
1.3 KiB
Python
Executable File

import requests
class RPCClient:
"""
RPCClient Class
"""
def create_vm(self, **kwargs):
url = "http://127.0.0.1:8080/create?"
if kwargs:
q = ""
for k in kwargs.keys():
q += "&{}={}".format(k, kwargs[k])
url += q
print(url)
resp = requests.get(url)
return resp.json()
def start_vm(self, vm):
"""get a vm and try starting it
return the ID or appropriate message
"""
pass
def stop_vm(self, id):
# stops vm of a given id
pass
if __name__ == "__main__":
import random
client = RPCClient()
print("Creating VM...")
name = "CentOS-8-Demo-{}-{}".format(
random.sample("abcdefghijklmn", 1)[0], random.sample(range(10), 1)[0]
)
iso_src = "/home/coolguy/Downloads/CentOS-8.2.2004-x86_64-boot.iso"
img_src = "/var/lib/libvirt/images/vol.img"
vm_info = client.create_vm(name=name, iso_source=iso_src, img_source=img_src)
if vm_info["status"]:
print("\n\nGuest '{}' has been created and booted".format(name))
print("UUID for {}: {}".format(name, vm_info["uuid"]))
print("ID for {} : {}".format(name, vm_info["id"]))
else:
print(vm_info["err"])