Skip to content

Commit

Permalink
Update/rewrite several functions to use paddles
Browse files Browse the repository at this point in the history
Functions updated: lock.lock_one(), lock.unlock_one(),
lock.destroy_if_vm(), lockstatus.get_status()

Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
  • Loading branch information
zmc committed May 20, 2014
1 parent d58d2c1 commit f2e88bc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
47 changes: 28 additions & 19 deletions teuthology/lock.py
Expand Up @@ -9,6 +9,8 @@
import tempfile
import os
import time
import requests
import json

import teuthology
from .config import config
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions teuthology/lockstatus.py
@@ -1,5 +1,5 @@
import json
import httplib2
import requests
import logging
import os
from .config import config
Expand All @@ -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
2 changes: 1 addition & 1 deletion teuthology/orchestra/remote.py
Expand Up @@ -429,7 +429,7 @@ def __init__(self, name, ipmiuser, ipmipass, ipmidomain, logfile=None,
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:
Expand Down
4 changes: 2 additions & 2 deletions teuthology/task/internal.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f2e88bc

Please sign in to comment.