Ejemplo n.º 1
0
    def from_file(self, config_parser):
        """
        Initialize parameter value from config_parser.

        :param config_parser: the configparser object from which get the parameter
        """
        section_name = get_file_section_name(self.section_key,
                                             self.section_label)

        if config_parser.has_option(section_name, self.key):
            self.value = config_parser.get(section_name, self.key)
            if self.value:
                self._check_allowed_values()
                sections = []
                for section_label in self.value.split(","):
                    sections.append(
                        self.referred_section_type(
                            self.referred_section_definition,
                            self.pcluster_config,
                            section_label=section_label.strip(),
                            parent_section=self.owner_section,
                        ).from_file(config_parser=config_parser,
                                    fail_on_absence=True))
                self._add_sections(sections)

        return self
Ejemplo n.º 2
0
    def _get_file_section_names(self):
        """Return the names of the sections as represented in the configuration file."""
        file_section_names = []
        for section_key, sections in self.__sections.items():
            for _, section in sections.items():
                file_section_names.append(
                    get_file_section_name(section_key, section.label))

        return file_section_names
Ejemplo n.º 3
0
    def clean_config_parser(self, hit_cluster_section):
        """
        Clean the attached config parser from old attributes.

        This operation is needed to avoid writing back unsupported parameters (like compute_instance_type) to the
        configuration file
        :param hit_cluster_section: The new HIT cluster section
        """
        config_parser = self.pcluster_config.config_parser
        if config_parser:
            config_parser.remove_section(
                get_file_section_name("cluster", hit_cluster_section.label))
Ejemplo n.º 4
0
 def to_file(self, config_parser, write_defaults=False):
     """Set parameter in the config_parser in the right section."""
     section_name = get_file_section_name(self.section_key,
                                          self.section_label)
     if (self.value is not None
             and (write_defaults or self.value != self.get_default_value())
             and self.get_string_value()):
         _ensure_section_existence(config_parser, section_name)
         config_parser.set(section_name, self.key, self.get_string_value())
     else:
         # remove parameter from config_parser if there
         try:
             config_parser.remove_option(section_name, self.key)
         except NoSectionError:
             pass
Ejemplo n.º 5
0
    def from_file(self, config_parser, fail_on_absence=False):
        """Initialize section configuration parameters by parsing config file."""
        params_definitions = self.definition.get("params")
        section_name = get_file_section_name(self.key, self.label)

        # Only params with PUBLIC visibility can be specified in config file
        public_param_keys = set([
            key for key, definition in params_definitions.items()
            if definition.get("visibility", Visibility.PUBLIC) ==
            Visibility.PUBLIC
        ])

        if config_parser.has_section(section_name):
            for param_key, param_definition in params_definitions.items():
                param_type = param_definition.get(
                    "type", self.get_default_param_type())

                param = param_type(
                    self.key,
                    self.label,
                    param_key,
                    param_definition,
                    pcluster_config=self.pcluster_config,
                    owner_section=self,
                ).from_file(config_parser)
                self.add_param(param)

            not_valid_keys = [
                key for key, value in config_parser.items(section_name)
                if key not in public_param_keys
            ]
            if not_valid_keys:
                self.pcluster_config.error(
                    "The configuration parameter{0} '{1}' {2} not allowed in the [{3}] section"
                    .format(
                        "s" if len(not_valid_keys) > 1 else "",
                        ",".join(not_valid_keys),
                        "are" if len(not_valid_keys) > 1 else "is",
                        section_name,
                    ))
        elif fail_on_absence:
            self.pcluster_config.error(
                "Section '[{0}]' not found in the config file.".format(
                    section_name))

        return self
Ejemplo n.º 6
0
    def from_file(self, config_parser):
        """
        Initialize parameter value from config_parser.

        :param config_parser: the configparser object from which get the parameter
        """
        section_name = get_file_section_name(self.section_key,
                                             self.section_label)
        if config_parser.has_option(section_name, self.key):

            if self.section_key not in self.pcluster_config.get_global_section_keys(
            ):
                self._validate_section_label()

            self.value = config_parser.get(section_name, self.key)
            self._check_allowed_values()

        return self
Ejemplo n.º 7
0
 def _add_sections(self, sections):
     if self.referred_section_definition.get("max_resources", 1) == 1:
         # Single section management
         if len(sections) > 1:
             self.pcluster_config.error(
                 "The value of '{0}' parameter is invalid. "
                 "It can only contain a single {1} section label.".format(
                     self.key, self.referred_section_key))
         self._replace_default_section(sections[0])
     else:
         for section in sections:
             if self.pcluster_config.get_section(section.key,
                                                 section.label):
                 self.pcluster_config.error(
                     "Multiple reference to section '[{0}]'. "
                     "Only one reference to each section is allowed from the same configuration file."
                     .format(
                         get_file_section_name(section.key, section.label)))
             self.pcluster_config.add_section(section)
Ejemplo n.º 8
0
    def validate(self):
        """Call the validator function of the section and of all the parameters."""
        if self.params:
            section_name = get_file_section_name(self.key, self.label)
            LOGGER.debug("Validating section '[%s]'...", section_name)

            # validate section
            for validation_func in self.definition.get("validators", []):
                errors, warnings = validation_func(self.key, self.label,
                                                   self.pcluster_config)
                if errors:
                    self.pcluster_config.error(
                        "The section [{0}] is wrongly configured\n"
                        "{1}".format(section_name, "\n".join(errors)))
                elif warnings:
                    self.pcluster_config.warn(
                        "The section [{0}] is wrongly configured\n{1}".format(
                            section_name, "\n".join(warnings)))
                else:
                    LOGGER.debug("Section '[%s]' is valid", section_name)

            # validate items
            LOGGER.debug("Validating parameters of section '[%s]'...",
                         section_name)
            for param_key, param_definition in self.definition.get(
                    "params").items():
                param_type = param_definition.get(
                    "type", self.get_default_param_type())

                param = self.get_param(param_key)
                if param:
                    param.validate()
                else:
                    # define a default param and validate it
                    param_type(self.key, self.label, param_key,
                               param_definition,
                               self.pcluster_config).validate()
            LOGGER.debug(
                "Parameters validation of section '[%s]' completed correctly.",
                section_name)
Ejemplo n.º 9
0
    def to_file(self, config_parser, write_defaults=False):
        """Create the section and add all the parameters in the config_parser."""
        section_name = get_file_section_name(self.key, self.label)

        for param_key, param_definition in self.definition.get(
                "params").items():
            if param_definition.get("visibility",
                                    Visibility.PUBLIC) == Visibility.PUBLIC:
                param = self.get_param(param_key)
                if not param:
                    # generate a default param
                    param_type = param_definition.get(
                        "type", self.get_default_param_type())
                    param = param_type(self.key, self.label, param_key,
                                       param_definition, self.pcluster_config)

                if write_defaults or param.value != param_definition.get(
                        "default", None):
                    # add section in the config file only if at least one parameter value is different by the default
                    _ensure_section_existence(config_parser, section_name)

                param.to_file(config_parser, write_defaults)
Ejemplo n.º 10
0
    def check(self):
        """
        Check the patch against the existing cluster stack.

        All changes in the patch are checked against the existing cluster; their conditions are verified and a detailed
        report is generated. Each line of the report will contain all the details about the detected change, together
        with the corresponding reason if the change is not applicable and any action needed to unlock the problem.

        :return A tuple containing the patch applicability and the report rows.
        """
        rows = [[
            "section", "parameter", "old value", "new value", "check",
            "reason", "action_needed"
        ]]

        patch_allowed = True

        for change in self.changes:
            check_result, reason, action_needed, print_change = change.update_policy.check(
                change, self)

            if check_result != UpdatePolicy.CheckResult.SUCCEEDED:
                patch_allowed = False

            if print_change:
                section_name = get_file_section_name(change.section_key,
                                                     change.section_label)
                rows.append([
                    section_name,
                    change.param_key,
                    change.old_value,
                    change.new_value,
                    check_result.value,
                    reason,
                    action_needed,
                ])

        return patch_allowed, rows
Ejemplo n.º 11
0
    def to_file(self, config_parser, write_defaults=False):
        """Convert the param value into a section in the config_parser and initialize it."""
        section = self.pcluster_config.get_section(self.referred_section_key,
                                                   self.value)
        if section:
            # evaluate all the parameters of the section and
            # add "*_settings = *" to the parent section
            # only if at least one parameter value is different from the default
            for param_key, param_definition in self.referred_section_definition.get(
                    "params").items():
                param_value = section.get_param_value(param_key)

                section_name = get_file_section_name(self.section_key,
                                                     self.section_label)
                if not config_parser.has_option(section_name, self.key) and (
                        write_defaults or
                    (param_value != param_definition.get("default", None))):
                    _ensure_section_existence(config_parser, section_name)
                    config_parser.set(section_name, self.key,
                                      self.get_string_value())

            # create section
            section.to_file(config_parser)