def testPrintUsageFoldersOnly(self): _method_name = 'testPrintUsageFoldersOnly' _FOLDERS_ONLY = '-folders_only' args = { '-oracle_home': os.environ['MW_HOME'] } model_paths = [ 'resources:/FileStore' ] try: # Loop through valid list of model sections for model_path in model_paths: # Set print usage context args['-print_usage'] = '%s %s' % (model_path, _FOLDERS_ONLY) self._logger.info('args={0}', str(args), class_name=self._class_name, method_name=_method_name) model_context = ModelContext(self._program_name, args) model_validator = Validator(model_context, wlst_mode=WlstModes.ONLINE) model_validator.print_usage(model_path) self.assertEquals(True, True) except ValidateException, ve: self.fail(ve.getLocalizedMessage())
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 testModelValidation(self): _method_name = 'testModelValidation' _model_file = self._resources_dir + '/variablestest.yaml' _variable_file = self._resources_dir + '/variablestest.properties' _archive_file = self._resources_dir + '/variablestest.zip' mw_home = os.environ['MW_HOME'] args_map = { '-oracle_home': mw_home, '-model_file': _model_file, '-variable_file': _variable_file, '-archive_file': _archive_file } model_context = ModelContext('ValidationTestCase', args_map) try: model_dictionary = FileToPython(model_context.get_model_file()).parse() model_validator = Validator(model_context, wlst_mode=WlstModes.ONLINE) return_code = model_validator.validate_in_tool_mode(model_dictionary, model_context.get_variable_file(), model_context.get_archive_file_name()) self._logger.info('The Validator.validate_in_tool_mode() call returned {0}', Validator.ReturnCode.from_value(return_code), class_name=self._class_name, method_name=_method_name) except TranslateException, te: return_code = Validator.ReturnCode.STOP self._logger.severe('WLSDPLY-20009', self._program_name, model_context.get_model_file(), te.getLocalizedMessage(), error=te, class_name=self._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)
def __perform_model_file_validation(model_file_name, model_context): """ :param model_file_name: :param model_context: :return: :raises ValidationException: """ _method_name = '__perform_model_file_validation' __logger.entering(model_file_name, class_name=_class_name, method_name=_method_name) try: model_dictionary = cla_helper.merge_model_files(model_file_name) model_validator = Validator(model_context, logger=__logger) model_validator.validate_in_standalone_mode( model_dictionary, model_context.get_variable_file(), model_context.get_archive_file_name()) except TranslateException, 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_validate_exception( te.getLocalizedMessage(), error=te) __logger.throwing(ex, class_name=_class_name, method_name=_method_name) raise ex
def __perform_model_file_validation(model_file_name, model_context): """ :param model_file_name: :param model_context: :return: :raises ValidationException: """ _method_name = '__perform_model_file_validation' __logger.entering(model_file_name, class_name=_class_name, method_name=_method_name) try: model_validator = Validator(model_context, logger=__logger) variable_map = model_validator.load_variables(model_context.get_variable_file()) model_dictionary = cla_helper.merge_model_files(model_file_name, variable_map) if cla_helper.check_persist_model(): persist_model_dict = copy.deepcopy(model_dictionary) variables.substitute(persist_model_dict, variable_map, model_context) cla_helper.persist_model(model_context, persist_model_dict) model_validator.validate_in_standalone_mode(model_dictionary, variable_map, model_context.get_archive_file_name()) except (TranslateException, VariableException), 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_validate_exception(te.getLocalizedMessage(), error=te) __logger.throwing(ex, class_name=_class_name, method_name=_method_name) raise ex
def validate_model(program_name, model_dictionary, model_context, aliases, wlst_mode): """ Validate the model dictionary based on the specified model context and aliases. The tool will exit if exceptions are encountered, or the validation returns a STOP code. :param program_name: the program name, for logging :param model_dictionary: the model dictionary :param model_context: the model context :param aliases: the aliases :param wlst_mode: offline or online :return: """ _method_name = 'validate_model' try: validator = Validator(model_context, aliases, wlst_mode=wlst_mode) # no need to pass the variable file for processing, substitution has already been performed return_code = validator.validate_in_tool_mode( model_dictionary, variables_file_name=None, archive_file_name=model_context.get_archive_file_name()) except ValidateException, ex: __logger.severe('WLSDPLY-20000', program_name, ex.getLocalizedMessage(), error=ex, class_name=_class_name, method_name=_method_name) clean_up_temp_files() tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
def testYamlModelValidation(self): """ Parse and validate a YAML model with '-' list type and attributes with negative values. """ _model_file = self._resources_dir + '/simple-model.yaml' _archive_file = self._resources_dir + "/SingleAppDomain.zip" _method_name = 'testYamlModelValidation' mw_home = os.environ['MW_HOME'] args_map = { '-oracle_home': mw_home, '-model_file': _model_file, '-archive_file': _archive_file } model_context = ModelContext('ValidationTestCase', args_map) try: model_dictionary = FileToPython(model_context.get_model_file()).parse() model_validator = Validator(model_context, wlst_mode=WlstModes.ONLINE) return_code = model_validator.validate_in_tool_mode(model_dictionary, model_context.get_variable_file(), model_context.get_archive_file_name()) self._logger.info('The Validator.validate_in_tool_mode() call returned {0}', Validator.ReturnCode.from_value(return_code), class_name=self._class_name, method_name=_method_name) except TranslateException, te: return_code = Validator.ReturnCode.STOP self._logger.severe('WLSDPLY-20009', self._program_name, model_context.get_model_file(), te.getLocalizedMessage(), error=te, class_name=self._class_name, method_name=_method_name)
def testPrintUsageRecursive(self): _method_name = 'testPrintUsageRecursive' _RECURSIVE = '-recursive' args = { '-oracle_home': os.environ['MW_HOME'] } model_paths = [ 'appDeployments:/Application' ] try: # Loop through valid list of model sections for model_path in model_paths: # Set print usage context args['-print_usage'] = '%s %s' % (model_path, _RECURSIVE) self._logger.info('args={0}', str(args), class_name=self._class_name, method_name=_method_name) model_context = ModelContext(self._program_name, args) model_validator = Validator(model_context, wlst_mode=WlstModes.ONLINE) model_validator.print_usage(model_path) self.assertEquals(True, True) except ValidateException, ve: self.fail(ve.getLocalizedMessage())
def validate_model(model_dictionary, model_context, aliases): _method_name = 'validate_model' try: validator = Validator(model_context, aliases, wlst_mode=__wlst_mode) # no need to pass the variable file for processing, substitution has already been performed return_code = validator.validate_in_tool_mode(model_dictionary, variables_file_name=None, archive_file_name=model_context.get_archive_file_name()) except ValidateException, ex: __logger.severe('WLSDPLY-20000', _program_name, ex.getLocalizedMessage(), error=ex, class_name=_class_name, method_name=_method_name) __clean_up_temp_files() tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
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 compare(self): """ Do the actual compare of the models. :return: whether the difference is safe for online dynamic update """ _method_name = "compare" # arguments have been verified and same extensions model_file_name = None # validate models first try: if FileUtils.isYamlFile(JFile(os.path.splitext(self.current_dict_file)[1].lower())): model_file_name = self.current_dict_file FileToPython(model_file_name, True).parse() model_file_name = self.past_dict_file FileToPython(model_file_name, True).parse() self.model_context.set_validation_method('lax') aliases = Aliases(model_context=self.model_context, wlst_mode=WlstModes.OFFLINE, exception_type=ExceptionType.COMPARE) validator = Validator(self.model_context, aliases, wlst_mode=WlstModes.OFFLINE) variable_map = validator.load_variables(self.model_context.get_variable_file()) model_file_name = self.current_dict_file model_dictionary = cla_helper.merge_model_files(model_file_name, variable_map) variables.substitute(model_dictionary, variable_map, self.model_context) # Run this utility in stand-alone mode instead of tool mode, # which has stricter checks for the tools. # An archive is not used with the compare models and if the model # references a file in an archive, the compareModel will fail if # running in the stricter tool mode (even with lax). # arg_map = dict() arg_map[CommandLineArgUtil.MODEL_FILE_SWITCH] = model_file_name model_context_copy = self.model_context.copy(arg_map) val_copy = Validator(model_context_copy, aliases, wlst_mode=WlstModes.OFFLINE) # any variables should have been substituted at this point validate_variables = {} return_code = val_copy.validate_in_standalone_mode(model_dictionary, validate_variables, archive_file_name=None) if return_code == Validator.ReturnCode.STOP: _logger.severe('WLSDPLY-05705', model_file_name) return VALIDATION_FAIL current_dict = model_dictionary model_file_name = self.past_dict_file model_dictionary = cla_helper.merge_model_files(model_file_name, variable_map) variables.substitute(model_dictionary, variable_map, self.model_context) arg_map[CommandLineArgUtil.MODEL_FILE_SWITCH] = model_file_name model_context_copy = self.model_context.copy(arg_map) val_copy = Validator(model_context_copy, aliases, wlst_mode=WlstModes.OFFLINE) return_code = val_copy.validate_in_standalone_mode(model_dictionary, validate_variables, archive_file_name=None) if return_code == Validator.ReturnCode.STOP: _logger.severe('WLSDPLY-05705', model_file_name) return VALIDATION_FAIL past_dict = model_dictionary 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
def compare(self): """ Do the actual compare of the models. :return: whether the difference is safe for online dynamic update """ _method_name = "compare" # arguments have been verified and same extensions model_file_name = None # validate models first try: if os.path.splitext(self.current_dict_file)[1].lower() == ".yaml": model_file_name = self.current_dict_file FileToPython(model_file_name, True).parse() model_file_name = self.past_dict_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) variable_map = validator.load_variables( self.model_context.get_variable_file()) model_file_name = self.current_dict_file model_dictionary = cla_helper.merge_model_files( model_file_name, variable_map) variables.substitute(model_dictionary, variable_map, self.model_context) return_code = validator.validate_in_tool_mode( model_dictionary, variables_file_name=None, archive_file_name=None) if return_code == Validator.ReturnCode.STOP: __logger.severe('WLSDPLY-05705', model_file_name) return VALIDATION_FAIL current_dict = model_dictionary model_file_name = self.past_dict_file model_dictionary = cla_helper.merge_model_files( model_file_name, variable_map) variables.substitute(model_dictionary, variable_map, self.model_context) return_code = validator.validate_in_tool_mode( model_dictionary, variables_file_name=None, archive_file_name=None) if return_code == Validator.ReturnCode.STOP: __logger.severe('WLSDPLY-05705', model_file_name) return VALIDATION_FAIL past_dict = model_dictionary 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
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)
exit_code = ex.getExitCode() if exit_code != CommandLineArgUtil.HELP_EXIT_CODE: __logger.severe('WLSDPLY-20008', _program_name, ex.getLocalizedMessage(), error=ex, class_name=_class_name, method_name=_method_name) cla_helper.clean_up_temp_files() sys.exit(exit_code) print_usage = model_context.get_print_usage() if print_usage is not None: try: model_validator = Validator(model_context, logger=__logger) model_validator.print_usage(print_usage) except ValidateException, ve: __logger.severe('WLSDPLY-05404', _program_name, ve.getLocalizedMessage(), error=ve, class_name=_class_name, method_name=_method_name) sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE) else: try: model_file_name = model_context.get_model_file() if model_file_name is not None: __perform_model_file_validation(model_file_name, model_context)
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