From 1d1697213f1333eae4c0e0d68c13b452200407e7 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Tue, 20 May 2014 18:02:41 -0500 Subject: [PATCH] Re-order functions in module This is for readability. Signed-off-by: Zack Cerza --- teuthology/lock.py | 288 ++++++++++++++++++++++----------------------- 1 file changed, 144 insertions(+), 144 deletions(-) diff --git a/teuthology/lock.py b/teuthology/lock.py index 5c17fe005c..350eeadcc9 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -21,125 +21,6 @@ logging.getLogger("requests.packages.urllib3.connectionpool").setLevel( logging.WARNING) -def lock_many(ctx, num, machinetype, user=None, description=None): - machinetypes = misc.get_multi_machine_types(machinetype) - if user is None: - user = misc.get_user() - for machinetype in machinetypes: - uri = os.path.join(config.lock_server, 'nodes', 'lock_many', '') - response = requests.post( - uri, - json.dumps( - dict( - locked_by=user, - count=num, - machine_type=machinetype, - description=description, - )) - ) - if response.ok: - machines = {machine['name']: machine['ssh_pub_key'] - for machine in response.json()} - log.debug('locked {machines}'.format( - machines=', '.join(machines.keys()))) - if machinetype == 'vps': - ok_machs = {} - for machine in machines: - if provision.create_if_vm(ctx, machine): - ok_machs[machine] = machines[machine] - else: - log.error('Unable to create virtual machine: %s', - machine) - unlock_one(ctx, machine) - return ok_machs - return machines - elif response.status_code == 503: - log.error('Insufficient nodes available to lock %d %s nodes.', - num, machinetype) - log.error(response.text) - else: - log.error('Could not lock %d %s nodes, reason: unknown.', - num, machinetype) - return [] - - -def lock_one(name, user=None, description=None): - if user is None: - user = misc.get_user() - request = dict(name=name, locked=True, locked_by=user, - description=description) - uri = os.path.join(config.lock_server, 'nodes', name, 'lock', '') - response = requests.put(uri, json.dumps(request)) - success = response.ok - if success: - log.debug('locked %s as %s', name, user) - else: - try: - reason = response.json().get('message') - except ValueError: - reason = str(response.status_code) - log.error('failed to lock {node}. reason: {reason}'.format( - node=name, reason=reason)) - return response - - -def unlock_one(ctx, name, user=None): - if user is None: - user = misc.get_user() - request = dict(name=name, locked=False, locked_by=user, description=None) - uri = os.path.join(config.lock_server, 'nodes', name, 'lock', '') - response = requests.put(uri, json.dumps(request)) - success = response.ok - if success: - log.debug('unlocked %s', name) - if not provision.destroy_if_vm(ctx, name): - log.error('downburst destroy failed for %s', name) - log.info('%s is not locked' % name) - else: - try: - reason = response.json().get('message') - except ValueError: - reason = str(response.status_code) - log.error('failed to unlock {node}. reason: {reason}'.format( - node=name, reason=reason)) - return success - - -def list_locks(machine_type=None): - uri = os.path.join(config.lock_server, 'nodes', '') - if machine_type: - uri += '?machine_type=' + machine_type - response = requests.get(uri) - success = response.ok - if success: - return response.json() - return None - - -def update_lock(ctx, name, description=None, status=None, ssh_pub_key=None): - status_info = get_status(name) - phys_host = status_info['vpshost'] - if phys_host: - keyscan_out = '' - while not keyscan_out: - time.sleep(10) - keyscan_out, _ = keyscan_check([name]) - updated = {} - if description is not None: - updated['description'] = description - if status is not None: - updated['up'] = (status == 'up') - if ssh_pub_key is not None: - updated['ssh_pub_key'] = ssh_pub_key - - if updated: - response = requests.put( - config.lock_server + '/nodes/' + name, - json.dumps(updated)) - return response.ok - return True - - def main(ctx): if ctx.verbose: teuthology.log.setLevel(logging.DEBUG) @@ -320,31 +201,123 @@ def main(ctx): return ret -def updatekeys(ctx): - loglevel = logging.INFO - if ctx.verbose: - loglevel = logging.DEBUG +def lock_many(ctx, num, machinetype, user=None, description=None): + machinetypes = misc.get_multi_machine_types(machinetype) + if user is None: + user = misc.get_user() + for machinetype in machinetypes: + uri = os.path.join(config.lock_server, 'nodes', 'lock_many', '') + response = requests.post( + uri, + json.dumps( + dict( + locked_by=user, + count=num, + machine_type=machinetype, + description=description, + )) + ) + if response.ok: + machines = {machine['name']: machine['ssh_pub_key'] + for machine in response.json()} + log.debug('locked {machines}'.format( + machines=', '.join(machines.keys()))) + if machinetype == 'vps': + ok_machs = {} + for machine in machines: + if provision.create_if_vm(ctx, machine): + ok_machs[machine] = machines[machine] + else: + log.error('Unable to create virtual machine: %s', + machine) + unlock_one(ctx, machine) + return ok_machs + return machines + elif response.status_code == 503: + log.error('Insufficient nodes available to lock %d %s nodes.', + num, machinetype) + log.error(response.text) + else: + log.error('Could not lock %d %s nodes, reason: unknown.', + num, machinetype) + return [] - logging.basicConfig( - level=loglevel, - ) - misc.read_config(ctx) +def lock_one(name, user=None, description=None): + if user is None: + user = misc.get_user() + request = dict(name=name, locked=True, locked_by=user, + description=description) + uri = os.path.join(config.lock_server, 'nodes', name, 'lock', '') + response = requests.put(uri, json.dumps(request)) + success = response.ok + if success: + log.debug('locked %s as %s', name, user) + else: + try: + reason = response.json().get('message') + except ValueError: + reason = str(response.status_code) + log.error('failed to lock {node}. reason: {reason}'.format( + node=name, reason=reason)) + return response - machines = [misc.canonicalize_hostname(m) for m in ctx.machines] - if ctx.targets: +def unlock_one(ctx, name, user=None): + if user is None: + user = misc.get_user() + request = dict(name=name, locked=False, locked_by=user, description=None) + uri = os.path.join(config.lock_server, 'nodes', name, 'lock', '') + response = requests.put(uri, json.dumps(request)) + success = response.ok + if success: + log.debug('unlocked %s', name) + if not provision.destroy_if_vm(ctx, name): + log.error('downburst destroy failed for %s', name) + log.info('%s is not locked' % name) + else: 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)) + reason = response.json().get('message') + except ValueError: + reason = str(response.status_code) + log.error('failed to unlock {node}. reason: {reason}'.format( + node=name, reason=reason)) + return success - return scan_for_locks(ctx, machines) + +def list_locks(machine_type=None): + uri = os.path.join(config.lock_server, 'nodes', '') + if machine_type: + uri += '?machine_type=' + machine_type + response = requests.get(uri) + success = response.ok + if success: + return response.json() + return None + + +def update_lock(ctx, name, description=None, status=None, ssh_pub_key=None): + status_info = get_status(name) + phys_host = status_info['vpshost'] + if phys_host: + keyscan_out = '' + while not keyscan_out: + time.sleep(10) + keyscan_out, _ = keyscan_check([name]) + updated = {} + if description is not None: + updated['description'] = description + if status is not None: + updated['up'] = (status == 'up') + if ssh_pub_key is not None: + updated['ssh_pub_key'] = ssh_pub_key + + if updated: + response = requests.put( + config.lock_server + '/nodes/' + name, + json.dumps(updated)) + return response.ok + return True def keyscan_check(machines): @@ -353,7 +326,7 @@ def keyscan_check(machines): for lock in locks: current_locks[lock['name']] = lock - if len(machines) == 0: + if not machines: machines = current_locks.keys() for i, machine in enumerate(machines): @@ -369,6 +342,38 @@ def keyscan_check(machines): return (out, current_locks) +def updatekeys(ctx): + loglevel = logging.INFO + if ctx.verbose: + loglevel = logging.DEBUG + + logging.basicConfig( + level=loglevel, + ) + + misc.read_config(ctx) + + machines = [misc.canonicalize_hostname(m) 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 scan_for_locks(ctx, machines) + + +def scan_for_locks(ctx, machines): + out, current_locks = keyscan_check(machines) + return update_keys(ctx, out, current_locks) + + def update_keys(ctx, out, current_locks): ret = 0 for key_entry in out.splitlines(): @@ -385,11 +390,6 @@ def update_keys(ctx, out, current_locks): return ret -def scan_for_locks(ctx, machines): - out, current_locks = keyscan_check(machines) - return update_keys(ctx, out, current_locks) - - def do_summary(ctx): lockd = collections.defaultdict(lambda: [0, 0, 'unknown']) for l in list_locks(ctx.machine_type): -- 2.39.5