class VMware(object): """ Represents the hypervisor client for VMware """ def __init__(self, ip, username, password): """ Initializes the object with credentials and connection information """ self.sdk = Sdk(ip, username, password) self.state_mapping = { 'poweredOn': 'RUNNING', 'poweredOff': 'HALTED', 'suspended': 'PAUSED' } def get_state(self, vmid): """ Get the current power state of a virtual machine @param vmid: hypervisor id of the virtual machine """ return self.state_mapping[self.sdk.get_power_state(vmid)] def create_vm_from_template(self, name, source_vm, disks, ip, mountpoint, wait=True): """ Create a new vmachine from an existing template """ task = self.sdk.create_vm_from_template(name, source_vm, disks, ip, mountpoint, wait) if wait is True: if self.sdk.validate_result(task): task_info = self.sdk.get_task_info(task) return task_info.info.result.value return None def clone_vm(self, vmid, name, disks, wait=False): """ Clone a vmachine @param vmid: hypervisor id of the virtual machine @param name: name of the virtual machine @param disks: list of disk information @param wait: wait for action to complete """ task = self.sdk.clone_vm(vmid, name, disks, wait) if wait is True: if self.sdk.validate_result(task): task_info = self.sdk.get_task_info(task) return task_info.info.result.value return None def delete_vm(self, vmid, storagedriver_mountpoint, storagedriver_storage_ip, devicename, disks_info=None, wait=False): """ Remove the vmachine from the hypervisor @param vmid: hypervisor id of the virtual machine @param wait: wait for action to complete """ if disks_info is None: disks_info = [] _ = disks_info self.sdk.delete_vm(vmid, storagedriver_mountpoint, storagedriver_storage_ip, devicename, wait) def get_vm_object(self, vmid): """ Gets the VMware virtual machine object from VMware by its identifier """ return self.sdk.get_vm(vmid) def get_vm_agnostic_object(self, vmid): """ Gets the VMware virtual machine object from VMware by its identifier """ return self.sdk.make_agnostic_config(self.sdk.get_vm(vmid)) def get_vm_object_by_devicename(self, devicename, ip, mountpoint): """ Gets the VMware virtual machine object from VMware by devicename and datastore identifiers """ return self.sdk.make_agnostic_config( self.sdk.get_nfs_datastore_object(ip, mountpoint, devicename)[0]) def get_vms_by_nfs_mountinfo(self, ip, mountpoint): """ Gets a list of agnostic vm objects for a given ip and mountpoint """ for vm in self.sdk.get_vms(ip, mountpoint): yield self.sdk.make_agnostic_config(vm) def is_datastore_available(self, ip, mountpoint): """ @param ip : hypervisor ip to query for datastore presence @param mountpoint: nfs mountpoint on hypervisor @rtype: boolean @return: True | False """ return self.sdk.is_datastore_available(ip, mountpoint) def set_as_template(self, vmid, disks, wait=False): """ Configure a vm as template This lets the machine exist on the hypervisor but configures all disks as "Independent Non-persistent" @param vmid: hypervisor id of the virtual machine """ return self.sdk.set_disk_mode(vmid, disks, 'independent_nonpersistent', wait) def mount_nfs_datastore(self, name, remote_host, remote_path): """ Mounts a given NFS export as a datastore """ return self.sdk.mount_nfs_datastore(name, remote_host, remote_path) def test_connection(self): """ Checks whether this node is a vCenter """ return self.sdk.test_connection() def clean_backing_disk_filename(self, path): """ Cleans a backing disk filename to the corresponding disk filename """ _ = self return path.replace('-flat.vmdk', '.vmdk').strip('/') def get_backing_disk_path(self, machinename, devicename): """ Builds the path for the file backing a given device/disk """ _ = self return '/{}/{}-flat.vmdk'.format(machinename.replace(' ', '_'), devicename) def get_disk_path(self, machinename, devicename): """ Builds the path for the file backing a given device/disk """ _ = self return '/{}/{}.vmdk'.format(machinename.replace(' ', '_'), devicename) def clean_vmachine_filename(self, path): """ Cleans a VM filename """ _ = self return path.strip('/') def get_vmachine_path(self, machinename, storagerouter_machineid): """ Builds the path for the file representing a given vmachine """ _ = self, storagerouter_machineid # For compatibility purposes only machinename = machinename.replace(' ', '_') return '/{}/{}.vmx'.format(machinename, machinename) def get_rename_scenario(self, old_name, new_name): """ Gets the rename scenario based on the old and new name """ _ = self if old_name.endswith('.vmx') and new_name.endswith('.vmx'): return 'RENAME' elif old_name.endswith('.vmx~') and new_name.endswith('.vmx'): return 'UPDATE' return 'UNSUPPORTED' def should_process(self, devicename, machine_ids=None): """ Checks whether a given device should be processed """ _ = self, devicename, machine_ids return True def file_exists(self, vpool, devicename): """ Check if devicename exists on the given vpool """ _ = self filename = '/mnt/{0}/{1}'.format(vpool.name, devicename) return os.path.exists(filename) and os.path.isfile(filename)
class VMware(object): """ Represents the hypervisor client for VMware """ def __init__(self, ip, username, password): """ Initializes the object with credentials and connection information """ self.sdk = Sdk(ip, username, password) self.state_mapping = {'poweredOn' : 'RUNNING', 'poweredOff': 'HALTED', 'suspended' : 'PAUSED'} def get_state(self, vmid): """ Get the current power state of a virtual machine @param vmid: hypervisor id of the virtual machine """ return self.state_mapping[self.sdk.get_power_state(vmid)] def create_vm_from_template(self, name, source_vm, disks, ip, mountpoint, wait=True): """ Create a new vmachine from an existing template """ task = self.sdk.create_vm_from_template(name, source_vm, disks, ip, mountpoint, wait) if wait is True: if self.sdk.validate_result(task): task_info = self.sdk.get_task_info(task) return task_info.info.result.value return None def clone_vm(self, vmid, name, disks, wait=False): """ Clone a vmachine @param vmid: hypervisor id of the virtual machine @param name: name of the virtual machine @param disks: list of disk information @param wait: wait for action to complete """ task = self.sdk.clone_vm(vmid, name, disks, wait) if wait is True: if self.sdk.validate_result(task): task_info = self.sdk.get_task_info(task) return task_info.info.result.value return None def delete_vm(self, vmid, storagedriver_mountpoint, storagedriver_storage_ip, devicename, disks_info=None, wait=False): """ Remove the vmachine from the hypervisor @param vmid: hypervisor id of the virtual machine @param wait: wait for action to complete """ if disks_info is None: disks_info = [] _ = disks_info self.sdk.delete_vm(vmid, storagedriver_mountpoint, storagedriver_storage_ip, devicename, wait) def get_vm_object(self, vmid): """ Gets the VMware virtual machine object from VMware by its identifier """ return self.sdk.get_vm(vmid) def get_vm_agnostic_object(self, vmid): """ Gets the VMware virtual machine object from VMware by its identifier """ return self.sdk.make_agnostic_config(self.sdk.get_vm(vmid)) def get_vm_object_by_devicename(self, devicename, ip, mountpoint): """ Gets the VMware virtual machine object from VMware by devicename and datastore identifiers """ return self.sdk.make_agnostic_config(self.sdk.get_nfs_datastore_object(ip, mountpoint, devicename)[0]) def get_vms_by_nfs_mountinfo(self, ip, mountpoint): """ Gets a list of agnostic vm objects for a given ip and mountpoint """ for vm in self.sdk.get_vms(ip, mountpoint): yield self.sdk.make_agnostic_config(vm) def is_datastore_available(self, ip, mountpoint): """ @param ip : hypervisor ip to query for datastore presence @param mountpoint: nfs mountpoint on hypervisor @rtype: boolean @return: True | False """ return self.sdk.is_datastore_available(ip, mountpoint) def set_as_template(self, vmid, disks, wait=False): """ Configure a vm as template This lets the machine exist on the hypervisor but configures all disks as "Independent Non-persistent" @param vmid: hypervisor id of the virtual machine """ return self.sdk.set_disk_mode(vmid, disks, 'independent_nonpersistent', wait) def mount_nfs_datastore(self, name, remote_host, remote_path): """ Mounts a given NFS export as a datastore """ return self.sdk.mount_nfs_datastore(name, remote_host, remote_path) def test_connection(self): """ Checks whether this node is a vCenter """ return self.sdk.test_connection() def clean_backing_disk_filename(self, path): """ Cleans a backing disk filename to the corresponding disk filename """ _ = self return path.replace('-flat.vmdk', '.vmdk').strip('/') def get_backing_disk_path(self, machinename, devicename): """ Builds the path for the file backing a given device/disk """ _ = self if machinename is None: return '/{0}-flat.vmdk'.format(devicename) return '/{0}/{1}-flat.vmdk'.format(machinename.replace(' ', '_'), devicename) def get_disk_path(self, machinename, devicename): """ Builds the path for the file backing a given device/disk """ _ = self if machinename is None: return '/{0}.vmdk'.format(devicename) return '/{0}/{1}.vmdk'.format(machinename.replace(' ', '_'), devicename) def clean_vmachine_filename(self, path): """ Cleans a VM filename """ _ = self return path.strip('/') def get_vmachine_path(self, machinename, storagerouter_machineid): """ Builds the path for the file representing a given vmachine """ _ = self, storagerouter_machineid # For compatibility purposes only machinename = machinename.replace(' ', '_') return '/{0}/{1}.vmx'.format(machinename, machinename) def get_rename_scenario(self, old_name, new_name): """ Gets the rename scenario based on the old and new name """ _ = self if old_name.endswith('.vmx') and new_name.endswith('.vmx'): return 'RENAME' elif old_name.endswith('.vmx~') and new_name.endswith('.vmx'): return 'UPDATE' return 'UNSUPPORTED' def should_process(self, devicename, machine_ids=None): """ Checks whether a given device should be processed """ _ = self, devicename, machine_ids return True def file_exists(self, storagedriver, devicename): """ Check if devicename exists on the given vpool """ return self.sdk.file_exists(storagedriver.storage_ip, storagedriver.mountpoint, self.clean_vmachine_filename(devicename))
class VCenter(object): """ Represents the management center for vcenter server """ def __init__(self, ip, username, password): """ Initializes the object with credentials and connection information """ self.sdk = Sdk(ip, username, password) self.STATE_MAPPING = {'poweredOn': 'RUNNING', 'poweredOff': 'HALTED', 'suspended': 'PAUSED'} def get_host_status_by_ip(self, host_ip): """ Return host status from vCenter Server Must be connected to vCenter """ return self.STATE_MAPPING.get(self.sdk.get_host_status_by_ip(host_ip), 'UNKNOWN') def get_host_status_by_pk(self, pk): """ Return host status from vCenter Server Must be connected to vCenter """ return self.STATE_MAPPING.get(self.sdk.get_host_status_by_pk(pk), 'UNKNOWN') def get_host_primary_key(self, host_ip): """ Return host status from vCenter Server Must be connected to vCenter """ return self.sdk.get_host_primary_key(host_ip) def test_connection(self): """ Checks whether this node is a vCenter Should always be True (depends on ip) Test connection returns True even if connecting to an Esx host directly, extra check to make sure sdk points to vCenter """ self.sdk.test_connection() return self.sdk.is_vcenter def get_hosts(self): """ Gets a list of all hosts/hypervisors """ return self.sdk.get_hosts() def configure_vpool_for_host(self, vpool_guid, ip): pass def unconfigure_vpool_for_host(self, vpool_guid, remove_volume_type, ip): pass def get_guests(self): """ Gets a list of all guests Return: dict {hypervisor_hostname: [{id: vm_id, name: vm_name}... ] ...} """ return self.sdk.get_all_vms() def get_guest_by_guid(self, guid): """ Return guest info by guid :param guid: UUID Return: dict {attr: value} """ vm = self.sdk.get_vm(guid) return {'id': vm.obj_identifier.value, 'name': vm.name} def get_vdisk_model_by_devicepath(self, devicepath): """ Return vdisk model info (name) :param devicepath: full device path :return: dict """ vds = self.sdk.get_all_vdisks() for vd in vds: if vd['filename'] == devicepath: return vd def get_vdisk_device_info(self, volumeid): """ This method does not make sense for vCenter as you cannot retrieve a Virtual Disk by uuid """ raise NotImplementedError('Method <get_vdisk_device_info> not implemented for vCenter Management Center') def get_vmachine_device_info(self, instanceid): """ Return device info """ return self.sdk.get_vm_device_info(instanceid) def get_vm_agnostic_object(self, devicename, ip, mountpoint): """ devicename: clHp75aS65QhsAHy/instance-00000001.xml ip: 127.0.0.1 mountpoint: /mnt/saio Return vm agnostic object {'backing': {'datastore': '/mnt/saio', 'filename': 'clHp75aS65QhsAHy/instance-00000001.xml'}, 'datastores': {'/mnt/saio': '127.0.0.1:/mnt/saio'}, 'disks': [{'backingfilename': 'volume1.raw', 'datastore': '/mnt/saio', 'filename': 'volume1.raw', 'name': 'volume1', 'order': 0}], 'id': '4a607820-202c-496b-b942-591a9a67fe0f', 'name': 'instance1'} """ for host_id in self.sdk.get_hosts(): try: vm_object, _ = self.sdk.get_nfs_datastore_object(ip, mountpoint, devicename, host=host_id) if vm_object is not None: return self.sdk.make_agnostic_config(vm_object, host=host_id) except RuntimeError: pass return None def is_host_configured_for_vpool(self, vpool_guid, ip): _ = self _ = ip _ = vpool_guid return False def is_host_configured(self, ip): _ = self _ = ip return False def configure_host(self, ip): pass def unconfigure_host(self, ip): pass
class VCenter(object): """ Represents the management center for vcenter server """ def __init__(self, ip, username, password): """ Initializes the object with credentials and connection information """ self.sdk = Sdk(ip, username, password) self.STATE_MAPPING = { 'poweredOn': 'RUNNING', 'poweredOff': 'HALTED', 'suspended': 'PAUSED' } def get_host_status_by_ip(self, host_ip): """ Return host status from vCenter Server Must be connected to vCenter :param host_ip: IP of host """ return self.STATE_MAPPING.get(self.sdk.get_host_status_by_ip(host_ip), 'UNKNOWN') def get_host_status_by_pk(self, pk): """ Return host status from vCenter Server Must be connected to vCenter :param pk: Primary key of host """ return self.STATE_MAPPING.get(self.sdk.get_host_status_by_pk(pk), 'UNKNOWN') def get_host_primary_key(self, host_ip): """ Return host status from vCenter Server Must be connected to vCenter :param host_ip: IP of host """ return self.sdk.get_host_primary_key(host_ip) def test_connection(self): """ Checks whether this node is a vCenter Should always be True (depends on ip) Test connection returns True even if connecting to an Esx host directly, extra check to make sure sdk points to vCenter """ self.sdk.test_connection() return self.sdk.is_vcenter def get_hosts(self): """ Gets a list of all hosts/hypervisors """ return self.sdk.get_hosts() def configure_vpool_for_host(self, vpool_guid, ip): """ Make necessary configurations on host :param vpool_guid: Guid of the vPool to configure :param ip: IP of the host :return: None """ _ = self _ = vpool_guid _ = ip pass def unconfigure_vpool_for_host(self, vpool_guid, remove_volume_type, ip): """ Unconfigure the host for the vPool :param vpool_guid: Guid of the vPool to unconfigure :param remove_volume_type: Remove volume type :param ip: IP of the host :return: None """ _ = self _ = vpool_guid _ = remove_volume_type _ = ip pass def get_guests(self): """ Gets a list of all guests Return: dict {hypervisor_hostname: [{id: vm_id, name: vm_name}... ] ...} """ return self.sdk.get_all_vms() def get_guest_by_guid(self, guid): """ Return guest info by guid :param guid: UUID Return: dict {attr: value} """ vm = self.sdk.get_vm(guid) return {'id': vm.obj_identifier.value, 'name': vm.name} def get_vdisk_model_by_devicepath(self, devicepath): """ Return vdisk model info (name) :param devicepath: full device path :return: dict """ vds = self.sdk.get_all_vdisks() for vd in vds: if vd['filename'] == devicepath: return vd def get_vdisk_device_info(self, volumeid): """ This method does not make sense for vCenter as you cannot retrieve a Virtual Disk by uuid :param volumeid: ID of the volume """ raise NotImplementedError( 'Method <get_vdisk_device_info> not implemented for vCenter Management Center' ) def get_vmachine_device_info(self, instanceid): """ Return device info :param instanceid: ID of the virtual machine instance """ return self.sdk.get_vm_device_info(instanceid) def get_vm_agnostic_object(self, devicename, ip, mountpoint): """ :param devicename: clHp75aS65QhsAHy/instance-00000001.xml :param ip: 127.0.0.1 :param mountpoint: /mnt/saio Return vm agnostic object {'backing': {'datastore': '/mnt/saio', 'filename': 'clHp75aS65QhsAHy/instance-00000001.xml'}, 'datastores': {'/mnt/saio': '127.0.0.1:/mnt/saio'}, 'disks': [{'backingfilename': 'volume1.raw', 'datastore': '/mnt/saio', 'filename': 'volume1.raw', 'name': 'volume1', 'order': 0}], 'id': '4a607820-202c-496b-b942-591a9a67fe0f', 'name': 'instance1'} """ for host_id in self.sdk.get_hosts(): try: vm_object, _ = self.sdk.get_nfs_datastore_object(ip, mountpoint, devicename, host=host_id) if vm_object is not None: return self.sdk.make_agnostic_config(vm_object, host=host_id) except RuntimeError: pass return None def is_host_configured_for_vpool(self, vpool_guid, ip): """ Verify whether the host is configured for this vPool :param vpool_guid: Guid of the vPool :param ip: IP of the host :return: True """ _ = self _ = ip _ = vpool_guid return True def is_host_configured(self, ip): """ Verify whether the host is configured :param ip: IP of the host :return: True """ _ = self _ = ip return True def configure_host(self, ip): """ Configure the host :param ip: IP of the host :return: None """ _ = self _ = ip pass def unconfigure_host(self, ip): """ Unconfigure the host :param ip: IP of the host :return: None """ _ = self _ = ip pass
class VCenter(object): """ Represents the management center for vcenter server """ def __init__(self, ip, username, password): """ Initializes the object with credentials and connection information """ self.sdk = Sdk(ip, username, password) self.STATE_MAPPING = { 'poweredOn': 'RUNNING', 'poweredOff': 'HALTED', 'suspended': 'PAUSED' } def get_host_status_by_ip(self, host_ip): """ Return host status from vCenter Server Must be connected to vCenter """ return self.STATE_MAPPING.get(self.sdk.get_host_status_by_ip(host_ip), 'UNKNOWN') def get_host_status_by_pk(self, pk): """ Return host status from vCenter Server Must be connected to vCenter """ return self.STATE_MAPPING.get(self.sdk.get_host_status_by_pk(pk), 'UNKNOWN') def get_host_primary_key(self, host_ip): """ Return host status from vCenter Server Must be connected to vCenter """ return self.sdk.get_host_primary_key(host_ip) def test_connection(self): """ Checks whether this node is a vCenter Should always be True (depends on ip) Test connection returns True even if connecting to an Esx host directly, extra check to make sure sdk points to vCenter """ self.sdk.test_connection() return self.sdk._is_vcenter def get_hosts(self): """ Gets a list of all hosts/hypervisors """ return self.sdk.get_hosts() def get_metadata(self, metadata, parameters): """ Get specific config values: - integratemgmt: True/False """ return {} def set_metadata(self, metadata): """ Update local metadata """ self.metadata = metadata def configure_vpool(self, vpool_name, mountpoint): pass def unconfigure_vpool(self, vpool_name, mountpoint, remove_volume_type): pass def get_guests(self): """ Gets a list of all guests Return: dict {hypervisor_hostname: [{id: vm_id, name: vm_name}... ] ...} """ return self.sdk.get_all_vms() def get_guest_by_guid(self, guid): """ Return guest info by guid :param guid: UUID Return: dict {attr: value} """ vm = self.sdk.get_vm(guid) return {'id': vm.obj_identifier.value, 'name': vm.name} def get_vdisk_model_by_devicepath(self, devicepath): """ Return vdisk model info (name) :param devicepath: full device path :return: dict """ vds = self.sdk.get_all_vdisks() for vd in vds: if vd['filename'] == devicepath: return vd def get_vdisk_device_info(self, volumeid): """ This method does not make sense for vCenter as you cannot retrieve a Virtual Disk by uuid """ raise NotImplementedError( 'Method <get_vdisk_device_info> not implemented for vCenter ManagementCenter' ) def get_vmachine_device_info(self, instanceid): """ Return device info """ return self.sdk.get_vm_device_info(instanceid) def get_vm_agnostic_object(self, devicename, ip, mountpoint): """ devicename: clHp75aS65QhsAHy/instance-00000001.xml ip: 127.0.0.1 mountpoint: /mnt/saio Return vm agnostic object {'backing': {'datastore': '/mnt/saio', 'filename': 'clHp75aS65QhsAHy/instance-00000001.xml'}, 'datastores': {'/mnt/saio': '127.0.0.1:/mnt/saio'}, 'disks': [{'backingfilename': 'volume1.raw', 'datastore': '/mnt/saio', 'filename': 'volume1.raw', 'name': 'volume1', 'order': 0}], 'id': '4a607820-202c-496b-b942-591a9a67fe0f', 'name': 'instance1'} """ return self.sdk.make_agnostic_config( self.sdk.get_nfs_datastore_object(ip, mountpoint, devicename)[0])