示例#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)
def __check_and_customize_model(model, model_context, aliases):
    """
    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"):
        __logger.info('WLSDPLY-06014',
                      _class_name=_class_name,
                      method_name=_method_name)

    inserted, variable_model, variable_file_name = VariableInjector(_program_name, model.get_model(), model_context,
                                                                    WebLogicHelper(
                                                                        __logger).get_actual_weblogic_version()).\
        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)
示例#3
0
    def __init__(self,
                 model,
                 model_context,
                 aliases,
                 exception_type=ExceptionType.CREATE,
                 logger=PlatformLogger('wlsdeploy.create')):

        self.logger = logger
        self.aliases = aliases
        self._exception_type = exception_type
        self.wlst_helper = WlstHelper(exception_type)
        self.model = Model(model)
        self.model_context = model_context
        self.wls_helper = WebLogicHelper(self.logger)
        self.attribute_setter = AttributeSetter(self.aliases, self.logger,
                                                exception_type)
        self.custom_folder_helper = CustomFolderHelper(self.aliases,
                                                       self.logger,
                                                       self.model_context,
                                                       exception_type)

        # Must be initialized by the subclass since only it has
        # the knowledge required to compute the domain name.
        self.archive_helper = None
        self.files_to_extract_from_archive = list()
        return
示例#4
0
def __discover(model_context, aliases, injector, helper):
    """
    Populate the model from the domain.
    :param model_context: the model context
    :param aliases: aliases instance for discover
    :param injector: variable injector instance
    :param helper: wlst_helper instance
    :return: the fully-populated model
    :raises DiscoverException: if an error occurred while discover the domain
    """
    _method_name = '__discover'
    model = Model()
    base_location = LocationContext()
    __connect_to_domain(model_context, helper)
    try:
        _add_domain_name(base_location, aliases, helper)
        DomainInfoDiscoverer(model_context, model.get_model_domain_info(), base_location, wlst_mode=__wlst_mode,
                             aliases=aliases, variable_injector=injector).discover()
        TopologyDiscoverer(model_context, model.get_model_topology(), base_location, wlst_mode=__wlst_mode,
                           aliases=aliases, variable_injector=injector).discover()
        ResourcesDiscoverer(model_context, model.get_model_resources(), base_location, wlst_mode=__wlst_mode,
                            aliases=aliases, variable_injector=injector).discover()
        DeploymentsDiscoverer(model_context, model.get_model_app_deployments(), base_location, wlst_mode=__wlst_mode,
                              aliases=aliases, variable_injector=injector).discover()
        __discover_multi_tenant(model, model_context, base_location, aliases, injector)
    except AliasException, ae:
        wls_version = WebLogicHelper(__logger).get_actual_weblogic_version()
        wlst_mode = WlstModes.from_value(__wlst_mode)
        ex = exception_helper.create_discover_exception('WLSDPLY-06000', model_context.get_domain_name(),
                                                        model_context.get_domain_home(), wls_version, wlst_mode,
                                                        ae.getLocalizedMessage(), error=ae)
        __logger.throwing(ex, class_name=_class_name, method_name=_method_name)
        raise ex
示例#5
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)
def __inject(model, model_context):
    """
    Inject variables into the model file that is loaded into the model_context.
    :param model_context: the model context
    :return: True if variables were inserted into model: The updated model
    """
    __kwargs[variable_injector.VARIABLE_FILE_APPEND_ARG] = variable_injector.VARIABLE_FILE_UPDATE
    inserted, variable_model, variable_file_name = VariableInjector(_program_name, model, model_context,
                                                                    WebLogicHelper(
                                                                        __logger).get_actual_weblogic_version()). \
        inject_variables_keyword_file(**__kwargs)
    if inserted:
        model = Model(variable_model)
    return inserted, model
示例#7
0
def __inject(model, model_context):
    """
    Inject variables into the model file that is loaded into the model_context.
    :param model_context: the model context
    :return: True if variables were inserted into model: The updated model
    """
    version = WebLogicHelper(__logger).get_actual_weblogic_version()
    injector = VariableInjector(_program_name, model, model_context, version)

    inserted, variable_model, variable_file_name =\
        injector.inject_variables_keyword_file(append_option=variable_injector.VARIABLE_FILE_UPDATE)

    if inserted:
        model = Model(variable_model)

    return inserted, model
示例#8
0
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        cla_helper.clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    aliases = Aliases(model_context, wlst_mode=__wlst_mode)
    validate_model(model_dictionary, model_context, aliases)

    if filter_helper.apply_filters(model_dictionary, "deploy"):
        # if any filters were applied, re-validate the model
        validate_model(model_dictionary, model_context, aliases)

    try:
        model = Model(model_dictionary)
        exit_code = __deploy(model, model_context, aliases)
    except DeployException, ex:
        __logger.severe('WLSDPLY-09015',
                        _program_name,
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        cla_helper.clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    cla_helper.clean_up_temp_files()

    tool_exit.end(model_context, exit_code)
    return
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)
示例#10
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