class PythonToYaml(object): """ A class that converts a Python dictionary into Yaml and writes the output to a file. """ _class_name = 'PythonToYaml' # 4 spaces _indent_unit = ' ' _requires_quotes_chars_regex = '[:{}\[\],&*#?|<>=!%@`-]' def __init__(self, dictionary): # Fix error handling for None self._dictionary = dictionary self._logger = PlatformLogger('wlsdeploy.yaml') self._hyphenate_lists = False return def write_to_yaml_file(self, file_name): """ Convert the Python dictionary to Yaml and write it to the specified file. :param file_name: the file name to which to write the Yaml output :return: The canonical java.io.File object for the Yaml File :raises: YamlException: if an error occurs while converting the dictionary to Yaml or writing to the file """ _method_name = 'writeToYamlFile' self._logger.entering(file_name, class_name=self._class_name, method_name=_method_name) try: yaml_file = JFileUtils.validateWritableFile(file_name) except JIllegalArgumentException, iae: yaml_ex = exception_helper.create_yaml_exception( 'WLSDPLY-18009', file_name, iae.getLocalizedMessage(), error=iae) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=yaml_ex) raise yaml_ex fos = None writer = None try: fos = JFileOutputStream(yaml_file, False) writer = JPrintWriter(fos, True) self._write_dictionary_to_yaml_file(self._dictionary, writer) except JFileNotFoundException, fnfe: yaml_ex = exception_helper.create_yaml_exception( 'WLSDPLY-18010', file_name, fnfe.getLocalizedMessage(), error=fnfe) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=yaml_ex) self._close_streams(fos, writer) raise yaml_ex
class PythonToJson(object): """ This class writes a Python dictionary out in a JSON format. """ _class_name = 'PythonToJson' # 4 spaces of indent _indent_unit = ' ' def __init__(self, dictionary): # Fix error handling for None self._dictionary = dictionary self._logger = PlatformLogger('wlsdeploy.json') return def write_to_json_file(self, file_name): """ Convert the Python dictionary to JSON and write it to the specified file. :param file_name: the name of the file :return: the java.io.File object of the JSON file """ _method_name = 'writeToJsonFile' self._logger.entering(file_name, class_name=self._class_name, method_name=_method_name) try: json_file = JFileUtils.validateWritableFile(file_name) except JIllegalArgumentException, iae: json_ex = exception_helper.create_json_exception( 'WLSDPLY-18015', file_name, iae.getLocalizedMessage(), error=iae) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) raise json_ex fos = None writer = None try: fos = JFileOutputStream(json_file, False) writer = JPrintWriter(fos, True) self._write_dictionary_to_json_file(self._dictionary, writer) except JFileNotFoundException, fnfe: json_ex = exception_helper.create_json_exception( 'WLSDPLY-18010', file_name, fnfe.getLocalizedMessage(), error=fnfe) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) self._close_streams(fos, writer) raise json_ex
def create_json_file(dictionary, file_name): """ Create a json file from the provided dictionary and store at the location specified by the file name. :param dictionary: information to persist in json format :param file_name: path and name of the file to persist """ _method_name = 'create_json_file' __logger.entering(file_name, class_name=CLASS_NAME, method_name=_method_name) try: fos = JFileOutputStream(file_name, False) except (FileNotFoundException, SecurityException), fe: __logger.warning('WLSDPLYST-01315', file_name, fe.getLocalizedMessage(), class_name=CLASS_NAME, method_name=_method_name) return
def main(): """ The main entry point for the discoverDomain tool. :param args: the command-line arguments """ _method_name = 'main' _logger.entering(class_name=_class_name, method_name=_method_name) for index, arg in enumerate(sys.argv): _logger.finer('sys.argv[{0}] = {1}', str(index), str(arg), class_name=_class_name, method_name=_method_name) _outputdir = None try: model_context = __process_args(sys.argv) _outputdir = model_context.get_compare_model_output_dir() model1 = model_context.get_trailing_argument(0) model2 = model_context.get_trailing_argument(1) for f in [model1, model2]: if not os.path.exists(f): raise CLAException("Model %s does not exists" % f) if os.path.isdir(f): raise CLAException("Model %s is a directory" % f) model1_file = JFile(model1) model2_file = JFile(model2) if not (FileUtils.isYamlFile(model1_file) or FileUtils.isJsonFile(model1_file)): raise CLAException("Model extension must be either yaml or json") if not (FileUtils.isYamlFile(model1_file) and FileUtils.isYamlFile(model2_file) or FileUtils.isJsonFile(model1_file) and FileUtils.isJsonFile(model2_file)): ext = os.path.splitext(model1)[1] raise CLAException("Model %s is not a %s file " % (model2, ext)) obj = ModelFileDiffer(model1, model2, model_context, _outputdir) rc = obj.compare() if rc == VALIDATION_FAIL: System.exit(2) if _outputdir: fos = None writer = None file_name = None if len(compare_msgs) > 0: try: file_name = _outputdir + '/compare_model_stdout' fos = JFileOutputStream(file_name, False) writer = JPrintWriter(fos, True) writer.println(BLANK_LINE) writer.println(BLANK_LINE) index = 1 for line in compare_msgs: msg_key = line[0] msg_value = line[1] writer.println("%s. %s" % (index, format_message(msg_key,msg_value.replace(PATH_TOKEN, "-->")))) index = index + 1 writer.println(BLANK_LINE) fos.close() 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) else:
print format_message('WLSDPLY-05706', self.current_dict_file, self.past_dict_file) print BLANK_LINE if len(net_diff.keys()) == 0: print format_message('WLSDPLY-05710') print BLANK_LINE return 0 if self.output_dir: 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()
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