2020-09-19 10:03:40 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""Agent for setting up accounts for sysdeploy clients"""
|
|
|
|
# pylint: disable=subprocess-run-check,too-few-public-methods
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import cherrypy
|
|
|
|
import libvirt
|
2020-09-24 12:41:10 +02:00
|
|
|
import json
|
2020-09-26 16:46:26 +02:00
|
|
|
import threading
|
|
|
|
from multiprocessing.pool import ThreadPool
|
|
|
|
pool = ThreadPool(processes=1)
|
2020-09-24 12:41:10 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-09-26 16:46:26 +02:00
|
|
|
#TEMPLATE_ENVIRONMENT.globals['STATIC_PREFIX'] = '/templates/'
|
|
|
|
|
2020-09-24 12:41:10 +02:00
|
|
|
def render_template(template_filename, context):
|
|
|
|
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
|
2020-09-19 10:03:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Client():
|
|
|
|
"""Main manager class"""
|
2020-09-24 12:41:10 +02:00
|
|
|
uris = [
|
|
|
|
#'qemu+ssh://pool@reserve.homedevops/system',
|
|
|
|
#'qemu+ssh://root@check2.homedevops/system',
|
|
|
|
#'qemu+ssh://root@check3.homedevops/system'
|
|
|
|
'qemu:///system'
|
|
|
|
]
|
2020-09-19 10:03:40 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.status = {}
|
|
|
|
self.connections = {}
|
|
|
|
for i in self.uris:
|
|
|
|
self.connections[i] = libvirt.open(i)
|
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def getavailable(self):
|
|
|
|
out = ""
|
|
|
|
for n, c in self.connections.items():
|
|
|
|
# cmap = c.getCPUMap()
|
|
|
|
# out += "CPUs: {}".format(str(cmap[0]))
|
|
|
|
# out += "Available: {}".format(str(cmap[1]))
|
|
|
|
nodeinfo = c.getInfo()
|
|
|
|
mem = nodeinfo[1]
|
|
|
|
cpus = nodeinfo[2]
|
|
|
|
usedmem = 0
|
|
|
|
usedcpus = 0
|
|
|
|
|
|
|
|
domain_ids = c.listDomainsID()
|
|
|
|
for did in domain_ids:
|
|
|
|
dom = c.lookupByID(did)
|
|
|
|
state, maxmem, mem2, cpus, cput = dom.info()
|
|
|
|
usedmem += maxmem/1024
|
|
|
|
usedcpus += cpus
|
|
|
|
|
|
|
|
out += "{}: {}/{} CPUs free,{}MiB/{}MiB memory free<br/>\n".format(
|
|
|
|
n,
|
|
|
|
cpus - usedcpus, cpus,
|
|
|
|
str((mem - usedmem)),
|
|
|
|
str(mem))
|
|
|
|
return out
|
2020-09-24 12:41:10 +02:00
|
|
|
|
|
|
|
@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:
|
2020-09-26 16:46:26 +02:00
|
|
|
async_result = pool.apply_async(self.__create_vm, (host, xml))
|
|
|
|
return async_result.get()
|
2020-09-24 12:41:10 +02:00
|
|
|
return "No host available"
|
2020-09-19 10:03:40 +02:00
|
|
|
|
2020-09-26 16:46:26 +02:00
|
|
|
|
|
|
|
def __create_vm(self, host, xml):
|
|
|
|
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})
|
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def gethost(self):
|
|
|
|
"""
|
|
|
|
It'll show relevant info about all available hosts
|
|
|
|
"""
|
|
|
|
|
|
|
|
host_data= []
|
|
|
|
|
|
|
|
for i, each in enumerate(self.uris):
|
|
|
|
tmp= {}
|
|
|
|
c= self.connections[each]
|
|
|
|
nodeinfo = c.getInfo()
|
|
|
|
mem = nodeinfo[1]
|
|
|
|
cpus = nodeinfo[2]
|
|
|
|
usedmem = 0
|
|
|
|
usedcpus = 0
|
|
|
|
|
|
|
|
tmp["sn"]= i+1
|
|
|
|
tmp["hostname"]= c.getHostname()
|
|
|
|
tmp["status"]= "Alive" if c.isAlive() else "Dead"
|
|
|
|
tmp["uri"]= c.getURI()
|
|
|
|
tmp["cpu"]= {}
|
|
|
|
tmp["mem"]= {}
|
|
|
|
|
|
|
|
domain_ids = c.listDomainsID()
|
|
|
|
for did in domain_ids:
|
|
|
|
dom = c.lookupByID(did)
|
|
|
|
state, maxmem, mem2, cpus2, cput = dom.info()
|
|
|
|
usedmem += maxmem/1024
|
|
|
|
usedcpus += cpus2
|
|
|
|
|
|
|
|
tmp["cpu"]["total"]= cpus
|
|
|
|
tmp["cpu"]["free"]= cpus - usedcpus
|
|
|
|
tmp["mem"]["total"]= mem
|
|
|
|
tmp["mem"]["free"]= int(mem - usedmem)
|
|
|
|
|
|
|
|
host_data.append(tmp)
|
|
|
|
|
|
|
|
return render_template("host.html", {"host_data": host_data})
|
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def getguest(self):
|
|
|
|
"""
|
|
|
|
It'll show host-wise available guests and detailed info about them
|
|
|
|
"""
|
|
|
|
|
|
|
|
guest_data= []
|
|
|
|
cnt=0
|
|
|
|
for i, each in enumerate(self.uris):
|
|
|
|
c= self.connections[each]
|
|
|
|
for did in c.listDomainsID():
|
|
|
|
dom= c.lookupByID(did)
|
|
|
|
tmp= {
|
|
|
|
"sn": cnt+1,
|
|
|
|
"guestname": dom.name(),
|
|
|
|
"hostname": c.getHostname(),
|
|
|
|
"id_": dom.ID(),
|
|
|
|
"mem": int(dom.info()[1]/1024),
|
|
|
|
"cpu": dom.info()[3],
|
|
|
|
"status": "Active" if dom.isActive() else "Inactive",
|
|
|
|
"uuid": dom.UUIDString()
|
|
|
|
}
|
|
|
|
|
|
|
|
guest_data.append(tmp)
|
|
|
|
cnt+=1
|
|
|
|
|
|
|
|
return render_template("guest.html", {"guest_data": guest_data})
|
|
|
|
|
|
|
|
|
|
|
|
def manage_host(self,host_id):
|
|
|
|
pass
|
2020-09-19 10:03:40 +02:00
|
|
|
|
|
|
|
ROOT = Client()
|
|
|
|
CONF = os.path.join(os.path.dirname(__file__), 'site.conf')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# cherrypy.config.update(CONF)
|
|
|
|
cherrypy.server.socket_host = '127.0.0.1'
|
|
|
|
cherrypy.server.socket_port = 8080
|
|
|
|
ENGINE = cherrypy.engine
|
|
|
|
# cherrypy.process.plugins.Daemonizer(engine).subscribe()
|
|
|
|
# cherrypy.process.plugins.DropPrivileges(
|
|
|
|
# ENGINE, uid='cherrypy', gid='cherrypy').subscribe()
|
2020-09-26 16:46:26 +02:00
|
|
|
cherrypy.tree.mount(Client(),
|
|
|
|
'/',
|
|
|
|
config={
|
|
|
|
'/':{
|
|
|
|
'tools.staticdir.on': True,
|
|
|
|
'tools.staticdir.dir': PATH+"/templates"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2020-09-19 10:03:40 +02:00
|
|
|
if hasattr(ENGINE, "signal_handler"):
|
|
|
|
ENGINE.signal_handler.subscribe()
|
|
|
|
if hasattr(ENGINE, "console_control_handler"):
|
|
|
|
ENGINE.console_control_handler.subscribe()
|
|
|
|
try:
|
|
|
|
ENGINE.start()
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
ENGINE.block()
|