def test_config_class_valid(): """Should return the config. Multiple Config with the same path should return the same object.""" config_file = get_fixture_path(os.path.join('config', 'valid', 'config.yaml')) config1 = cumin.Config(config=config_file) assert 'log_file' in config1 config2 = cumin.Config(config=config_file) assert config1 is config2
def test_config_class_valid_with_aliases(): """Should return the config including the aliases.""" config = cumin.Config(config=get_fixture_path(os.path.join('config', 'valid_with_aliases', 'config.yaml'))) assert 'log_file' in config assert 'aliases' in config assert 'role1' in config['aliases'] assert config['aliases']['role1'] == 'P{R:Class = Role::Role1}' assert 'group1' in config['aliases'] assert config['aliases']['group1'] == 'D{host10[10-22].example.org}'
def get_all_hosts(projects): """ Return the Cumin query with the list of projects that have NFS configured :param projects: set of projects to gather hosts for :return String """ config = cumin.Config() projects_q = ["O{{project:{}}}".format(project) for project in projects] cumin_query = "({})".format(" or ".join(projects_q)) return query.Query(config).execute(cumin_query)
def main(argv=None): """CLI entry point. Execute commands on hosts according to arguments. Arguments: argv: the list of command line arguments to use. If not specified it will be automatically taken from sys.argv [optional, default: None] """ if argv is None: argv = sys.argv[1:] signal.signal(signal.SIGINT, sigint_handler) # Setup try: args = parse_args(argv) user = get_running_user() config = cumin.Config(args.config) validate_config(config) setup_logging(config['log_file'], debug=args.debug, trace=args.trace) except cumin.CuminError as e: stderr(e) return 2 except Exception as e: # pylint: disable=broad-except stderr('Caught {name} exception: {msg}'.format( name=e.__class__.__name__, msg=e)) return 3 # Override config with command line arguments if args.backend is not None: config['default_backend'] = args.backend if args.transport is not None: config['transport'] = args.transport logger.info("Cumin called by user '%s' with args: %s", user, args) # Execution try: exit_code = run(args, config) except KeyboardInterruptError: stderr('Execution interrupted by Ctrl+c/SIGINT/Aborted') exit_code = 98 except Exception as e: # pylint: disable=broad-except stderr('Caught {name} exception: {msg}'.format( name=e.__class__.__name__, msg=e)) logger.exception('Failed to execute') exit_code = 99 return exit_code
def get_all_hosts(share): """ Return the Cumin query with the list of projects that have NFS configured :param share: NFS Share to filter for :return String """ config = cumin.Config() with open(NFS_MOUNT_FILE, 'r') as f: nfs_config = yaml.safe_load(f) projects = [] for project, mounts in nfs_config['private'].items(): if share == 'all' or share in mounts['mounts']: projects.append('O{project:%s}' % project) cumin_query = '({})'.format(' or '.join(projects)) return query.Query(config).execute(cumin_query)
PHAB_COMMENT_PRE = ('Script wmf-auto-reimage was launched by {user} on ' '{hostname} for hosts:\n```\n{hosts}\n```\n' 'The log can be found in `{log}`.') PHAB_COMMENT_POST = 'Completed auto-reimage of hosts:\n```\n{hosts}\n```\n' PHAB_COMMENT_POST_SUCCESS = 'and were **ALL** successful.\n' PHAB_COMMENT_POST_FAILED = 'Of which those **FAILED**:\n```\n{failed}\n```\n' WATCHER_LONG_SLEEP = 60 # Seconds to sleep between loops after the threshold WATCHER_LOG_LOOPS = 5 # Log progress after this number of long sleep loops PHAB_TASK_PATTERN = re.compile('^T[0-9]+$') HOSTS_PATTERN = re.compile('^[a-z0-9.-]+$') logger = logging.getLogger('wmf-auto-reimage') cumin_config = cumin.Config() cumin_config_installer = cumin.Config('/etc/cumin/config-installer.yaml') safe_stdout = sys.stdout # Temporarily borrowed code from switchdc until we move the reimage functionality into # the spin-off from switchdc. class Confctl(object): """Get and set conftool object values.""" def __init__(self, obj_type, config='/etc/conftool/config.yaml', schema='/etc/conftool/schema.yaml'): self._schema = loader.Schema.from_file(schema) self.entity = self._schema.entities[obj_type] kvobject.KVObject.setup(configuration.get(config))
def config(self): if not self._config: self._config = cumin.Config() return self._config
def test_config_class_invalid_aliases(): """A CuminError is raised if one of the backend aliases is invalid.""" with pytest.raises(cumin.CuminError, match='Unable to parse configuration file'): cumin.Config(config=get_fixture_path(os.path.join('config', 'valid_with_invalid_aliases', 'config.yaml')))
def test_config_class_empty_aliases(): """The configuration is loaded also if the aliases file is empty.""" config = cumin.Config(config=get_fixture_path(os.path.join('config', 'valid_with_empty_aliases', 'config.yaml'))) assert 'log_file' in config assert 'aliases' in config assert config['aliases'] == {}
def test_config_class_invalid(): """A CuminError is raised if the configuration cannot be parsed.""" with pytest.raises(cumin.CuminError, match='Unable to parse configuration file'): cumin.Config(config=get_fixture_path(os.path.join('config', 'invalid', 'config.yaml')))
def test_config_class_empty(): """An empty dictionary is returned if the configuration is empty.""" config = cumin.Config(config=get_fixture_path(os.path.join('config', 'empty', 'config.yaml'))) assert config == {}