summaryrefslogtreecommitdiff
path: root/src/ceph-create-keys
blob: 438e51d307679151473dac0c0ce42f822b61d481 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
import argparse
import errno
import json
import logging
import os
import subprocess
import sys
import time


log = logging.getLogger(os.path.basename(sys.argv[0]))

QUORUM_STATES = ['leader', 'peon']

def wait_for_quorum(cluster, mon_id):
    while True:
        p = subprocess.Popen(
            args=[
                'ceph',
                '--cluster={cluster}'.format(cluster=cluster),
                '--admin-daemon=/var/run/ceph/{cluster}-mon.{mon_id}.asok'.format(
                    cluster=cluster,
                    mon_id=mon_id,
                    ),
                'mon_status',
                ],
            stdout=subprocess.PIPE,
            )
        out = p.stdout.read()
        returncode = p.wait()
        if returncode != 0:
            log.info('ceph-mon admin socket not ready yet.')
            time.sleep(1)
            continue

        data = json.loads(out)
        state = data['state']
        if state not in QUORUM_STATES:
            log.info('ceph-mon is not in quorum: %r', state)
            time.sleep(1)
            continue

        break


def get_key(cluster, mon_id):
    path = '/etc/ceph/{cluster}.client.admin.keyring'.format(
        cluster=cluster,
        )
    if os.path.exists(path):
        log.info('Key exists already: %s', path)
        return
    tmp = '{path}.{pid}.tmp'.format(
        path=path,
        pid=os.getpid(),
        )
    while True:
        try:
            with file(tmp, 'w') as f:
                os.fchmod(f.fileno(), 0600)
                log.info('Talking to monitor...')
                returncode = subprocess.call(
                    args=[
                        'ceph',
                        '--cluster={cluster}'.format(cluster=cluster),
                        '--name=mon.',
                        '--keyring=/var/lib/ceph/mon/{cluster}-{mon_id}/keyring'.format(
                            cluster=cluster,
                            mon_id=mon_id,
                            ),
                        'auth',
                        'get-or-create',
                        'client.admin',
                        'mon', 'allow *',
                        'osd', 'allow *',
                        'mds', 'allow',
                        ],
                    stdout=f,
                    )
            if returncode != 0:
                log.info('Cannot get or create admin key')
                time.sleep(1)
                continue

            os.rename(tmp, path)
            break
        finally:
            try:
                os.unlink(tmp)
            except OSError as e:
                if e.errno == errno.ENOENT:
                    pass
                else:
                    raise

def bootstrap_key(cluster, type_, caps):
    path = '/var/lib/ceph/bootstrap-{type}/{cluster}.keyring'.format(
        type=type_,
        cluster=cluster,
        )
    if os.path.exists(path):
        log.info('Key exists already: %s', path)
        return
    tmp = '{path}.{pid}.tmp'.format(
        path=path,
        pid=os.getpid(),
        )

    args = [
        'ceph',
        '--cluster={cluster}'.format(cluster=cluster),
        'auth',
        'get-or-create',
        'client.bootstrap-{type}'.format(type=type_),
        ]
    for subsystem, subcaps in caps.iteritems():
        args.extend([
            subsystem,
            '; '.join(subcaps),
            ])

    while True:
        try:
            with file(tmp, 'w') as f:
                os.fchmod(f.fileno(), 0600)
                log.info('Talking to monitor...')
                returncode = subprocess.call(
                    args=args,
                    stdout=f,
                    )
            if returncode != 0:
                log.info('Cannot get or create bootstrap key for %s', type_)
                time.sleep(1)
                continue

            os.rename(tmp, path)
            break
        finally:
            try:
                os.unlink(tmp)
            except OSError as e:
                if e.errno == errno.ENOENT:
                    pass
                else:
                    raise


def parse_args():
    parser = argparse.ArgumentParser(
        description='Create Ceph client.admin key when ceph-mon is ready',
        )
    parser.add_argument(
        '-v', '--verbose',
        action='store_true', default=None,
        help='be more verbose',
        )
    parser.add_argument(
        '--cluster',
        metavar='NAME',
        help='name of the cluster',
        )
    parser.add_argument(
        '--id', '-i',
        metavar='ID',
        help='id of a ceph-mon that is coming up',
        required=True,
        )
    parser.set_defaults(
        cluster='ceph',
        )
    parser.set_defaults(
        # we want to hold on to this, for later
        prog=parser.prog,
        )
    args = parser.parse_args()
    return args


def main():
    args = parse_args()

    loglevel = logging.INFO
    if args.verbose:
        loglevel = logging.DEBUG

    logging.basicConfig(
        level=loglevel,
        )

    wait_for_quorum(cluster=args.cluster, mon_id=args.id)
    get_key(cluster=args.cluster, mon_id=args.id)
    bootstrap_key(
        cluster=args.cluster,
        type_='osd',
        caps=dict(
            mon=[
                'allow command osd create ...',
                'allow command osd crush set ...',
                r'allow command auth add * osd allow\ * mon allow\ rwx',
                'allow command mon getmap',
                ],
            ),
        )


if __name__ == '__main__':
    main()