Skip to content

Commit bf1607a

Browse files
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 <[email protected]>
1 parent 361cbd4 commit bf1607a

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

qa/tasks/cephadm.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,18 @@ def stop(ctx, config):
14201420
yield
14211421

14221422

1423+
def _expand_roles(ctx, config):
1424+
if 'all-roles' in config and len(config) == 1:
1425+
a = config['all-roles']
1426+
roles = teuthology.all_roles(ctx.cluster)
1427+
config = dict((id_, a) for id_ in roles if not id_.startswith('host.'))
1428+
elif 'all-hosts' in config and len(config) == 1:
1429+
a = config['all-hosts']
1430+
roles = teuthology.all_roles(ctx.cluster)
1431+
config = dict((id_, a) for id_ in roles if id_.startswith('host.'))
1432+
return config
1433+
1434+
14231435
def shell(ctx, config):
14241436
"""
14251437
Execute (shell) commands
@@ -1432,15 +1444,7 @@ def shell(ctx, config):
14321444
for k in config.pop('volumes', []):
14331445
args.extend(['-v', k])
14341446

1435-
if 'all-roles' in config and len(config) == 1:
1436-
a = config['all-roles']
1437-
roles = teuthology.all_roles(ctx.cluster)
1438-
config = dict((id_, a) for id_ in roles if not id_.startswith('host.'))
1439-
elif 'all-hosts' in config and len(config) == 1:
1440-
a = config['all-hosts']
1441-
roles = teuthology.all_roles(ctx.cluster)
1442-
config = dict((id_, a) for id_ in roles if id_.startswith('host.'))
1443-
1447+
config = _expand_roles(ctx, config)
14441448
config = _template_transform(ctx, config, config)
14451449
for role, cmd in config.items():
14461450
(remote,) = ctx.cluster.only(role).remotes.keys()
@@ -1481,18 +1485,8 @@ def exec(ctx, config):
14811485
TODO: this should probably be moved out of cephadm.py as it's pretty generic.
14821486
"""
14831487
assert isinstance(config, dict), "task exec got invalid config"
1484-
14851488
testdir = teuthology.get_testdir(ctx)
1486-
1487-
if 'all-roles' in config and len(config) == 1:
1488-
a = config['all-roles']
1489-
roles = teuthology.all_roles(ctx.cluster)
1490-
config = dict((id_, a) for id_ in roles if not id_.startswith('host.'))
1491-
elif 'all-hosts' in config and len(config) == 1:
1492-
a = config['all-hosts']
1493-
roles = teuthology.all_roles(ctx.cluster)
1494-
config = dict((id_, a) for id_ in roles if id_.startswith('host.'))
1495-
1489+
config = _expand_roles(ctx, config)
14961490
for role, ls in config.items():
14971491
(remote,) = ctx.cluster.only(role).remotes.keys()
14981492
log.info('Running commands on role %s host %s', role, remote.name)

0 commit comments

Comments
 (0)