RPC frontend for libvir

This commit is contained in:
coolguy-cell 2020-09-24 16:11:10 +05:30
parent 78d6d913ef
commit db5b2e96c1
No known key found for this signature in database
GPG Key ID: CD3A42E1D470AD70
2 changed files with 122 additions and 5 deletions

48
client.py Normal file
View File

@ -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"])

View File

@ -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()