Ejemplo n.º 1
0
def __check_and_customize_model(model, model_context, aliases,
                                password_injector):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model, before any tokenization
    :param model_context: configuration from command-line
    :param aliases: used for validation if model changes are made
    :param password_injector: injector created to collect and tokenize passwords, possibly None
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover",
                                   model_context):
        __logger.info('WLSDPLY-06014',
                      _class_name=_class_name,
                      method_name=_method_name)

    cache = None
    if password_injector is not None:
        cache = password_injector.get_variable_cache()

        # Generate k8s create secret script, possibly using lax validation method
        if model_context.is_targetted_config():
            validation_method = model_context.get_target_configuration(
            ).get_validation_method()
            model_context.set_validation_method(validation_method)
            target_configuration_helper.generate_k8s_script(
                model_context, cache, model.get_model())

            # if target handles password substitution, clear property cache to keep out of variables file.
            if model_context.get_target_configuration().manages_credentials():
                cache.clear()

    # Apply the injectors specified in model_variable_injector.json, or in the target configuration
    variable_injector = VariableInjector(
        _program_name, model.get_model(), model_context,
        WebLogicHelper(__logger).get_actual_weblogic_version(), cache)

    inserted, variable_model, variable_file_name = variable_injector.inject_variables_keyword_file(
    )

    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context,
                              wlst_mode=__wlst_mode,
                              aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(
            model.get_model(),
            variables_file_name=variable_file_name,
            archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015',
                         ex.getLocalizedMessage(),
                         class_name=_class_name,
                         method_name=_method_name)
Ejemplo n.º 2
0
def __check_and_customize_model(model, model_context, aliases, injector):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover", model_context):
        __logger.info('WLSDPLY-06014', _class_name=_class_name, method_name=_method_name)

    cache = None
    if injector is not None:
        cache = injector.get_variable_cache()
        # Generate k8s create secret script, after that clear the dictionary to avoid showing up in the variable file
        if model_context.is_targetted_config():
            validation_method = model_context.get_target_configuration()['validation_method']
            model_context.set_validation_method(validation_method)
            target_configuration_helper.generate_k8s_script(model_context.get_kubernetes_variable_file(), cache)
            cache.clear()

    variable_injector = VariableInjector(_program_name, model.get_model(), model_context,
                     WebLogicHelper(__logger).get_actual_weblogic_version(), cache)

    inserted, variable_model, variable_file_name = \
        variable_injector.inject_variables_keyword_file()

    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context, wlst_mode=__wlst_mode, aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(model.get_model(), variables_file_name=variable_file_name,
                                        archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015', ex.getLocalizedMessage(), class_name=_class_name, method_name=_method_name)
Ejemplo n.º 3
0
def __check_and_customize_model(model, model_context, aliases,
                                credential_injector):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model, before any tokenization
    :param model_context: configuration from command-line
    :param aliases: used for validation if model changes are made
    :param credential_injector: injector created to collect and tokenize credentials, possibly None
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover",
                                   model_context):
        __logger.info('WLSDPLY-06014',
                      _class_name=_class_name,
                      method_name=_method_name)

    # target config always present in model context, default config if not declared
    target_configuration = model_context.get_target_configuration()

    # if target config declared, use the validation method it contains (lax, etc.)
    if model_context.is_targetted_config():
        validation_method = target_configuration.get_validation_method()
        model_context.set_validation_method(validation_method)

    credential_cache = None
    if credential_injector is not None:
        # filter variables or secrets that are no longer in the model
        credential_injector.filter_unused_credentials(model.get_model())

        credential_cache = credential_injector.get_variable_cache()

        # Generate k8s create secret script
        if target_configuration.uses_credential_secrets():
            target_configuration_helper.generate_k8s_script(
                model_context, credential_cache, model.get_model(),
                ExceptionType.DISCOVER)

        # create additional output after filtering, but before variables have been inserted
        if model_context.is_targetted_config():
            target_configuration_helper.create_additional_output(
                model, model_context, aliases, credential_injector,
                ExceptionType.DISCOVER)

        # if target handles credential configuration, clear property cache to keep out of variables file.
        if model_context.get_target_configuration().manages_credentials():
            credential_cache.clear()

    # Apply the injectors specified in model_variable_injector.json, or in the target configuration.
    # Include the variable mappings that were collected in credential_cache.
    variable_injector = VariableInjector(
        _program_name, model.get_model(), model_context,
        WebLogicHelper(__logger).get_actual_weblogic_version(),
        credential_cache)

    inserted, variable_model, variable_file_name = variable_injector.inject_variables_keyword_file(
    )

    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context,
                              wlst_mode=__wlst_mode,
                              aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(
            model.get_model(),
            variables_file_name=variable_file_name,
            archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015',
                         ex.getLocalizedMessage(),
                         class_name=_class_name,
                         method_name=_method_name)
Ejemplo n.º 4
0
    def walk(self):
        """
        Replace password attributes in each model file with secret tokens, and write each model.
        Generate a script to create the required secrets.
        Create any additional output specified for the target environment.
        """
        _method_name = "walk"

        model_file_name = None

        try:
            model_file_list = self.model_files.split(',')
            for model_file in model_file_list:
                self.cache.clear()
                if os.path.splitext(model_file)[1].lower() == ".yaml":
                    model_file_name = model_file
                    FileToPython(model_file_name, True).parse()

                aliases = Aliases(model_context=self.model_context,
                                  wlst_mode=WlstModes.OFFLINE)

                validator = Validator(self.model_context,
                                      aliases,
                                      wlst_mode=WlstModes.OFFLINE)

                # Just merge and validate but without substitution
                model_dictionary = cla_helper.merge_model_files(
                    model_file_name, None)

                variable_file = self.model_context.get_variable_file()
                if not os.path.exists(variable_file):
                    variable_file = None

                return_code = validator.validate_in_tool_mode(
                    model_dictionary,
                    variables_file_name=variable_file,
                    archive_file_name=None)

                if return_code == Validator.ReturnCode.STOP:
                    self._logger.severe('WLSDPLY-05705', model_file_name)
                    return VALIDATION_FAIL

                self.current_dict = model_dictionary

                self.__walk_model_section(
                    model.get_model_domain_info_key(), self.current_dict,
                    aliases.get_model_section_top_level_folder_names(
                        DOMAIN_INFO))

                self.__walk_model_section(
                    model.get_model_topology_key(), self.current_dict,
                    aliases.get_model_topology_top_level_folder_names())

                self.__walk_model_section(
                    model.get_model_resources_key(), self.current_dict,
                    aliases.get_model_resources_top_level_folder_names())

                self.current_dict = self._apply_filter_and_inject_variable(
                    self.current_dict, self.model_context, validator)

                file_name = os.path.join(self.output_dir,
                                         os.path.basename(model_file_name))
                fos = JFileOutputStream(file_name, False)
                writer = JPrintWriter(fos, True)
                pty = PythonToYaml(self.current_dict)
                pty._write_dictionary_to_yaml_file(self.current_dict, writer)
                writer.close()

            self.cache.clear()
            for key in self.secrets_to_generate:
                self.cache[key] = ''

            # use a merged, substituted, filtered model to get domain name and create additional target output.
            full_model_dictionary = cla_helper.load_model(
                _program_name, self.model_context, self._aliases, "discover",
                WlstModes.OFFLINE)

            target_configuration_helper.generate_k8s_script(
                self.model_context, self.cache, full_model_dictionary)

            # create any additional outputs from full model dictionary
            target_configuration_helper.create_additional_output(
                Model(full_model_dictionary), self.model_context,
                self._aliases, ExceptionType.VALIDATE)

        except ValidateException, te:
            self._logger.severe('WLSDPLY-20009',
                                _program_name,
                                model_file_name,
                                te.getLocalizedMessage(),
                                error=te,
                                class_name=_class_name,
                                method_name=_method_name)
            ex = exception_helper.create_compare_exception(
                te.getLocalizedMessage(), error=te)
            self._logger.throwing(ex,
                                  class_name=_class_name,
                                  method_name=_method_name)
            return VALIDATION_FAIL
    def walk(self):

        _method_name = "walk"

        model_file_name = None

        try:

            model_file_list = self.model_files.split(',')
            for model_file in model_file_list:
                self.cache.clear()
                if os.path.splitext(model_file)[1].lower() == ".yaml":
                    model_file_name = model_file
                    FileToPython(model_file_name, True).parse()

                aliases = Aliases(model_context=self.model_context, wlst_mode=WlstModes.OFFLINE)

                validator = Validator(self.model_context, aliases, wlst_mode=WlstModes.OFFLINE)

                # Just merge and validate but without substitution
                model_dictionary = cla_helper.merge_model_files(model_file_name, None)

                variable_file = self.model_context.get_variable_file()
                if not os.path.exists(variable_file):
                    variable_file=None

                return_code = validator.validate_in_tool_mode(model_dictionary,
                                                          variables_file_name=variable_file,
                                                          archive_file_name=None)

                if return_code == Validator.ReturnCode.STOP:
                    __logger.severe('WLSDPLY-05705', model_file_name)
                    return VALIDATION_FAIL

                self.current_dict = model_dictionary


                self.__walk_model_section(model.get_model_domain_info_key(), self.current_dict,
                                          aliases.get_model_section_top_level_folder_names(DOMAIN_INFO))

                self.__walk_model_section(model.get_model_topology_key(), self.current_dict,
                                          aliases.get_model_topology_top_level_folder_names())

                self.__walk_model_section(model.get_model_resources_key(), self.current_dict,
                                              aliases.get_model_resources_top_level_folder_names())

                self.current_dict = self._apply_filter_and_inject_variable(self.current_dict, self.model_context,
                                                                           validator)


                file_name = os.path.join(self.output_dir, os.path.basename(model_file_name))
                fos = JFileOutputStream(file_name, False)
                writer = JPrintWriter(fos, True)
                pty = PythonToYaml(self.current_dict)
                pty._write_dictionary_to_yaml_file(self.current_dict, writer)
                writer.close()

            self.cache.clear()
            for key in self.secrets_to_generate:
                self.cache[key] = ''

            target_configuration_helper.generate_k8s_script(self.model_context.get_kubernetes_variable_file(),
                                                            self.cache)

        except ValidateException, te:
            __logger.severe('WLSDPLY-20009', _program_name, model_file_name, te.getLocalizedMessage(),
                            error=te, class_name=_class_name, method_name=_method_name)
            ex = exception_helper.create_compare_exception(te.getLocalizedMessage(), error=te)
            __logger.throwing(ex, class_name=_class_name, method_name=_method_name)
            return VALIDATION_FAIL