diff --git a/ccnode/ccnodehandlers.py b/ccnode/ccnodehandlers.py index 6acdd24a0f4020ffd87f9fbcdee61faa71738437..b9318a711518c7178e24b9a71853794a4891b31f 100644 --- a/ccnode/ccnodehandlers.py +++ b/ccnode/ccnodehandlers.py @@ -176,8 +176,8 @@ class NodeHandler(RpcHandler): self.hv_handle.stop_vm(vm, stop_mode) except VMError as err: logging.warning('Error while stopping %s: %s' % (vm, err)) - continue - logging.info('stop_vm: vm %s stopping' % vm) + else: + logging.info('stop_vm: vm %s stopping' % vm) @pure def start_vm(self, vm_names=None): @@ -189,15 +189,55 @@ class NodeHandler(RpcHandler): :type vm_names: :class:`list` of strings ''' if vm_names is None: - vm_names = [vm.get_name() for vm in self.hv_handle._vm_list] + vm_names = gen_vm_names(self.hv_handle) logging.debug('start_vm: starting vms %s' % vm_names) for vm in vm_names: try: self.hv_handle.start_vm(vm) except VMError as err: logging.warning('Error while starting %s: %s' % (vm ,err)) - continue - logging.info('start_vm: vm %s starting' % vm) + else: + logging.info('start_vm: vm %s starting' % vm) + + @pure + def suspend_vm(self, vm_names=None): + ''' + Suspends the specifed list of vms + If vm_names is None all vms in hypervisor will be suspended + + :param vm_names: the list of vms to suspend + :type vm_names: :class:`list` of strings + ''' + if vm_names is None: + vm_names = gen_vm_names(self.hv_handle) + logging.debug('suspend_vm: suspending vms %s' % vm_names) + for vm in vm_names: + try: + self.hv_handle.suspend_vm(vm) + except VMError as err: + logging.info('Error while suspending %s: %s' % (vm, err)) + else: + logging.info('suspend_vm: vm %s suspended' % vm) + + @pure + def resume_vm(self, vm_names=None): + ''' + Resumes the specified list of vms + If vm_names is None all vms in hypervisor will be resumed + + :param vm_names: the list of vms to resume + :type vm_names: :class:`str` + ''' + if vm_names is None: + vm_names = gen_vm_names(self.hv_handle) + logging.debug('resume_vm: resuming vms %s' % vm_names) + for vm in vm_names: + try: + self.hv_handle.resume_vm(vm) + except VMError as err: + logging.info('Error while resuming %s: %s' % (vm, err)) + else: + logging.info('resume_vm: vm %s resumed' % vm) @pure def execute_command(self, command): @@ -210,3 +250,12 @@ class NodeHandler(RpcHandler): ''' result = self.hv_handle.local_excecute(command) return result + + +### Helper Functions + +def gen_vm_names(hypervisor): + ''' + generates a list of vm names defined in hypervisor + ''' + return [vm.get_name() for vm in hypervisor._vm_list] diff --git a/ccnode/kvm.py b/ccnode/kvm.py index 49d495b194437689b258188552c4aa12d4de02d8..b90d7fcc79e71443573f39c1221f1b415ba389a3 100644 --- a/ccnode/kvm.py +++ b/ccnode/kvm.py @@ -56,14 +56,40 @@ class KvmHypervisor(LibvirtHypervisor): ''' Poweroff the specifed vm with the specified options - :param name: the name of the vm: + :param name: the name of the vm :type name: :class:`str` ''' for vm in self._vm_list: if vm.get_name() == name: vm.shutdown() if stop_mode else vm.force_poweroff() return - raise VMError('Virtual Machine %s not found: '% name) + raise VMError('Virtual Machine %s not found: ' % name) + + def suspend_vm(self, name): + ''' + Suspends the specifed vm + + :param name: the name of the vm + :type name: :class:`str` + ''' + for vm in self._vm_list: + if vm.get_name() == name: + vm.suspend() + return + raise VMError('Virtual machine %s not found: ' % name) + + def resume_vm(self, name): + ''' + Resumes the specifed vm + + :param name: the name of the vm + :type name: :class:`str` + ''' + for vm in self._vm_list: + if vm.get_name() == name: + vm.resume() + return + raise VMError('Virtual machine %s not found: ' % name) def local_excecute(self, command):