]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Move teuthology-lock's arg parsing to scripts/
authorZack Cerza <zack@cerza.org>
Tue, 8 Oct 2013 19:58:31 +0000 (14:58 -0500)
committerZack Cerza <zack@cerza.org>
Fri, 11 Oct 2013 00:09:34 +0000 (19:09 -0500)
Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
scripts/lock.py [new file with mode: 0644]
setup.py
teuthology/lock.py
teuthology/nuke.py
teuthology/task/internal.py

diff --git a/scripts/lock.py b/scripts/lock.py
new file mode 100644 (file)
index 0000000..0751fc4
--- /dev/null
@@ -0,0 +1,155 @@
+import argparse
+import textwrap
+
+import teuthology.lock
+
+
+def _positive_int(string):
+    value = int(string)
+    if value < 1:
+        raise argparse.ArgumentTypeError(
+            '{string} is not positive'.format(string=string))
+    return value
+
+
+def main():
+    teuthology.lock.main(parse_args())
+
+
+def parse_args():
+    parser = argparse.ArgumentParser(
+        description='Lock, unlock, or query lock status of machines',
+        epilog=textwrap.dedent('''
+            Examples:
+            teuthology-lock --summary
+            teuthology-lock --lock-many 1 --machine-type vps
+            teuthology-lock --lock -t target.yaml
+            teuthology-lock --list-targets plana01
+            teuthology-lock --list --brief --owner user@host
+            teuthology-lock --update --status down --desc testing plana01
+        '''),
+        formatter_class=argparse.RawTextHelpFormatter)
+    parser.add_argument(
+        '-v', '--verbose',
+        action='store_true',
+        default=False,
+        help='be more verbose',
+    )
+    group = parser.add_mutually_exclusive_group(required=True)
+    group.add_argument(
+        '--list',
+        action='store_true',
+        default=False,
+        help='Show lock info for machines owned by you, or only machines ' +
+        'specified. Can be restricted by --owner, --status, and --locked.',
+    )
+    group.add_argument(
+        '--list-targets',
+        action='store_true',
+        default=False,
+        help='Show lock info for all machines, or only machines specified, ' +
+        'in targets: yaml format. Can be restricted by --owner, --status, ' +
+        'and --locked.',
+    )
+    group.add_argument(
+        '--lock',
+        action='store_true',
+        default=False,
+        help='lock particular machines',
+    )
+    group.add_argument(
+        '--unlock',
+        action='store_true',
+        default=False,
+        help='unlock particular machines',
+    )
+    group.add_argument(
+        '--lock-many',
+        dest='num_to_lock',
+        type=_positive_int,
+        help='lock this many machines',
+    )
+    group.add_argument(
+        '--update',
+        action='store_true',
+        default=False,
+        help='update the description or status of some machines',
+    )
+    group.add_argument(
+        '--summary',
+        action='store_true',
+        default=False,
+        help='summarize locked-machine counts by owner',
+    )
+    parser.add_argument(
+        '-a', '--all',
+        action='store_true',
+        default=False,
+        help='list all machines, not just those owned by you',
+    )
+    parser.add_argument(
+        '--owner',
+        default=None,
+        help='owner of the lock(s) (must match to unlock a machine)',
+    )
+    parser.add_argument(
+        '-f',
+        action='store_true',
+        default=False,
+        help="don't exit after the first error, continue locking or " +
+        "unlocking other machines",
+    )
+    parser.add_argument(
+        '--desc',
+        default=None,
+        help='lock description',
+    )
+    parser.add_argument(
+        '--desc-pattern',
+        default=None,
+        help='lock description',
+    )
+    parser.add_argument(
+        '--machine-type',
+        default=None,
+        help='Type of machine to lock, valid choices: mira | plana | ' +
+        'burnupi | vps | saya | tala',
+    )
+    parser.add_argument(
+        '--status',
+        default=None,
+        choices=['up', 'down'],
+        help='whether a machine is usable for testing',
+    )
+    parser.add_argument(
+        '--locked',
+        default=None,
+        choices=['true', 'false'],
+        help='whether a machine is locked',
+    )
+    parser.add_argument(
+        '--brief',
+        action='store_true',
+        default=False,
+        help='Shorten information reported from --list',
+    )
+    parser.add_argument(
+        '-t', '--targets',
+        dest='targets',
+        default=None,
+        help='input yaml containing targets',
+    )
+    parser.add_argument(
+        'machines',
+        metavar='MACHINE',
+        default=[],
+        nargs='*',
+        help='machines to operate on',
+    )
+    parser.add_argument(
+        '--os-type',
+        default='ubuntu',
+        help='virtual machine type',
+    )
+
+    return parser.parse_args()
index 21f9bff9feb1898476c5f544d975237be78fac3f..53e3536575c8eb9916d67c6936ba3d9f70a69eb8 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,7 @@ setup(
             'teuthology-suite = scripts.suite:main',
             'teuthology-ls = scripts.ls:main',
             'teuthology-worker = scripts.worker:main',
-            'teuthology-lock = teuthology.lock:main',
+            'teuthology-lock = scripts.lock:main',
             'teuthology-schedule = teuthology.run:schedule',
             'teuthology-updatekeys = teuthology.lock:update_hostkeys',
             'teuthology-coverage = teuthology.coverage:analyze',
index 960fc5f4a2240a98c14b83ee4476963bc6416f41..a0c8ce46665edf0d34d8e6e9885468329e2e1261 100644 (file)
@@ -9,8 +9,6 @@ import collections
 import tempfile
 import os
 import time
-import textwrap
-from argparse import RawTextHelpFormatter
 
 from .config import config
 from . import lockstatus as ls
@@ -43,7 +41,7 @@ def lock_many(ctx, num, machinetype, user=None, description=None):
                     ok_machs[machine] = machines[machine]
                 else:
                     log.error('Unable to create virtual machine: %s' % machine)
-                    unlock(ctx, machine)
+                    unlock_one(ctx, machine)
             return ok_machs
         return machines
     if status == 503:
@@ -53,7 +51,7 @@ def lock_many(ctx, num, machinetype, user=None, description=None):
     return []
 
 
-def lock(ctx, name, user=None, description=None):
+def lock_one(ctx, name, user=None, description=None):
     if user is None:
         user = teuthology.get_user()
     success, _, _ = ls.send_request(
@@ -67,7 +65,7 @@ def lock(ctx, name, user=None, description=None):
     return success
 
 
-def unlock(ctx, name, user=None):
+def unlock_one(ctx, name, user=None):
     if user is None:
         user = teuthology.get_user()
     success, _, _ = ls.send_request(
@@ -117,158 +115,13 @@ def update_lock(ctx, name, description=None, status=None, sshpubkey=None):
     return True
 
 
-def _positive_int(string):
-    value = int(string)
-    if value < 1:
-        raise argparse.ArgumentTypeError(
-            '{string} is not positive'.format(string=string))
-    return value
-
-
 def canonicalize_hostname(s):
     if re.match('ubuntu@.*\.front\.sepia\.ceph\.com', s) is None:
         s = 'ubuntu@' + s + '.front.sepia.ceph.com'
     return s
 
 
-def main():
-    parser = argparse.ArgumentParser(
-        description='Lock, unlock, or query lock status of machines',
-        epilog=textwrap.dedent('''
-            Examples:
-            teuthology-lock --summary
-            teuthology-lock --lock-many 1 --machine-type vps
-            teuthology-lock --lock -t target.yaml
-            teuthology-lock --list-targets plana01
-            teuthology-lock --list --brief --owner user@host
-            teuthology-lock --update --status down --desc testing plana01
-        '''),
-        formatter_class=RawTextHelpFormatter)
-    parser.add_argument(
-        '-v', '--verbose',
-        action='store_true',
-        default=False,
-        help='be more verbose',
-    )
-    group = parser.add_mutually_exclusive_group(required=True)
-    group.add_argument(
-        '--list',
-        action='store_true',
-        default=False,
-        help='Show lock info for machines owned by you, or only machines ' +
-        'specified. Can be restricted by --owner, --status, and --locked.',
-    )
-    group.add_argument(
-        '--list-targets',
-        action='store_true',
-        default=False,
-        help='Show lock info for all machines, or only machines specified, ' +
-        'in targets: yaml format. Can be restricted by --owner, --status, ' +
-        'and --locked.',
-    )
-    group.add_argument(
-        '--lock',
-        action='store_true',
-        default=False,
-        help='lock particular machines',
-    )
-    group.add_argument(
-        '--unlock',
-        action='store_true',
-        default=False,
-        help='unlock particular machines',
-    )
-    group.add_argument(
-        '--lock-many',
-        dest='num_to_lock',
-        type=_positive_int,
-        help='lock this many machines',
-    )
-    group.add_argument(
-        '--update',
-        action='store_true',
-        default=False,
-        help='update the description or status of some machines',
-    )
-    group.add_argument(
-        '--summary',
-        action='store_true',
-        default=False,
-        help='summarize locked-machine counts by owner',
-    )
-    parser.add_argument(
-        '-a', '--all',
-        action='store_true',
-        default=False,
-        help='list all machines, not just those owned by you',
-    )
-    parser.add_argument(
-        '--owner',
-        default=None,
-        help='owner of the lock(s) (must match to unlock a machine)',
-    )
-    parser.add_argument(
-        '-f',
-        action='store_true',
-        default=False,
-        help="don't exit after the first error, continue locking or " +
-        "unlocking other machines",
-    )
-    parser.add_argument(
-        '--desc',
-        default=None,
-        help='lock description',
-    )
-    parser.add_argument(
-        '--desc-pattern',
-        default=None,
-        help='lock description',
-    )
-    parser.add_argument(
-        '--machine-type',
-        default=None,
-        help='Type of machine to lock, valid choices: mira | plana | ' +
-        'burnupi | vps | saya | tala',
-    )
-    parser.add_argument(
-        '--status',
-        default=None,
-        choices=['up', 'down'],
-        help='whether a machine is usable for testing',
-    )
-    parser.add_argument(
-        '--locked',
-        default=None,
-        choices=['true', 'false'],
-        help='whether a machine is locked',
-    )
-    parser.add_argument(
-        '--brief',
-        action='store_true',
-        default=False,
-        help='Shorten information reported from --list',
-    )
-    parser.add_argument(
-        '-t', '--targets',
-        dest='targets',
-        default=None,
-        help='input yaml containing targets',
-    )
-    parser.add_argument(
-        'machines',
-        metavar='MACHINE',
-        default=[],
-        nargs='*',
-        help='machines to operate on',
-    )
-    parser.add_argument(
-        '--os-type',
-        default='ubuntu',
-        help='virtual machine type',
-    )
-
-    ctx = parser.parse_args()
-
+def main(ctx):
     loglevel = logging.INFO
     if ctx.verbose:
         loglevel = logging.DEBUG
@@ -396,7 +249,7 @@ def main():
 
     elif ctx.lock:
         for machine in machines:
-            if not lock(ctx, machine, user):
+            if not lock_one(ctx, machine, user):
                 ret = 1
                 if not ctx.f:
                     return ret
@@ -405,7 +258,7 @@ def main():
                 create_if_vm(ctx, machine)
     elif ctx.unlock:
         for machine in machines:
-            if not unlock(ctx, machine, user):
+            if not unlock_one(ctx, machine, user):
                 ret = 1
                 if not ctx.f:
                     return ret
@@ -425,7 +278,7 @@ def main():
                 if len(result) < ctx.num_to_lock:
                     log.error("Locking failed.")
                     for machn in result:
-                        unlock(ctx, machn)
+                        unlock_one(ctx, machn)
                     ret = 1
                 else:
                     log.info("Successfully Locked:\n%s\n" % shortnames)
index b06ed245d3d38547b6ab2806d53a0c5c8296e572..cebd7f515bccf8a59ff19a55b77ddcc5007d03a5 100644 (file)
@@ -369,7 +369,7 @@ def nuke(ctx, log, should_unlock, sync_clocks=True, reboot_all=True,
 
 def nuke_one(ctx, targets, log, should_unlock, synch_clocks, reboot_all,
              check_locks, noipmi):
-    from teuthology.lock import unlock
+    from teuthology.lock import unlock_one
     ret = None
     ctx = argparse.Namespace(
         config=dict(targets=targets),
@@ -390,7 +390,7 @@ def nuke_one(ctx, targets, log, should_unlock, synch_clocks, reboot_all,
     else:
         if should_unlock:
             for target in targets.keys():
-                unlock(ctx, target, ctx.owner)
+                unlock_one(ctx, target, ctx.owner)
     return ret
 
 
index 638c05c6324c9faf1635f7cf864d2f1c397d5cd8..618487dfbdd7ec5cd9d62717a56216c66c118a9a 100644 (file)
@@ -129,7 +129,7 @@ def lock_machines(ctx, config):
         if ctx.summary.get('success', False):
             log.info('Unlocking machines...')
             for machine in ctx.config['targets'].iterkeys():
-                lock.unlock(ctx, machine, ctx.owner)
+                lock.unlock_one(ctx, machine, ctx.owner)
 
 def save_config(ctx, config):
     log.info('Saving configuration')