From 8f263cd076662b12303f0a8d3d194f400165ba3e Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Fri, 7 Feb 2014 12:23:27 +0530 Subject: [PATCH] undeploy_lab use case and minor fixes --- Controller.py | 8 +++++++- State.py | 3 ++- VMPool.py | 20 +++++++++++++++++++- VMPoolManager.py | 1 + 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Controller.py b/Controller.py index 68e5857..b259fe9 100644 --- a/Controller.py +++ b/Controller.py @@ -6,6 +6,7 @@ #from time import strftime from datetime import datetime +import time import LabManager import VMPoolManager @@ -22,7 +23,7 @@ def test_lab(self, lab_id, lab_src_url, revision_tag=None): % (lab_id, lab_src_url)) try: lab_spec = LabManager.get_lab_reqs(lab_id, lab_src_url, revision_tag) - lab_spec['lab_id'] = lab_id + lab_spec['lab']['description']['id'] = lab_spec['lab_id'] = lab_id lab_spec['lab_src_url'] = lab_src_url lab_spec['revision_tag'] = revision_tag lab_spec['lab']['runtime_requirements']['hosting'] = 'dedicated' @@ -52,6 +53,7 @@ def update_state(self, state): state['lab_history']['released_by'] = 'dummy' #state['lab_history']['released_on'] = strftime("%Y-%m-%d %H:%M:%S") state['lab_history']['released_on'] = datetime.utcnow() + self.system.state.append(state) def undeploy_lab(self, lab_id): Logging.LOGGER.debug("Controller.undeploy_lab for lab_id %s" % lab_id) @@ -64,3 +66,7 @@ def undeploy_lab(self, lab_id): c = Controller() #print c.test_lab("ovpl01", "https://github.com/nrchandan/vlab-computer-programming") print c.test_lab("ovpl01", "https://github.com/avinassh/cse09") + #print c.test_lab("ovpl01", "https://github.com/avinassh/cse09") + #print c.test_lab("cse30", "https://github.com/avinassh/cse09") + #print c.undeploy_lab("ovpl01") + #print c.undeploy_lab("cse30") diff --git a/State.py b/State.py index 03cce87..cae83c3 100644 --- a/State.py +++ b/State.py @@ -18,4 +18,5 @@ def save(self): """Writes the current state to mongodb(disk)""" if "ovpl" in self.db.collection_names(): self.db.ovpl.rename("ovpl-last", dropTarget=True) - self.db.ovpl.insert(self.state) \ No newline at end of file + if bool(self.state): + self.db.ovpl.insert(self.state) \ No newline at end of file diff --git a/VMPool.py b/VMPool.py index d53037c..a872bae 100644 --- a/VMPool.py +++ b/VMPool.py @@ -140,9 +140,27 @@ def destroy_vm(self, vm_id): result = requests.post(url=adapter_url, data=payload) Logging.LOGGER.debug("Response text from adapter: " + result.text) if result.status_code == requests.codes.ok and "Success" in result.text: + Logging.LOGGER.debug("VMPool.destroy_vm()") + return True + else: + Logging.LOGGER.error("Error destroying vm: " + result.text) + except Exception, e: + Logging.LOGGER.error("Error communicating with adapter: " + str(e)) + + def save_state(self, lab_id, vm_id): + for r in self.system.state: + if r['lab_spec']['lab_id'] == lab_id and r['vm_info']['vm_id'] == vm_id and r['vmpool_info']['vmpool_id'] == self.vmpool_id: + self.system.state.remove(r) + self.system.save() + break + + def destroy_and_save(self, lab_id, vm_id): + if self.destroy_vm(vm_id): + self.save_state(lab_id, vm_id) def undeploy_lab(self, lab_id): - map(self.destroy_vm, self.dedicated_vms(lab_id)) + Logging.LOGGER.debug("VMPool.undeploy_lab()") + map(lambda vm_id: self.destroy_and_save(lab_id, vm_id), self.dedicated_vms(lab_id)) def dedicated_vms(self, lab_id): this_lab_vms = set([r['vm_info']['vm_id'] for r in self.system.state if r['lab_spec']['lab_id']==lab_id and r['vmpool_info']['vmpool_id']==self.vmpool_id]) diff --git a/VMPoolManager.py b/VMPoolManager.py index 563d761..e30f9fa 100644 --- a/VMPoolManager.py +++ b/VMPoolManager.py @@ -46,6 +46,7 @@ def create_vm(self, lab_spec): return vmpool.create_vm(lab_spec) def undeploy_lab(self, lab_id): + Logging.LOGGER.debug("VMPoolManager.undeploy_lab()") used_pools = self.get_used_pools(lab_id) for pool_id in used_pools: self.VMPools[pool_id].undeploy_lab(lab_id)