From: Zack Cerza Date: Thu, 18 Sep 2014 18:24:18 +0000 (-0600) Subject: Rewrite teuthology-updatekeys X-Git-Tag: 1.1.0~1159 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=d6515e2c122afc4b9997bc10be7d622bd8eaff80;p=teuthology.git Rewrite teuthology-updatekeys Signed-off-by: Zack Cerza --- diff --git a/scripts/updatekeys.py b/scripts/updatekeys.py index d2ac6cca96..524c9c07e6 100644 --- a/scripts/updatekeys.py +++ b/scripts/updatekeys.py @@ -1,51 +1,28 @@ -import argparse +import docopt import sys import teuthology.lock +doc = """ +usage: teuthology-updatekeys -h + teuthology-updatekeys [-v] [-t | -a | ...] -def main(): - status = teuthology.lock.updatekeys(parse_args()) - sys.exit(status) - +Update any hostkeys that have changed. You can list specific machines to run +on, or use -a to check all of them automatically. -def parse_args(): - parser = argparse.ArgumentParser(description=""" -Update any hostkeys that have changed. You can list specific machines -to run on, or use -a to check all of them automatically. -""") - parser.add_argument( - '-v', '--verbose', - action='store_true', - default=False, - help='be more verbose', - ) - group = parser.add_mutually_exclusive_group() - group.add_argument( - '-t', '--targets', - default=None, - help='input yaml containing targets to check', - ) - group.add_argument( - '-a', '--all', - action='store_true', - default=False, - help='update hostkeys of all machines in the db', - ) - group.add_argument( - 'machines', - metavar='MACHINES', - default=[], - nargs='*', - help='hosts to check for updated keys', - ) +positional arguments: + MACHINES hosts to check for updated keys - args = parser.parse_args() +optional arguments: + -h, --help Show this help message and exit + -v, --verbose Be more verbose + -t , --targets + Input yaml containing targets to check + -a, --all Update hostkeys of all machines in the db +""" - if not (args.all or args.targets or args.machines): - parser.print_usage() - print "{name}: error: You must specify machines to update".format( - name='teuthology-updatekeys') - sys.exit(2) - return args +def main(): + args = docopt.docopt(doc) + status = teuthology.lock.updatekeys(args) + sys.exit(status) diff --git a/teuthology/lock.py b/teuthology/lock.py index 83e1337a79..aff86ff53f 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -60,12 +60,12 @@ def get_distro_from_downburst(): log.info('Using default values for supported os_type/os_version') return default_table - + def vps_version_or_type_valid(machine_type, os_type, os_version): """ Check os-type and os-version parameters when locking a vps. Os-type will always be set (defaults to ubuntu). - + In the case where downburst does not handle list-json (an older version of downburst, for instance), a message is printed and this checking is skipped (so that this code should behave as it did before this @@ -92,7 +92,7 @@ def validate_distro_version(version, supported_versions): if version in supported_versions: return True for parts in supported_versions: - part = parts.split('(') + part = parts.split('(') if len(part) == 2: if version == part[0]: return True @@ -504,42 +504,39 @@ def ssh_keyscan(hostnames): p.wait() keys_dict = dict() + for line in p.stderr.readlines(): + if not line.startswith('#'): + log.error(line) for line in p.stdout.readlines(): host, key = line.strip().split(' ', 1) keys_dict[host] = key return keys_dict -def updatekeys(ctx): - loglevel = logging.INFO - if ctx.verbose: - loglevel = logging.DEBUG - +def updatekeys(args): + loglevel = logging.DEBUG if args['--verbose'] else logging.INFO logging.basicConfig( level=loglevel, ) - - misc.read_config(ctx) - - machines = [misc.canonicalize_hostname(m, user=None) for m in ctx.machines] - - if ctx.targets: - try: - with file(ctx.targets) as f: - g = yaml.safe_load_all(f) - for new in g: - if 'targets' in new: - for t in new['targets'].iterkeys(): - machines.append(t) - except IOError as e: - raise argparse.ArgumentTypeError(str(e)) - - return do_update_keys(machines) - - -def do_update_keys(machines): + all_ = args['--all'] + if all_: + machines = [] + elif args['']: + machines = [misc.canonicalize_hostname(m, user=None) + for m in args['']] + elif args['--targets']: + targets = args['--targets'] + with file(targets) as f: + docs = yaml.safe_load_all(f) + for doc in docs: + machines = [n for n in doc.get('targets', dict()).iterkeys()] + + return do_update_keys(machines, all_) + + +def do_update_keys(machines, all_=False): reference = list_locks(keyed_by_name=True) - if not machines: + if all_: machines = reference.keys() keys_dict = ssh_keyscan(machines) return push_new_keys(keys_dict, reference)