From: John Mulligan Date: Tue, 27 Feb 2024 14:44:51 +0000 (-0500) Subject: qa/tasks: reduce duplicated code X-Git-Tag: v19.1.0~82^2~4 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=bd922cf657e1c68a01083e91ebf0a13b6dc16d2a;p=ceph.git qa/tasks: reduce duplicated code All `exec`-style function in teuthology appear to have a transformation block that expands names like `all-roles` and `all-hosts`. With the new cephadm.exec task that block appeared twice in cephadm.py. This change removes the duplication by creating an _expand_roles function that can be called from the command executing functions. Signed-off-by: John Mulligan (cherry picked from commit bf1607a4a14e92a745cd8d7e743e5f81b7d407b7) --- diff --git a/qa/tasks/cephadm.py b/qa/tasks/cephadm.py index 69510c645e3a1..fdc9fdcd7392f 100644 --- a/qa/tasks/cephadm.py +++ b/qa/tasks/cephadm.py @@ -1420,6 +1420,18 @@ def stop(ctx, config): yield +def _expand_roles(ctx, config): + if 'all-roles' in config and len(config) == 1: + a = config['all-roles'] + roles = teuthology.all_roles(ctx.cluster) + config = dict((id_, a) for id_ in roles if not id_.startswith('host.')) + elif 'all-hosts' in config and len(config) == 1: + a = config['all-hosts'] + roles = teuthology.all_roles(ctx.cluster) + config = dict((id_, a) for id_ in roles if id_.startswith('host.')) + return config + + def shell(ctx, config): """ Execute (shell) commands @@ -1432,15 +1444,7 @@ def shell(ctx, config): for k in config.pop('volumes', []): args.extend(['-v', k]) - if 'all-roles' in config and len(config) == 1: - a = config['all-roles'] - roles = teuthology.all_roles(ctx.cluster) - config = dict((id_, a) for id_ in roles if not id_.startswith('host.')) - elif 'all-hosts' in config and len(config) == 1: - a = config['all-hosts'] - roles = teuthology.all_roles(ctx.cluster) - config = dict((id_, a) for id_ in roles if id_.startswith('host.')) - + config = _expand_roles(ctx, config) config = _template_transform(ctx, config, config) for role, cmd in config.items(): (remote,) = ctx.cluster.only(role).remotes.keys() @@ -1481,18 +1485,8 @@ def exec(ctx, config): TODO: this should probably be moved out of cephadm.py as it's pretty generic. """ assert isinstance(config, dict), "task exec got invalid config" - testdir = teuthology.get_testdir(ctx) - - if 'all-roles' in config and len(config) == 1: - a = config['all-roles'] - roles = teuthology.all_roles(ctx.cluster) - config = dict((id_, a) for id_ in roles if not id_.startswith('host.')) - elif 'all-hosts' in config and len(config) == 1: - a = config['all-hosts'] - roles = teuthology.all_roles(ctx.cluster) - config = dict((id_, a) for id_ in roles if id_.startswith('host.')) - + config = _expand_roles(ctx, config) for role, ls in config.items(): (remote,) = ctx.cluster.only(role).remotes.keys() log.info('Running commands on role %s host %s', role, remote.name)