Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / blktrace.py
1 """
2 Run blktrace program through teuthology
3 """
4 import contextlib
5 import logging
6
7 from teuthology import misc as teuthology
8 from teuthology import contextutil
9 from teuthology.orchestra import run
10
11 log = logging.getLogger(__name__)
12 blktrace = '/usr/sbin/blktrace'
13 daemon_signal = 'term'
14
15 @contextlib.contextmanager
16 def setup(ctx, config):
17     """
18     Setup all the remotes
19     """
20     osds = ctx.cluster.only(teuthology.is_type('osd', config['cluster']))
21     log_dir = '{tdir}/archive/performance/blktrace'.format(tdir=teuthology.get_testdir(ctx))
22
23     for remote, roles_for_host in osds.remotes.iteritems():
24         log.info('Creating %s on %s' % (log_dir, remote.name))
25         remote.run(
26             args=['mkdir', '-p', '-m0755', '--', log_dir],
27             wait=False,
28             )
29     yield
30
31 @contextlib.contextmanager
32 def execute(ctx, config):
33     """
34     Run the blktrace program on remote machines.
35     """
36     procs = []
37     testdir = teuthology.get_testdir(ctx)
38     log_dir = '{tdir}/archive/performance/blktrace'.format(tdir=testdir)
39
40     osds = ctx.cluster.only(teuthology.is_type('osd'))
41     for remote, roles_for_host in osds.remotes.iteritems():
42         roles_to_devs = ctx.disk_config.remote_to_roles_to_dev[remote]
43         for role in teuthology.cluster_roles_of_type(roles_for_host, 'osd',
44                                                      config['cluster']):
45             if roles_to_devs.get(role):
46                 dev = roles_to_devs[role]
47                 log.info("running blktrace on %s: %s" % (remote.name, dev))
48
49                 proc = remote.run(
50                     args=[
51                         'cd',
52                         log_dir,
53                         run.Raw(';'),
54                         'daemon-helper',
55                         daemon_signal,
56                         'sudo',
57                         blktrace,
58                         '-o',
59                         dev.rsplit("/", 1)[1],
60                         '-d',
61                         dev,
62                         ],
63                     wait=False,
64                     stdin=run.PIPE,
65                     )
66                 procs.append(proc)
67     try:
68         yield
69     finally:
70         osds = ctx.cluster.only(teuthology.is_type('osd'))
71         log.info('stopping blktrace processs')
72         for proc in procs:
73             proc.stdin.close()
74
75 @contextlib.contextmanager
76 def task(ctx, config):
77     """
78     Usage:
79         blktrace:
80
81     or:
82         blktrace:
83           cluster: backup
84
85     Runs blktrace on all osds in the specified cluster (the 'ceph' cluster by
86     default).
87     """
88     if config is None:
89         config = {}
90     config['cluster'] = config.get('cluster', 'ceph')
91
92     with contextutil.nested(
93         lambda: setup(ctx=ctx, config=config),
94         lambda: execute(ctx=ctx, config=config),
95         ):
96         yield