From 45dccc81195723945a68fa030b6a081762522641 Mon Sep 17 00:00:00 2001 From: deflax Date: Thu, 21 Sep 2017 03:07:56 +0300 Subject: [PATCH] vmdelete --- grid.py | 6 ++++++ plugin.py | 24 +++++++++++++++++++----- proxmaster.py | 40 ++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/grid.py b/grid.py index 911eb94..04dc265 100644 --- a/grid.py +++ b/grid.py @@ -55,6 +55,12 @@ def writedb(src_data): pass return None +def deletedb(cubeid): + """ remove metadata file """ + dbfile = 'db/vm.{}.json'.format(cubeid) + os.remove(dbfile) + return None + def query_happiness(region_id): """ analyzes grid data for the reuqested region and returns proposed slave_id, diff --git a/plugin.py b/plugin.py index 0c6dace..6892609 100644 --- a/plugin.py +++ b/plugin.py @@ -1,7 +1,7 @@ #. -*- coding: utf-8 - # required proxmox permissions: PVEAdmin # -# afx 2015-2016 +# afx 2015-2017 # site from proxmoxer import ProxmoxAPI @@ -64,9 +64,8 @@ def vmcreate(req): except: vm_pass = 'datapoint' #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' - #vm_id = str(grid.generate_vmid()) #TODO: this should be between 100 and 65000 vm_id = random.randint(1000, 9999) cubeid = int(time.time() * 10000 * 10000) deploy = { 'cube': int(cubeid), @@ -122,13 +121,24 @@ def vmcreate(req): print(str(create_result)) #start the machihe - #time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run - #vmstart(cubeid) + time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run response = { 'status': 'CREATE', 'cube': cubeid, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name } grid.writedb(deploy) 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): """ returns the status of the machine """ @@ -153,6 +163,7 @@ def vmstart(cubeid): result = proxobject.nodes(slave_name).qemu(vm_id).status.start.post() if vm_type == 'lxc': result = proxobject.nodes(slave_name).lxc(vm_id).status.start.post() + #TODO: SET START AT BOOT FLAG response = { 'status':'START' } return response @@ -168,6 +179,7 @@ def vmshutdown(cubeid): result = proxobject.nodes(slave_name).qemu(vm_id).status.shutdown.post() if vm_type == 'lxc': 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)) response = { 'status':'SHUTDOWN', 'vmid':vm_id } return response @@ -184,6 +196,7 @@ def vmstop(cubeid): result = proxobject.nodes(slave_name).qemu(vm_id).status.stop.post() if vm_type == 'lxc': 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)) response = { 'status':'STOP', 'vmid':vm_id } 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=" 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)) #response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] } response = { 'status':'VNC', 'url':vnc_url } diff --git a/proxmaster.py b/proxmaster.py index 33ab077..ae4641e 100644 --- a/proxmaster.py +++ b/proxmaster.py @@ -38,12 +38,12 @@ def selector(fn, req, vmid=0): if fn == 'vmcreate': body = plugin.vmcreate(json) + elif fn == 'vmremove': + body = plugin.vmremove(vmid) + elif fn == 'vmstatus': body = plugin.vmstatus(vmid) - elif fn == 'vmdelete': - body = plugin.vmdelete(vmid) - elif fn == 'vmsuspend': body = plugin.vmsuspend(vmid) @@ -140,26 +140,26 @@ class CreateResource(object): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp): """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) 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): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): """ check vm status """ - #logger.info('grid> status ' + str(vmid)) + logger.info('grid> status ' + str(vmid)) resp.status, response = selector('vmstatus', req, vmid) 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): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): @@ -180,7 +180,7 @@ class StartResource(object): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): """ Start the instance """ - #logger.info('grid> start ' + str(vmid)) + logger.info('grid> start ' + str(vmid)) resp.status, response = selector('vmstart', req, vmid) req.context['result'] = response @@ -188,7 +188,7 @@ class ShutdownResource(object): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): """ ACPI Shutdown the instance """ - #logger.info('grid> shutdown ' + str(vmid)) + logger.info('grid> shutdown ' + str(vmid)) resp.status, response = selector('vmshutdown', req, vmid) req.context['result'] = response @@ -196,7 +196,7 @@ class StopResource(object): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): """ Stop the instance """ - #logger.info('grid> stop ' + str(vmid)) + logger.info('grid> stop ' + str(vmid)) resp.status, response = selector('vmstop', req, vmid) req.context['result'] = response @@ -212,7 +212,7 @@ class VNCResource(object): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): """ 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) req.context['result'] = response @@ -228,12 +228,12 @@ wsgi_app = api = application = falcon.API(middleware=[ res_create = CreateResource() api.add_route('/vmcreate', res_create) +res_remove = RemoveResource() +api.add_route('/vmremove/{vmid}', res_remove) + res_status = StatusResource() api.add_route('/vmstatus/{vmid}', res_status) -res_delete = DeleteResource() -api.add_route('/vmdelete/{vmid}', res_delete) - res_suspend = SuspendResource() api.add_route('/vmsuspend/{vmid}', res_suspend)