This commit is contained in:
deflax 2017-09-21 03:07:56 +03:00
parent e84e2ee981
commit 45dccc8119
3 changed files with 45 additions and 25 deletions

View file

@ -55,6 +55,12 @@ def writedb(src_data):
pass pass
return None return None
def deletedb(cubeid):
""" remove metadata file """
dbfile = 'db/vm.{}.json'.format(cubeid)
os.remove(dbfile)
return None
def query_happiness(region_id): def query_happiness(region_id):
""" analyzes grid data for the reuqested region and returns proposed slave_id, """ analyzes grid data for the reuqested region and returns proposed slave_id,

View file

@ -1,7 +1,7 @@
#. -*- coding: utf-8 - #. -*- coding: utf-8 -
# required proxmox permissions: PVEAdmin # required proxmox permissions: PVEAdmin
# #
# afx 2015-2016 # afx 2015-2017
# site # site
from proxmoxer import ProxmoxAPI from proxmoxer import ProxmoxAPI
@ -64,9 +64,8 @@ def vmcreate(req):
except: except:
vm_pass = 'datapoint' vm_pass = 'datapoint'
#slave_name = str(grid.query_happiness(region_id, weight)) #TODO: provide weight parameters here and calculate route #slave_name = str(grid.query_happiness(region_id, weight)) #TODO: provide weight parameters here and calculate route
#slave_name = 'lexx' #staic route #slave_name = 'lexx'
slave_name = 'warrior' slave_name = 'warrior'
#vm_id = str(grid.generate_vmid()) #TODO: this should be between 100 and 65000
vm_id = random.randint(1000, 9999) vm_id = random.randint(1000, 9999)
cubeid = int(time.time() * 10000 * 10000) cubeid = int(time.time() * 10000 * 10000)
deploy = { 'cube': int(cubeid), deploy = { 'cube': int(cubeid),
@ -122,13 +121,24 @@ def vmcreate(req):
print(str(create_result)) print(str(create_result))
#start the machihe #start the machihe
#time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run
#vmstart(cubeid)
response = { 'status': 'CREATE', 'cube': cubeid, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name } response = { 'status': 'CREATE', 'cube': cubeid, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
grid.writedb(deploy) grid.writedb(deploy)
return response return response
def vmremove(cubeid):
""" terminate a vm """
slave_name, vm_type, vm_id, vmhost, vmowner = grid.queryvm(cubeid)
proxobject = auth(slave_name)
ioconfig.logger.info('%s[%s]> deleting %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).delete()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).delete()
grid.deletedb(cubeid)
return result
def vmstatus(cubeid): def vmstatus(cubeid):
""" returns the status of the machine """ """ returns the status of the machine """
@ -153,6 +163,7 @@ def vmstart(cubeid):
result = proxobject.nodes(slave_name).qemu(vm_id).status.start.post() result = proxobject.nodes(slave_name).qemu(vm_id).status.start.post()
if vm_type == 'lxc': if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.start.post() result = proxobject.nodes(slave_name).lxc(vm_id).status.start.post()
#TODO: SET START AT BOOT FLAG
response = { 'status':'START' } response = { 'status':'START' }
return response return response
@ -168,6 +179,7 @@ def vmshutdown(cubeid):
result = proxobject.nodes(slave_name).qemu(vm_id).status.shutdown.post() result = proxobject.nodes(slave_name).qemu(vm_id).status.shutdown.post()
if vm_type == 'lxc': if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.shutdown.post() result = proxobject.nodes(slave_name).lxc(vm_id).status.shutdown.post()
#TODO: REMOVE START AT BOOT FLAG
#ioconfig.logger.info('slave[{}]> {}'.format(slave_name, result)) #ioconfig.logger.info('slave[{}]> {}'.format(slave_name, result))
response = { 'status':'SHUTDOWN', 'vmid':vm_id } response = { 'status':'SHUTDOWN', 'vmid':vm_id }
return response return response
@ -184,6 +196,7 @@ def vmstop(cubeid):
result = proxobject.nodes(slave_name).qemu(vm_id).status.stop.post() result = proxobject.nodes(slave_name).qemu(vm_id).status.stop.post()
if vm_type == 'lxc': if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.stop.post() result = proxobject.nodes(slave_name).lxc(vm_id).status.stop.post()
#TODO: REMOVE START AT BOOT FLAG
#ioconfig.logger.info('slave[{}]> {}'.format(slave_name, result)) #ioconfig.logger.info('slave[{}]> {}'.format(slave_name, result))
response = { 'status':'STOP', 'vmid':vm_id } response = { 'status':'STOP', 'vmid':vm_id }
return response return response
@ -290,6 +303,7 @@ def vmvnc(cubeid):
prefix = external_url + "?host=" + vnchost + "&port=" + listenport + "&view_only=false&encrypt=1&true_color=1&password=" prefix = external_url + "?host=" + vnchost + "&port=" + listenport + "&view_only=false&encrypt=1&true_color=1&password="
vnc_url = prefix + ticket['ticket'] vnc_url = prefix + ticket['ticket']
time.sleep(3) #wait few seconds for the parallel vncwebsocket
ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, listenport)) ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, listenport))
#response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] } #response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] }
response = { 'status':'VNC', 'url':vnc_url } response = { 'status':'VNC', 'url':vnc_url }

View file

@ -38,12 +38,12 @@ def selector(fn, req, vmid=0):
if fn == 'vmcreate': if fn == 'vmcreate':
body = plugin.vmcreate(json) body = plugin.vmcreate(json)
elif fn == 'vmremove':
body = plugin.vmremove(vmid)
elif fn == 'vmstatus': elif fn == 'vmstatus':
body = plugin.vmstatus(vmid) body = plugin.vmstatus(vmid)
elif fn == 'vmdelete':
body = plugin.vmdelete(vmid)
elif fn == 'vmsuspend': elif fn == 'vmsuspend':
body = plugin.vmsuspend(vmid) body = plugin.vmsuspend(vmid)
@ -140,26 +140,26 @@ class CreateResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp): def on_post(self, req, resp):
"""Create a cluster node, returns array of: status, vmid, pass, ipv4, """ """Create a cluster node, returns array of: status, vmid, pass, ipv4, """
#logger.info('grid> create new cube') logger.info('grid> create new cube')
resp.status, response = selector('vmcreate', req) resp.status, response = selector('vmcreate', req)
req.context['result'] = response req.context['result'] = response
class RemoveResource(object):
@falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid):
""" remove machine completely"""
logger.info('grid> remove ' + str(vmid))
resp.status, response = selector('vmremove', req, vmid)
req.context['result'] = response
class StatusResource(object): class StatusResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" check vm status """ """ check vm status """
#logger.info('grid> status ' + str(vmid)) logger.info('grid> status ' + str(vmid))
resp.status, response = selector('vmstatus', req, vmid) resp.status, response = selector('vmstatus', req, vmid)
req.context['result'] = response req.context['result'] = response
class DeleteResource(object):
@falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid):
""" delete machine completely"""
#logger.info('grid> delete ' + str(vmid))
resp.status, response = selector('vmdelete', req, vmid)
req.context['result'] = response
class SuspendResource(object): class SuspendResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
@ -180,7 +180,7 @@ class StartResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Start the instance """ """ Start the instance """
#logger.info('grid> start ' + str(vmid)) logger.info('grid> start ' + str(vmid))
resp.status, response = selector('vmstart', req, vmid) resp.status, response = selector('vmstart', req, vmid)
req.context['result'] = response req.context['result'] = response
@ -188,7 +188,7 @@ class ShutdownResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" ACPI Shutdown the instance """ """ ACPI Shutdown the instance """
#logger.info('grid> shutdown ' + str(vmid)) logger.info('grid> shutdown ' + str(vmid))
resp.status, response = selector('vmshutdown', req, vmid) resp.status, response = selector('vmshutdown', req, vmid)
req.context['result'] = response req.context['result'] = response
@ -196,7 +196,7 @@ class StopResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Stop the instance """ """ Stop the instance """
#logger.info('grid> stop ' + str(vmid)) logger.info('grid> stop ' + str(vmid))
resp.status, response = selector('vmstop', req, vmid) resp.status, response = selector('vmstop', req, vmid)
req.context['result'] = response req.context['result'] = response
@ -212,7 +212,7 @@ class VNCResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Create a VNC link to the instance """ """ Create a VNC link to the instance """
#logger.info('grid> vnc ' + str(vmid)) logger.info('grid> vnc ' + str(vmid))
resp.status, response = selector('vmvnc', req, vmid) resp.status, response = selector('vmvnc', req, vmid)
req.context['result'] = response req.context['result'] = response
@ -228,12 +228,12 @@ wsgi_app = api = application = falcon.API(middleware=[
res_create = CreateResource() res_create = CreateResource()
api.add_route('/vmcreate', res_create) api.add_route('/vmcreate', res_create)
res_remove = RemoveResource()
api.add_route('/vmremove/{vmid}', res_remove)
res_status = StatusResource() res_status = StatusResource()
api.add_route('/vmstatus/{vmid}', res_status) api.add_route('/vmstatus/{vmid}', res_status)
res_delete = DeleteResource()
api.add_route('/vmdelete/{vmid}', res_delete)
res_suspend = SuspendResource() res_suspend = SuspendResource()
api.add_route('/vmsuspend/{vmid}', res_suspend) api.add_route('/vmsuspend/{vmid}', res_suspend)