Exemplo n.º 1
0
    def _process_update_set(self, updateSet):
        """Processes the updateSet and returns VM events."""

        events = []
        host_name = None
        clus_name = None
        clus_id = None
        LOG.debug("Processing UpdateSet version: %s.", updateSet.version)
        filterSet = updateSet.filterSet
        if not filterSet:
            return events
        for propFilterUpdate in filterSet:
            objectSet = propFilterUpdate.objectSet
            if not objectSet:
                continue
            for objectUpdate in objectSet:
                try:
                    obj_mor = objectUpdate.obj
                    if obj_mor._type != "VirtualMachine":
                        continue
                    if objectUpdate.kind == "enter":
                        event_type = constants.VM_CREATED
                    elif objectUpdate.kind == "modify":
                        event_type = constants.VM_UPDATED
                    elif objectUpdate.kind == "leave":
                        event_type = constants.VM_DELETED
                    else:
                        continue
                    host_changed = False
                    vm_uuid = None
                    changes = common_util.convert_objectupdate_to_dict(
                        objectUpdate)
                    if changes.get('config.extraConfig["nvp.vm-uuid"]'):
                        vm_uuid = changes.get('config.extraConfig'
                                              '["nvp.vm-uuid"]').value
                        event_type = constants.VM_CREATED
                        cache.VCCache.add_vm_mor_for_uuid(vm_uuid, obj_mor)
                    else:
                        vm_uuid = cache.VCCache.get_vmuuid_for_moid(
                            obj_mor.value)
                    if vm_uuid:
                        old_vm = cache.VCCache.get_vm_model_for_uuid(vm_uuid)
                        LOG.debug("Old VM: %s.", old_vm)
                        LOG.debug("cache.VCCache.vm_uuid_to_model: %s.",
                                  cache.VCCache.vm_uuid_to_model)
                        new_vm = None
                        if old_vm:
                            if event_type == constants.VM_CREATED:
                                # Our cache has information about VM. But event
                                # received is VM_CREATED. This means it is
                                # session restart case. So we should not add
                                # this to new event.
                                LOG.debug("Session restart event for VM %s",
                                          vm_uuid)
                                continue
                            new_vm = copy.deepcopy(old_vm)
                        else:
                            new_vm = model.VirtualMachine(name=None,
                                                          vnics=[],
                                                          uuid=None,
                                                          key=None)
                            LOG.debug("VM not found in cache. New created: "
                                      " %s.", new_vm)
                        new_vm.uuid = vm_uuid
                        new_vm.key = obj_mor.value
                        if changes.get('name'):
                            new_vm.name = changes.get('name')
                        if changes.get('runtime.host'):
                            # Host got changed / New VM.
                            clus_mor = self.session._call_method(
                                vim_util,
                                "get_dynamic_property",
                                changes.get('runtime.host'),
                                "HostSystem", "parent")
                            # Cache the VM and Cluster.
                            cache.VCCache.add_cluster_mor_for_vm(vm_uuid,
                                                                 clus_mor)
                        if event_type != constants.VM_DELETED:
                            extraconfigs = (
                                resource_util.get_extraconfigs_for_vm(
                                    self.session, obj_mor))
                            if changes.get('config.hardware.device'):
                                devices = changes.get('config.hardware.device')
                                nicdvs = network_util.get_vnics_from_devices(
                                    devices)
                                i = 0
                                vnics = []
                                for nicdev in nicdvs:
                                    macadd = nicdev.macAddress
                                    port = nicdev.backing.port
                                    pgkey = port.portgroupKey
                                    portid = extraconfigs.get("nvp.iface-id.%d"
                                                              % i)
                                    vnic = model.VirtualNic(
                                        mac_address=macadd,
                                        port_uuid=portid,
                                        vm_id=vm_uuid,
                                        vm_name=new_vm.name,
                                        nic_type=None,
                                        pg_id=pgkey,
                                        key=None)
                                    vnics.append(vnic)
                                    i += 1
                                new_vm.vnics = vnics
                            host_mor = resource_util.get_host_mor_for_vm(
                                self.session, vm_uuid)
                            clus_mor = resource_util.get_cluster_mor_for_vm(
                                self.session, vm_uuid)
                            host_name = (
                                resource_util.get_hostname_for_host_mor(
                                    self.session, host_mor))
                            old_host_name = (
                                cache.VCCache.get_esx_hostname_for_vm(vm_uuid))
                            if old_host_name and old_host_name != host_name:
                                host_changed = True
                            cache.VCCache.add_esx_hostname_for_vm(vm_uuid,
                                                                  host_name)
                            clus_name = (
                                resource_util.get_clustername_for_cluster_mor(
                                    self.session, clus_mor))
                            clus_id = (
                                resource_util.get_clusterid_for_cluster_mor(
                                    self.session, clus_mor))
                        elif event_type == constants.VM_DELETED:
                            host_name = cache.VCCache.get_esx_hostname_for_vm(
                                vm_uuid)
                        event = model.Event(event_type, new_vm, None,
                                            host_name, clus_name, clus_id,
                                            host_changed)
                        events.append(event)
                        cache.VCCache.add_vm_model_for_uuid(vm_uuid, new_vm)
                        LOG.debug("Added vm to cache: %s.", new_vm.uuid)
                    else:
                        LOG.debug("Ignoring update for VM: %s.",
                                  changes.get('name'))
                except Exception:
                    LOG.exception(_LE("Exception while processing update set "
                                      "for event %(event)s for vm %(vm)s."),
                                  {'event': event_type, 'vm': vm_uuid})
        LOG.debug("Finished processing UpdateSet version: %s.",
                  updateSet.version)
        return events
 def _wait_for_port_update_on_vm(self, vm_mor, pgmor):
     property_collector = None
     try:
         LOG.debug("Creating new property collector.")
         property_collector = self.session._call_method(
             vim_util, "create_property_collector")
         self._register_vm_for_updates(vm_mor, property_collector)
         version = ""
         pg_key, port_key, swuuid = (None, None, None)
         while self.state == constants.DRIVER_RUNNING:
             LOG.debug("Waiting for VM %(vm)s to connect to "
                       "port group %(pg)s.",
                       {'vm': vm_mor.value, 'pg': pgmor.value})
             try:
                 update_set = self.session._call_method(
                     vim_util, "wait_for_updates_ex", version,
                     collector=property_collector)
             except error_util.SocketTimeoutException:
                 LOG.exception(_("Socket Timeout Exception."))
                 # Ignore timeout.
                 continue
             if update_set:
                 version = update_set.version
                 filterSet = update_set.filterSet
                 if not filterSet:
                     continue
                 for propFilterUpdate in filterSet:
                     objectSet = propFilterUpdate.objectSet
                     if not objectSet:
                         continue
                     for objectUpdate in objectSet:
                         if objectUpdate.kind == "leave":
                             LOG.warn(_("VM %(vm)s got deleted while "
                                        "waiting for it to connect to "
                                        "port group %(pg)s."),
                                      {'vm': vm_mor.value,
                                       'pg': pgmor.value})
                             return (pg_key, port_key, swuuid)
                         changes = common_util.convert_objectupdate_to_dict(
                             objectUpdate)
                         devices = changes.get('config.hardware.device')
                         nicdvs = network_util.get_vnics_from_devices(
                             devices)
                         if not nicdvs:
                             continue
                         for device in nicdvs:
                             if (hasattr(device, "backing") and
                                     hasattr(device.backing, "port") and
                                     device.backing.port):
                                 port = device.backing.port
                                 if (hasattr(port, "portgroupKey")):
                                     pg_key = port.portgroupKey
                                     if (pg_key == pgmor.value and
                                             hasattr(port, "portKey")):
                                         port_key = port.portKey
                                         swuuid = port.switchUuid
                                         LOG.info(_("VM %(vm)s connected "
                                                    "to port group: "
                                                    "%(pg)s."),
                                                  {'vm': vm_mor.value,
                                                   'pg': pgmor.value})
                                         return (pg_key, port_key, swuuid)
     except Exception as e:
         LOG.exception(_("Exception while waiting for VM %(vm)s "
                         "to connect to port group %(pg)s: %(err)s."),
                       {'vm': vm_mor.value, 'pg': pgmor.value, 'err': e})
         raise e
     finally:
         LOG.debug("Destroying the property collector created.")
         self.session._call_method(vim_util,
                                   "destroy_property_collector",
                                   property_collector)
Exemplo n.º 3
0
 def _wait_for_port_update_on_vm(self, vm_mor, pgmor):
     property_collector = None
     try:
         LOG.debug("Creating new property collector.")
         property_collector = self.session._call_method(
             vim_util, "create_property_collector")
         self._register_vm_for_updates(vm_mor, property_collector)
         version = ""
         pg_key, port_key, swuuid = (None, None, None)
         while self.state == constants.DRIVER_RUNNING:
             LOG.debug(
                 "Waiting for VM %(vm)s to connect to "
                 "port group %(pg)s.", {
                     'vm': vm_mor.value,
                     'pg': pgmor.value
                 })
             try:
                 update_set = self.session._call_method(
                     vim_util,
                     "wait_for_updates_ex",
                     version,
                     collector=property_collector)
             except error_util.SocketTimeoutException:
                 LOG.exception(_LE("Socket Timeout Exception."))
                 # Ignore timeout.
                 continue
             if update_set:
                 version = update_set.version
                 filterSet = update_set.filterSet
                 if not filterSet:
                     continue
                 for propFilterUpdate in filterSet:
                     objectSet = propFilterUpdate.objectSet
                     if not objectSet:
                         continue
                     for objectUpdate in objectSet:
                         if objectUpdate.kind == "leave":
                             LOG.warning(
                                 _LW("VM %(vm)s got deleted while "
                                     "waiting for it to connect "
                                     "to port group %(pg)s."), {
                                         'vm': vm_mor.value,
                                         'pg': pgmor.value
                                     })
                             return (pg_key, port_key, swuuid)
                         changes = common_util.convert_objectupdate_to_dict(
                             objectUpdate)
                         devices = changes.get('config.hardware.device')
                         nicdvs = network_util.get_vnics_from_devices(
                             devices)
                         if not nicdvs:
                             continue
                         for device in nicdvs:
                             if (hasattr(device, "backing")
                                     and hasattr(device.backing, "port")
                                     and device.backing.port):
                                 port = device.backing.port
                                 if (hasattr(port, "portgroupKey")):
                                     pg_key = port.portgroupKey
                                     if (pg_key == pgmor.value
                                             and hasattr(port, "portKey")):
                                         port_key = port.portKey
                                         swuuid = port.switchUuid
                                         LOG.info(
                                             _LI("VM %(vm)s connected "
                                                 "to port group: "
                                                 "%(pg)s."), {
                                                     'vm': vm_mor.value,
                                                     'pg': pgmor.value
                                                 })
                                         return (pg_key, port_key, swuuid)
     except Exception as e:
         LOG.exception(
             _LE("Exception while waiting for VM %(vm)s "
                 "to connect to port group %(pg)s: %(err)s."), {
                     'vm': vm_mor.value,
                     'pg': pgmor.value,
                     'err': e
                 })
         raise e
     finally:
         LOG.debug("Destroying the property collector created.")
         self.session._call_method(vim_util, "destroy_property_collector",
                                   property_collector)
Exemplo n.º 4
0
    def _process_update_set(self, updateSet):
        """Processes the updateSet and returns VM events."""

        events = []
        host_name = None
        clus_name = None
        clus_id = None
        LOG.debug("Processing UpdateSet version: %s.", updateSet.version)
        filterSet = updateSet.filterSet
        if not filterSet:
            return events
        for propFilterUpdate in filterSet:
            objectSet = propFilterUpdate.objectSet
            if not objectSet:
                continue
            for objectUpdate in objectSet:
                try:
                    obj_mor = objectUpdate.obj
                    if obj_mor._type != "VirtualMachine":
                        continue
                    if objectUpdate.kind == "enter":
                        event_type = constants.VM_CREATED
                    elif objectUpdate.kind == "modify":
                        event_type = constants.VM_UPDATED
                    elif objectUpdate.kind == "leave":
                        event_type = constants.VM_DELETED
                    else:
                        continue
                    host_changed = False
                    vm_uuid = None
                    changes = common_util.convert_objectupdate_to_dict(
                        objectUpdate)
                    if changes.get('config.extraConfig["nvp.vm-uuid"]'):
                        vm_uuid = changes.get('config.extraConfig'
                                              '["nvp.vm-uuid"]').value
                        event_type = constants.VM_CREATED
                        if vm_uuid is not None:
                            vm_mor = cache.VCCache.get_vm_mor_for_uuid(vm_uuid)
                            if vm_mor is None:
                                cache.VCCache.add_vm_mor_for_uuid(vm_uuid,
                                                                  obj_mor)
                    else:
                        vm_uuid = cache.VCCache.get_vmuuid_for_moid(
                            obj_mor.value)
                    if vm_uuid:
                        old_vm = cache.VCCache.get_vm_model_for_uuid(vm_uuid)
                        LOG.debug("Old VM: %s.", old_vm)
                        LOG.debug("cache.VCCache.vm_uuid_to_model: %s.",
                                  cache.VCCache.vm_uuid_to_model)
                        new_vm = None
                        if old_vm:
                            if event_type == constants.VM_CREATED:
                                # Our cache has information about VM. But event
                                # received is VM_CREATED. This means it is
                                # session restart case. So we should not add
                                # this to new event.
                                LOG.debug("Session restart event for VM %s",
                                          vm_uuid)
                                continue
                            new_vm = copy.deepcopy(old_vm)
                        else:
                            new_vm = model.VirtualMachine(name=None,
                                                          vnics=[],
                                                          uuid=None,
                                                          key=None)
                            LOG.debug("VM not found in cache. New created: "
                                      " %s.", new_vm)
                        new_vm.uuid = vm_uuid
                        new_vm.key = obj_mor.value
                        if changes.get('name'):
                            new_vm.name = changes.get('name')
                        if changes.get('runtime.host'):
                            # Host got changed / New VM.
                            clus_mor = self.session._call_method(
                                vim_util,
                                "get_dynamic_property",
                                changes.get('runtime.host'),
                                "HostSystem", "parent")
                            # Cache the VM and Cluster.
                            cache.VCCache.add_cluster_mor_for_vm(vm_uuid,
                                                                 clus_mor)
                        if event_type != constants.VM_DELETED:
                            extraconfigs = (
                                resource_util.get_extraconfigs_for_vm(
                                    self.session, obj_mor))
                            if changes.get('config.hardware.device'):
                                devices = changes.get('config.hardware.device')
                                nicdvs = network_util.get_vnics_from_devices(
                                    devices)
                                i = 0
                                vnics = []
                                for nicdev in nicdvs:
                                    macadd = nicdev.macAddress
                                    port = nicdev.backing.port
                                    pgkey = port.portgroupKey
                                    portid = extraconfigs.get("nvp.iface-id.%d"
                                                              % i)
                                    vnic = model.VirtualNic(
                                        mac_address=macadd,
                                        port_uuid=portid,
                                        vm_id=vm_uuid,
                                        vm_name=new_vm.name,
                                        nic_type=None,
                                        pg_id=pgkey,
                                        key=None)
                                    vnics.append(vnic)
                                    i += 1
                                new_vm.vnics = vnics
                            host_mor = resource_util.get_host_mor_for_vm(
                                self.session, vm_uuid)
                            clus_mor = resource_util.get_cluster_mor_for_vm(
                                self.session, vm_uuid)
                            host_name = (
                                resource_util.get_hostname_for_host_mor(
                                    self.session, host_mor))
                            old_host_name = (
                                cache.VCCache.get_esx_hostname_for_vm(vm_uuid))
                            if old_host_name and old_host_name != host_name:
                                host_changed = True
                            cache.VCCache.add_esx_hostname_for_vm(vm_uuid,
                                                                  host_name)
                            clus_name = (
                                resource_util.get_clustername_for_cluster_mor(
                                    self.session, clus_mor))
                            clus_id = (
                                resource_util.get_clusterid_for_cluster_mor(
                                    self.session, clus_mor))
                        elif event_type == constants.VM_DELETED:
                            host_name = cache.VCCache.get_esx_hostname_for_vm(
                                vm_uuid)
                        event = model.Event(event_type, new_vm, None,
                                            host_name, clus_name, clus_id,
                                            host_changed)
                        events.append(event)
                        cache.VCCache.add_vm_model_for_uuid(vm_uuid, new_vm)
                        LOG.debug("Added vm to cache: %s.", new_vm.uuid)
                    else:
                        LOG.debug("Ignoring update for VM: %s.",
                                  changes.get('name'))
                except Exception:
                    LOG.exception(_LE("Exception while processing update set "
                                      "for event %(event)s for vm %(vm)s."),
                                  {'event': event_type, 'vm': vm_uuid})
        LOG.debug("Finished processing UpdateSet version: %s.",
                  updateSet.version)
        return events