Adding threds to libvirt driver

This patch adds threds to poweroff/poweron/reset methods
to increase the velocity of VMs management.

Change-Id: I03c821d44810cfa76468f1a371b8e46f0ba03311
This commit is contained in:
Yaroslav Lobankov 2016-08-29 17:02:14 +03:00
parent e1f57a21ce
commit 186424edfc
3 changed files with 62 additions and 22 deletions

View File

@ -48,6 +48,7 @@ def main():
logging.info('Pick and power off one of cluster nodes')
one = nodes.pick()
one.poweroff()
one.poweron()
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',

View File

@ -21,4 +21,4 @@ class OSFError(OSFException):
class PowerManagmentError(OSFError):
"""Base Error for Power Managment API"""
"""Base Error class for Power Management API"""

View File

@ -12,7 +12,8 @@
# limitations under the License.
import logging
from xml.dom import minidom
import threading
import traceback
import libvirt
@ -20,6 +21,29 @@ from os_failures.api import error
from os_failures.api import power_management
class ThreadsWrapper(object):
def __init__(self, target):
self.target = target
self.threads = []
self.errors = []
def _target(self, **kwargs):
try:
self.target(**kwargs)
except Exception as exc:
logging.error(traceback.format_exc())
self.errors.append(exc)
def start_thread(self, **kwargs):
thread = threading.Thread(target=self._target, kwargs=kwargs)
thread.start()
self.threads.append(thread)
def join_threads(self):
for thread in self.threads:
thread.join()
class LibvirtDriver(power_management.PowerManagement):
def __init__(self, params):
self.connection_uri = params['connection_uri']
@ -37,32 +61,47 @@ class LibvirtDriver(power_management.PowerManagement):
def _find_domain_by_mac_address(self, mac_address):
for domain in self.conn.listAllDomains():
xml = minidom.parseString(domain.XMLDesc())
mac_list = xml.getElementsByTagName('mac')
for mac in mac_list:
if mac_address == mac.getAttribute('address'):
return domain
if mac_address in domain.XMLDesc():
return domain
raise error.PowerManagmentError(
'Node with MAC address %s not found!' % mac_address)
def poweroff(self, mac_addresses_list):
def _poweroff(self, mac_address):
logging.info('Power off domain with MAC address: %s', mac_address)
domain = self._find_domain_by_mac_address(mac_address)
domain.destroy()
logging.info('Domain (%s) was powered off' % mac_address)
def _poweron(self, mac_address):
logging.info('Power on domain with MAC address: %s', mac_address)
domain = self._find_domain_by_mac_address(mac_address)
domain.create()
logging.info('Domain (%s) was powered on' % mac_address)
def _reset(self, mac_address):
logging.info('Reset domain with MAC address: %s', mac_address)
domain = self._find_domain_by_mac_address(mac_address)
domain.reset()
logging.info('Domain (%s) was reset' % mac_address)
@staticmethod
def _run(target, mac_addresses_list):
tw = ThreadsWrapper(target)
for mac_address in mac_addresses_list:
logging.info('Power off domain with MAC address: %s', mac_address)
domain = self._find_domain_by_mac_address(mac_address)
domain.destroy()
logging.info('Domain (%s) was powered off' % mac_address)
tw.start_thread(mac_address=mac_address)
tw.join_threads()
if tw.errors:
raise error.PowerManagmentError(
'There are some errors when working the libvirt driver. '
'Please, check logs for more details.')
def poweroff(self, mac_addresses_list):
self._run(self._poweroff, mac_addresses_list)
def poweron(self, mac_addresses_list):
for mac_address in mac_addresses_list:
logging.info('Power on domain with MAC address: %s', mac_address)
domain = self._find_domain_by_mac_address(mac_address)
domain.create()
logging.info('Domain (%s) was powered on' % mac_address)
self._run(self._poweron, mac_addresses_list)
def reset(self, mac_addresses_list):
for mac_address in mac_addresses_list:
logging.info('Reset domain with MAC address: %s', mac_address)
domain = self._find_domain_by_mac_address(mac_address)
domain.reset()
logging.info('Domain (%s) was reset' % mac_address)
self._run(self._reset, mac_addresses_list)