blob: 8d359bdb936190027d3b9f56e4412e800a5974ca [file] [log] [blame]
jamesrenc3940222010-02-19 21:57:37 +00001import common
2import os
3from autotest_lib.frontend.afe import models as afe_models, model_logic
4from autotest_lib.frontend.planner import models
5from autotest_lib.frontend.shared import rest_client
6from autotest_lib.client.common_lib import global_config, utils
7
8
9PLANNER_LABEL_PREFIX = 'planner_'
10PLANNER_ATOMIC_GROUP_NAME = 'planner_global_atomic_group'
11SERVER = global_config.global_config.get_config_value('SERVER', 'hostname')
12LAZY_LOADED_FILES = {}
13
14
15def create_plan_label(plan):
16 """
17 Creates the host label to apply on the plan hosts
18 """
19 group, _ = afe_models.AtomicGroup.objects.get_or_create(
20 name=PLANNER_ATOMIC_GROUP_NAME)
21 if group.invalid:
22 group.invalid = False
23 group.save()
24
25 name = PLANNER_LABEL_PREFIX + plan.name
26 if bool(afe_models.Label.valid_objects.filter(name=name)):
27 raise model_logic.ValidationError('Label %s already exists, '
28 'cannot start plan' % name)
29 label = afe_models.Label(name=name, atomic_group=group)
30 label.save()
31
32 return label
33
34
35def start_plan(plan, label):
36 """
37 Takes the necessary steps to start a test plan in Autotest
38 """
39 afe_rest = rest_client.Resource.load(
40 'http://%s/afe/server/resources' % SERVER)
41
42 keyvals = {'server': SERVER,
43 'plan_id': plan.id,
44 'label_name': label.name}
45
46 info = afe_rest.execution_info.get().execution_info
47 info['control_file'] = _get_execution_engine_control()
48 info['machines_per_execution'] = None
49
50 job_req = {'name': plan.name + '_execution_engine',
51 'execution_info': info,
52 'queue_entries': (),
53 'keyvals': keyvals}
54
55 afe_rest.jobs.post(job_req)
56
57
58def _get_execution_engine_control():
59 """
60 Gets the control file to run the execution engine
61 """
62 return lazy_load(os.path.join(os.path.dirname(__file__),
63 'execution_engine_control.srv'))
64
65
66def lazy_load(path):
67 """
68 Lazily loads the file indicated by the path given, and caches the result
69 """
70 if path not in LAZY_LOADED_FILES:
71 LAZY_LOADED_FILES[path] = utils.read_file(path)
72
73 return LAZY_LOADED_FILES[path]