def test_config(config): # Get config from yaml file yaml_config = get_yaml(config) # if the file is empty, which is still valid yaml, set as an empty dict yaml_config = {} if not yaml_config else prune_nones(yaml_config) # Voluptuous can't verify the schema of a dict if it doesn't have keys, # so make sure the keys are at least there and are dict() for k in ['client', 'logging']: if k not in yaml_config: yaml_config[k] = {} else: yaml_config[k] = prune_nones(yaml_config[k]) return SchemaCheck(yaml_config, config_file.client(), 'Client Configuration', 'full configuration dictionary').result()
def config_override(ctx, config_dict): """Override file-based and default config options with command-line provided ones""" if config_dict is None: config_dict = {} for k in ['client', 'logging']: if not k in config_dict: config_dict[k] = {} for k in list(ctx.params.keys()): if k in ['dry_run', 'config']: pass elif k == 'host': if 'host' in ctx.params and ctx.params['host'] is not None: config_dict['client']['hosts'] = ctx.params[k] elif k in ['loglevel', 'logfile', 'logformat', 'ecs']: if k in ctx.params and ctx.params[k] is not None: config_dict['logging'][k] = ctx.params[k] else: if k in ctx.params and ctx.params[k] is not None: config_dict['client'][k] = ctx.params[k] # After override, prune the nones for k in ['client', 'logging']: config_dict[k] = prune_nones(config_dict[k]) return SchemaCheck(config_dict, config_file.client(), 'Client Configuration', 'full configuration dictionary').result()
def option_schema_check(action, option_dict): clean_options = SchemaCheck( prune_nones(option_dict), options.get_schema(action), 'options', '{0} singleton action "options"'.format(action) ).result() return prune_excluded(clean_options)
def check_options(self, option_dict): try: self.logger.debug( 'Validating provided options: {0}'.format(option_dict)) # Kludgy work-around to needing 'repository' in options for these actions # but only to pass the schema check. It's removed again below. if self.action in ['delete_snapshots', 'restore']: option_dict['repository'] = self.repository _ = SchemaCheck( prune_nones(option_dict), options.get_schema(self.action), 'options', '{0} singleton action "options"'.format(self.action)).result() self.options = self.prune_excluded(_) # Remove this after the schema check, as the action class won't need it as an arg if self.action in ['delete_snapshots', 'restore']: del self.options['repository'] except ConfigurationError as e: self.logger.critical('Unable to parse options: {0}'.format(e)) sys.exit(1)
def check_options(self, option_dict): try: self.logger.debug('Validating provided options: {0}'.format(option_dict)) # Kludgy work-around to needing 'repository' in options for these actions # but only to pass the schema check. It's removed again below. if self.action in ['delete_snapshots', 'restore']: option_dict['repository'] = self.repository _ = SchemaCheck( prune_nones(option_dict), options.get_schema(self.action), 'options', '{0} singleton action "options"'.format(self.action) ).result() self.options = self.prune_excluded(_) # Remove this after the schema check, as the action class won't need it as an arg if self.action in ['delete_snapshots', 'restore']: del self.options['repository'] except ConfigurationError as e: self.logger.critical('Unable to parse options: {0}'.format(e)) sys.exit(1)
def option_schema_check(action, option_dict): """Validate command-line options against the option schema""" clean_options = SchemaCheck( prune_nones(option_dict), options.get_schema(action), 'options', '{0} singleton action "options"'.format(action)).result() return prune_excluded(clean_options)
def process_action(client, config, **kwargs): """ Do the `action` in the configuration dictionary, using the associated args. Other necessary args may be passed as keyword arguments :arg config: An `action` dictionary. """ logger = logging.getLogger(__name__) # Make some placeholder variables here for readability logger.debug('Configuration dictionary: {0}'.format(config)) logger.debug('kwargs: {0}'.format(kwargs)) action = config['action'] # This will always have some defaults now, so no need to do the if... # # OLD WAY: opts = config['options'] if 'options' in config else {} opts = config['options'] logger.debug('opts: {0}'.format(opts)) mykwargs = {} action_class = CLASS_MAP[action] # Add some settings to mykwargs... if action == 'delete_indices': mykwargs['master_timeout'] = ( kwargs['master_timeout'] if 'master_timeout' in kwargs else 30) ### Update the defaults with whatever came with opts, minus any Nones mykwargs.update(prune_nones(opts)) logger.debug('Action kwargs: {0}'.format(mykwargs)) ### Set up the action ### if action == 'alias': # Special behavior for this action, as it has 2 index lists logger.debug('Running "{0}" action'.format(action.upper())) action_obj = action_class(**mykwargs) removes = IndexList(client) adds = IndexList(client) if 'remove' in config: logger.debug( 'Removing indices from alias "{0}"'.format(opts['name'])) removes.iterate_filters(config['remove']) action_obj.remove( removes, warn_if_no_indices= opts['warn_if_no_indices']) if 'add' in config: logger.debug('Adding indices to alias "{0}"'.format(opts['name'])) adds.iterate_filters(config['add']) action_obj.add(adds, warn_if_no_indices=opts['warn_if_no_indices']) elif action in [ 'cluster_routing', 'create_index', 'rollover']: action_obj = action_class(client, **mykwargs) elif action == 'delete_snapshots' or action == 'restore': logger.debug('Running "{0}"'.format(action)) slo = SnapshotList(client, repository=opts['repository']) slo.iterate_filters(config) # We don't need to send this value to the action mykwargs.pop('repository') action_obj = action_class(slo, **mykwargs) else: logger.debug('Running "{0}"'.format(action.upper())) ilo = IndexList(client) ilo.iterate_filters(config) action_obj = action_class(ilo, **mykwargs) ### Do the action if 'dry_run' in kwargs and kwargs['dry_run'] == True: action_obj.do_dry_run() else: logger.debug('Doing the action here.') action_obj.do_action()