ceph/teuthology/schedule.py

83 lines
2.6 KiB
Python
Raw Normal View History

2013-10-09 20:13:01 +00:00
import yaml
import teuthology.beanstalk
from teuthology.misc import deep_merge, get_user
from teuthology import report
2013-10-09 20:13:01 +00:00
def main(args):
if not args['--last-in-suite']:
if args['--email']:
raise ValueError(
'--email is only applicable to the last job in a suite')
if args['--timeout']:
raise ValueError(
'--timeout is only applicable to the last job in a suite')
job_config = build_config(args)
schedule_job(job_config, args['--num'])
2013-10-09 20:13:01 +00:00
def build_config(args):
"""
Given a dict of arguments, build a job config
"""
config_paths = args.get('<conf_file>', list())
conf_dict = dict()
for conf_path in config_paths:
with file(conf_path) as partial_file:
partial_dict = yaml.safe_load(partial_file)
conf_dict = deep_merge(conf_dict, partial_dict)
2013-10-09 20:13:01 +00:00
# strip out targets; the worker will allocate new ones when we run
# the job with --lock.
if 'targets' in conf_dict:
del conf_dict['targets']
args['config'] = conf_dict
owner = args['--owner']
if owner is None:
owner = 'scheduled_{user}'.format(user=get_user())
2013-10-09 20:13:01 +00:00
job_config = dict(
name=args['--name'],
last_in_suite=args['--last-in-suite'],
email=args['--email'],
description=args['--description'],
owner=owner,
verbose=args['--verbose'],
machine_type=args['--worker'],
tube=args['--worker'],
priority=int(args['--priority']),
2013-10-09 20:13:01 +00:00
)
# Update the dict we just created, and not the other way around, to let
# settings in the yaml override what's passed on the command line. This is
# primarily to accommodate jobs with multiple machine types.
job_config.update(conf_dict)
if args['--timeout'] is not None:
job_config['results_timeout'] = args['--timeout']
return job_config
2013-10-09 20:13:01 +00:00
def schedule_job(job_config, num=1):
"""
Schedule a job.
:param job_config: The complete job dict
:param num: The number of times to schedule the job
"""
num = int(num)
2013-10-09 20:13:01 +00:00
job = yaml.safe_dump(job_config)
tube = job_config.pop('tube')
beanstalk = teuthology.beanstalk.connect()
beanstalk.use(tube)
2013-10-09 20:13:01 +00:00
while num > 0:
jid = beanstalk.put(
job,
ttr=60 * 60 * 24,
priority=job_config['priority'],
2013-10-09 20:13:01 +00:00
)
print 'Job scheduled with name {name} and ID {jid}'.format(
name=job_config['name'], jid=jid)
job_config['job_id'] = str(jid)
report.try_push_job_info(job_config, dict(status='queued'))
2013-10-09 20:13:01 +00:00
num -= 1