Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / rest_api.py
1 """
2 Rest Api
3 """
4 import logging
5 import contextlib
6 import time
7
8 from teuthology import misc as teuthology
9 from teuthology import contextutil
10 from teuthology.orchestra import run
11 from teuthology.orchestra.daemon import DaemonGroup
12
13 log = logging.getLogger(__name__)
14
15
16 @contextlib.contextmanager
17 def run_rest_api_daemon(ctx, api_clients):
18     """
19     Wrapper starts the rest api daemons
20     """
21     if not hasattr(ctx, 'daemons'):
22         ctx.daemons = DaemonGroup()
23     remotes = ctx.cluster.only(teuthology.is_type('client')).remotes
24     for rems, roles in remotes.iteritems():
25         for whole_id_ in roles:
26             if whole_id_ in api_clients:
27                 id_ = whole_id_[len('clients'):]
28                 run_cmd = [
29                     'sudo',
30                     'daemon-helper',
31                     'kill',
32                     'ceph-rest-api',
33                     '-n',
34                     'client.rest{id}'.format(id=id_), ]
35                 cl_rest_id = 'client.rest{id}'.format(id=id_)
36                 ctx.daemons.add_daemon(rems, 'restapi',
37                     cl_rest_id,
38                     args=run_cmd,
39                     logger=log.getChild(cl_rest_id),
40                     stdin=run.PIPE,
41                     wait=False,
42                     )
43                 for i in range(1, 12):
44                     log.info('testing for ceph-rest-api try {0}'.format(i))
45                     run_cmd = [
46                         'wget',
47                         '-O',
48                         '/dev/null',
49                         '-q',
50                         'http://localhost:5000/api/v0.1/status'
51                     ]
52                     proc = rems.run(
53                         args=run_cmd,
54                         check_status=False
55                     )
56                     if proc.exitstatus == 0:
57                         break
58                     time.sleep(5)
59                 if proc.exitstatus != 0:
60                     raise RuntimeError('Cannot contact ceph-rest-api')
61     try:
62         yield
63
64     finally:
65         """
66         TO DO: destroy daemons started -- modify iter_daemons_of_role
67         """
68         teuthology.stop_daemons_of_type(ctx, 'restapi')
69
70 @contextlib.contextmanager
71 def task(ctx, config):
72     """
73     Start up rest-api.
74
75     To start on on all clients::
76
77         tasks:
78         - ceph:
79         - rest-api:
80
81     To only run on certain clients::
82
83         tasks:
84         - ceph:
85         - rest-api: [client.0, client.3]
86
87     or
88
89         tasks:
90         - ceph:
91         - rest-api:
92             client.0:
93             client.3:
94
95     The general flow of things here is:
96         1. Find clients on which rest-api is supposed to run (api_clients)
97         2. Generate keyring values
98         3. Start up ceph-rest-api daemons
99     On cleanup:
100         4. Stop the daemons
101         5. Delete keyring value files.
102     """
103     api_clients = []
104     remotes = ctx.cluster.only(teuthology.is_type('client')).remotes
105     log.info(remotes)
106     if config == None:
107         api_clients = ['client.{id}'.format(id=id_)
108             for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client')]
109     else:
110         api_clients = config
111     log.info(api_clients)
112     testdir = teuthology.get_testdir(ctx)
113     coverage_dir = '{tdir}/archive/coverage'.format(tdir=testdir)
114     for rems, roles in remotes.iteritems():
115         for whole_id_ in roles:
116             if whole_id_ in api_clients:
117                 id_ = whole_id_[len('client.'):]
118                 keyring = '/etc/ceph/ceph.client.rest{id}.keyring'.format(
119                         id=id_)
120                 rems.run(
121                     args=[
122                         'sudo',
123                         'adjust-ulimits',
124                         'ceph-coverage',
125                         coverage_dir,
126                         'ceph-authtool',
127                         '--create-keyring',
128                         '--gen-key',
129                         '--name=client.rest{id}'.format(id=id_),
130                         '--set-uid=0',
131                         '--cap', 'mon', 'allow *',
132                         '--cap', 'osd', 'allow *',
133                         '--cap', 'mds', 'allow',
134                         keyring,
135                         run.Raw('&&'),
136                         'sudo',
137                         'chmod',
138                         '0644',
139                         keyring,
140                         ],
141                     )
142                 rems.run(
143                     args=[
144                         'sudo',
145                         'sh',
146                         '-c',
147                         run.Raw("'"),
148                         "echo",
149                         '[client.rest{id}]'.format(id=id_),
150                         run.Raw('>>'),
151                         "/etc/ceph/ceph.conf",
152                         run.Raw("'")
153                         ]
154                     )
155                 rems.run(
156                     args=[
157                         'sudo',
158                         'sh',
159                         '-c',
160                         run.Raw("'"),
161                         'echo',
162                         'restapi',
163                         'keyring',
164                         '=',
165                         '/etc/ceph/ceph.client.rest{id}.keyring'.format(id=id_),
166                         run.Raw('>>'),
167                         '/etc/ceph/ceph.conf',
168                         run.Raw("'"),
169                         ]
170                     )
171                 rems.run(
172                     args=[
173                         'sudo',
174                         'ceph',
175                         'auth',
176                         'import',
177                         '-i',
178                         '/etc/ceph/ceph.client.rest{id}.keyring'.format(id=id_),
179                     ]
180                 )
181     with contextutil.nested(
182             lambda: run_rest_api_daemon(ctx=ctx, api_clients=api_clients),):
183         yield
184