Skip to content

Commit

Permalink
qa/tasks: reduce duplicated code
Browse files Browse the repository at this point in the history
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 <jmulligan@redhat.com>
  • Loading branch information
phlogistonjohn committed Mar 21, 2024
1 parent 361cbd4 commit bf1607a
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions qa/tasks/cephadm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bf1607a

Please sign in to comment.