def set_mgmt_classes(self, mgmt_classes): """ Assigns a list of configuration management classes that can be assigned to any object, such as those used by Puppet's external_nodes feature. """ mgmt_classes_split = utils.input_string_or_list(mgmt_classes) self.mgmt_classes = utils.input_string_or_list(mgmt_classes_split)
def set_owners(self, data): """ The owners field is a comment unless using an authz module that pays attention to it, like authz_ownership, which ships with Cobbler but is off by default. """ self.owners = utils.input_string_or_list(data) return True
def set_boot_loaders(self, boot_loaders: str): """ Setter of the boot loaders. :param boot_loaders: The boot loaders for the system. """ if boot_loaders == "<<inherit>>": self.boot_loaders = "<<inherit>>" return if boot_loaders: boot_loaders_split = utils.input_string_or_list(boot_loaders) if self.profile and self.profile != "": profile = self.collection_mgr.profiles().find( name=self.profile) parent_boot_loaders = profile.get_boot_loaders() elif self.image and self.image != "": image = self.collection_mgr.images().find(name=self.image) parent_boot_loaders = image.get_boot_loaders() if not set(boot_loaders_split).issubset(parent_boot_loaders): raise CX( "Error with system %s - not all boot_loaders %s are supported %s" % (self.name, boot_loaders_split, parent_boot_loaders)) self.boot_loaders = boot_loaders_split else: self.boot_loaders = []
def boot_loaders(self, boot_loaders: list): """ Setter of the boot loaders. :param boot_loaders: The boot loaders for the profile. :raises ValueError: In case the supplied boot loaders were not a subset of the valid ones. """ if boot_loaders == enums.VALUE_INHERITED: self._boot_loaders = enums.VALUE_INHERITED return if boot_loaders: boot_loaders_split = utils.input_string_or_list(boot_loaders) distro = self.get_conceptual_parent() if distro: distro_boot_loaders = distro.boot_loaders else: distro_boot_loaders = utils.get_supported_system_boot_loaders() if not set(boot_loaders_split).issubset(distro_boot_loaders): raise ValueError("Error with profile %s - not all boot_loaders %s are supported %s" % (self.name, boot_loaders_split, distro_boot_loaders)) self._boot_loaders = boot_loaders_split else: self._boot_loaders = []
def mgmt_classes(self, mgmt_classes: list): """ Setter for the ``mgmt_classes`` property. :param mgmt_classes: The new options for the management classes of an item. """ self._mgmt_classes = utils.input_string_or_list(mgmt_classes)
def owners(self, owners: list): """ Setter for the ``owners`` property. :param owners: The new list of owners. Will not be validated for existence. """ self._owners = utils.input_string_or_list(owners)
def boot_loaders(self, boot_loaders: str): """ Setter of the boot loaders. :param boot_loaders: The boot loaders for the system. :raises CX """ if boot_loaders == enums.VALUE_INHERITED: self._boot_loaders = enums.VALUE_INHERITED return if boot_loaders: boot_loaders_split = utils.input_string_or_list(boot_loaders) if self.profile: profile = self.api.profiles().find(name=self.profile) parent_boot_loaders = profile.boot_loaders elif self.image: image = self.api.images().find(name=self.image) parent_boot_loaders = image.boot_loaders else: parent_boot_loaders = [] if not set(boot_loaders_split).issubset(parent_boot_loaders): raise CX( "Error with system \"%s\" - not all boot_loaders are supported (given: \"%s\"; supported:" "\"%s\")" % (self.name, str(boot_loaders_split), str(parent_boot_loaders))) self._boot_loaders = boot_loaders_split else: self._boot_loaders = []
def ipv6_static_routes(self, routes: list): """ TODO :param routes: """ self._ipv6_static_routes = utils.input_string_or_list(routes)
def set_files(self, files): """ Setter for the files of the object. :param files: A string or list which contains the new files. """ self.files = utils.input_string_or_list(files)
def apt_dists(self, value: Union[str, list]): """ Setter for the apt dists. :param value: The new value for ``apt_dists``. """ self._apt_dists = utils.input_string_or_list(value)
def cnames(self, cnames: list): """ TODO :param cnames: """ self._cnames = utils.input_string_or_list(cnames)
def __setattr__(self, name, value): """ This sets the value of the settings named in the args. :param name: The setting to set its value. :param value: The value of the setting "name". Must be the correct the type. :return: 0 if the action was completed successfully. No return if there is an error. :raises AttributeError: Raised if the setting with "name" has the wrong type. """ if name in DEFAULTS: try: if DEFAULTS[name][1] == "str": value = str(value) elif DEFAULTS[name][1] == "int": value = int(value) elif DEFAULTS[name][1] == "bool": value = utils.input_boolean(value) elif DEFAULTS[name][1] == "float": value = float(value) elif DEFAULTS[name][1] == "list": value = utils.input_string_or_list(value) elif DEFAULTS[name][1] == "dict": value = utils.input_string_or_dict(value)[1] except Exception as error: raise AttributeError from error self.__dict__[name] = value update_settings_file(self.to_dict()) return 0 else: pass
def set_rpm_list(self, rpms): """ Rather than mirroring the entire contents of a repository (Fedora Extras, for instance, contains games, and we probably don't want those), make it possible to list the packages one wants out of those repos, so only those packages + deps can be mirrored. """ self.rpm_list = utils.input_string_or_list(rpms)
def boot_loaders(self, boot_loaders: list): """ Setter of the boot loaders. :param boot_loaders: The boot loaders for the image. :raises TypeError: In case this was of a not allowed type. :raises ValueError: In case the str which contained the list could not be successfully split. """ # allow the magic inherit string to persist if boot_loaders == enums.VALUE_INHERITED: self._boot_loaders = enums.VALUE_INHERITED return if boot_loaders: boot_loaders_split = utils.input_string_or_list(boot_loaders) if not isinstance(boot_loaders_split, list): raise TypeError("boot_loaders needs to be of type list!") if not set(boot_loaders_split).issubset( self.supported_boot_loaders): raise ValueError( "Error with image %s - not all boot_loaders %s are supported %s" % (self.name, boot_loaders_split, self.supported_boot_loaders)) self._boot_loaders = boot_loaders_split else: self._boot_loaders = []
def set_packages(self, packages): """ Setter for the packages of the managementclass. :param packages: A string or list which contains the new packages. """ self.packages = utils.input_string_or_list(packages)
def set_boot_loaders(self, boot_loaders): """ Setter of the boot loaders. :param boot_loaders: The boot loaders for the profile. """ if boot_loaders == "<<inherit>>": self.boot_loaders = "<<inherit>>" return if boot_loaders: boot_loaders_split = utils.input_string_or_list(boot_loaders) distro = self.get_conceptual_parent() if distro: distro_boot_loaders = distro.get_boot_loaders() else: distro_boot_loaders = utils.get_supported_system_boot_loaders() if not set(boot_loaders_split).issubset(distro_boot_loaders): raise CX( "Error with profile %s - not all boot_loaders %s are supported %s" % (self.name, boot_loaders_split, distro_boot_loaders)) self.boot_loaders = boot_loaders_split else: self.boot_loaders = []
def rpm_list(self, rpms: Union[str, list]): """ Setter for the ``rpm_list`` property. :param rpms: The rpm to mirror. This may be a string or list. """ self._rpm_list = utils.input_string_or_list(rpms)
def apt_components(self, value: Union[str, list]): """ Setter for the apt command property. :param value: The new value for ``apt_components``. """ self._apt_components = utils.input_string_or_list(value)
def filter_systems_or_profiles(self, selected_items, list_type): """ Return a list of valid profile or system objects selected from all profiles or systems by name, or everything if selected_items is empty """ if list_type == 'profile': all_objs = [profile for profile in self.api.profiles()] elif list_type == 'system': all_objs = [system for system in self.api.systems()] else: utils.die(self.logger, "Invalid list_type argument: " + list_type) all_objs.sort(key=lambda profile: profile.name) # no profiles/systems selection is made, let's process everything if not selected_items: return all_objs which_objs = [] selected_list = utils.input_string_or_list(selected_items) for obj in all_objs: if obj.name in selected_list: which_objs.append(obj) selected_list.remove(obj.name) for bad_name in selected_list: self.logger.warning("WARNING: %s is not a valid %s" % (bad_name, list_type)) if not which_objs: utils.die(self.logger, "No valid systems or profiles were specified.") return which_objs
def __setattr__(self, name, value): if name in DEFAULTS: try: if DEFAULTS[name][1] == "str": value = str(value) elif DEFAULTS[name][1] == "int": value = int(value) elif DEFAULTS[name][1] == "bool": if utils.input_boolean(value): value = 1 else: value = 0 elif DEFAULTS[name][1] == "float": value = float(value) elif DEFAULTS[name][1] == "list": value = utils.input_string_or_list(value) elif DEFAULTS[name][1] == "dict": value = utils.input_string_or_dict(value)[1] except: raise AttributeError self.__dict__[name] = value if not utils.update_settings_file(self.to_dict()): raise AttributeError return 0 else: # FIXME. Not sure why __dict__ is part of name # workaround applied, ignore exception # raise AttributeError pass
def boot_loaders(self, boot_loaders: list): """ Setter of the boot loaders. :param boot_loaders: The boot loaders for the profile. :raises ValueError: In case the supplied boot loaders were not a subset of the valid ones. """ if boot_loaders == enums.VALUE_INHERITED: self._boot_loaders = enums.VALUE_INHERITED return if boot_loaders: boot_loaders_split = utils.input_string_or_list(boot_loaders) parent = self.parent if parent is not None: parent_boot_loaders = parent.boot_loaders else: self.logger.warning( "Parent of profile \"%s\" could not be found for resolving the parent bootloaders.", self.name) parent_boot_loaders = [] if not set(boot_loaders_split).issubset(parent_boot_loaders): raise CX( "Error with profile \"%s\" - not all boot_loaders are supported (given: \"%s\"; supported:" "\"%s\")" % (self.name, str(boot_loaders_split), str(parent_boot_loaders))) self._boot_loaders = boot_loaders_split else: self._boot_loaders = []
def test_input_string_or_list(test_input, expected_result): # Arrange # Act result = utils.input_string_or_list(test_input) # Assert assert expected_result == result
def owners(self, owners: list): """ TODO :param owners: :return: """ self._owners = utils.input_string_or_list(owners)
def mgmt_classes(self, mgmt_classes: list): """ Assigns a list of configuration management classes that can be assigned to any object, such as those used by Puppet's external_nodes feature. :param mgmt_classes: The new options for the management classes of an item. """ self._mgmt_classes = utils.input_string_or_list(mgmt_classes)
def set_owners(self, data): """ The owners field is a comment unless using an authz module that pays attention to it, like authz_ownership, which ships with Cobbler but is off by default. :param data: This can be a string or a list which contains all owners. """ self.owners = utils.input_string_or_list(data)
def rpm_list(self, rpms: Union[str, list]): """ Rather than mirroring the entire contents of a repository (Fedora Extras, for instance, contains games, and we probably don't want those), make it possible to list the packages one wants out of those repos, so only those packages and deps can be mirrored. :param rpms: The rpm to mirror. This may be a string or list. """ self._rpm_list = utils.input_string_or_list(rpms)
def set_apt_dists(self, value): """ Setter for the apt dists. :param value: The new value for ``apt_dists``. :return: ``True`` if everything went correctly. """ self.apt_dists = utils.input_string_or_list(value) return True
def set_ipv6_secondaries(self, addresses, interface): intf = self.__get_interface(interface) data = utils.input_string_or_list(addresses) secondaries = [] for address in data: if address == "" or utils.is_ip(address): secondaries.append(address) else: raise CX("invalid format for IPv6 IP address (%s)" % address) intf["ipv6_secondaries"] = secondaries
def set_ipv6_secondaries(self, addresses, interface): intf = self.__get_interface(interface) data = utils.input_string_or_list(addresses) secondaries = [] for address in data: if address == "" or utils.is_ip(address): secondaries.append(address) else: raise CX(_("invalid format for IPv6 IP address (%s)") % address) intf["ipv6_secondaries"] = secondaries
def ipv6_secondaries(self, addresses: list): """ TODO :param addresses: """ data = utils.input_string_or_list(addresses) secondaries = [] for address in data: if address == "" or utils.is_ip(address): secondaries.append(address) else: raise AddressValueError( "invalid format for IPv6 IP address (%s)" % address) self._ipv6_secondaries = secondaries
def __find_compare(cls, from_search, from_obj): """ Only one of the two parameters shall be given in this method. If you give both ``from_obj`` will be preferred. :param from_search: Tries to parse this str in the format as a search result string. :param from_obj: Tries to parse this str in the format of an obj str. :return: True if the comparison succeeded, False otherwise. :raises CX """ if isinstance(from_obj, str): # FIXME: fnmatch is only used for string to string comparisions which should cover most major usage, if # not, this deserves fixing from_obj_lower = from_obj.lower() from_search_lower = from_search.lower() # It's much faster to not use fnmatch if it's not needed if '?' not in from_search_lower and '*' not in from_search_lower and '[' not in from_search_lower: match = from_obj_lower == from_search_lower else: match = fnmatch.fnmatch(from_obj_lower, from_search_lower) return match else: if isinstance(from_search, str): if isinstance(from_obj, list): from_search = utils.input_string_or_list(from_search) for x in from_search: if x not in from_obj: return False return True if isinstance(from_obj, dict): (junk, from_search) = utils.input_string_or_dict( from_search, allow_multiples=True) for x in list(from_search.keys()): y = from_search[x] if x not in from_obj: return False if not (y == from_obj[x]): return False return True if isinstance(from_obj, bool): if from_search.lower() in ["true", "1", "y", "yes"]: inp = True else: inp = False if inp == from_obj: return True return False raise TypeError("find cannot compare type: %s" % type(from_obj))
def __find_compare(self, from_search, from_obj): if isinstance(from_obj, str): # FIXME: fnmatch is only used for string to string comparisions # which should cover most major usage, if not, this deserves fixing from_obj_lower = from_obj.lower() from_search_lower = from_search.lower() # it's much faster to not use fnmatch if it's not needed if '?' not in from_search_lower and '*' not in from_search_lower and '[' not in from_search_lower: match = from_obj_lower == from_search_lower else: match = fnmatch.fnmatch(from_obj_lower, from_search_lower) return match else: if isinstance(from_search, str): if isinstance(from_obj, list): from_search = utils.input_string_or_list(from_search) for x in from_search: if x not in from_obj: return False return True if isinstance(from_obj, dict): (junk, from_search) = utils.input_string_or_dict(from_search, allow_multiples=True) for x in list(from_search.keys()): y = from_search[x] if x not in from_obj: return False if not (y == from_obj[x]): return False return True if isinstance(from_obj, bool): if from_search.lower() in ["true", "1", "y", "yes"]: inp = True else: inp = False if inp == from_obj: return True return False raise CX(_("find cannot compare type: %s") % type(from_obj))
def __find_compare(self, from_search, from_obj): if isinstance(from_obj, basestring): # FIXME: fnmatch is only used for string to string comparisions # which should cover most major usage, if not, this deserves fixing if fnmatch.fnmatch(from_obj.lower(), from_search.lower()): return True else: return False else: if isinstance(from_search, basestring): if isinstance(from_obj, list): from_search = utils.input_string_or_list(from_search) for x in from_search: if x not in from_obj: return False return True if isinstance(from_obj, dict): (junk, from_search) = utils.input_string_or_dict(from_search, allow_multiples=True) for x in from_search.keys(): y = from_search[x] if x not in from_obj: return False if not (y == from_obj[x]): return False return True if isinstance(from_obj, bool): if from_search.lower() in ["true", "1", "y", "yes"]: inp = True else: inp = False if inp == from_obj: return True return False raise CX(_("find cannot compare type: %s") % type(from_obj))
def set_apt_dists(self, value): self.apt_dists = utils.input_string_or_list(value) return True
def set_apt_components(self, value): self.apt_components = utils.input_string_or_list(value)
def set_packages(self, packages): self.packages = utils.input_string_or_list(packages)
def set_ipv6_static_routes(self, routes, interface): intf = self.__get_interface(interface) data = utils.input_string_or_list(routes) intf["ipv6_static_routes"] = data return True
def set_cnames(self, cnames, interface): intf = self.__get_interface(interface) data = utils.input_string_or_list(cnames) intf["cnames"] = data return True
def set_files(self, files): self.files = utils.input_string_or_list(files)