def gatherkeys(args):
- oldmask = os.umask(077)
+ """
+ Gather keys from any mon and store in current working directory.
+
+ Backs up keys from previous installs and stores new keys.
+ """
+ oldmask = os.umask(0o77)
try:
- # client.admin
- keyring = '/etc/ceph/{cluster}.client.admin.keyring'.format(
- cluster=args.cluster)
- r = fetch_file(
- args=args,
- frompath=keyring,
- topath='{cluster}.client.admin.keyring'.format(
- cluster=args.cluster),
- _hosts=args.mon,
- )
- if not r:
- raise exc.KeyNotFoundError(keyring, args.mon)
-
- # mon.
- keyring = '/var/lib/ceph/mon/{cluster}-{{hostname}}/keyring'.format(
- cluster=args.cluster)
- r = fetch_file(
- args=args,
- frompath=keyring,
- topath='{cluster}.mon.keyring'.format(cluster=args.cluster),
- _hosts=args.mon,
- )
- if not r:
- raise exc.KeyNotFoundError(keyring, args.mon)
-
- # bootstrap
- for what in ['osd', 'mds', 'rgw']:
- keyring = '/var/lib/ceph/bootstrap-{what}/{cluster}.keyring'.format(
- what=what,
- cluster=args.cluster)
- r = fetch_file(
- args=args,
- frompath=keyring,
- topath='{cluster}.bootstrap-{what}.keyring'.format(
- cluster=args.cluster,
- what=what),
- _hosts=args.mon,
- )
- if not r:
- if what in ['osd', 'mds']:
- raise exc.KeyNotFoundError(keyring, args.mon)
- else:
- LOG.warning(("No RGW bootstrap key found. Will not be able to "
- "deploy RGW daemons"))
+ try:
+ tmpd = tempfile.mkdtemp()
+ LOG.info("Storing keys in temp directory %s", tmpd)
+ sucess = False
+ for host in args.mon:
+ sucess = gatherkeys_with_mon(args, host, tmpd)
+ if sucess:
+ break
+ if not sucess:
+ LOG.error("Failed to connect to host:%s" ,', '.join(args.mon))
+ raise RuntimeError('Failed to connect any mon')
+ had_error = False
+ date_string = time.strftime("%Y%m%d%H%M%S")
+ for keytype in ["admin", "mds", "mon", "osd", "rgw"]:
+ filename = keytype_path_to(args, keytype)
+ tmp_path = os.path.join(tmpd, filename)
+ if not os.path.exists(tmp_path):
+ LOG.error("No key retrived for '%s'" , keytype)
+ had_error = True
+ continue
+ if not os.path.exists(filename):
+ LOG.info("Storing %s" % (filename))
+ shutil.move(tmp_path, filename)
+ continue
+ if _keyring_equivalent(tmp_path, filename):
+ LOG.info("keyring '%s' already exists" , filename)
+ continue
+ backup_keyring = "%s-%s" % (filename, date_string)
+ LOG.info("Replacing '%s' and backing up old key as '%s'", filename, backup_keyring)
+ shutil.copy(filename, backup_keyring)
+ shutil.move(tmp_path, filename)
+ if had_error:
+ raise RuntimeError('Failed to get all key types')
+ finally:
+ LOG.info("Destroy temp directory %s" %(tmpd))
+ shutil.rmtree(tmpd)
finally:
os.umask(oldmask)