|
16 | 16 | Hypervisor/ManagementCenter factory module |
17 | 17 | Using the module requires libvirt api to be available on the MACHINE THAT EXECUTES THE CODE |
18 | 18 | """ |
19 | | - |
20 | 19 | from ovs_extensions.generic.filemutex import file_mutex |
| 20 | +from ovs_extensions.generic.toolbox import ExtensionsToolbox |
| 21 | +from ovs.lib.helpers.toolbox import Toolbox |
| 22 | +from ...helpers.ci_constants import CIConstants |
21 | 23 |
|
22 | 24 |
|
23 | | -class HypervisorFactory(object): |
| 25 | +class HypervisorFactory(CIConstants): |
24 | 26 | """ |
25 | 27 | HypervisorFactory class provides functionality to get abstracted hypervisor |
26 | 28 | """ |
27 | | - |
28 | 29 | hypervisors = {} |
29 | 30 |
|
30 | | - @staticmethod |
31 | | - def get(ip, username, password, hvtype): |
| 31 | + @classmethod |
| 32 | + def get(cls, hv_credentials=None): |
32 | 33 | """ |
33 | 34 | Returns the appropriate hypervisor client class for a given PMachine |
| 35 | + :param hv_credentials: object that contains ip, user, password and hypervisor type |
| 36 | + :type hv_credentials: HypervisorCredentials object |
34 | 37 | """ |
35 | | - key = '{0}_{1}'.format(ip, username) |
36 | | - if key not in HypervisorFactory.hypervisors: |
37 | | - mutex = file_mutex('hypervisor_{0}'.format(key)) |
38 | | - try: |
39 | | - mutex.acquire(30) |
40 | | - if key not in HypervisorFactory.hypervisors: |
41 | | - if hvtype == 'VMWARE': |
42 | | - # Not yet tested. Needs to be rewritten |
43 | | - raise NotImplementedError("{0} has not yet been implemented".format(hvtype)) |
44 | | - from .hypervisors.vmware import VMware |
45 | | - hypervisor = VMware(ip, username, password) |
46 | | - elif hvtype == 'KVM': |
47 | | - from .hypervisors.kvm import KVM |
48 | | - hypervisor = KVM(ip, username, password) |
49 | | - else: |
50 | | - raise NotImplementedError('Hypervisor {0} is not yet supported'.format(hvtype)) |
51 | | - HypervisorFactory.hypervisors[key] = hypervisor |
52 | | - finally: |
53 | | - mutex.release() |
54 | | - return HypervisorFactory.hypervisors[key] |
| 38 | + if hv_credentials is None: |
| 39 | + return cls.get(HypervisorCredentials(ip=CIConstants.HYPERVISOR_INFO['ip'], |
| 40 | + user=CIConstants.HYPERVISOR_INFO['user'], |
| 41 | + password=CIConstants.HYPERVISOR_INFO['password'], |
| 42 | + type=CIConstants.HYPERVISOR_INFO['type'])) |
| 43 | + if not isinstance(hv_credentials, HypervisorCredentials): |
| 44 | + raise TypeError('Credentials must be of type HypervisorCredentials') |
| 45 | + return cls.hypervisors.get(hv_credentials, cls._add_hypervisor(hv_credentials)) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def _add_hypervisor(hypervisor_credentials): |
| 49 | + ip = hypervisor_credentials.ip |
| 50 | + username = hypervisor_credentials.user |
| 51 | + password = hypervisor_credentials.password |
| 52 | + hvtype = hypervisor_credentials.type |
| 53 | + mutex = file_mutex('hypervisor_{0}'.format(hash(hypervisor_credentials))) |
| 54 | + try: |
| 55 | + mutex.acquire(30) |
| 56 | + if hypervisor_credentials not in HypervisorFactory.hypervisors: |
| 57 | + if hvtype == 'VMWARE': |
| 58 | + # Not yet tested. Needs to be rewritten |
| 59 | + raise NotImplementedError("{0} has not yet been implemented".format(hvtype)) |
| 60 | + from .hypervisors.vmware import VMware |
| 61 | + hypervisor = VMware(ip, username, password) |
| 62 | + elif hvtype == 'KVM': |
| 63 | + from .hypervisors.kvm import KVM |
| 64 | + hypervisor = KVM(ip, username, password) |
| 65 | + else: |
| 66 | + raise NotImplementedError('Hypervisor {0} is not yet supported'.format(hvtype)) |
| 67 | + HypervisorFactory.hypervisors[hypervisor_credentials] = hypervisor |
| 68 | + return hypervisor |
| 69 | + finally: |
| 70 | + mutex.release() |
| 71 | + |
| 72 | + |
| 73 | +class HypervisorCredentials(object): |
| 74 | + def __init__(self, ip, user, password, type): |
| 75 | + required_params = {'ip': (str, Toolbox.regex_ip), |
| 76 | + 'user': (str, None), |
| 77 | + 'password': (str, None), |
| 78 | + 'type': (str, ['KVM', 'VMWARE'])} |
| 79 | + ExtensionsToolbox.verify_required_params(required_params, {'ip': ip, |
| 80 | + 'user': user, |
| 81 | + 'password': password, |
| 82 | + 'type': type}) |
| 83 | + self.ip = ip |
| 84 | + self.user = user |
| 85 | + self.password = password |
| 86 | + self.type = type |
| 87 | + |
| 88 | + def __str__(self): |
| 89 | + return 'hypervisor at ip {0} of type {1}'.format(self.ip, self.type) |
0 commit comments