showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 1 | """\ |
| 2 | Functions to expose over the RPC interface. |
| 3 | """ |
| 4 | |
| 5 | __author__ = 'jamesren@google.com (James Ren)' |
| 6 | |
| 7 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 8 | import os |
| 9 | import common |
| 10 | from django.db import models as django_models |
| 11 | from autotest_lib.frontend import thread_local |
| 12 | from autotest_lib.frontend.afe import model_logic, models as afe_models |
| 13 | from autotest_lib.frontend.afe import rpc_utils as afe_rpc_utils |
| 14 | from autotest_lib.frontend.planner import models, rpc_utils |
| 15 | from 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 | |
| 20 | def get_plan(id): |
| 21 | return afe_rpc_utils.prepare_for_serialization( |
| 22 | models.Plan.smart_get(id).get_object_dict()) |
| 23 | |
| 24 | |
| 25 | def modify_plan(id, **data): |
| 26 | models.Plan.smart_get(id).update_object(data) |
| 27 | |
| 28 | |
| 29 | # more advanced calls |
| 30 | |
| 31 | def 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 | |
| 90 | def 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 | |
| 105 | def 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')) |