/usr/sbin/ceph-create-keys is in ceph-base 12.2.4-0ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | #! /usr/bin/python
import argparse
import errno
import json
import logging
import os
import subprocess
import sys
import time
import pwd
import grp
LOG = logging.getLogger(os.path.basename(sys.argv[0]))
QUORUM_STATES = ['leader', 'peon']
def get_ceph_uid():
try:
uid = pwd.getpwnam('ceph').pw_uid
except:
uid = -1
return uid
def get_ceph_gid():
try:
gid = grp.getgrnam('ceph').gr_gid
except:
gid = -1
return gid
def wait_for_quorum(cluster, mon_id, wait_count=600):
# wait 10 minutes by default
while wait_count > 0:
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)
wait_count -= 1
continue
if out == '':
LOG.info('ceph-mon admin socket returned no data')
time.sleep(1)
wait_count -= 1
continue
try:
data = json.loads(out)
except:
LOG.info('failed to parse json %s', out)
sys.exit(errno.EINVAL)
state = data['state']
if state not in QUORUM_STATES:
LOG.info('ceph-mon is not in quorum: %r', state)
time.sleep(1)
wait_count -= 1
continue
break
if wait_count == 0:
raise SystemExit("ceph-mon was not able to join quorum within %d seconds" % wait_count)
def get_key(cluster, mon_id, wait_count=600):
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(),
)
pathdir = os.path.dirname(path)
if not os.path.exists(pathdir):
os.makedirs(pathdir)
os.chmod(pathdir, 0770)
os.chown(pathdir, get_ceph_uid(), get_ceph_gid())
while wait_count > 0:
try:
with file(tmp, 'w') as f:
os.fchmod(f.fileno(), 0600)
os.fchown(f.fileno(), get_ceph_uid(), get_ceph_gid())
LOG.info('Talking to monitor...')
args_prefix = [
"ceph",
'--connect-timeout=20',
'--cluster={cluster}'.format(cluster=cluster),
'--name=mon.',
'--keyring=/var/lib/ceph/mon/{cluster}-{mon_id}/keyring'.format(
cluster=cluster,
mon_id=mon_id,
),
]
# First try getting the key if it already exists, to handle
# the case where it exists but doesn't match the caps
# we would pass into get-or-create.
returncode = subprocess.call(
args=args_prefix + [
'auth',
'get',
'client.admin',
],
stdout=f,
)
if returncode == errno.ENOENT:
returncode = subprocess.call(
args=args_prefix + [
'auth',
'get-or-create',
'client.admin',
'mon', 'allow *',
'osd', 'allow *',
'mds', 'allow *',
'mgr', 'allow *',
],
stdout=f,
)
else:
returncode = subprocess.call(
args=args_prefix + [
'auth',
'caps',
'client.admin',
'mon', 'allow *',
'osd', 'allow *',
'mds', 'allow *',
'mgr', 'allow *',
],
stdout=f,
)
if returncode != 0:
if returncode == errno.EPERM or returncode == errno.EACCES:
LOG.info('Cannot get or create admin key, permission denied')
sys.exit(returncode)
else:
LOG.info('Cannot get or create admin key')
time.sleep(1)
wait_count -= 1
continue
os.rename(tmp, path)
break
finally:
try:
os.unlink(tmp)
except OSError as e:
if e.errno == errno.ENOENT:
pass
else:
raise
if wait_count == 0:
raise SystemExit("Could not get or create the admin key after %d seconds" % wait_count)
def bootstrap_key(cluster, type_, wait_count=600):
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',
'--connect-timeout=20',
'--cluster={cluster}'.format(cluster=cluster),
'auth',
'get-or-create',
'client.bootstrap-{type}'.format(type=type_),
'mon',
'allow profile bootstrap-{type}'.format(type=type_),
]
pathdir = os.path.dirname(path)
if not os.path.exists(pathdir):
os.makedirs(pathdir)
os.chmod(pathdir, 0770)
os.chown(pathdir, get_ceph_uid(), get_ceph_gid())
while wait_count > 0:
try:
with file(tmp, 'w') as f:
os.fchmod(f.fileno(), 0600)
os.fchown(f.fileno(), get_ceph_uid(), get_ceph_gid())
LOG.info('Talking to monitor...')
returncode = subprocess.call(
args=args,
stdout=f,
)
if returncode != 0:
if returncode == errno.EPERM or returncode == errno.EACCES:
LOG.info('Cannot get or create bootstrap key for %s, permission denied', type_)
break
else:
LOG.info('Cannot get or create bootstrap key for %s', type_)
time.sleep(1)
wait_count -= 1
continue
os.rename(tmp, path)
break
finally:
try:
os.unlink(tmp)
except OSError as e:
if e.errno == errno.ENOENT:
pass
else:
raise
if wait_count == 0:
raise SystemExit("Could not get or create %s bootstrap key after %d seconds" % (type_, wait_count))
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.add_argument(
'--timeout', '-t',
metavar='TIMEOUT',
type=int,
help='timeout in seconds to wait',
)
parser.set_defaults(
cluster='ceph',
timeout=600,
)
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, wait_count=args.timeout)
get_key(cluster=args.cluster, mon_id=args.id, wait_count=args.timeout)
bootstrap_key(
cluster=args.cluster,
type_='osd',
wait_count=args.timeout,
)
bootstrap_key(
cluster=args.cluster,
type_='rgw',
wait_count=args.timeout,
)
bootstrap_key(
cluster=args.cluster,
type_='mds',
wait_count=args.timeout,
)
bootstrap_key(
cluster=args.cluster,
type_='rbd',
wait_count=args.timeout,
)
if __name__ == '__main__':
main()
|