From db5b2e96c10e8edc3622c4ab8771cb4451233223 Mon Sep 17 00:00:00 2001 From: coolguy-cell Date: Thu, 24 Sep 2020 16:11:10 +0530 Subject: [PATCH] RPC frontend for libvir --- client.py | 48 ++++++++++++++++++++++++++++++++ virtpool.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 client.py diff --git a/client.py b/client.py new file mode 100644 index 0000000..abbba82 --- /dev/null +++ b/client.py @@ -0,0 +1,48 @@ +import os +import requests +import json + +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__": + client= RPCClient() + print("Creating VM...") + name= "CentOS-8-Demo" + 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"]) \ No newline at end of file diff --git a/virtpool.py b/virtpool.py index 791952b..1a4c3d9 100644 --- a/virtpool.py +++ b/virtpool.py @@ -6,15 +6,29 @@ import sys import cherrypy import libvirt +import json + +import uuid +from jinja2 import Environment, FileSystemLoader +PATH = os.path.dirname(os.path.abspath(__file__)) + +TEMPLATE_ENVIRONMENT = Environment( + autoescape=False, + loader=FileSystemLoader(os.path.join(PATH, 'templates')), + trim_blocks=False) + +def render_template(template_filename, context): + return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context) class Client(): """Main manager class""" - uris = { - 'qemu+ssh://pool@reserve.homedevops/system', - 'qemu+ssh://root@check2.homedevops/system', - 'qemu+ssh://root@check3.homedevops/system' - } + uris = [ + #'qemu+ssh://pool@reserve.homedevops/system', + #'qemu+ssh://root@check2.homedevops/system', + #'qemu+ssh://root@check3.homedevops/system' + 'qemu:///system' + ] def __init__(self): self.status = {} @@ -48,6 +62,61 @@ class Client(): str((mem - usedmem)), str(mem)) return out + + @cherrypy.expose + def create( + self, + name, + iso_source, + img_source, + domain_type="kvm", + memory_unit="MiB", + memory=1024, + current_memory= 1024, + vcpu=2, + arch="x86_64", + machine="pc-q35-4.2", + offset="utc", + emulator="/usr/bin/qemu-system-x86_64" + ): + + """ create a VM with given specs + boot it and return the ID for future ref + """ + + #TODO: Check host resources(cpus, mem, etc.) before creating a VM + + param= { + "name": name, + "uuid": str(uuid.uuid4()), + "iso_source": iso_source, + "img_source": img_source, + "type": domain_type, + "memory_unit": memory_unit, + "memory": memory, + "current_memory": current_memory, + "vcpu":vcpu, + "arch": arch, + "machine": machine, + "offset": offset, + "emulator": emulator, + } + + context= { + 'args': param + } + + xml= render_template("config.tmpl.xml", context) + #print(xml) + + host= self.connections[self.uris[0]] + if host: + try: + vm= host.createXML(xml) + return json.dumps({"uuid":vm.UUIDString(), "id":vm.ID(), "status":1}) + except Exception as e: + return json.dumps({"err":str(e), "status":0}) + return "No host available" ROOT = Client()