def _write_to_yaml_file(self, file_name):
        """
        Convert the Python dictionary to Yaml and write it to a file.
        :param file_name: the Yaml file name
        :return: the java.io.File object
        """
        _method_name = '_write_to_yaml_file'

        from wlsdeploy.yaml.yaml_translator import PythonToYaml as JPythonToYaml
        self.logger.finer('WLSDPLY-01712',
                          'YAML',
                          file_name,
                          class_name=self._class_name,
                          method_name=_method_name)
        try:
            writer = JPythonToYaml(self.dictionary)
            writer.set_hyphenate_lists(self._hyphenate_yaml_lists)
            writer.write_to_yaml_file(file_name)
        except JYamlException, ye:
            translate_ex = exception_helper.create_translate_exception(
                'WLSDPLY-01713', file_name, ye.getLocalizedMessage(), error=ye)
            self.logger.throwing(translate_ex,
                                 class_name=self._class_name,
                                 method_name=_method_name)
            raise translate_ex
Beispiel #2
0
     fos = None
     writer = None
     file_name = None
     try:
         print format_message('WLSDPLY-05711', self.output_dir)
         print BLANK_LINE
         file_name = self.output_dir + '/diffed_model.json'
         fos = JFileOutputStream(file_name, False)
         writer = JPrintWriter(fos, True)
         json_object = PythonToJson(net_diff)
         json_object._write_dictionary_to_json_file(net_diff, writer)
         writer.close()
         file_name = self.output_dir + '/diffed_model.yaml'
         fos = JFileOutputStream(file_name, False)
         writer = JPrintWriter(fos, True)
         pty = PythonToYaml(net_diff)
         pty._write_dictionary_to_yaml_file(net_diff, writer)
         writer.close()
     except JIOException, ioe:
         if fos:
             fos.close()
         if writer:
             writer.close()
         _logger.severe('WLSDPLY-05708', file_name, ioe.getLocalizedMessage(),
                        error=ioe, class_name=_class_name, method_name=_method_name)
         return 2
 else:
     print format_message('WLSDPLY-05707')
     print BLANK_LINE
     pty = PythonToYaml(net_diff)
     pty._write_dictionary_to_yaml_file(net_diff, System.out)
    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
Beispiel #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
            return 0

        if self.output_dir:
            file_name = None
            try:
                print format_message('WLSDPLY-05711', self.output_dir)
                print BLANK_LINE

                # write the change model as a JSON file
                file_name = self.output_dir + '/diffed_model.json'
                json_object = PythonToJson(change_model)
                json_object.write_to_json_file(file_name)

                # write the change model as a YAML file
                file_name = self.output_dir + '/diffed_model.yaml'
                pty = PythonToYaml(change_model)
                pty.write_to_yaml_file(file_name)

            except YamlException, ye:
                _logger.severe('WLSDPLY-05708', file_name, ye.getLocalizedMessage(),
                               error=ye, class_name=_class_name, method_name=_method_name)
                return 2
        else:
            # write the change model to standard output in YAML format
            print format_message('WLSDPLY-05707')
            print BLANK_LINE
            pty = PythonToYaml(change_model)
            pty._write_dictionary_to_yaml_file(change_model, System.out)

        return 0