Skip to content
Snippets Groups Projects
Commit bebb9c9a authored by Benziane Chakib's avatar Benziane Chakib
Browse files

Added suspend and resume method for the node handler

parent 856c8ef2
No related branches found
No related tags found
No related merge requests found
......@@ -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]
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment