blob: eaf4de9b8cafde05e977a94d9d1566ad6f5071cf [file] [log] [blame]
showard26b7ec72009-12-21 22:43:57 +00001"""\
2Functions to expose over the RPC interface.
3"""
4
5__author__ = 'jamesren@google.com (James Ren)'
6
7
jamesrenc3940222010-02-19 21:57:37 +00008import os
9import common
10from django.db import models as django_models
11from autotest_lib.frontend import thread_local
12from autotest_lib.frontend.afe import model_logic, models as afe_models
13from autotest_lib.frontend.afe import rpc_utils as afe_rpc_utils
14from autotest_lib.frontend.planner import models, rpc_utils
15from autotest_lib.client.common_lib import utils
16
17# basic getter/setter calls
18# TODO: deprecate the basic calls and reimplement them in the REST framework
19
20def get_plan(id):
21 return afe_rpc_utils.prepare_for_serialization(
22 models.Plan.smart_get(id).get_object_dict())
23
24
25def modify_plan(id, **data):
26 models.Plan.smart_get(id).update_object(data)
27
28
29# more advanced calls
30
31def submit_plan(name, hosts, host_labels, tests,
32 support=None, label_override=None):
33 """
34 Submits a plan to the Test Planner
35
36 @param name: the name of the plan
37 @param hosts: a list of hostnames
38 @param host_labels: a list of host labels. The hosts under test will update
39 to reflect changes in the label
40 @param tests: a list of test control files to run
41 @param support: the global support object
42 @param label_override: label to prepend to all AFE jobs for this test plan.
43 Defaults to the plan name.
44 """
45 host_objects = []
46 label_objects = []
47
48 for host in hosts or []:
49 try:
50 host_objects.append(
51 afe_models.Host.valid_objects.get(hostname=host))
52 except afe_models.Host.DoesNotExist:
53 raise model_logic.ValidationError(
54 {'hosts': 'host %s does not exist' % host})
55
56 for label in host_labels or []:
57 try:
58 label_objects.append(afe_models.Label.valid_objects.get(name=label))
59 except afe_models.Label.DoesNotExist:
60 raise model_logic.ValidationError(
61 {'host_labels': 'host label %s does not exist' % label})
62
63 plan, created = models.Plan.objects.get_or_create(name=name)
64 if not created:
65 raise model_logic.ValidationError(
66 {'name': 'Plan name %s already exists' % name})
67
68 try:
69 label = rpc_utils.create_plan_label(plan)
70 except:
71 plan.delete()
72 raise
73
74 plan.label_override = label_override
75 plan.support = support or ''
76 plan.save()
77
78 plan.owners.add(afe_models.User.current_user())
79
80 for host in host_objects:
81 planner_host = models.Host.objects.create(plan=plan, host=host)
82
83 plan.host_labels.add(*label_objects)
84
85 rpc_utils.start_plan(plan, label)
86
87 return plan.id
88
89
90def get_hosts(plan_id):
91 """
92 Gets the hostnames of all the hosts in this test plan.
93
94 Resolves host labels in the plan.
95 """
96 plan = models.Plan.smart_get(plan_id)
97
98 hosts = set(plan.hosts.all().values_list('hostname', flat=True))
99 for label in plan.host_labels.all():
100 hosts.update(label.host_set.all().values_list('hostname', flat=True))
101
102 return afe_rpc_utils.prepare_for_serialization(hosts)
103
104
105def get_atomic_group_control_file():
106 """
107 Gets the control file to apply the atomic group for a set of machines
108 """
109 return rpc_utils.lazy_load(os.path.join(os.path.dirname(__file__),
110 'set_atomic_group_control.srv'))