2013-10-16 19:31:26 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import beanstalkc
|
|
|
|
import yaml
|
|
|
|
import psutil
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2013-10-17 14:42:02 +00:00
|
|
|
import logging
|
2013-10-16 19:31:26 +00:00
|
|
|
|
|
|
|
from .config import config
|
|
|
|
|
2013-10-17 14:42:02 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-10-16 19:31:26 +00:00
|
|
|
|
|
|
|
def main(args):
|
|
|
|
suite_name = args['--suite']
|
|
|
|
archive_base = args['--archive']
|
|
|
|
owner = args['--owner']
|
|
|
|
machine_type = args['--machine_type']
|
|
|
|
|
|
|
|
kill_suite(suite_name, archive_base, owner, machine_type)
|
|
|
|
|
|
|
|
|
|
|
|
def kill_suite(suite_name, archive_base=None, owner=None, machine_type=None):
|
2013-10-17 18:02:30 +00:00
|
|
|
suite_info = {}
|
2013-10-16 19:31:26 +00:00
|
|
|
if archive_base:
|
|
|
|
suite_archive_dir = os.path.join(archive_base, suite_name)
|
2013-10-17 18:02:30 +00:00
|
|
|
suite_info = find_suite_info(suite_archive_dir)
|
|
|
|
machine_type = suite_info['machine_type']
|
|
|
|
owner = suite_info['owner']
|
2013-10-16 19:31:26 +00:00
|
|
|
|
|
|
|
remove_beanstalk_jobs(suite_name, machine_type)
|
2013-10-17 18:02:30 +00:00
|
|
|
kill_processes(suite_name, suite_info.get('pids'))
|
2013-10-16 19:31:26 +00:00
|
|
|
nuke_machines(suite_name, owner)
|
|
|
|
|
|
|
|
|
|
|
|
def find_suite_info(suite_archive_dir):
|
2013-10-17 18:02:30 +00:00
|
|
|
suite_info_fields = [
|
|
|
|
'machine_type',
|
|
|
|
'owner',
|
|
|
|
]
|
|
|
|
|
2013-12-10 22:35:05 +00:00
|
|
|
suite_info = dict(pids=[])
|
2013-10-16 19:31:26 +00:00
|
|
|
job_info = {}
|
2013-10-17 18:02:30 +00:00
|
|
|
for job_id in os.listdir(suite_archive_dir):
|
|
|
|
job_dir = os.path.join(suite_archive_dir, job_id)
|
|
|
|
if not os.path.isdir(job_dir):
|
|
|
|
break
|
|
|
|
job_info = find_job_info(job_dir)
|
|
|
|
for key in job_info.keys():
|
|
|
|
if key in suite_info_fields and key not in suite_info:
|
|
|
|
suite_info[key] = job_info[key]
|
2013-12-10 22:25:28 +00:00
|
|
|
if 'pid' in job_info:
|
2013-12-10 22:35:05 +00:00
|
|
|
suite_info['pids'].append(job_info['pid'])
|
2013-10-17 18:02:30 +00:00
|
|
|
return suite_info
|
2013-10-16 19:31:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_job_info(job_archive_dir):
|
2013-10-17 18:02:30 +00:00
|
|
|
job_info = {}
|
|
|
|
|
2013-10-16 19:31:26 +00:00
|
|
|
info_file = os.path.join(job_archive_dir, 'info.yaml')
|
|
|
|
if os.path.isfile(info_file):
|
2013-10-17 18:02:30 +00:00
|
|
|
job_info.update(yaml.safe_load(open(info_file, 'r')))
|
|
|
|
|
|
|
|
conf_file = os.path.join(job_archive_dir, 'config.yaml')
|
|
|
|
if os.path.isfile(conf_file):
|
|
|
|
job_info.update(yaml.safe_load(open(conf_file, 'r')))
|
|
|
|
|
|
|
|
return job_info
|
2013-10-16 19:31:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def remove_beanstalk_jobs(suite_name, tube_name):
|
|
|
|
qhost = config.queue_host
|
|
|
|
qport = config.queue_port
|
|
|
|
if qhost is None or qport is None:
|
|
|
|
raise RuntimeError(
|
|
|
|
'Beanstalk queue information not found in {conf_path}'.format(
|
|
|
|
conf_path=config.teuthology_yaml))
|
2013-10-17 14:42:02 +00:00
|
|
|
log.info("Checking Beanstalk Queue...")
|
2013-10-16 19:31:26 +00:00
|
|
|
beanstalk = beanstalkc.Connection(host=qhost, port=qport)
|
|
|
|
beanstalk.watch(tube_name)
|
|
|
|
beanstalk.ignore('default')
|
|
|
|
|
|
|
|
curjobs = beanstalk.stats_tube(tube_name)['current-jobs-ready']
|
|
|
|
if curjobs != 0:
|
|
|
|
x = 1
|
|
|
|
while x != curjobs:
|
|
|
|
x += 1
|
|
|
|
job = beanstalk.reserve(timeout=20)
|
|
|
|
job_config = yaml.safe_load(job.body)
|
|
|
|
if suite_name == job_config['name']:
|
|
|
|
job_id = job.stats()['id']
|
|
|
|
msg = "Deleting job from queue. ID: " + \
|
|
|
|
"{id} Name: {name} Desc: {desc}".format(
|
|
|
|
id=str(job_id),
|
|
|
|
name=job_config['name'],
|
|
|
|
desc=job_config['description'],
|
|
|
|
)
|
2013-10-17 14:42:02 +00:00
|
|
|
log.info(msg)
|
2013-10-16 19:31:26 +00:00
|
|
|
job.delete()
|
|
|
|
else:
|
|
|
|
print "No jobs in Beanstalk Queue"
|
|
|
|
beanstalk.close()
|
|
|
|
|
|
|
|
|
2013-10-17 18:02:30 +00:00
|
|
|
def kill_processes(suite_name, pids=None):
|
|
|
|
if pids:
|
|
|
|
to_kill = set(pids).intersection(psutil.get_pid_list())
|
|
|
|
else:
|
|
|
|
to_kill = find_pids(suite_name)
|
|
|
|
|
|
|
|
if len(to_kill) == 0:
|
|
|
|
log.info("No teuthology processes running")
|
|
|
|
else:
|
|
|
|
log.info("Killing Pids: " + str(to_kill))
|
|
|
|
for pid in to_kill:
|
|
|
|
subprocess.call(['sudo', 'kill', str(pid)])
|
|
|
|
|
|
|
|
|
|
|
|
def find_pids(suite_name):
|
2013-10-16 19:31:26 +00:00
|
|
|
suite_pids = []
|
|
|
|
for pid in psutil.get_pid_list():
|
|
|
|
try:
|
|
|
|
p = psutil.Process(pid)
|
|
|
|
except psutil.NoSuchProcess:
|
|
|
|
continue
|
|
|
|
if suite_name in p.cmdline and sys.argv[0] not in p.cmdline:
|
2013-10-17 18:02:30 +00:00
|
|
|
suite_pids.append(pid)
|
|
|
|
return suite_pids
|
2013-10-16 19:31:26 +00:00
|
|
|
|
|
|
|
|
2013-10-17 14:42:02 +00:00
|
|
|
def find_targets(suite_name, owner):
|
|
|
|
lock_args = [
|
|
|
|
'teuthology-lock',
|
|
|
|
'--list-targets',
|
|
|
|
'--desc-pattern',
|
|
|
|
'/' + suite_name + '/',
|
|
|
|
'--status',
|
|
|
|
'up',
|
|
|
|
'--owner',
|
|
|
|
owner
|
|
|
|
]
|
|
|
|
proc = subprocess.Popen(lock_args, stdout=subprocess.PIPE)
|
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
out_obj = yaml.safe_load(stdout)
|
|
|
|
if not out_obj or 'targets' not in out_obj:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return out_obj
|
|
|
|
|
|
|
|
|
2013-10-16 19:31:26 +00:00
|
|
|
def nuke_machines(suite_name, owner):
|
2013-10-17 14:42:02 +00:00
|
|
|
targets_dict = find_targets(suite_name, owner)
|
|
|
|
targets = targets_dict.get('targets')
|
|
|
|
if not targets:
|
|
|
|
log.info("No locked machines. Not nuking anything")
|
|
|
|
|
|
|
|
to_nuke = []
|
2013-10-16 19:31:26 +00:00
|
|
|
for target in targets:
|
2013-10-17 14:42:02 +00:00
|
|
|
to_nuke.append(target.split('@')[1].split('.')[0])
|
|
|
|
|
|
|
|
target_file = tempfile.NamedTemporaryFile(delete=False)
|
|
|
|
target_file.write(yaml.safe_dump(targets_dict))
|
|
|
|
target_file.close()
|
|
|
|
|
|
|
|
log.info("Nuking machines: " + str(to_nuke))
|
|
|
|
nuke_args = [
|
|
|
|
'teuthology-nuke',
|
|
|
|
'-t',
|
|
|
|
target_file.name,
|
|
|
|
'--unlock',
|
|
|
|
'-r',
|
|
|
|
'--owner',
|
|
|
|
owner
|
|
|
|
]
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
nuke_args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
for line in iter(proc.stdout.readline, ''):
|
|
|
|
line = line.replace('\r', '').replace('\n', '')
|
|
|
|
log.info(line)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
os.unlink(target_file.name)
|