Exemple #1
0
    def add_vif(self, network_identifier=None, **kwargs):
        """Add a virtual network interface to local VM.
        If mac_address is specified, it will be assigned to the interface.
        network_identifier must be specified.
        """
        #NOTE: User need to specify network name in UI form of publish services
        #TODO: Ensure the new VIF becomes eth2 inside VM otherwise need to
        #      find a way to discover device number
        mac_address = None
        for key in kwargs:
            if key == 'mac_address':
                mac_address = kwargs[key]
        if not network_identifier:
            exc_msg = exception.exc_strs.NETWORK_MISSING_PARAMETER % \
                                                    'network_identifier'
            raise exception.HAPIFailure(
                exception.exc_codes.NETWORK_MISSING_PARAMETER, exc_msg)

        client_factory = self.session._get_vim().client.factory
        nic_spec = util.create_vif_spec(client_factory, network_identifier,
                                        mac_address)
        device_config_spec = [nic_spec]
        vm_config_spec = client_factory.create('ns0:VirtualMachineConfigSpec')
        vm_config_spec.deviceChange = device_config_spec
        reconfig_task = self.session._call_method(self.session._get_vim(),
                                                  "ReconfigVM_Task",
                                                  self.vm,
                                                  spec=vm_config_spec)
        util.wait_for_task(self.session, reconfig_task)
Exemple #2
0
    def add_vif(self, network_identifier=None, **kwargs):
        """Add a virtual network interface to local VM.

        device must be specified
        mac_address can be omitted"""
        bridge = network_identifier
        network_by_bridge = self.get_network_by_component(bridge)
        mac_address = device = ''
        for key in kwargs:
            if key == 'mac_address':
                mac_address = kwargs[key]
            elif key == 'device':
                device = kwargs[key]
        if device == '':
            exc_msg = exception.exc_strs.NETWORK_MISSING_PARAMETER % 'device'
            raise \
        exception.HAPIFailure(exception.exc_codes.NETWORK_MISSING_PARAMETER,
                              exc_msg)
        vif_ref = self.session.xenapi.VIF.create({
            'VM': self.vm,
            'device': device,
            'network': network_by_bridge,
            'MAC': mac_address,
            'MTU': "1500",
            'other_config': {},
            'qos_algorithm_type': "",
            'qos_algorithm_params': {}
        })
        self.session.xenapi.VIF.plug(vif_ref)
        return self.session.xenapi.VIF.get_uuid(vif_ref)
Exemple #3
0
 def create_virtualdisk(self,
                        size=1024,
                        name_label='OS_VPX_virtualdisk',
                        key=None):
     try:
         # add a default key to other-config
         other_config = {'os-vpx-extra': 'true'}
         if key:
             other_config[key] = 'true'
         vdi_ref = self.session.xenapi.VDI.create({
             'name_label': name_label,
             'name_description': '',
             'SR': self.container,
             'virtual_size': str(size),
             'type': 'User',
             'sharable': False,
             'read_only': False,
             'xenstore_data': {},
             'other_config': other_config,
             'sm_config': {},
             'tags': []
         })
         return self.session.xenapi.VDI.get_uuid(vdi_ref)
     except XenAPI.Failure, inner_exception:
         raise exception.HAPIFailure(
             exception.exc_codes.STORAGE_DISKCREATE_ERROR,
             exception.exc_strs.STORAGE_DISKCREATE_ERROR, inner_exception)
Exemple #4
0
 def createSession(cls, session_type=None):
     cls.hv_session = config_util.load_session(session_type)
     if cls.hv_session is None:
         raise exception.HAPIFailure(exception.exc_codes.\
                                     UNCLASSIFIED_SESSION_FAILURE,
                                     exception.exc_strs.\
                                     UNCLASSIFIED_SESSION_FAILURE)
     return cls.hv_session
Exemple #5
0
    def create_virtualdisk(self,
                           size=1024,
                           name_label='OS_VPX_virtualdisk',
                           key=None):
        """Create virtual disk of specified size with name as
        '[datastore1] name_label.__key__.vmdk'.
        TODO: We might want to create sub folders with name 'key' to
        maintain name_label and key separate as entities."""
        if not key:
            key = "geppetto-openstack"
        #Retrieve all virtual disks matching the specified key
        vmdk_list = self.find_virtualdisk(key)

        new_seqnumber = util.get_max_seqnumber(vmdk_list, name_label, key)
        new_diskname = name_label + '__' + str(new_seqnumber + 1) \
                                  + '__' + key + '__.vmdk'
        size_kb = int(size) / 1024
        vmdk_create_spec = util.get_vmdk_create_spec(
            self.session._get_vim().client.factory, size_kb, self.adapter_type)

        datastore_name = self.container.name
        datacenter_obj = util.get_datacenter(self.session)
        virtual_disk_path = util.format_virtual_disk_path(
            datastore_name, new_diskname)
        try:
            create_task = self.session._call_method(
                self.session._get_vim(),
                "CreateVirtualDisk_Task",
                self.session.service_content.virtualDiskManager,
                name=virtual_disk_path,
                datacenter=datacenter_obj.mor,
                spec=vmdk_create_spec)
            #Wait for the asynchronous task to complete
            is_created = util.wait_for_task(self.session, create_task)
            if is_created:
                return virtual_disk_path
            else:
                raise exception.HAPIFailure(
                    exception.exc_codes.STORAGE_DISKCREATE_ERROR,
                    exception.exc_strs.STORAGE_DISKCREATE_ERROR, None)
        #TODO: Add VSphereAPI.Failure instead of Exception
        except Exception, inner_exception:
            raise exception.HAPIFailure(
                exception.exc_codes.STORAGE_DISKCREATE_ERROR,
                exception.exc_strs.STORAGE_DISKCREATE_ERROR, inner_exception)
Exemple #6
0
 def delete_vif(self, vif):
     try:
         vif_ref = self.session.xenapi.VIF.get_by_uuid(vif['uuid'])
         self.session.xenapi.VIF.unplug(vif_ref)
         self.session.xenapi.VIF.destroy(vif_ref)
     except XenAPI.Failure, inner_exception:
         raise exception.HAPIFailure(exception.exc_codes.NETWORK_VIF_ERROR,
                                     exception.exc_strs.NETWORK_VIF_ERROR,
                                     inner_exception)
Exemple #7
0
 def _login(self, host, username, password, wsdl_file, version):
     """Creates a session with the ESX host."""
     try:
         self._vsphereapi = VSphereAPISession(host, username, password,
                                              wsdl_file)
     except Exception, inner_exc:
         raise exception.HAPIFailure(
             exception.exc_codes.UNCLASSIFIED_AUTHENTICATION_FAILURE,
             exception.exc_strs.UNCLASSIFIED_AUTHENTICATION_FAILURE,
             inner_exc)
Exemple #8
0
class VSphereAPISession(object):
    """Manages a session with the ESX host and handles
    vSphere API calls made to the host.
    """
    def __init__(self,
                 host=None,
                 user="******",
                 password=None,
                 wsdl_file=None,
                 api_retry_count=10,
                 scheme="https"):
        self.api_retry_count = api_retry_count
        self.host = host
        self.user = user
        self.password = password
        self.scheme = scheme
        self.wsdl_file = wsdl_file
        self._session_id = None
        self.vim = None
        self._create_session()
        self.service_content = self.vim.get_service_content()
        self.client_factory = self.vim.client.factory

    def _create_session(self):
        """Creates a session with the ESX host."""
        while True:
            try:
                # Login and setup the session with the ESX host for making
                # API calls
                self.vim = self._get_vim_object()
                session = self.vim.Login(
                    self.vim.get_service_content().sessionManager,
                    userName=self.user,
                    password=self.password)
                # Terminate the earlier session, since there is a limit
                # to the number of sessions a client can own
                if self._session_id:
                    try:
                        self.vim.TerminateSession(
                            self.vim.get_service_content().sessionManager,
                            sessionId=[self._session_id])
                    except Exception, excep:
                        print(excep)
                self._session_id = session.key
                return
            except Exception, excep:
                excep.message = "In vsphereapi _create_session, " \
                                "exception while creating session. " + excep
                raise excep

            except Exception, inner_exc:
                raise exception.HAPIFailure(
                    exception.exc_codes.UNCLASSIFIED_AUTHENTICATION_FAILURE,
                    exception.exc_strs.UNCLASSIFIED_AUTHENTICATION_FAILURE,
                    inner_exc)
Exemple #9
0
 def find_virtualdisk(self, search_key=None, **kwargs):
     try:
         return [
             vdi['uuid'] for vdi in
             self.session.xenapi.VDI.get_all_records().itervalues()
             if vdi['other_config'].get(search_key) == 'true'
         ]
     except XenAPI.Failure, inner_exception:
         raise exception.HAPIFailure(
             exception.exc_codes.STORAGE_DISKFIND_ERROR,
             exception.exc_strs.STORAGE_DISKFIND_ERROR, inner_exception)
Exemple #10
0
    def login(self, username='******', password=None):
        host = _get_connection_url()
        config = config_util.parse_config('/etc/openstack/hapi')
        username = config_util.config_get(config, 'HAPI_USER', 'root')
        pwd = password != None and password or \
                            config_util.config_get(config, 'HAPI_PASS')
        if not pwd:
            raise exception.HAPIFailure(
                exception.exc_codes.UNCLASSIFIED_PASSWORD_UNCONFIGURED,
                exception.exc_strs.UNCLASSIFIED_PASSWORD_UNCONFIGURED)

        wsdl_directory = config_util.config_get(config, 'WSDL_LOC',
                                                '/etc/openstack/visdk')
        if not wsdl_directory:
            raise exception.HAPIFailure(
                exception.exc_codes.UNCLASSIFIED_MISSING_VMWAREWSDL_FILES,
                exception.exc_strs.UNCLASSIFIED_MISSING_VMWAREWSDL_FILES)
        wsdl_file = "file://" + wsdl_directory + "/vimService.wsdl"
        self._login(host, username, pwd, wsdl_file,
                    config_util.get_vpx_version())
Exemple #11
0
 def login(self, username='******', password=None):
     url = _get_connection_url()
     config = config_util.parse_config('/etc/openstack/hapi')
     username = config_util.config_get(config, 'HAPI_USER', 'root')
     pwd = password != None and password or \
                         config_util.config_get(config, 'HAPI_PASS')
     if not pwd:
         raise exception.HAPIFailure(
             exception.exc_codes.UNCLASSIFIED_PASSWORD_UNCONFIGURED,
             exception.exc_strs.UNCLASSIFIED_PASSWORD_UNCONFIGURED)
     self._login(url, username, pwd, config_util.get_vpx_version())
Exemple #12
0
 def get_properties(self, property_list):
     vm_prop_dict = {}
     vm_rec = self.session.xenapi.VM.get_record(self.vm)
     for vm_property in property_list:
         try:
             vm_prop_dict[vm_property] = vm_rec[self._mapping[vm_property]]
         except:
             raise exception.HAPIFailure(
                 exception.exc_codes.VM_PROPERTY_ERROR,
                 exception.exc_strs.VM_PROPERTY_ERROR)
     return vm_prop_dict
Exemple #13
0
 def _login(self, url, username, password, version):
     try:
         self._xapi = XenAPI.Session(url)
         self._xapi._ServerProxy__transport.user_agent = \
                                             'os-vpx/%s' % version
         self._xapi.xenapi.login_with_password(username, password)
     except XenAPI.Failure, inner_exc:
         raise exception.HAPIFailure(
             exception.exc_codes.UNCLASSIFIED_AUTHENTICATION_FAILURE,
             exception.exc_strs.UNCLASSIFIED_AUTHENTICATION_FAILURE,
             inner_exc)
Exemple #14
0
 def find_vif(self, **kwargs):
     device = None
     for key in kwargs:
         if key == 'device':
             device = kwargs[key]
     if device == None:
         exc_msg = exception.exc_strs.NETWORK_MISSING_PARAMETER % 'device'
         raise exception.HAPIFailure(
             exception.exc_codes.NETWORK_MISSING_PARAMETER, exc_msg)
     #Search specified device by looking for deviceInfo.label
     #If found return key (VirtualDevice property) - class VirtualPCNet32
     return util.find_device_by_label(self.session, self.vm, device)
Exemple #15
0
 def is_virtualdisk_in_use(self, vdisk_id):
     try:
         vdisk = self.session.xenapi.VDI.get_by_uuid(vdisk_id)
         allowed = self.session.xenapi.VDI.get_allowed_operations(vdisk)
         if 'destroy' in allowed:
             return False
         else:
             return True
     except XenAPI.Failure, inner_exception:
         raise exception.HAPIFailure(
             exception.exc_codes.STORAGE_DISKFIND_ERROR,
             exception.exc_strs.STORAGE_DISKFIND_ERROR, inner_exception)
Exemple #16
0
 def set_properties(self, **kwargs):
     for key in kwargs:
         if key == interface.VM.NAME:
             self.session.xenapi.VM.set_name_label(self.vm, kwargs[key])
         elif key == interface.VM.NAME_DESCRIPTION:
             self.session.xenapi.VM.set_name_description(
                 self.vm, kwargs[key])
         elif key == interface.VM.TAGS:
             self.session.xenapi.VM.set_tags(self.vm, kwargs[key])
         else:
             raise exception.HAPIFailure(
                 exception.exc_codes.VM_PROPERTY_ERROR,
                 exception.exc_strs.VM_PROPERTY_ERROR)
Exemple #17
0
 def set_properties(self, **kwargs):
     """Set properties of local VM as per specified key value pair args."""
     args = {}
     for key in kwargs:
         if key == interface.VM.NAME_DESCRIPTION:
             args[key] = kwargs[key]
         elif key == interface.VM.NAME:
             args[key] = kwargs[key]
         else:
             pass
     try:
         util.set_config_params(self.session, self.vm, args)
     except:
         raise exception.HAPIFailure(exception.exc_codes.VM_PROPERTY_ERROR,
                                     exception.exc_strs.VM_PROPERTY_ERROR)
Exemple #18
0
 def get_properties(self, property_list):
     vm_prop_dict = {}
     for vm_property in property_list:
         #if self._mapping.has_key(vm_property) == False:
         #    raise NotImplementedError()
         try:
             if vm_property != self._mapping[vm_property]:
                 vm_prop_dict[vm_property] = self.session._call_method(\
                             vsphereapi.vim_util, "get_dynamic_property",
                             self.vm, "VirtualMachine",
                             self._mapping[vm_property])
         except:
             raise exception.HAPIFailure(
                 exception.exc_codes.VM_PROPERTY_ERROR,
                 exception.exc_strs.VM_PROPERTY_ERROR)
     return vm_prop_dict
Exemple #19
0
    def is_virtualdisk_in_use(self, vdisk_id):
        """Checks if the specified virtual disk is attached
        to local VM or not. Returns True if the virtual disk is attached."""
        vmdk_name = util.format_virtual_disk_path(self.container.name,
                                                  vdisk_id)
        try:
            disks = util.get_disks_of_vm(self.session, self.vm)
            for disk in disks:
                if disk == vmdk_name:
                    return True
            return False

        except Exception, inner_exception:
            raise exception.HAPIFailure(
                exception.exc_codes.STORAGE_DISKFIND_ERROR,
                exception.exc_strs.STORAGE_DISKFIND_ERROR, inner_exception)
Exemple #20
0
    def delete_vif(self, vif):
        try:
            key = vif
            client_factory = self.session._get_vim().client.factory
            delete_spec = util.delete_vif_spec(client_factory, key)

            reconfig_task = self.session._call_method(self.session._get_vim(),
                                                      "ReconfigVM_Task",
                                                      self.vm,
                                                      spec=vm_config_spec)
            util.wait_for_task(self.session, reconfig_task)

        #TODO: Add vSphere API Exception
        except Exception, inner_exception:
            raise exception.HAPIFailure(exception.exc_codes.NETWORK_VIF_ERROR,
                                        exception.exc_strs.NETWORK_VIF_ERROR,
                                        inner_exception)
Exemple #21
0
 def find_vif(self, **kwargs):
     device = None
     for key in kwargs:
         if key == 'device':
             device = kwargs[key]
     if device == None:
         exc_msg = exception.exc_strs.NETWORK_MISSING_PARAMETER % 'device'
         raise \
     exception.HAPIFailure(exception.exc_codes.NETWORK_MISSING_PARAMETER,
                           exc_msg)
     vif = [
         vif
         for vif in self.session.xenapi.VIF.get_all_records().itervalues()
         if ((vif['VM'] == self.vm) and (str(vif['device']) == device))
     ]
     if len(vif) == 1:
         return vif[0]
     else:
         return None
Exemple #22
0
    def find_virtualdisk(self, search_key=None, **kwargs):
        """Finds virtual disk in the host that matches the specified key.
        Returns virtual disk name."""
        disk_list = []
        try:
            vmdk_list = util.get_disks_in_datastore(self.session,
                                                    self.container)
            for full_name in vmdk_list:

                split_full_name = full_name.rsplit('__' + search_key + '__.',
                                                   1)
                if len(split_full_name) > 1:
                    name_with_seqnumber = split_full_name[0]
                    split_name = name_with_seqnumber.rsplit('__', 1)
                    if len(split_name) > 1:
                        disk_list.append(full_name)
            return disk_list
        except Exception, inner_exception:
            raise exception.HAPIFailure(
                exception.exc_codes.STORAGE_DISKFIND_ERROR,
                exception.exc_strs.STORAGE_DISKFIND_ERROR, inner_exception)
Exemple #23
0
    def attach_virtualdisk(self, vdisk, **kwargs):
        """Attach specified virtual disk to local VM."""
        size_kb = 1024
        for key in kwargs:
            if key == 'size':
                size_kb = kwargs[key] * 1024 * 1024  # size will be in GB

        if vdisk[0] == '[':
            vdisk_path = vdisk
        else:
            vdisk_path = util.format_virtual_disk_path(self.container.name,
                                                       vdisk)
        disks = util.get_disks_of_vm(self.session, self.vm)
        unit_number = len(disks)
        #Avoid clash with SCSI Controller that sits on it's own bus.
        if unit_number >= 7:
            unit_number = unit_number + 1
        try:
            attach_config_spec = util.get_vmdk_attach_spec(
                self.session._get_vim().client.factory, size_kb, vdisk_path,
                self.adapter_type, unit_number)
            #("Reconfiguring VM instance %s to attach the disk %s")
            #                                         % (self.vm, vdisk_path)
            reconfig_task = self.session._call_method(self.session._get_vim(),
                                                      "ReconfigVM_Task",
                                                      self.vm,
                                                      spec=attach_config_spec)
            util.wait_for_task(self.session, reconfig_task)
            #NOTE:Forced scsi_host bus scan to hook up to the hot added disk
            util.scan_scsi_bus()
            time.sleep(2)

        #TODO: Add VSphereAPI.Failure type instead of Exception
        except Exception, inner_exception:
            raise exception.HAPIFailure(
                exception.exc_codes.STORAGE_DISKATTACH_ERROR,
                exception.exc_strs.STORAGE_DISKATTACH_ERROR, inner_exception)
Exemple #24
0
 def attach_virtualdisk(self, vdisk, **kwargs):
     for key in kwargs:
         if key == 'user_device':
             device = kwargs[key]
     vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdisk)
     vdi_rec = self.session.xenapi.VDI.get_record(vdi_ref)
     vbd_ref = None
     try:
         vbd_ref = self.session.xenapi.VBD.create({
             'VM':
             self.vm,
             'VDI':
             vdi_ref,
             'userdevice':
             str(device),
             'bootable':
             False,
             'mode':
             'RW',
             'type':
             'disk',
             'unpluggable':
             True,
             'empty':
             False,
             'other_config':
             vdi_rec['other_config'],
             'qos_algorithm_type':
             '',
             'qos_algorithm_params': {},
             'qos_supported_algorithms': [],
         })
         self.session.xenapi.VBD.plug(vbd_ref)
     except XenAPI.Failure, inner_exception:
         raise exception.HAPIFailure(
             exception.exc_codes.STORAGE_DISKATTACH_ERROR,
             exception.exc_strs.STORAGE_DISKATTACH_ERROR, inner_exception)