Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / exec_on_cleanup.py
1 """
2 Exececute custom commands during unwind/cleanup
3 """
4 import logging
5 import contextlib
6
7 from teuthology import misc as teuthology
8 from teuthology import contextutil
9
10 log = logging.getLogger(__name__)
11
12 @contextlib.contextmanager
13 def task(ctx, config):
14     """
15     Execute commands on a given role
16
17         tasks:
18         - ceph:
19         - kclient: [client.a]
20         - exec:
21             client.a:
22               - "echo 'module libceph +p' > /sys/kernel/debug/dynamic_debug/control"
23               - "echo 'module ceph +p' > /sys/kernel/debug/dynamic_debug/control"
24         - interactive:
25
26     It stops and fails with the first command that does not return on success. It means
27     that if the first command fails, the second won't run at all.
28
29     To avoid confusion it is recommended to explicitly enclose the commands in 
30     double quotes. For instance if the command is false (without double quotes) it will
31     be interpreted as a boolean by the YAML parser.
32
33     :param ctx: Context
34     :param config: Configuration
35     """
36     try:
37         yield
38     finally:
39         log.info('Executing custom commands...')
40         assert isinstance(config, dict), "task exec got invalid config"
41
42         testdir = teuthology.get_testdir(ctx)
43
44         if 'all' in config and len(config) == 1:
45             a = config['all']
46             roles = teuthology.all_roles(ctx.cluster)
47             config = dict((id_, a) for id_ in roles)
48
49             for role, ls in config.iteritems():
50                 (remote,) = ctx.cluster.only(role).remotes.iterkeys()
51                 log.info('Running commands on role %s host %s', role, remote.name)
52                 for c in ls:
53                     c.replace('$TESTDIR', testdir)
54                     remote.run(
55                         args=[
56                             'sudo',
57                             'TESTDIR={tdir}'.format(tdir=testdir),
58                             'bash',
59                             '-c',
60                             c],
61                     )
62