From 952fa24fb43afa42d34920bbe4ccbaf3deb2cb4b Mon Sep 17 00:00:00 2001 From: Travis Rhoden Date: Tue, 18 Nov 2014 20:21:37 +0000 Subject: [PATCH] Return non-zero when gatherkeys fails Immediately abort and exit when a needed key cannot be found on the list of given hosts. Define a new exception, KeyNotFoundError, and raise that from gatherkeys() when the requested file does not exist on any of the given hosts to try. Since we now raise an exception on error, there is no longer any need to return a value from gatherkeys(). Fixes: 10043 Signed-off-by: Travis Rhoden --- ceph_deploy/exc.py | 19 +++++++++++++++++++ ceph_deploy/gatherkeys.py | 37 ++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/ceph_deploy/exc.py b/ceph_deploy/exc.py index fdb1db6..a0ac09b 100644 --- a/ceph_deploy/exc.py +++ b/ceph_deploy/exc.py @@ -95,3 +95,22 @@ class GenericError(DeployError): def __str__(self): return self.message + + +class KeyNotFoundError(DeployError): + """ + Could not find keyring file + """ + def __init__(self, keyring, hosts): + self.keyring = keyring + self.hosts = hosts + + def __str__(self): + return '{doc}: {keys}'.format( + doc=self.__doc__.strip(), + keys=', '.join( + [self.keyring.format(hostname=host) + + " on host {hostname}".format(hostname=host) + for host in self.hosts] + ) + ) diff --git a/ceph_deploy/gatherkeys.py b/ceph_deploy/gatherkeys.py index 9b276bf..e3b355e 100644 --- a/ceph_deploy/gatherkeys.py +++ b/ceph_deploy/gatherkeys.py @@ -1,7 +1,7 @@ import os.path import logging -from ceph_deploy import hosts +from ceph_deploy import hosts, exc from ceph_deploy.cliutil import priority @@ -14,11 +14,10 @@ def fetch_file(args, frompath, topath, _hosts): return True else: for hostname in _hosts: - LOG.debug('Checking %s for %s', hostname, frompath) + filepath = frompath.format(hostname=hostname) + LOG.debug('Checking %s for %s', hostname, filepath) distro = hosts.get(hostname, username=args.username) - key = distro.conn.remote_module.get_file( - frompath.format(hostname=hostname) - ) + key = distro.conn.remote_module.get_file(filepath) if key is not None: LOG.debug('Got %s key from %s.', topath, hostname) @@ -26,51 +25,51 @@ def fetch_file(args, frompath, topath, _hosts): f.write(key) return True distro.conn.exit() - LOG.warning('Unable to find %s on %s', frompath, _hosts) + LOG.warning('Unable to find %s on %s', filepath, hostname) return False def gatherkeys(args): - ret = 0 - # client.admin + keyring = '/etc/ceph/{cluster}.client.admin.keyring'.format( + cluster=args.cluster) r = fetch_file( args=args, - frompath='/etc/ceph/{cluster}.client.admin.keyring'.format( - cluster=args.cluster), + frompath=keyring, topath='{cluster}.client.admin.keyring'.format( cluster=args.cluster), _hosts=args.mon, ) if not r: - ret = 1 + 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='/var/lib/ceph/mon/%s-{hostname}/keyring' % args.cluster, + frompath=keyring, topath='{cluster}.mon.keyring'.format(cluster=args.cluster), _hosts=args.mon, ) if not r: - ret = 1 + raise exc.KeyNotFoundError(keyring, args.mon) # bootstrap for what in ['osd', 'mds']: + keyring = '/var/lib/ceph/bootstrap-{what}/{cluster}.keyring'.format( + what=what, + cluster=args.cluster) r = fetch_file( args=args, - frompath='/var/lib/ceph/bootstrap-{what}/{cluster}.keyring'.format( - cluster=args.cluster, - what=what), + frompath=keyring, topath='{cluster}.bootstrap-{what}.keyring'.format( cluster=args.cluster, what=what), _hosts=args.mon, ) if not r: - ret = 1 - - return ret + raise exc.KeyNotFoundError(keyring, args.mon) @priority(40) -- 2.47.3