def _set_nexus(self, nexus_wwn=None): ''' Sets the nexus initiator WWN. Raises an exception if the nexus is already set or if the TPG does not use a nexus. ''' self._check_self() if not self.has_feature('nexus'): raise RTSLibError("The TPG does not use a nexus.") elif self._get_nexus(): raise RTSLibError("The TPG's nexus initiator WWN is already set.") else: if nexus_wwn is None: nexus_wwn = generate_wwn(self.parent_target.wwn_type) elif not is_valid_wwn(self.parent_target.wwn_type, nexus_wwn): raise RTSLibError("WWN '%s' is not of type '%s'." % (nexus_wwn, self.parent_target.wwn_type)) fwrite("%s/nexus" % self.path, nexus_wwn)
def parse_specfile(spec_file): ''' Parses the fabric module spec file. @param spec_file: the path to the specfile to parse. @type spec_file: str @return: a dict of spec options ''' # Recognized options and their default values name = os.path.basename(spec_file).partition(".")[0] defaults = dict(features=['discovery_auth', 'acls', 'acls_auth', 'nps', 'tpgts'], kernel_module="%s_target_mod" % name, configfs_group=name, wwn_from_files=[], wwn_from_files_filter='', wwn_from_cmds=[], wwn_from_cmds_filter='', wwn_type='free') spec = ConfigObj(spec_file).dict() # Do not allow unknown options unknown_options = set(spec.keys()) - set(defaults.keys()) if unknown_options: raise RTSLibError("Unknown option(s) in %s: %s" % (spec_file, list(unknown_options))) # Use defaults for missing options missing_options = set(defaults.keys()) - set(spec.keys()) for option in missing_options: spec[option] = defaults[option] # Type conversion and checking for option in spec: spec_type = type(spec[option]).__name__ defaults_type = type(defaults[option]).__name__ if spec_type != defaults_type: # Type mismatch, go through acceptable conversions if spec_type == 'str' and defaults_type == 'list': spec[option] = [spec[option]] else: raise RTSLibError("Wrong type for option '%s' in %s. " % (option, spec_file) + "Expected type '%s' and got '%s'." % (defaults_type, spec_type)) # Generate the list of fixed WWNs if not empty wwn_list = None wwn_type = spec['wwn_type'] if spec['wwn_from_files']: for wwn_pattern in spec['wwn_from_files']: for wwn_file in glob.iglob(wwn_pattern): wwns_in_file = [wwn for wwn in re.split('\t|\0|\n| ', fread(wwn_file)) if wwn.strip()] if spec['wwn_from_files_filter']: wwns_filtered = [] for wwn in wwns_in_file: filter = "echo %s|%s" \ % (wwn, spec['wwn_from_files_filter']) wwns_filtered.append(exec_argv(filter, shell=True)) else: wwns_filtered = wwns_in_file if wwn_list is None: wwn_list = set([]) wwn_list.update(set([wwn for wwn in wwns_filtered if is_valid_wwn(wwn_type, wwn) if wwn] )) if spec['wwn_from_cmds']: for wwn_cmd in spec['wwn_from_cmds']: cmd = subprocess.Popen(wwn_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) (cmd_result, _) = cmd.communicate() cmd_result = "\n".join([line.strip() for line in cmd_result.split("\n") if line.strip()]) wwns_from_cmd = [wwn for wwn in re.split('\t|\0|\n| ', cmd_result) if wwn.strip()] if spec['wwn_from_cmds_filter']: wwns_filtered = [] for wwn in wwns_from_cmd: filter = "echo %s|%s" \ % (wwn, spec['wwn_from_cmds_filter']) wwns_filtered.append(exec_argv(filter, shell=True)) else: wwns_filtered = wwns_from_cmd if wwn_list is None: wwn_list = set([]) wwn_list.update(set([wwn for wwn in wwns_filtered if is_valid_wwn(wwn_type, wwn) if wwn] )) spec['wwn_list'] = wwn_list return spec
def is_valid_wwn(self, wwn): ''' Checks whether or not the provided WWN is valid for this fabric module according to the spec file. ''' return is_valid_wwn(self.spec['wwn_type'], wwn, self.spec['wwn_list'])
def parse_specfile(spec_file): ''' Parses the fabric module spec file. @param spec_file: the path to the specfile to parse. @type spec_file: str @return: a dict of spec options ''' # Recognized options and their default values name = os.path.basename(spec_file).partition(".")[0] defaults = dict( features=['discovery_auth', 'acls', 'acls_auth', 'nps', 'tpgts'], kernel_module="%s_target_mod" % name, configfs_group=name, wwn_from_files=[], wwn_from_files_filter='', wwn_from_cmds=[], wwn_from_cmds_filter='', wwn_type='free') spec = ConfigObj(spec_file).dict() # Do not allow unknown options unknown_options = set(spec.keys()) - set(defaults.keys()) if unknown_options: raise RTSLibError("Unknown option(s) in %s: %s" % (spec_file, list(unknown_options))) # Use defaults for missing options missing_options = set(defaults.keys()) - set(spec.keys()) for option in missing_options: spec[option] = defaults[option] # Type conversion and checking for option in spec: spec_type = type(spec[option]).__name__ defaults_type = type(defaults[option]).__name__ if spec_type != defaults_type: # Type mismatch, go through acceptable conversions if spec_type == 'str' and defaults_type == 'list': spec[option] = [spec[option]] else: raise RTSLibError("Wrong type for option '%s' in %s. " % (option, spec_file) + "Expected type '%s' and got '%s'." % (defaults_type, spec_type)) # Generate the list of fixed WWNs if not empty wwn_list = None wwn_type = spec['wwn_type'] if spec['wwn_from_files']: for wwn_pattern in spec['wwn_from_files']: for wwn_file in glob.iglob(wwn_pattern): wwns_in_file = [ wwn for wwn in re.split('\t|\0|\n| ', fread(wwn_file)) if wwn.strip() ] if spec['wwn_from_files_filter']: wwns_filtered = [] for wwn in wwns_in_file: filter = "echo %s|%s" \ % (wwn, spec['wwn_from_files_filter']) wwns_filtered.append(exec_argv(filter, shell=True)) else: wwns_filtered = wwns_in_file if wwn_list is None: wwn_list = set([]) wwn_list.update( set([ wwn for wwn in wwns_filtered if is_valid_wwn(wwn_type, wwn) if wwn ])) if spec['wwn_from_cmds']: for wwn_cmd in spec['wwn_from_cmds']: cmd = subprocess.Popen(wwn_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) (cmd_result, _) = cmd.communicate() cmd_result = "\n".join([ line.strip() for line in cmd_result.split("\n") if line.strip() ]) wwns_from_cmd = [ wwn for wwn in re.split('\t|\0|\n| ', cmd_result) if wwn.strip() ] if spec['wwn_from_cmds_filter']: wwns_filtered = [] for wwn in wwns_from_cmd: filter = "echo %s|%s" \ % (wwn, spec['wwn_from_cmds_filter']) wwns_filtered.append(exec_argv(filter, shell=True)) else: wwns_filtered = wwns_from_cmd if wwn_list is None: wwn_list = set([]) wwn_list.update( set([ wwn for wwn in wwns_filtered if is_valid_wwn(wwn_type, wwn) if wwn ])) spec['wwn_list'] = wwn_list return spec
def _parse_spec(self): ''' Parses the fabric module spec file. ''' # Recognized options and their default values defaults = dict(features=['discovery_auth', 'acls', 'acls_auth', 'nps', 'tpgts'], kernel_module="%s_target_mod" % self.name, configfs_group=self.name, wwn_from_files=[], wwn_from_files_filter='', wwn_from_cmds=[], wwn_from_cmds_filter='', wwn_type='free') spec_file = "%s/%s.spec" % (self.spec_dir, self.name) spec = ConfigObj(spec_file).dict() if spec: self.spec_file = spec_file else: self.spec_file = '' # Do not allow unknown options unknown_options = set(spec.keys()) - set(defaults.keys()) if unknown_options: raise RTSLibError("Unknown option(s) in %s: %s" % (spec_file, list(unknown_options))) # Use defaults for missing options missing_options = set(defaults.keys()) - set(spec.keys()) for option in missing_options: spec[option] = defaults[option] # Type conversion and checking for option in spec: spec_type = type(spec[option]).__name__ defaults_type = type(defaults[option]).__name__ if spec_type != defaults_type: # Type mismatch, go through acceptable conversions if spec_type == 'str' and defaults_type == 'list': spec[option] = [spec[option]] else: raise RTSLibError("Wrong type for option '%s' in %s. " % (option, spec_file) + "Expected type '%s' and got '%s'." % (defaults_type, spec_type)) # Generate the list of fixed WWNs if not empty wwn_list = None wwn_type = spec['wwn_type'] if spec['wwn_from_files']: for wwn_pattern in spec['wwn_from_files']: for wwn_file in glob.iglob(wwn_pattern): wwns_in_file = [wwn for wwn in re.split('\t|\0|\n| ', fread(wwn_file)) if wwn.strip()] if spec['wwn_from_files_filter']: wwns_filtered = [] for wwn in wwns_in_file: filter = "echo %s|%s" \ % (wwn, spec['wwn_from_files_filter']) wwns_filtered.append(exec_argv(filter, shell=True)) else: wwns_filtered = wwns_in_file if wwn_list is None: wwn_list = set([]) wwn_list.update(set([wwn for wwn in wwns_filtered if is_valid_wwn(wwn_type, wwn) if wwn] )) if spec['wwn_from_cmds']: for wwn_cmd in spec['wwn_from_cmds']: cmd_result = exec_argv(wwn_cmd, shell=True) wwns_from_cmd = [wwn for wwn in re.split('\t|\0|\n| ', cmd_result) if wwn.strip()] if spec['wwn_from_cmds_filter']: wwns_filtered = [] for wwn in wwns_from_cmd: filter = "echo %s|%s" \ % (wwn, spec['wwn_from_cmds_filter']) wwns_filtered.append(exec_argv(filter, shell=True)) else: wwns_filtered = wwns_from_cmd if wwn_list is None: wwn_list = set([]) wwn_list.update(set([wwn for wwn in wwns_filtered if is_valid_wwn(wwn_type, wwn) if wwn] )) spec['wwn_list'] = wwn_list return spec