def apply_filters(model, tool_type, model_context=None): """ Apply any filters configured for the specified tool type to the specified model. :param model: the model to be filtered :param tool_type: the name of the filter tool type :param model_context: optional, used to find target filters :return: True if any filter was applied, False otherwise :raises: BundleAwareException of the specified type: if an error occurs """ _method_name = 'apply_filters' global __filter_file_location __filter_file_location = path_utils.find_config_path('model_filters.json') filter_applied = False target_configuration = None try: filters_dictionary = {} # if target specified in model context, use the filters from target config if model_context: target_configuration = model_context.get_target_configuration() if target_configuration and 'model_filters' in target_configuration: filters_dictionary = target_configuration['model_filters'] target_path = os.path.join('targets', model_context.get_target()) # Fix the tokenized path in the filter path for filter_list in filters_dictionary: for current_filter in filters_dictionary[filter_list]: filter_path = dictionary_utils.get_element(current_filter, 'path') if (filter_path is not None) and filter_path.startswith(TARGET_CONFIG_TOKEN): filter_path = target_path + filter_path.replace(TARGET_CONFIG_TOKEN, '') current_filter['path'] = path_utils.find_config_path(filter_path) elif os.path.isfile(__filter_file_location): filters_dictionary = FileToPython(__filter_file_location).parse() else: __logger.info('WLSDPLY-20017', __filter_file_location, class_name=__class_name, method_name=_method_name) if tool_type in filters_dictionary: filter_list = filters_dictionary[tool_type] for filter in filter_list: filter_applied = _apply_filter(model, filter) or filter_applied else: __logger.info('WLSDPLY-20016', tool_type, __filter_file_location, class_name=__class_name, method_name=_method_name) except Exception, ex: __logger.severe('WLSDPLY-20018', str(ex), error=ex, class_name=__class_name, method_name=_method_name)
def __init__(self, program_name, domain_type): """ The DomainTypedef constructor. :param program_name: the name of the program create this object :param domain_type: the domain type """ _method_name = '__init__' self._logger = PlatformLogger('wlsdeploy.create') self._program_name = program_name self._domain_type = domain_type self.wls_helper = WebLogicHelper(self._logger) file_name = domain_type + self.__domain_typedef_extension self._domain_typedef_filename = path_utils.find_config_path( os.path.join('typedefs', file_name)) # No need to explicitly validate the filename since the JsonToPython constructor does that... try: json_parser = JsonToPython(self._domain_typedef_filename) self._domain_typedefs_dict = json_parser.parse() except IllegalArgumentException, iae: ex = exception_helper.create_cla_exception( 'WLSDPLY-12300', self._program_name, self._domain_type, self._domain_typedef_filename, iae.getLocalizedMessage(), error=iae) ex.setExitCode(CommandLineArgUtil.ARG_VALIDATION_ERROR_EXIT_CODE) self._logger.throwing(ex, class_name=self.__class_name, method_name=_method_name) raise ex
def _create_file(template_name, template_hash, model_context, output_dir, exception_type): """ Read the template from the resource stream, perform any substitutions, and write it to a file with the same name in the output directory. :param template_name: the name of the template file, and the output file :param template_hash: a dictionary of substitution values :param model_context: used to determine location and content for the output :param output_dir: the directory to write the output file :param exception_type: the type of exception to throw if needed """ _method_name = '_create_file' target_key = model_context.get_target() template_subdir = "targets/" + target_key + "/" + template_name template_path = path_utils.find_config_path(template_subdir) output_file = File(output_dir, template_name) __logger.info('WLSDPLY-01662', output_file, class_name=__class_name, method_name=_method_name) file_template_helper.create_file_from_file(template_path, template_hash, output_file, exception_type)
def _load_properties_file(): """ Load the properties from the WLSDEPLOY properties file into dictionary :return: tool config properties in dict format """ _method_name = 'load_properties_file' _logger.entering(class_name=_class_name, method_name=_method_name) wlsdeploy_path = path_utils.find_config_path(TOOL_PROPERTIES_FILE_NAME) result = None try: result = string_utils.load_properties(wlsdeploy_path) except IOException, ioe: _logger.warning('WLSDPLY-01570', wlsdeploy_path, ioe.getMessage(), class_name=_class_name, method_name=_method_name)
def get_target_configuration(self): """ Return the target configuration based on the target. :return: target configuration """ target_configuration = self._target if target_configuration: target_path = os.path.join('targets', target_configuration, 'target.json') target_configuration_file = path_utils.find_config_path( target_path) if os.path.exists(target_configuration_file): file_handle = open(target_configuration_file) configuration_dict = eval(file_handle.read()) return configuration_dict return None
def get_target_configuration(self): """ Return the target configuration object, based on the target name. Lazy-load this the first time it is requested. Return a default target configuration if none was specified. :return: target configuration object """ if self._target_configuration is None: configuration_dict = {} if self._target: target_path = os.path.join('targets', self._target, 'target.json') target_configuration_file = path_utils.find_config_path(target_path) if os.path.exists(target_configuration_file): file_handle = open(target_configuration_file) configuration_dict = eval(file_handle.read()) self._target_configuration = TargetConfiguration(configuration_dict) return self._target_configuration
def _create_injector_file_list(variables_dictionary, keyword_dictionary): _method_name = '_create_file_dictionary' injector_file_list = [] if CUSTOM_KEYWORD in variables_dictionary: if KEYWORD_FILES in variables_dictionary[CUSTOM_KEYWORD]: injector_file_list = variables_dictionary[CUSTOM_KEYWORD][ KEYWORD_FILES] if type(injector_file_list) != list: injector_file_list = injector_file_list.split(',') _logger.fine('WLSDPLY-19501', injector_file_list, class_name=_class_name, method_name=_method_name) else: _logger.info('WLSDPLY-19512', class_name=_class_name, method_name=_method_name) del variables_dictionary[CUSTOM_KEYWORD] for keyword in variables_dictionary: if keyword in keyword_dictionary: filename = keyword_dictionary[keyword] if filename and filename not in injector_file_list: if not os.path.isabs(filename): filename = path_utils.find_config_path( os.path.join(INJECTORS_LOCATION, filename)) injector_file_list.append(filename) _logger.finer('WLSDPLY-19508', filename, keyword, class_name=_class_name, method_name=_method_name) else: _logger.warning('WLSDPLY-19503', keyword, class_name=_class_name, method_name=_method_name) return injector_file_list
def _validate_target_arg(self, value): method_name = 'validate_kubernetes_script_file_switch' # Check if the target configuration file exists target_configuration_file = path_utils.find_config_path(os.path.join('targets', value, 'target.json')) if not os.path.exists(target_configuration_file): ex = exception_helper.create_cla_exception('WLSDPLY-01643', value, target_configuration_file) ex.setExitCode(self.ARG_VALIDATION_ERROR_EXIT_CODE) self._logger.throwing(ex, class_name=self._class_name, method_name=method_name) raise ex else: try: # verify the file is in proper format file_handle = open(target_configuration_file) config_dictionary = eval(file_handle.read()) target_configuration = TargetConfiguration(config_dictionary) validation_method = target_configuration.get_validation_method() if (validation_method is not None) and (validation_method not in ['strict', 'lax']): ex = exception_helper.create_cla_exception('WLSDPLY-01645', target_configuration_file) ex.setExitCode(self.ARG_VALIDATION_ERROR_EXIT_CODE) self._logger.throwing(ex, class_name=self._class_name, method_name=method_name) raise ex credentials_method = target_configuration.get_credentials_method() if (credentials_method is not None) and (credentials_method not in CREDENTIALS_METHODS): ex = exception_helper.create_cla_exception('WLSDPLY-01648', target_configuration_file, credentials_method, CREDENTIALS_METHOD, ', '.join(CREDENTIALS_METHODS)) ex.setExitCode(self.ARG_VALIDATION_ERROR_EXIT_CODE) self._logger.throwing(ex, class_name=self._class_name, method_name=method_name) raise ex except SyntaxError, se: ex = exception_helper.create_cla_exception('WLSDPLY-01644', target_configuration_file, se) ex.setExitCode(self.ARG_VALIDATION_ERROR_EXIT_CODE) self._logger.throwing(ex, class_name=self._class_name, method_name=method_name) raise ex
def inject_variables_keyword_file(self, append_option=None): """ Replace attribute values with variables and generate a variable dictionary. The variable replacement is driven from the values in the model variable helper file. This file can either contain the name of a replacement file, or a list of pre-defined keywords for canned replacement files. Return the variable dictionary with the variable name inserted into the model, and the value that the inserted variable replaced. :param append_option: 'append', 'update', or None :return: flag indicating insertion, the revised model, and variable file path """ _method_name = 'inject_variables_keyword_file' _logger.entering(class_name=_class_name, method_name=_method_name) # check for file location overrides from command line injector_file_override = self.__model_context.get_variable_injector_file( ) if injector_file_override is not None: variable_injector_location_file = injector_file_override _logger.info('WLSDPLY-19600', injector_file_override, class_name=_class_name, method_name=_method_name) else: variable_injector_location_file = path_utils.find_config_path( VARIABLE_INJECTOR_FILE_NAME) keywords_file_override = self.__model_context.get_variable_keywords_file( ) if keywords_file_override is not None: variable_keywords_location_file = keywords_file_override _logger.info('WLSDPLY-19601', keywords_file_override, class_name=_class_name, method_name=_method_name) else: variable_keywords_location_file = path_utils.find_config_path( VARIABLE_KEYWORDS_FILE_NAME) # discover may have passed -variable_file; inject variables may have passed -variable_properties_file variable_file_override = self.__model_context.get_variable_file() properties_file_override = self.__model_context.get_variable_properties_file( ) if variable_file_override is not None: variable_file_location = variable_file_override elif properties_file_override is not None: variable_file_location = properties_file_override _logger.info('WLSDPLY-19602', properties_file_override, class_name=_class_name, method_name=_method_name) else: variable_file_location = variables.get_default_variable_file_name( self.__model_context) if variable_file_location: variable_file_location = self._replace_tokens( variable_file_location) variables_injector_dictionary = self._load_variable_injector_file( variable_injector_location_file) keywords_dictionary = _load_keywords_file( variable_keywords_location_file) return_model = self.__original variables_inserted = False if not variable_file_location: _logger.warning('WLSDPLY-19520', variable_injector_location_file, class_name=_class_name, method_name=_method_name) else: append, stage_dictionary = _load_variable_file( variable_file_location, append_option) self.add_to_cache(stage_dictionary) if variables_injector_dictionary and keywords_dictionary: _logger.info('WLSDPLY-19533', variable_injector_location_file, class_name=_class_name, method_name=_method_name) injector_file_list = _create_injector_file_list( variables_injector_dictionary, keywords_dictionary) # perform the actual replacement of value with token variables variables_file_dictionary = self.inject_variables_keyword_dictionary( injector_file_list) if variables_file_dictionary: self.add_to_cache(variables_file_dictionary) else: _logger.fine('WLSDPLY-19532', variable_injector_location_file, class_name=_class_name, method_name=_method_name) variable_dictionary = self.get_variable_cache() if variable_dictionary is not None and len( variable_dictionary) > 0: variables_inserted = self._write_variables_file( variable_dictionary, variable_file_location, append) if variables_inserted: _logger.info('WLSDPLY-19518', variable_file_location, class_name=_class_name, method_name=_method_name) return_model = self.__model else: _logger.info('WLSDPLY-19519', class_name=_class_name, method_name=_method_name) variable_file_location = None _logger.exiting(class_name=_class_name, method_name=_method_name, result=variables_inserted) return variables_inserted, return_model, variable_file_location