2011-07-02 01:18:03 +00:00
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
2011-07-14 00:14:52 +00:00
|
|
|
import subprocess
|
2011-07-02 01:18:03 +00:00
|
|
|
import urllib
|
2011-07-11 21:49:53 +00:00
|
|
|
import yaml
|
2012-06-05 00:41:57 +00:00
|
|
|
import re
|
2012-06-06 22:15:47 +00:00
|
|
|
import collections
|
2013-04-03 01:27:38 +00:00
|
|
|
import tempfile
|
2013-04-24 00:28:27 +00:00
|
|
|
import os
|
2013-06-07 01:43:43 +00:00
|
|
|
import time
|
2011-07-02 01:18:03 +00:00
|
|
|
|
2013-04-12 17:55:54 +00:00
|
|
|
from teuthology import lockstatus as ls
|
2011-07-02 01:18:03 +00:00
|
|
|
from teuthology import misc as teuthology
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-02-05 20:53:08 +00:00
|
|
|
def lock_many(ctx, num, machinetype, user=None, description=None):
|
2011-07-02 01:18:03 +00:00
|
|
|
if user is None:
|
|
|
|
user = teuthology.get_user()
|
2013-04-12 17:55:54 +00:00
|
|
|
success, content, status = ls.send_request('POST', ls._lock_url(ctx),
|
2013-03-25 23:46:48 +00:00
|
|
|
urllib.urlencode(dict(
|
|
|
|
user=user,
|
|
|
|
num=num,
|
|
|
|
machinetype=machinetype,
|
|
|
|
desc=description,
|
|
|
|
)))
|
2011-07-02 01:18:03 +00:00
|
|
|
if success:
|
|
|
|
machines = json.loads(content)
|
2011-07-14 23:47:29 +00:00
|
|
|
log.debug('locked {machines}'.format(machines=', '.join(machines.keys())))
|
2011-07-02 01:18:03 +00:00
|
|
|
return machines
|
2012-03-07 16:34:55 +00:00
|
|
|
if status == 503:
|
2012-07-12 23:36:40 +00:00
|
|
|
log.error('Insufficient nodes available to lock %d nodes.', num)
|
2012-03-07 16:34:55 +00:00
|
|
|
else:
|
|
|
|
log.error('Could not lock %d nodes, reason: unknown.', num)
|
2011-07-02 01:18:03 +00:00
|
|
|
return []
|
|
|
|
|
2013-07-13 03:16:28 +00:00
|
|
|
def lock(ctx, name, user=None, description=None):
|
2011-07-02 01:18:03 +00:00
|
|
|
if user is None:
|
|
|
|
user = teuthology.get_user()
|
2013-04-12 17:55:54 +00:00
|
|
|
success, _, _ = ls.send_request('POST', ls._lock_url(ctx) + '/' + name,
|
2013-07-13 03:16:28 +00:00
|
|
|
urllib.urlencode(dict(user=user, desc=description)))
|
2011-07-02 01:18:03 +00:00
|
|
|
if success:
|
|
|
|
log.debug('locked %s as %s', name, user)
|
|
|
|
else:
|
|
|
|
log.error('failed to lock %s', name)
|
|
|
|
return success
|
|
|
|
|
2011-07-07 18:43:35 +00:00
|
|
|
def unlock(ctx, name, user=None):
|
2011-07-02 01:18:03 +00:00
|
|
|
if user is None:
|
|
|
|
user = teuthology.get_user()
|
2013-04-12 17:55:54 +00:00
|
|
|
success, _ , _ = ls.send_request('DELETE', ls._lock_url(ctx) + '/' + name + '?' + \
|
2011-07-02 01:18:03 +00:00
|
|
|
urllib.urlencode(dict(user=user)))
|
|
|
|
if success:
|
|
|
|
log.debug('unlocked %s', name)
|
2013-04-24 00:28:27 +00:00
|
|
|
if not destroy_if_vm(ctx, name):
|
|
|
|
log.error('downburst destroy failed for %s',name)
|
2011-07-02 01:18:03 +00:00
|
|
|
else:
|
|
|
|
log.error('failed to unlock %s', name)
|
|
|
|
return success
|
|
|
|
|
2011-07-07 18:43:35 +00:00
|
|
|
def list_locks(ctx):
|
2013-04-12 17:55:54 +00:00
|
|
|
success, content, _ = ls.send_request('GET', ls._lock_url(ctx))
|
2011-07-02 01:18:03 +00:00
|
|
|
if success:
|
|
|
|
return json.loads(content)
|
|
|
|
return None
|
|
|
|
|
2011-07-14 00:14:52 +00:00
|
|
|
def update_lock(ctx, name, description=None, status=None, sshpubkey=None):
|
2013-06-07 01:43:43 +00:00
|
|
|
status_info = ls.get_status(ctx, name)
|
|
|
|
phys_host = status_info['vpshost']
|
|
|
|
if phys_host:
|
|
|
|
keyscan_out = ''
|
|
|
|
while not keyscan_out:
|
|
|
|
time.sleep(10)
|
|
|
|
keyscan_out, _ = keyscan_check(ctx, [name])
|
2011-07-02 01:18:03 +00:00
|
|
|
updated = {}
|
|
|
|
if description is not None:
|
|
|
|
updated['desc'] = description
|
|
|
|
if status is not None:
|
|
|
|
updated['status'] = status
|
2011-07-14 00:14:52 +00:00
|
|
|
if sshpubkey is not None:
|
|
|
|
updated['sshpubkey'] = sshpubkey
|
2011-07-02 01:18:03 +00:00
|
|
|
|
|
|
|
if updated:
|
2013-04-12 17:55:54 +00:00
|
|
|
success, _, _ = ls.send_request('PUT', ls._lock_url(ctx) + '/' + name,
|
2011-07-20 01:34:42 +00:00
|
|
|
body=urllib.urlencode(updated),
|
|
|
|
headers={'Content-type': 'application/x-www-form-urlencoded'})
|
2011-07-02 01:18:03 +00:00
|
|
|
return success
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _positive_int(string):
|
|
|
|
value = int(string)
|
|
|
|
if value < 1:
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
'{string} is not positive'.format(string=string))
|
|
|
|
return value
|
|
|
|
|
2012-06-06 01:33:36 +00:00
|
|
|
def canonicalize_hostname(s):
|
|
|
|
if re.match('ubuntu@.*\.front\.sepia\.ceph\.com', s) is None:
|
|
|
|
s = 'ubuntu@' + s + '.front.sepia.ceph.com'
|
|
|
|
return s
|
|
|
|
|
2011-07-02 01:18:03 +00:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="""
|
|
|
|
Lock, unlock, or query lock status of machines.
|
|
|
|
""")
|
|
|
|
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,
|
2012-03-07 17:02:39 +00:00
|
|
|
help='Show lock info for machines owned by you, or only machines specified. Can be restricted by --owner, --status, and --locked.',
|
2011-07-02 01:18:03 +00:00
|
|
|
)
|
2011-08-24 17:03:43 +00:00
|
|
|
group.add_argument(
|
|
|
|
'--list-targets',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2012-03-07 17:02:39 +00:00
|
|
|
help='Show lock info for all machines, or only machines specified, in targets: yaml format. Can be restricted by --owner, --status, and --locked.',
|
2011-08-24 17:03:43 +00:00
|
|
|
)
|
2011-07-02 01:18:03 +00:00
|
|
|
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',
|
|
|
|
)
|
2012-06-06 22:15:47 +00:00
|
|
|
group.add_argument(
|
|
|
|
'--summary',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='summarize locked-machine counts by owner',
|
|
|
|
)
|
2011-09-03 02:12:16 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-a', '--all',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='list all machines, not just those owned by you',
|
|
|
|
)
|
2011-07-02 01:18:03 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--owner',
|
|
|
|
default=None,
|
2011-08-02 23:13:28 +00:00
|
|
|
help='owner of the lock(s) (must match to unlock a machine)',
|
2011-07-02 01:18:03 +00:00
|
|
|
)
|
|
|
|
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,
|
2013-02-18 21:13:11 +00:00
|
|
|
help='lock description',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--desc-pattern',
|
|
|
|
default=None,
|
|
|
|
help='lock description',
|
2011-07-02 01:18:03 +00:00
|
|
|
)
|
2013-02-05 20:53:08 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--machine-type',
|
2013-07-13 20:09:07 +00:00
|
|
|
default=None,
|
2013-02-05 20:53:08 +00:00
|
|
|
help='Type of machine to lock',
|
|
|
|
)
|
2011-07-02 01:18:03 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--status',
|
|
|
|
default=None,
|
|
|
|
choices=['up', 'down'],
|
2011-08-02 23:13:28 +00:00
|
|
|
help='whether a machine is usable for testing',
|
2011-07-02 01:18:03 +00:00
|
|
|
)
|
2012-03-07 17:02:39 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--locked',
|
|
|
|
default=None,
|
|
|
|
choices=['true', 'false'],
|
|
|
|
help='whether a machine is locked',
|
|
|
|
)
|
2012-06-06 22:15:47 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--brief',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Shorten information reported from --list',
|
|
|
|
)
|
2011-07-11 22:25:36 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-t', '--targets',
|
|
|
|
dest='targets',
|
|
|
|
default=None,
|
2011-07-12 01:00:03 +00:00
|
|
|
help='input yaml containing targets',
|
|
|
|
)
|
2011-07-02 01:18:03 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'machines',
|
|
|
|
metavar='MACHINE',
|
|
|
|
default=[],
|
|
|
|
nargs='*',
|
|
|
|
help='machines to operate on',
|
|
|
|
)
|
2013-04-03 01:27:38 +00:00
|
|
|
parser.add_argument(
|
2013-07-29 23:30:40 +00:00
|
|
|
'--os-type',
|
2013-04-03 01:27:38 +00:00
|
|
|
default='ubuntu',
|
|
|
|
help='virtual machine type',
|
|
|
|
)
|
2011-07-02 01:18:03 +00:00
|
|
|
|
|
|
|
ctx = parser.parse_args()
|
|
|
|
|
|
|
|
loglevel = logging.ERROR
|
|
|
|
if ctx.verbose:
|
|
|
|
loglevel = logging.DEBUG
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=loglevel,
|
|
|
|
)
|
|
|
|
|
2011-07-07 18:43:35 +00:00
|
|
|
teuthology.read_config(ctx)
|
|
|
|
|
2011-07-02 01:18:03 +00:00
|
|
|
ret = 0
|
|
|
|
user = ctx.owner
|
2012-06-06 01:33:36 +00:00
|
|
|
machines = [canonicalize_hostname(m) for m in ctx.machines]
|
2011-07-02 01:18:03 +00:00
|
|
|
machines_to_update = []
|
2011-07-12 01:00:03 +00:00
|
|
|
|
2011-07-11 22:25:36 +00:00
|
|
|
if ctx.targets:
|
|
|
|
try:
|
|
|
|
with file(ctx.targets) as f:
|
|
|
|
g = yaml.safe_load_all(f)
|
|
|
|
for new in g:
|
|
|
|
if 'targets' in new:
|
2011-07-14 23:47:29 +00:00
|
|
|
for t in new['targets'].iterkeys():
|
2011-07-12 01:00:03 +00:00
|
|
|
machines.append(t)
|
2011-07-11 22:25:36 +00:00
|
|
|
except IOError, e:
|
|
|
|
raise argparse.ArgumentTypeError(str(e))
|
2011-07-02 01:18:03 +00:00
|
|
|
|
2011-07-07 21:56:12 +00:00
|
|
|
if ctx.f:
|
|
|
|
assert ctx.lock or ctx.unlock, \
|
|
|
|
'-f is only supported by --lock and --unlock'
|
2012-06-21 21:32:51 +00:00
|
|
|
if machines:
|
2012-06-06 22:15:47 +00:00
|
|
|
assert ctx.lock or ctx.unlock or ctx.list or ctx.list_targets \
|
2012-06-07 20:20:02 +00:00
|
|
|
or ctx.update, \
|
2011-07-07 21:56:12 +00:00
|
|
|
'machines cannot be specified with that operation'
|
|
|
|
else:
|
2012-06-06 22:15:47 +00:00
|
|
|
assert ctx.num_to_lock or ctx.list or ctx.list_targets or ctx.summary,\
|
2011-07-07 21:56:12 +00:00
|
|
|
'machines must be specified for that operation'
|
2011-09-03 02:12:16 +00:00
|
|
|
if ctx.all:
|
|
|
|
assert ctx.list or ctx.list_targets, \
|
|
|
|
'--all can only be used with --list and --list-targets'
|
|
|
|
assert ctx.owner is None, \
|
|
|
|
'--all and --owner are mutually exclusive'
|
|
|
|
assert not machines, \
|
|
|
|
'--all and listing specific machines are incompatible'
|
2013-07-13 20:09:07 +00:00
|
|
|
if ctx.num_to_lock:
|
|
|
|
assert ctx.machine_type, \
|
|
|
|
'must specify machine type to lock'
|
2011-07-07 21:56:12 +00:00
|
|
|
|
2012-06-06 22:15:47 +00:00
|
|
|
if ctx.brief:
|
|
|
|
assert ctx.list, '--brief only applies to --list'
|
|
|
|
|
2011-08-24 17:03:43 +00:00
|
|
|
if ctx.list or ctx.list_targets:
|
2011-08-02 22:53:37 +00:00
|
|
|
assert ctx.desc is None, '--desc does nothing with --list'
|
2011-07-02 01:18:03 +00:00
|
|
|
|
|
|
|
if machines:
|
2013-04-12 17:55:54 +00:00
|
|
|
statuses = [ls.get_status(ctx, machine) for machine in machines]
|
2011-07-02 01:18:03 +00:00
|
|
|
else:
|
2011-07-07 18:43:35 +00:00
|
|
|
statuses = list_locks(ctx)
|
2013-04-03 01:27:38 +00:00
|
|
|
vmachines = []
|
|
|
|
|
|
|
|
for vmachine in statuses:
|
|
|
|
if vmachine['vpshost']:
|
|
|
|
if vmachine['locked']:
|
|
|
|
vmachines.append(vmachine['name'])
|
|
|
|
if vmachines:
|
2013-05-10 17:18:44 +00:00
|
|
|
# Avoid ssh-keyscans for everybody when listing all machines
|
|
|
|
# Listing specific machines will update the keys.
|
2013-04-03 01:27:38 +00:00
|
|
|
if machines:
|
2013-05-10 17:18:44 +00:00
|
|
|
scan_for_locks(ctx, vmachines)
|
2013-04-12 17:55:54 +00:00
|
|
|
statuses = [ls.get_status(ctx, machine) for machine in machines]
|
2013-04-03 01:27:38 +00:00
|
|
|
else:
|
|
|
|
statuses = list_locks(ctx)
|
2011-07-02 01:18:03 +00:00
|
|
|
if statuses:
|
2013-07-13 20:11:40 +00:00
|
|
|
if ctx.machine_type:
|
|
|
|
statuses = [status for status in statuses \
|
|
|
|
if status['type'] == ctx.machine_type]
|
2011-09-03 02:12:16 +00:00
|
|
|
if not machines and ctx.owner is None and not ctx.all:
|
|
|
|
ctx.owner = teuthology.get_user()
|
2011-08-02 22:53:37 +00:00
|
|
|
if ctx.owner is not None:
|
|
|
|
statuses = [status for status in statuses \
|
|
|
|
if status['locked_by'] == ctx.owner]
|
|
|
|
if ctx.status is not None:
|
|
|
|
statuses = [status for status in statuses \
|
|
|
|
if status['up'] == (ctx.status == 'up')]
|
2012-03-07 17:02:39 +00:00
|
|
|
if ctx.locked is not None:
|
|
|
|
statuses = [status for status in statuses \
|
|
|
|
if status['locked'] == (ctx.locked == 'true')]
|
2013-02-18 21:13:11 +00:00
|
|
|
if ctx.desc is not None:
|
|
|
|
statuses = [status for status in statuses \
|
|
|
|
if status['description'] == ctx.desc]
|
|
|
|
if ctx.desc_pattern is not None:
|
|
|
|
statuses = [status for status in statuses \
|
2013-03-12 16:49:42 +00:00
|
|
|
if status['description'] is not None and \
|
|
|
|
status['description'].find(ctx.desc_pattern) >= 0]
|
2011-08-24 17:03:43 +00:00
|
|
|
if ctx.list:
|
2012-06-06 22:15:47 +00:00
|
|
|
if ctx.brief:
|
|
|
|
for s in statuses:
|
2012-06-07 03:29:28 +00:00
|
|
|
locked = "un" if s['locked'] == 0 else " "
|
2012-06-06 22:15:47 +00:00
|
|
|
mo = re.match('\w+@(\w+?)\..*', s['name'])
|
|
|
|
host = mo.group(1) if mo else s['name']
|
2012-06-07 03:29:28 +00:00
|
|
|
print '{host} {locked}locked {owner} "{desc}"'.format(
|
|
|
|
locked = locked, host = host,
|
2012-06-06 22:15:47 +00:00
|
|
|
owner=s['locked_by'], desc=s['description'])
|
|
|
|
else:
|
|
|
|
print json.dumps(statuses, indent=4)
|
2011-08-24 17:03:43 +00:00
|
|
|
else:
|
|
|
|
frag = { 'targets': {} }
|
|
|
|
for f in statuses:
|
|
|
|
frag['targets'][f['name']] = f['sshpubkey']
|
|
|
|
print yaml.safe_dump(frag, default_flow_style=False)
|
2011-07-02 01:18:03 +00:00
|
|
|
else:
|
|
|
|
log.error('error retrieving lock statuses')
|
|
|
|
ret = 1
|
2012-06-07 20:20:02 +00:00
|
|
|
|
2012-06-06 22:15:47 +00:00
|
|
|
elif ctx.summary:
|
2012-06-07 20:20:02 +00:00
|
|
|
do_summary(ctx)
|
|
|
|
return 0
|
2012-06-06 22:15:47 +00:00
|
|
|
|
2011-07-02 01:18:03 +00:00
|
|
|
elif ctx.lock:
|
|
|
|
for machine in machines:
|
2011-07-07 18:43:35 +00:00
|
|
|
if not lock(ctx, machine, user):
|
2011-07-02 01:18:03 +00:00
|
|
|
ret = 1
|
|
|
|
if not ctx.f:
|
|
|
|
return ret
|
|
|
|
else:
|
|
|
|
machines_to_update.append(machine)
|
2013-04-24 00:28:27 +00:00
|
|
|
create_if_vm(ctx, machine)
|
2011-07-02 01:18:03 +00:00
|
|
|
elif ctx.unlock:
|
|
|
|
for machine in machines:
|
2011-07-07 18:43:35 +00:00
|
|
|
if not unlock(ctx, machine, user):
|
2011-07-02 01:18:03 +00:00
|
|
|
ret = 1
|
|
|
|
if not ctx.f:
|
|
|
|
return ret
|
|
|
|
else:
|
|
|
|
machines_to_update.append(machine)
|
2013-04-24 00:28:27 +00:00
|
|
|
destroy_if_vm(ctx, machine)
|
2011-07-02 01:18:03 +00:00
|
|
|
elif ctx.num_to_lock:
|
2013-02-05 20:53:08 +00:00
|
|
|
result = lock_many(ctx, ctx.num_to_lock, ctx.machine_type, user)
|
2011-07-02 01:18:03 +00:00
|
|
|
if not result:
|
|
|
|
ret = 1
|
|
|
|
else:
|
2011-07-14 23:47:29 +00:00
|
|
|
machines_to_update = result.keys()
|
2013-04-03 01:27:38 +00:00
|
|
|
if ctx.machine_type == 'vps':
|
|
|
|
print "Locks successful"
|
|
|
|
print "Unable to display keys at this time (virtual machines are rebooting)."
|
|
|
|
print "Please run teuthology-lock --list-targets once these machines come up."
|
|
|
|
else:
|
|
|
|
print yaml.safe_dump(dict(targets=result), default_flow_style=False)
|
2011-07-02 01:18:03 +00:00
|
|
|
elif ctx.update:
|
2011-07-07 21:56:12 +00:00
|
|
|
assert ctx.desc is not None or ctx.status is not None, \
|
|
|
|
'you must specify description or status to update'
|
|
|
|
assert ctx.owner is None, 'only description and status may be updated'
|
2011-07-02 01:18:03 +00:00
|
|
|
machines_to_update = machines
|
|
|
|
|
|
|
|
if ctx.desc is not None or ctx.status is not None:
|
|
|
|
for machine in machines_to_update:
|
2011-07-07 18:43:35 +00:00
|
|
|
update_lock(ctx, machine, ctx.desc, ctx.status)
|
2011-07-02 01:18:03 +00:00
|
|
|
|
|
|
|
return ret
|
2011-07-14 00:14:52 +00:00
|
|
|
|
|
|
|
def update_hostkeys():
|
|
|
|
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(
|
|
|
|
'-t', '--targets',
|
|
|
|
default=None,
|
|
|
|
help='input yaml containing targets to check',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'machines',
|
|
|
|
metavar='MACHINES',
|
|
|
|
default=[],
|
|
|
|
nargs='*',
|
|
|
|
help='hosts to check for updated keys',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--verbose',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='be more verbose',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-a', '--all',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='update hostkeys of all machines in the db',
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx = parser.parse_args()
|
|
|
|
|
|
|
|
loglevel = logging.ERROR
|
|
|
|
if ctx.verbose:
|
|
|
|
loglevel = logging.DEBUG
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=loglevel,
|
|
|
|
)
|
|
|
|
|
|
|
|
teuthology.read_config(ctx)
|
|
|
|
|
|
|
|
assert ctx.all or ctx.targets or ctx.machines, 'You must specify machines to update'
|
|
|
|
if ctx.all:
|
|
|
|
assert not ctx.targets and not ctx.machines, \
|
|
|
|
'You can\'t specify machines with the --all option'
|
2012-06-06 01:33:36 +00:00
|
|
|
machines = [canonicalize_hostname(m) for m in ctx.machines]
|
2011-07-14 00:14:52 +00:00
|
|
|
|
|
|
|
if ctx.targets:
|
|
|
|
try:
|
|
|
|
with file(ctx.targets) as f:
|
|
|
|
g = yaml.safe_load_all(f)
|
|
|
|
for new in g:
|
|
|
|
if 'targets' in new:
|
2011-07-14 23:47:29 +00:00
|
|
|
for t in new['targets'].iterkeys():
|
2011-07-14 00:14:52 +00:00
|
|
|
machines.append(t)
|
|
|
|
except IOError, e:
|
|
|
|
raise argparse.ArgumentTypeError(str(e))
|
2013-04-03 01:27:38 +00:00
|
|
|
|
|
|
|
return scan_for_locks(ctx, machines)
|
2011-07-14 00:14:52 +00:00
|
|
|
|
2013-04-24 00:28:27 +00:00
|
|
|
def keyscan_check(ctx, machines):
|
2011-07-14 00:14:52 +00:00
|
|
|
locks = list_locks(ctx)
|
|
|
|
current_locks = {}
|
|
|
|
for lock in locks:
|
|
|
|
current_locks[lock['name']] = lock
|
|
|
|
|
2013-04-24 00:28:27 +00:00
|
|
|
if hasattr(ctx, 'all'):
|
|
|
|
if ctx.all:
|
|
|
|
machines = current_locks.keys()
|
2011-07-14 00:14:52 +00:00
|
|
|
|
|
|
|
for i, machine in enumerate(machines):
|
|
|
|
if '@' in machine:
|
|
|
|
_, machines[i] = machine.rsplit('@')
|
|
|
|
args = ['ssh-keyscan']
|
|
|
|
args.extend(machines)
|
|
|
|
p = subprocess.Popen(
|
|
|
|
args=args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
out, _ = p.communicate()
|
2013-07-04 02:07:35 +00:00
|
|
|
#assert p.returncode == 0, 'ssh-keyscan failed'
|
2013-04-24 00:28:27 +00:00
|
|
|
return (out, current_locks)
|
2011-07-14 00:14:52 +00:00
|
|
|
|
2013-04-24 00:28:27 +00:00
|
|
|
def update_keys(ctx, out, current_locks):
|
2011-07-14 00:14:52 +00:00
|
|
|
ret = 0
|
|
|
|
for key_entry in out.splitlines():
|
|
|
|
hostname, pubkey = key_entry.split(' ', 1)
|
|
|
|
# TODO: separate out user
|
|
|
|
full_name = 'ubuntu@{host}'.format(host=hostname)
|
|
|
|
log.info('Checking %s', full_name)
|
|
|
|
assert full_name in current_locks, 'host is not in the database!'
|
|
|
|
if current_locks[full_name]['sshpubkey'] != pubkey:
|
|
|
|
log.info('New key found. Updating...')
|
|
|
|
if not update_lock(ctx, full_name, sshpubkey=pubkey):
|
|
|
|
log.error('failed to update %s!', full_name)
|
|
|
|
ret = 1
|
|
|
|
return ret
|
2013-04-24 00:28:27 +00:00
|
|
|
|
|
|
|
def scan_for_locks(ctx, machines):
|
|
|
|
out, current_locks = keyscan_check(ctx, machines)
|
|
|
|
return update_keys(ctx, out, current_locks)
|
2012-06-07 20:20:02 +00:00
|
|
|
|
|
|
|
def do_summary(ctx):
|
2013-02-08 00:06:21 +00:00
|
|
|
lockd = collections.defaultdict(lambda: [0,0,'unknown'])
|
2012-06-07 20:20:02 +00:00
|
|
|
for l in list_locks(ctx):
|
2013-07-13 20:09:15 +00:00
|
|
|
if ctx.machine_type and l['type'] != ctx.machine_type:
|
2013-03-21 01:30:29 +00:00
|
|
|
continue
|
2013-02-08 00:06:21 +00:00
|
|
|
who = l['locked_by'] if l['locked'] == 1 else '(free)', l['type']
|
2012-06-07 20:20:02 +00:00
|
|
|
lockd[who][0] += 1
|
|
|
|
lockd[who][1] += l['up'] # up is 1 or 0
|
2013-02-08 00:06:21 +00:00
|
|
|
lockd[who][2] = l['type']
|
2012-06-07 20:20:02 +00:00
|
|
|
|
2013-02-08 00:06:21 +00:00
|
|
|
locks = sorted([p for p in lockd.iteritems()], key=lambda sort: (sort[1][2],sort[1][0]))
|
2012-06-07 20:20:02 +00:00
|
|
|
total_count, total_up = 0, 0
|
2013-02-08 00:06:21 +00:00
|
|
|
print "TYPE COUNT UP OWNER"
|
|
|
|
|
|
|
|
for (owner, (count, upcount, machinetype)) in locks:
|
|
|
|
#if machinetype == spectype:
|
|
|
|
print "{machinetype:8s} {count:3d} {up:3d} {owner}".format(count = count,
|
|
|
|
up = upcount, owner = owner[0], machinetype=machinetype)
|
|
|
|
total_count += count
|
|
|
|
total_up += upcount
|
|
|
|
|
|
|
|
print " --- ---"
|
|
|
|
print "{cnt:12d} {up:3d}".format(cnt = total_count, up = total_up)
|
|
|
|
|
2013-04-03 01:27:38 +00:00
|
|
|
def decanonicalize_hostname(s):
|
|
|
|
if re.match('ubuntu@.*\.front\.sepia\.ceph\.com', s):
|
|
|
|
s = s[len('ubuntu@'): -len('.front.sepia.ceph.com')]
|
|
|
|
return s
|
2013-04-23 04:08:22 +00:00
|
|
|
|
|
|
|
def _get_downburst_exec():
|
|
|
|
"""
|
|
|
|
First check for downburst in the user's path.
|
|
|
|
Then check in ~/src, ~ubuntu/src, and ~teuthology/src.
|
|
|
|
Return '' if no executable downburst is found.
|
|
|
|
"""
|
|
|
|
path = os.environ.get('PATH', None)
|
|
|
|
if path:
|
|
|
|
for p in os.environ.get('PATH','').split(os.pathsep):
|
|
|
|
pth = os.path.join(p, 'downburst')
|
|
|
|
if os.access(pth, os.X_OK):
|
|
|
|
return pth
|
|
|
|
import pwd
|
|
|
|
little_old_me = pwd.getpwuid(os.getuid()).pw_name
|
|
|
|
for user in [little_old_me, 'ubuntu', 'teuthology']:
|
|
|
|
pth = "/home/%s/src/downburst/virtualenv/bin/downburst" % user
|
|
|
|
if os.access(pth, os.X_OK):
|
|
|
|
return pth
|
|
|
|
return ''
|
|
|
|
|
2013-04-03 01:27:38 +00:00
|
|
|
#
|
|
|
|
# Use downburst to create a virtual machine
|
|
|
|
#
|
2013-04-24 00:28:27 +00:00
|
|
|
def create_if_vm(ctx, machine_name):
|
|
|
|
status_info = ls.get_status(ctx, machine_name)
|
|
|
|
phys_host = status_info['vpshost']
|
|
|
|
if not phys_host:
|
|
|
|
return False
|
2013-07-25 21:45:02 +00:00
|
|
|
from teuthology.misc import get_distro
|
|
|
|
os_type = get_distro(ctx)
|
2013-07-26 23:15:19 +00:00
|
|
|
default_os_version = dict(
|
2013-07-23 22:50:50 +00:00
|
|
|
ubuntu="12.04",
|
|
|
|
fedora="18",
|
|
|
|
centos="6.4",
|
|
|
|
opensuse="12.2",
|
|
|
|
sles="11-sp2",
|
|
|
|
rhel="6.3",
|
|
|
|
debian='6.0'
|
|
|
|
)
|
2013-04-03 01:27:38 +00:00
|
|
|
createMe = decanonicalize_hostname(machine_name)
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp:
|
2013-07-04 02:07:35 +00:00
|
|
|
try:
|
|
|
|
lcnfg = ctx.config['downburst']
|
2013-07-30 00:03:30 +00:00
|
|
|
except (KeyError, AttributeError):
|
2013-07-04 02:07:35 +00:00
|
|
|
lcnfg = {}
|
|
|
|
|
2013-07-23 22:50:50 +00:00
|
|
|
distro = lcnfg.get('distro', os_type.lower())
|
2013-07-30 00:03:30 +00:00
|
|
|
try:
|
|
|
|
distroversion = ctx.config.get('os_version', default_os_version[distro])
|
|
|
|
except AttributeError:
|
|
|
|
distroversion = default_os_version[distro]
|
|
|
|
|
2013-07-04 02:07:35 +00:00
|
|
|
file_info = {}
|
|
|
|
file_info['disk-size'] = lcnfg.get('disk-size', '30G')
|
|
|
|
file_info['ram'] = lcnfg.get('ram', '1.9G')
|
|
|
|
file_info['cpus'] = lcnfg.get('cpus', 1)
|
|
|
|
file_info['networks'] = lcnfg.get('networks',
|
2013-07-09 05:22:22 +00:00
|
|
|
[{'source' : 'front', 'mac' : status_info['mac']}])
|
2013-07-23 22:50:50 +00:00
|
|
|
file_info['distro'] = distro
|
2013-07-30 00:03:30 +00:00
|
|
|
file_info['distroversion'] = distroversion
|
2013-07-04 02:07:35 +00:00
|
|
|
file_info['additional-disks'] = lcnfg.get(
|
|
|
|
'additional-disks', 3)
|
|
|
|
file_info['additional-disks-size'] = lcnfg.get(
|
|
|
|
'additional-disks-size', '200G')
|
|
|
|
file_info['arch'] = lcnfg.get('arch', 'x86_64')
|
|
|
|
file_out = {'downburst': file_info}
|
2013-04-25 19:37:17 +00:00
|
|
|
yaml.safe_dump(file_out, tmp)
|
2013-04-03 01:27:38 +00:00
|
|
|
metadata = "--meta-data=%s" % tmp.name
|
2013-04-23 04:08:22 +00:00
|
|
|
dbrst = _get_downburst_exec()
|
|
|
|
if not dbrst:
|
|
|
|
log.info("Error: no downburst executable found")
|
|
|
|
return False
|
|
|
|
p = subprocess.Popen([dbrst, '-c', phys_host,
|
2013-04-03 01:27:38 +00:00
|
|
|
'create', metadata, createMe],
|
|
|
|
stdout=subprocess.PIPE,stderr=subprocess.PIPE,)
|
|
|
|
owt,err = p.communicate()
|
|
|
|
if err:
|
2013-06-07 01:43:43 +00:00
|
|
|
log.info("Downburst completed on %s: %s" %
|
2013-04-03 01:27:38 +00:00
|
|
|
(machine_name,err))
|
|
|
|
else:
|
|
|
|
log.info("%s created: %s" % (machine_name,owt))
|
2013-07-23 23:10:24 +00:00
|
|
|
#If the guest already exists first destroy then re-create:
|
|
|
|
if 'exists' in err:
|
|
|
|
log.info("Guest files exist. Re-creating guest: %s" %
|
|
|
|
(machine_name))
|
|
|
|
destroy_if_vm(ctx, machine_name)
|
|
|
|
create_if_vm(ctx, machine_name)
|
2013-04-03 01:27:38 +00:00
|
|
|
return True
|
|
|
|
#
|
|
|
|
# Use downburst to destroy a virtual machine
|
|
|
|
#
|
2013-04-24 00:28:27 +00:00
|
|
|
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']
|
|
|
|
if not phys_host:
|
|
|
|
return True
|
2013-04-03 01:27:38 +00:00
|
|
|
destroyMe = decanonicalize_hostname(machine_name)
|
2013-04-23 04:08:22 +00:00
|
|
|
dbrst = _get_downburst_exec()
|
|
|
|
if not dbrst:
|
|
|
|
log.info("Error: no downburst executable found")
|
|
|
|
return False
|
|
|
|
p = subprocess.Popen([dbrst, '-c', phys_host,
|
2013-04-03 01:27:38 +00:00
|
|
|
'destroy', destroyMe],
|
|
|
|
stdout=subprocess.PIPE,stderr=subprocess.PIPE,)
|
|
|
|
owt,err = p.communicate()
|
|
|
|
if err:
|
|
|
|
log.info("Error occurred while deleting %s" % destroyMe)
|
2013-04-24 00:28:27 +00:00
|
|
|
return False
|
2013-04-03 01:27:38 +00:00
|
|
|
else:
|
|
|
|
log.info("%s destroyed: %s" % (machine_name,owt))
|
2013-04-24 00:28:27 +00:00
|
|
|
return True
|
2013-04-03 01:27:38 +00:00
|
|
|
|