]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Rewrite teuthology-updatekeys
authorZack Cerza <zack.cerza@inktank.com>
Thu, 18 Sep 2014 18:24:18 +0000 (12:24 -0600)
committerZack Cerza <zack.cerza@inktank.com>
Thu, 18 Sep 2014 18:45:51 +0000 (12:45 -0600)
Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
scripts/updatekeys.py
teuthology/lock.py

index d2ac6cca969ebd5bc3aec761ce092f9caf3524ad..524c9c07e6577adad7b4bc2b638d0545075db2e9 100644 (file)
@@ -1,51 +1,28 @@
-import argparse
+import docopt
 import sys
 
 import teuthology.lock
 
+doc = """
+usage: teuthology-updatekeys -h
+       teuthology-updatekeys [-v] [-t <targets> | -a | <machine> ...]
 
-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>, --targets <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)
index 83e1337a79e0efb884b157aa8a6fed644db1c7e8..aff86ff53f7e905fe75231a5410c920dea883c7d 100644 (file)
@@ -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['<machine>']:
+        machines = [misc.canonicalize_hostname(m, user=None)
+                    for m in args['<machine>']]
+    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)