-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceph-create-keys
executable file
·145 lines (127 loc) · 3.73 KB
/
ceph-create-keys
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
#!/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(),
)
wait_for_quorum(cluster=cluster, mon_id=mon_id)
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 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,
)
get_key(cluster=args.cluster, mon_id=args.id)
if __name__ == '__main__':
main()