summaryrefslogtreecommitdiffstats
path: root/src/ceph/qa/tasks/rest_api.py
blob: e86f77eb52596bff03720f62243d4591559a8034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Rest Api
"""
import logging
import contextlib
import time

from teuthology import misc as teuthology
from teuthology import contextutil
from teuthology.orchestra import run
from teuthology.orchestra.daemon import DaemonGroup

log = logging.getLogger(__name__)


@contextlib.contextmanager
def run_rest_api_daemon(ctx, api_clients):
    """
    Wrapper starts the rest api daemons
    """
    if not hasattr(ctx, 'daemons'):
        ctx.daemons = DaemonGroup()
    remotes = ctx.cluster.only(teuthology.is_type('client')).remotes
    for rems, roles in remotes.iteritems():
        for whole_id_ in roles:
            if whole_id_ in api_clients:
                id_ = whole_id_[len('clients'):]
                run_cmd = [
                    'sudo',
                    'daemon-helper',
                    'kill',
                    'ceph-rest-api',
                    '-n',
                    'client.rest{id}'.format(id=id_), ]
                cl_rest_id = 'client.rest{id}'.format(id=id_)
                ctx.daemons.add_daemon(rems, 'restapi',
                    cl_rest_id,
                    args=run_cmd,
                    logger=log.getChild(cl_rest_id),
                    stdin=run.PIPE,
                    wait=False,
                    )
                for i in range(1, 12):
                    log.info('testing for ceph-rest-api try {0}'.format(i))
                    run_cmd = [
                        'wget',
                        '-O',
                        '/dev/null',
                        '-q',
                        'http://localhost:5000/api/v0.1/status'
                    ]
                    proc = rems.run(
                        args=run_cmd,
                        check_status=False
                    )
                    if proc.exitstatus == 0:
                        break
                    time.sleep(5)
                if proc.exitstatus != 0:
                    raise RuntimeError('Cannot contact ceph-rest-api')
    try:
        yield

    finally:
        """
        TO DO: destroy daemons started -- modify iter_daemons_of_role
        """
        teuthology.stop_daemons_of_type(ctx, 'restapi')

@contextlib.contextmanager
def task(ctx, config):
    """
    Start up rest-api.

    To start on on all clients::

        tasks:
        - ceph:
        - rest-api:

    To only run on certain clients::

        tasks:
        - ceph:
        - rest-api: [client.0, client.3]

    or

        tasks:
        - ceph:
        - rest-api:
            client.0:
            client.3:

    The general flow of things here is:
        1. Find clients on which rest-api is supposed to run (api_clients)
        2. Generate keyring values
        3. Start up ceph-rest-api daemons
    On cleanup:
        4. Stop the daemons
        5. Delete keyring value files.
    """
    api_clients = []
    remotes = ctx.cluster.only(teuthology.is_type('client')).remotes
    log.info(remotes)
    if config == None:
        api_clients = ['client.{id}'.format(id=id_)
            for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client')]
    else:
        api_clients = config
    log.info(api_clients)
    testdir = teuthology.get_testdir(ctx)
    coverage_dir = '{tdir}/archive/coverage'.format(tdir=testdir)
    for rems, roles in remotes.iteritems():
        for whole_id_ in roles:
            if whole_id_ in api_clients:
                id_ = whole_id_[len('client.'):]
                keyring = '/etc/ceph/ceph.client.rest{id}.keyring'.format(
                        id=id_)
                rems.run(
                    args=[
                        'sudo',
                        'adjust-ulimits',
                        'ceph-coverage',
                        coverage_dir,
                        'ceph-authtool',
                        '--create-keyring',
                        '--gen-key',
                        '--name=client.rest{id}'.format(id=id_),
                        '--set-uid=0',
                        '--cap', 'mon', 'allow *',
                        '--cap', 'osd', 'allow *',
                        '--cap', 'mds', 'allow',
                        keyring,
                        run.Raw('&&'),
                        'sudo',
                        'chmod',
                        '0644',
                        keyring,
                        ],
                    )
                rems.run(
                    args=[
                        'sudo',
                        'sh',
                        '-c',
                        run.Raw("'"),
                        "echo",
                        '[client.rest{id}]'.format(id=id_),
                        run.Raw('>>'),
                        "/etc/ceph/ceph.conf",
                        run.Raw("'")
                        ]
                    )
                rems.run(
                    args=[
                        'sudo',
                        'sh',
                        '-c',
                        run.Raw("'"),
                        'echo',
                        'restapi',
                        'keyring',
                        '=',
                        '/etc/ceph/ceph.client.rest{id}.keyring'.format(id=id_),
                        run.Raw('>>'),
                        '/etc/ceph/ceph.conf',
                        run.Raw("'"),
                        ]
                    )
                rems.run(
                    args=[
                        'sudo',
                        'ceph',
                        'auth',
                        'import',
                        '-i',
                        '/etc/ceph/ceph.client.rest{id}.keyring'.format(id=id_),
                    ]
                )
    with contextutil.nested(
            lambda: run_rest_api_daemon(ctx=ctx, api_clients=api_clients),):
        yield