def get_discovered_targets(self): '''return a list of discovered target objects ''' logger.info("get_discovered_targets") discovered_targets = [] client = WmiClient('root\\wmi') for query in client.execute_query('SELECT * from MSIscsiInitiator_TargetClass'): endpoints = [] iqn = query.Properties_.Item('TargetName').Value for portal_group in query.Properties_.Item('PortalGroups').Value: for portal in portal_group.Properties_.Item('Portals').Value: endpoint = base.Endpoint(portal.Properties_.Item('Address').Value, portal.Properties_.Item('Port').Value) if endpoint not in endpoints: endpoints.append(endpoint) # u'SendTargets:*test 0003260 ROOT\\ISCSIPRT\\0000_0 ' # HOSTDEV-2764: u'SendTargets:*172.16.130.9 0003260 ROOT\\ISCSIPRT\\0000_0 172.16.130.55' discovery_mechanism = query.Properties_.Item('DiscoveryMechanism').Value if not discovery_mechanism: logger.debug("invalid discovery mechanism (empty): {!r}".format(discovery_mechanism)) continue discovery_mechanism = discovery_mechanism.strip().split() if len(discovery_mechanism) < 2: logger.debug("invalid discovery mechanism: {!r}".format(discovery_mechanism)) continue discovery_endpoint_ip = discovery_mechanism[0].split(':')[-1].strip("*") discovery_endpoint_port = int(discovery_mechanism[1]) discovery_endpoint = base.Endpoint(discovery_endpoint_ip, discovery_endpoint_port) target = base.Target(endpoints, discovery_endpoint, iqn) if target not in discovered_targets: discovered_targets.append(target) return discovered_targets
def _get_sessions_of_target(target): client = WmiClient('root\\wmi') wql = "SELECT * from MSiSCSIInitiator_SessionClass where TargetName='%s'" % str(target.get_iqn()) query = client.execute_query(wql) target_sessions = [] for session in query: hct = None uid = session.Properties_.Item('SessionId').Value if not uid: continue connections = session.Properties_.Item('ConnectionInformation').Value if not connections: continue conn_0 = connections[0] source_ip = conn_0.Properties_.Item('InitiatorAddress').Value source_iqn = session.Properties_.Item('InitiatorName').Value target_address = conn_0.Properties_.Item('TargetAddress').Value target_port = conn_0.Properties_.Item('TargetPort').Value if not session.Properties_.Item('Devices').Value: hct = HCT(-1, 0, -1) else: devices = list(session.Properties_.Item('Devices').Value) hct = HCT(devices[0].Properties_.Item('ScsiPortNumber').Value, devices[0].Properties_.Item('ScsiPathId').Value, devices[0].Properties_.Item('ScsiTargetId').Value) target_sessions.append(base.Session(target, base.Endpoint(target_address, target_port), source_ip, source_iqn, uid, hct)) return target_sessions
def _get_sessions_of_target(target): client = WmiClient('root\\wmi') wql = "SELECT * from MSiSCSIInitiator_SessionClass where TargetName='%s'" % str( target.get_iqn()) query = client.execute_query(wql) target_sessions = [] for session in query: hct = None uid = session.Properties_.Item('SessionId').Value if not uid: continue connections = session.Properties_.Item( 'ConnectionInformation').Value if not connections: continue conn_0 = connections[0] source_ip = conn_0.Properties_.Item('InitiatorAddress').Value source_iqn = session.Properties_.Item('InitiatorName').Value target_address = conn_0.Properties_.Item('TargetAddress').Value target_port = conn_0.Properties_.Item('TargetPort').Value if not session.Properties_.Item('Devices').Value: hct = HCT(-1, 0, -1) else: devices = list(session.Properties_.Item('Devices').Value) hct = HCT( devices[0].Properties_.Item('ScsiPortNumber').Value, devices[0].Properties_.Item('ScsiPathId').Value, devices[0].Properties_.Item('ScsiTargetId').Value) target_sessions.append( base.Session(target, base.Endpoint(target_address, target_port), source_ip, source_iqn, uid, hct)) return target_sessions
def get_source_iqn(self): from .iscsi_exceptions import NotReadyException client = WmiClient('root\\wmi') query = list(client.execute_query('SELECT * FROM MSIscsiInitiator_MethodClass')) if not query: raise NotReadyException("Could not query iSCSI initiator from WMI") iqn = query[0].Properties_.Item("ISCSINodeName").Value return IQN(iqn)
def get_source_iqn(self): from .iscsi_exceptions import NotReadyException client = WmiClient('root\\wmi') query = list( client.execute_query('SELECT * FROM MSIscsiInitiator_MethodClass')) if not query: raise NotReadyException("Could not query iSCSI initiator from WMI") iqn = query[0].Properties_.Item("ISCSINodeName").Value return IQN(iqn)
def _create_initiator_obj_if_needed(self): if not self._initiator: iscsi_software = MicrosoftSoftwareInitiator() if not iscsi_software.is_installed(): raise RuntimeError("iscsi sw isn't running") client = WmiClient('root\\wmi') query = client.execute_query('SELECT * from MSIscsiInitiator_InitiatorClass') initiator_name = list(query)[0].Properties_.Item('InitiatorName').Value iqn = self.get_source_iqn() self._initiator = base.Initiator(iqn, initiator_name)
def _get_discovery_endpoints(self): '''return all discovery endpoints currently use only for undiscover ''' discovery_endpoints = [] client = WmiClient('root\\wmi') query = client.execute_query('SELECT * FROM MSiSCSIInitiator_SendTargetPortalClass') for discovery_endpoint in query: endpoint = base.Endpoint(discovery_endpoint.Properties_.Item('PortalAddress').Value, discovery_endpoint.Properties_.Item('PortalPort').Value) if endpoint not in discovery_endpoints: discovery_endpoints.append(endpoint) return discovery_endpoints
def _create_initiator_obj_if_needed(self): if not self._initiator: iscsi_software = MicrosoftSoftwareInitiator() if not iscsi_software.is_installed(): raise RuntimeError("iscsi sw isn't running") client = WmiClient('root\\wmi') query = client.execute_query( 'SELECT * from MSIscsiInitiator_InitiatorClass') initiator_name = list(query)[0].Properties_.Item( 'InitiatorName').Value iqn = self.get_source_iqn() self._initiator = base.Initiator(iqn, initiator_name)
def _get_connectivity_using_wmi(self): '''returns a list of dicts which contain all available targets with it's main parameters ''' availble_targets_connectivity = [] client = WmiClient('root\\wmi') for target in client.execute_query('SELECT * from MSIscsiInitiator_TargetClass'): iqn = target.Properties_.Item('TargetName').Value for portal_group in target.Properties_.Item('PortalGroups').Value: for portal in portal_group.Properties_.Item('Portals').Value: target_connectivity = {'dst_ip': portal.Properties_.Item('Address').Value, 'dst_port': portal.Properties_.Item('Port').Value, 'iqn': iqn} if target_connectivity not in availble_targets_connectivity: availble_targets_connectivity.append(target_connectivity) return availble_targets_connectivity
def set_source_iqn(self, iqn): '''receive an iqn as a string, verify it's valid and set it. returns iqn type of the new IQN or None if fails ''' logger.info("iqn before the change is {!r} going to change to {!r}".format(self.get_source_iqn(), iqn)) IQN(iqn) # raise if iqn doesn't exist client = WmiClient('root\\wmi') query = list(client.execute_query("SELECT * FROM MSIscsiInitiator_MethodClass"))[0] initiator_name = query.Methods_.Item("SetIscsiInitiatorNodeName") parameters = initiator_name.InParameters.SpawnInstance_() parameters.Properties_.Item("InitiatorNodeName").Value = iqn query.ExecMethod_('SetIscsiInitiatorNodeName', parameters) logger.info("iqn is now {!r}".format(self.get_source_iqn())) return self.get_source_iqn()
def _get_discovery_endpoints(self): '''return all discovery endpoints currently use only for undiscover ''' discovery_endpoints = [] client = WmiClient('root\\wmi') query = client.execute_query( 'SELECT * FROM MSiSCSIInitiator_SendTargetPortalClass') for discovery_endpoint in query: endpoint = base.Endpoint( discovery_endpoint.Properties_.Item('PortalAddress').Value, discovery_endpoint.Properties_.Item('PortalPort').Value) if endpoint not in discovery_endpoints: discovery_endpoints.append(endpoint) return discovery_endpoints
def set_source_iqn(self, iqn): '''receive an iqn as a string, verify it's valid and set it. returns iqn type of the new IQN or None if fails ''' logger.info( "iqn before the change is {!r} going to change to {!r}".format( self.get_source_iqn(), iqn)) IQN(iqn) # raise if iqn doesn't exist client = WmiClient('root\\wmi') query = list( client.execute_query( "SELECT * FROM MSIscsiInitiator_MethodClass"))[0] initiator_name = query.Methods_.Item("SetIscsiInitiatorNodeName") parameters = initiator_name.InParameters.SpawnInstance_() parameters.Properties_.Item("InitiatorNodeName").Value = iqn query.ExecMethod_('SetIscsiInitiatorNodeName', parameters) logger.info("iqn is now {!r}".format(self.get_source_iqn())) return self.get_source_iqn()
def _get_connectivity_using_wmi(self): '''returns a list of dicts which contain all available targets with it's main parameters ''' availble_targets_connectivity = [] client = WmiClient('root\\wmi') for target in client.execute_query( 'SELECT * from MSIscsiInitiator_TargetClass'): iqn = target.Properties_.Item('TargetName').Value for portal_group in target.Properties_.Item('PortalGroups').Value: for portal in portal_group.Properties_.Item('Portals').Value: target_connectivity = { 'dst_ip': portal.Properties_.Item('Address').Value, 'dst_port': portal.Properties_.Item('Port').Value, 'iqn': iqn } if target_connectivity not in availble_targets_connectivity: availble_targets_connectivity.append( target_connectivity) return availble_targets_connectivity
def get_discovered_targets(self): '''return a list of discovered target objects ''' logger.info("get_discovered_targets") discovered_targets = [] client = WmiClient('root\\wmi') for query in client.execute_query( 'SELECT * from MSIscsiInitiator_TargetClass'): endpoints = [] iqn = query.Properties_.Item('TargetName').Value for portal_group in query.Properties_.Item('PortalGroups').Value: for portal in portal_group.Properties_.Item('Portals').Value: endpoint = base.Endpoint( portal.Properties_.Item('Address').Value, portal.Properties_.Item('Port').Value) if endpoint not in endpoints: endpoints.append(endpoint) # u'SendTargets:*test 0003260 ROOT\\ISCSIPRT\\0000_0 ' # HOSTDEV-2764: u'SendTargets:*172.16.130.9 0003260 ROOT\\ISCSIPRT\\0000_0 172.16.130.55' discovery_mechanism = query.Properties_.Item( 'DiscoveryMechanism').Value if not discovery_mechanism: logger.debug( "invalid discovery mechanism (empty): {!r}".format( discovery_mechanism)) continue discovery_mechanism = discovery_mechanism.strip().split() if len(discovery_mechanism) < 2: logger.debug("invalid discovery mechanism: {!r}".format( discovery_mechanism)) continue discovery_endpoint_ip = discovery_mechanism[0].split( ':')[-1].strip("*") discovery_endpoint_port = int(discovery_mechanism[1]) discovery_endpoint = base.Endpoint(discovery_endpoint_ip, discovery_endpoint_port) target = base.Target(endpoints, discovery_endpoint, iqn) if target not in discovered_targets: discovered_targets.append(target) return discovered_targets
def _refresh_wmi_db(self): client = WmiClient('root\\wmi') for item in client.execute_query("SELECT * FROM MSIscsiInitiator_MethodClass"): item.ExecMethod_('RefreshTargetList', None)
def _refresh_wmi_db(self): client = WmiClient('root\\wmi') for item in client.execute_query( "SELECT * FROM MSIscsiInitiator_MethodClass"): item.ExecMethod_('RefreshTargetList', None)