From 04b319d8479b27a47ca039bafd7ca6711f97038e Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Fri, 16 May 2014 15:54:13 -0500 Subject: [PATCH] Update/rewrite several functions to use paddles Functions updated: lock.lock_one(), lock.unlock_one(), lock.destroy_if_vm(), lockstatus.get_status() Signed-off-by: Zack Cerza --- teuthology/lock.py | 47 ++++++++++++++++++++-------------- teuthology/lockstatus.py | 10 +++++--- teuthology/orchestra/remote.py | 2 +- teuthology/task/internal.py | 4 +-- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/teuthology/lock.py b/teuthology/lock.py index 20704d2f20..74bea13b83 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -9,6 +9,8 @@ import collections import tempfile import os import time +import requests +import json import teuthology from .config import config @@ -56,38 +58,45 @@ def lock_many(ctx, num, machinetype, user=None, description=None): return [] -def lock_one(ctx, name, user=None, description=None): +def lock_one(name, user=None, description=None): if user is None: user = misc.get_user() - success, _, _ = ls.send_request( - 'POST', - config.lock_server + '/' + name, - urllib.urlencode(dict(user=user, desc=description))) + 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: - log.error('failed to lock %s', name) - return success + 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() - success, _, http_ret = ls.send_request( - 'DELETE', - config.lock_server + '/' + name + '?' + - urllib.urlencode(dict(user=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 destroy_if_vm(ctx, name): log.error('downburst destroy failed for %s', name) log.info('%s is not locked' % name) else: - log.error('failed to unlock %s', name) - failure_types = {403: 'You do not have %s locked', - 404: '%s is an invalid host name'} - if http_ret in failure_types: - log.error(failure_types[http_ret], name) + 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 @@ -252,7 +261,7 @@ def main(ctx): elif ctx.lock: for machine in machines: - if not lock_one(ctx, machine, user): + if not lock_one(machine, user): ret = 1 if not ctx.f: return ret @@ -502,8 +511,8 @@ def destroy_if_vm(ctx, machine_name): """ Return False only on vm downburst failures. """ - status_info = ls.get_status(ctx, machine_name) - phys_host = status_info['vpshost'] + status_info = ls.get_status(machine_name) + phys_host = status_info['vm_host'] if not phys_host: return True destroyMe = decanonicalize_hostname(machine_name) diff --git a/teuthology/lockstatus.py b/teuthology/lockstatus.py index 44821cc4ca..d2d90f79ff 100644 --- a/teuthology/lockstatus.py +++ b/teuthology/lockstatus.py @@ -1,5 +1,5 @@ -import json import httplib2 +import requests import logging import os from .config import config @@ -17,8 +17,10 @@ def send_request(method, url, body=None, headers=None): return (False, None, resp.status) -def get_status(ctx, name): - success, content, _ = send_request('GET', os.path.join(config.lock_server, name)) +def get_status(name): + uri = os.path.join(config.lock_server, 'nodes', name, '') + response = requests.get(uri) + success = response.ok if success: - return json.loads(content) + return response.json() return None diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index 6e7792b802..000d30da6b 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -429,7 +429,7 @@ class VirtualConsole(): raise RuntimeError("libvirt not found") self.shortname = getShortName(name) - status_info = ls.get_status('', self.shortname) + status_info = ls.get_status(self.shortname) try: phys_host = status_info['vpshost'] except TypeError: diff --git a/teuthology/task/internal.py b/teuthology/task/internal.py index 024297b96e..dd8d8f558a 100644 --- a/teuthology/task/internal.py +++ b/teuthology/task/internal.py @@ -129,7 +129,7 @@ def lock_machines(ctx, config): log.info("Error in virtual machine keys") newscandict = {} for dkey in newly_locked.iterkeys(): - stats = lockstatus.get_status(ctx, dkey) + stats = lockstatus.get_status(dkey) newscandict[dkey] = stats['sshpubkey'] ctx.config['targets'] = newscandict else: @@ -169,7 +169,7 @@ def check_lock(ctx, config): return log.info('Checking locks...') for machine in ctx.config['targets'].iterkeys(): - status = lockstatus.get_status(ctx, machine) + status = lockstatus.get_status(machine) log.debug('machine status is %s', repr(status)) assert status is not None, \ 'could not read lock status for {name}'.format(name=machine) -- 2.39.5