showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 1 | from django import http |
Eric Li | 0a99391 | 2011-05-17 12:56:25 -0700 | [diff] [blame] | 2 | from autotest_lib.frontend.shared import query_lib, resource_lib, exceptions |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 3 | from autotest_lib.frontend.afe import control_file, models, rpc_utils |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 4 | from autotest_lib.frontend.afe import model_attributes |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 5 | from autotest_lib.frontend import thread_local |
| 6 | from autotest_lib.client.common_lib import host_protections |
| 7 | |
Eric Li | 0a99391 | 2011-05-17 12:56:25 -0700 | [diff] [blame] | 8 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 9 | class EntryWithInvalid(resource_lib.InstanceEntry): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 10 | def put(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 11 | if self.instance.invalid: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 12 | raise http.Http404('%s has been deleted' % self.instance) |
| 13 | return super(EntryWithInvalid, self).put() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 14 | |
| 15 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 16 | def delete(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 17 | if self.instance.invalid: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 18 | raise http.Http404('%s has already been deleted' % self.instance) |
| 19 | return super(EntryWithInvalid, self).delete() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class AtomicGroupClass(EntryWithInvalid): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 23 | model = models.AtomicGroup |
| 24 | |
| 25 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 26 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 27 | def from_uri_args(cls, request, ag_name, **kwargs): |
| 28 | return cls(request, models.AtomicGroup.objects.get(name=ag_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 32 | return {'ag_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def short_representation(self): |
| 36 | rep = super(AtomicGroupClass, self).short_representation() |
| 37 | rep['name'] = self.instance.name |
| 38 | return rep |
| 39 | |
| 40 | |
| 41 | def full_representation(self): |
| 42 | rep = super(AtomicGroupClass, self).full_representation() |
| 43 | rep.update({'max_number_of_machines': |
| 44 | self.instance.max_number_of_machines, |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 45 | 'labels': |
| 46 | AtomicLabelTaggingCollection(fixed_entry=self).link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 47 | return rep |
| 48 | |
| 49 | |
| 50 | @classmethod |
| 51 | def create_instance(cls, input_dict, containing_collection): |
| 52 | cls._check_for_required_fields(input_dict, ('name',)) |
| 53 | return models.AtomicGroup.add_object(name=input_dict['name']) |
| 54 | |
| 55 | |
| 56 | def update(self, input_dict): |
| 57 | data = {'max_number_of_machines': |
| 58 | input_dict.get('max_number_of_machines')} |
| 59 | data = input_dict.remove_unspecified_fields(data) |
| 60 | self.instance.update_object(**data) |
| 61 | |
| 62 | |
| 63 | class AtomicGroupClassCollection(resource_lib.Collection): |
| 64 | queryset = models.AtomicGroup.valid_objects.all() |
| 65 | entry_class = AtomicGroupClass |
| 66 | |
| 67 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 68 | class Label(EntryWithInvalid): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 69 | model = models.Label |
| 70 | |
| 71 | @classmethod |
| 72 | def add_query_selectors(cls, query_processor): |
| 73 | query_processor.add_field_selector('name') |
| 74 | query_processor.add_field_selector( |
| 75 | 'is_platform', field='platform', |
| 76 | value_transform=query_processor.read_boolean) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 80 | def from_uri_args(cls, request, label_name, **kwargs): |
| 81 | return cls(request, models.Label.objects.get(name=label_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 85 | return {'label_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def short_representation(self): |
| 89 | rep = super(Label, self).short_representation() |
| 90 | rep.update({'name': self.instance.name, |
| 91 | 'is_platform': bool(self.instance.platform)}) |
| 92 | return rep |
| 93 | |
| 94 | |
| 95 | def full_representation(self): |
| 96 | rep = super(Label, self).full_representation() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 97 | atomic_group_class = AtomicGroupClass.from_optional_instance( |
| 98 | self._request, self.instance.atomic_group) |
| 99 | rep.update({'atomic_group_class': |
| 100 | atomic_group_class.short_representation(), |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 101 | 'hosts': HostLabelingCollection(fixed_entry=self).link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 102 | return rep |
| 103 | |
| 104 | |
| 105 | @classmethod |
| 106 | def create_instance(cls, input_dict, containing_collection): |
| 107 | cls._check_for_required_fields(input_dict, ('name',)) |
| 108 | return models.Label.add_object(name=input_dict['name']) |
| 109 | |
| 110 | |
| 111 | def update(self, input_dict): |
| 112 | # TODO update atomic group |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 113 | if 'is_platform' in input_dict: |
| 114 | self.instance.platform = input_dict['is_platform'] |
| 115 | self.instance.save() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | class LabelCollection(resource_lib.Collection): |
| 119 | queryset = models.Label.valid_objects.all() |
| 120 | entry_class = Label |
| 121 | |
| 122 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 123 | class AtomicLabelTagging(resource_lib.Relationship): |
| 124 | related_classes = {'label': Label, 'atomic_group_class': AtomicGroupClass} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 125 | |
| 126 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 127 | class AtomicLabelTaggingCollection(resource_lib.RelationshipCollection): |
| 128 | entry_class = AtomicLabelTagging |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 129 | |
| 130 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 131 | class User(resource_lib.InstanceEntry): |
| 132 | model = models.User |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 133 | _permitted_methods = ('GET,') |
| 134 | |
| 135 | |
| 136 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 137 | def from_uri_args(cls, request, username, **kwargs): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 138 | if username == '@me': |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 139 | username = models.User.current_user().login |
| 140 | return cls(request, models.User.objects.get(login=username)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 144 | return {'username': self.instance.login} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def short_representation(self): |
| 148 | rep = super(User, self).short_representation() |
| 149 | rep['username'] = self.instance.login |
| 150 | return rep |
| 151 | |
| 152 | |
| 153 | def full_representation(self): |
| 154 | rep = super(User, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 155 | accessible_hosts = HostCollection(self._request) |
| 156 | accessible_hosts.set_query_parameters(accessible_by=self.instance.login) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 157 | rep.update({'jobs': 'TODO', |
| 158 | 'recurring_runs': 'TODO', |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 159 | 'acls': |
| 160 | UserAclMembershipCollection(fixed_entry=self).link(), |
| 161 | 'accessible_hosts': accessible_hosts.link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 162 | return rep |
| 163 | |
| 164 | |
| 165 | class UserCollection(resource_lib.Collection): |
| 166 | _permitted_methods = ('GET',) |
| 167 | queryset = models.User.objects.all() |
| 168 | entry_class = User |
| 169 | |
| 170 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 171 | class Acl(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 172 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 173 | model = models.AclGroup |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 174 | |
| 175 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 176 | def from_uri_args(cls, request, acl_name, **kwargs): |
| 177 | return cls(request, models.AclGroup.objects.get(name=acl_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 181 | return {'acl_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | def short_representation(self): |
| 185 | rep = super(Acl, self).short_representation() |
| 186 | rep['name'] = self.instance.name |
| 187 | return rep |
| 188 | |
| 189 | |
| 190 | def full_representation(self): |
| 191 | rep = super(Acl, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 192 | rep.update({'users': |
| 193 | UserAclMembershipCollection(fixed_entry=self).link(), |
| 194 | 'hosts': |
| 195 | HostAclMembershipCollection(fixed_entry=self).link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 196 | return rep |
| 197 | |
| 198 | |
| 199 | @classmethod |
| 200 | def create_instance(cls, input_dict, containing_collection): |
| 201 | cls._check_for_required_fields(input_dict, ('name',)) |
| 202 | return models.AclGroup.add_object(name=input_dict['name']) |
| 203 | |
| 204 | |
| 205 | def update(self, input_dict): |
| 206 | pass |
| 207 | |
| 208 | |
| 209 | class AclCollection(resource_lib.Collection): |
| 210 | queryset = models.AclGroup.objects.all() |
| 211 | entry_class = Acl |
| 212 | |
| 213 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 214 | class UserAclMembership(resource_lib.Relationship): |
| 215 | related_classes = {'user': User, 'acl': Acl} |
| 216 | |
| 217 | # TODO: check permissions |
| 218 | # TODO: check for and add/remove "Everyone" |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 219 | |
| 220 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 221 | class UserAclMembershipCollection(resource_lib.RelationshipCollection): |
| 222 | entry_class = UserAclMembership |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | class Host(EntryWithInvalid): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 226 | model = models.Host |
| 227 | |
| 228 | @classmethod |
| 229 | def add_query_selectors(cls, query_processor): |
| 230 | query_processor.add_field_selector('hostname') |
| 231 | query_processor.add_field_selector( |
| 232 | 'locked', value_transform=query_processor.read_boolean) |
| 233 | query_processor.add_field_selector( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 234 | 'locked_by', field='locked_by__login', |
| 235 | doc='Username of user who locked this host, if locked') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 236 | query_processor.add_field_selector('status') |
| 237 | query_processor.add_field_selector( |
| 238 | 'protection_level', field='protection', |
| 239 | doc='Verify/repair protection level', |
| 240 | value_transform=cls._read_protection) |
| 241 | query_processor.add_field_selector( |
| 242 | 'accessible_by', field='aclgroup__users__login', |
| 243 | doc='Username of user with access to this host') |
| 244 | query_processor.add_related_existence_selector( |
| 245 | 'has_label', models.Label, 'name') |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 246 | |
| 247 | |
| 248 | @classmethod |
| 249 | def _read_protection(cls, protection_input): |
| 250 | return host_protections.Protection.get_value(protection_input) |
| 251 | |
| 252 | |
| 253 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 254 | def from_uri_args(cls, request, hostname, **kwargs): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 255 | return cls(request, models.Host.objects.get(hostname=hostname)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 259 | return {'hostname': self.instance.hostname} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 260 | |
| 261 | |
| 262 | def short_representation(self): |
| 263 | rep = super(Host, self).short_representation() |
| 264 | # TODO calling platform() over and over is inefficient |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 265 | platform_rep = (Label.from_optional_instance(self._request, |
| 266 | self.instance.platform()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 267 | .short_representation()) |
| 268 | rep.update({'hostname': self.instance.hostname, |
| 269 | 'locked': bool(self.instance.locked), |
| 270 | 'status': self.instance.status, |
| 271 | 'platform': platform_rep}) |
| 272 | return rep |
| 273 | |
| 274 | |
| 275 | def full_representation(self): |
| 276 | rep = super(Host, self).full_representation() |
| 277 | protection = host_protections.Protection.get_string( |
| 278 | self.instance.protection) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 279 | locked_by = (User.from_optional_instance(self._request, |
| 280 | self.instance.locked_by) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 281 | .short_representation()) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 282 | labels = HostLabelingCollection(fixed_entry=self) |
| 283 | acls = HostAclMembershipCollection(fixed_entry=self) |
| 284 | queue_entries = QueueEntryCollection(self._request) |
| 285 | queue_entries.set_query_parameters(host=self.instance.hostname) |
| 286 | health_tasks = HealthTaskCollection(self._request) |
| 287 | health_tasks.set_query_parameters(host=self.instance.hostname) |
| 288 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 289 | rep.update({'locked_by': locked_by, |
| 290 | 'locked_on': self._format_datetime(self.instance.lock_time), |
| 291 | 'invalid': self.instance.invalid, |
| 292 | 'protection_level': protection, |
| 293 | # TODO make these efficient |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 294 | 'labels': labels.full_representation(), |
| 295 | 'acls': acls.full_representation(), |
| 296 | 'queue_entries': queue_entries.link(), |
| 297 | 'health_tasks': health_tasks.link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 298 | return rep |
| 299 | |
| 300 | |
| 301 | @classmethod |
| 302 | def create_instance(cls, input_dict, containing_collection): |
| 303 | cls._check_for_required_fields(input_dict, ('hostname',)) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 304 | # include locked here, rather than waiting for update(), to avoid race |
| 305 | # conditions |
| 306 | host = models.Host.add_object(hostname=input_dict['hostname'], |
| 307 | locked=input_dict.get('locked', False)) |
| 308 | return host |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 309 | |
| 310 | def update(self, input_dict): |
| 311 | data = {'locked': input_dict.get('locked'), |
| 312 | 'protection': input_dict.get('protection_level')} |
| 313 | data = input_dict.remove_unspecified_fields(data) |
| 314 | |
| 315 | if 'protection' in data: |
| 316 | data['protection'] = self._read_protection(data['protection']) |
| 317 | |
| 318 | self.instance.update_object(**data) |
| 319 | |
| 320 | if 'platform' in input_dict: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 321 | label = self.resolve_link(input_dict['platform']) .instance |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 322 | if not label.platform: |
Eric Li | 0a99391 | 2011-05-17 12:56:25 -0700 | [diff] [blame] | 323 | raise exceptions.BadRequest('Label %s is not a platform' % label.name) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 324 | for label in self.instance.labels.filter(platform=True): |
| 325 | self.instance.labels.remove(label) |
| 326 | self.instance.labels.add(label) |
| 327 | |
| 328 | |
| 329 | class HostCollection(resource_lib.Collection): |
| 330 | queryset = models.Host.valid_objects.all() |
| 331 | entry_class = Host |
| 332 | |
| 333 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 334 | class HostLabeling(resource_lib.Relationship): |
| 335 | related_classes = {'host': Host, 'label': Label} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 336 | |
| 337 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 338 | class HostLabelingCollection(resource_lib.RelationshipCollection): |
| 339 | entry_class = HostLabeling |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 340 | |
| 341 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 342 | class HostAclMembership(resource_lib.Relationship): |
| 343 | related_classes = {'host': Host, 'acl': Acl} |
| 344 | |
| 345 | # TODO: acl.check_for_acl_violation_acl_group() |
| 346 | # TODO: models.AclGroup.on_host_membership_change() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 347 | |
| 348 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 349 | class HostAclMembershipCollection(resource_lib.RelationshipCollection): |
| 350 | entry_class = HostAclMembership |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 351 | |
| 352 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 353 | class Test(resource_lib.InstanceEntry): |
| 354 | model = models.Test |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 355 | |
| 356 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 357 | @classmethod |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 358 | def add_query_selectors(cls, query_processor): |
| 359 | query_processor.add_field_selector('name') |
| 360 | |
| 361 | |
| 362 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 363 | def from_uri_args(cls, request, test_name, **kwargs): |
| 364 | return cls(request, models.Test.objects.get(name=test_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 365 | |
| 366 | |
| 367 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 368 | return {'test_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 369 | |
| 370 | |
| 371 | def short_representation(self): |
| 372 | rep = super(Test, self).short_representation() |
| 373 | rep['name'] = self.instance.name |
| 374 | return rep |
| 375 | |
| 376 | |
| 377 | def full_representation(self): |
| 378 | rep = super(Test, self).full_representation() |
| 379 | rep.update({'author': self.instance.author, |
| 380 | 'class': self.instance.test_class, |
| 381 | 'control_file_type': |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 382 | model_attributes.TestTypes.get_string( |
| 383 | self.instance.test_type), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 384 | 'control_file_path': self.instance.path, |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 385 | 'sync_count': self.instance.sync_count, |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 386 | 'dependencies': |
| 387 | TestDependencyCollection(fixed_entry=self).link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 388 | }) |
| 389 | return rep |
| 390 | |
| 391 | |
| 392 | @classmethod |
| 393 | def create_instance(cls, input_dict, containing_collection): |
| 394 | cls._check_for_required_fields(input_dict, |
| 395 | ('name', 'control_file_type', |
| 396 | 'control_file_path')) |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 397 | test_type = model_attributes.TestTypes.get_value( |
| 398 | input['control_file_type']) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 399 | return models.Test.add_object(name=input_dict['name'], |
| 400 | test_type=test_type, |
| 401 | path=input_dict['control_file_path']) |
| 402 | |
| 403 | |
| 404 | def update(self, input_dict): |
| 405 | data = {'test_type': input_dict.get('control_file_type'), |
| 406 | 'path': input_dict.get('control_file_path'), |
| 407 | 'class': input_dict.get('class'), |
| 408 | } |
| 409 | data = input_dict.remove_unspecified_fields(data) |
| 410 | self.instance.update_object(**data) |
| 411 | |
| 412 | |
| 413 | class TestCollection(resource_lib.Collection): |
| 414 | queryset = models.Test.objects.all() |
| 415 | entry_class = Test |
| 416 | |
| 417 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 418 | class TestDependency(resource_lib.Relationship): |
| 419 | related_classes = {'test': Test, 'label': Label} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 420 | |
| 421 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 422 | class TestDependencyCollection(resource_lib.RelationshipCollection): |
| 423 | entry_class = TestDependency |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 424 | |
| 425 | |
| 426 | # TODO profilers |
| 427 | |
| 428 | |
| 429 | class ExecutionInfo(resource_lib.Resource): |
| 430 | _permitted_methods = ('GET','POST') |
| 431 | _job_fields = models.Job.get_field_dict() |
| 432 | _DEFAULTS = { |
| 433 | 'control_file': '', |
| 434 | 'is_server': True, |
| 435 | 'dependencies': [], |
| 436 | 'machines_per_execution': 1, |
| 437 | 'run_verify': bool(_job_fields['run_verify'].default), |
| 438 | 'timeout_hrs': _job_fields['timeout'].default, |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame^] | 439 | 'maximum_runtime_mins': _job_fields['max_runtime_mins'].default, |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 440 | 'cleanup_before_job': |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 441 | model_attributes.RebootBefore.get_string( |
| 442 | models.DEFAULT_REBOOT_BEFORE), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 443 | 'cleanup_after_job': |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 444 | model_attributes.RebootAfter.get_string( |
| 445 | models.DEFAULT_REBOOT_AFTER), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 449 | def _query_parameters_accepted(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 450 | return (('tests', 'Comma-separated list of test names to run'), |
| 451 | ('kernels', 'TODO'), |
| 452 | ('client_control_file', |
| 453 | 'Client control file segment to run after all specified ' |
| 454 | 'tests'), |
| 455 | ('profilers', |
| 456 | 'Comma-separated list of profilers to activate during the ' |
| 457 | 'job'), |
| 458 | ('use_container', 'TODO'), |
| 459 | ('profile_only', |
| 460 | 'If true, run only profiled iterations; otherwise, always run ' |
| 461 | 'at least one non-profiled iteration in addition to a ' |
| 462 | 'profiled iteration'), |
| 463 | ('upload_kernel_config', |
| 464 | 'If true, generate a server control file code that uploads ' |
| 465 | 'the kernel config file to the client and tells the client of ' |
| 466 | 'the new (local) path when compiling the kernel; the tests ' |
| 467 | 'must be server side tests')) |
| 468 | |
| 469 | |
| 470 | @classmethod |
| 471 | def execution_info_from_job(cls, job): |
| 472 | return {'control_file': job.control_file, |
| 473 | 'is_server': job.control_type == models.Job.ControlType.SERVER, |
| 474 | 'dependencies': [label.name for label |
| 475 | in job.dependency_labels.all()], |
| 476 | 'machines_per_execution': job.synch_count, |
| 477 | 'run_verify': bool(job.run_verify), |
| 478 | 'timeout_hrs': job.timeout, |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame^] | 479 | 'maximum_runtime_mins': job.max_runtime_mins, |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 480 | 'cleanup_before_job': |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 481 | model_attributes.RebootBefore.get_string(job.reboot_before), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 482 | 'cleanup_after_job': |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 483 | model_attributes.RebootAfter.get_string(job.reboot_after), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | |
| 487 | def _get_execution_info(self, input_dict): |
| 488 | tests = input_dict.get('tests', '') |
| 489 | client_control_file = input_dict.get('client_control_file', None) |
| 490 | if not tests and not client_control_file: |
| 491 | return self._DEFAULTS |
| 492 | |
| 493 | test_list = tests.split(',') |
| 494 | if 'profilers' in input_dict: |
| 495 | profilers_list = input_dict['profilers'].split(',') |
| 496 | else: |
| 497 | profilers_list = [] |
| 498 | kernels = input_dict.get('kernels', '') # TODO |
| 499 | if kernels: |
| 500 | kernels = [dict(version=kernel) for kernel in kernels.split(',')] |
| 501 | |
| 502 | cf_info, test_objects, profiler_objects, label = ( |
| 503 | rpc_utils.prepare_generate_control_file( |
| 504 | test_list, kernels, None, profilers_list)) |
| 505 | control_file_contents = control_file.generate_control( |
| 506 | tests=test_objects, kernels=kernels, |
| 507 | profilers=profiler_objects, is_server=cf_info['is_server'], |
| 508 | client_control_file=client_control_file, |
| 509 | profile_only=input_dict.get('profile_only', None), |
| 510 | upload_kernel_config=input_dict.get( |
| 511 | 'upload_kernel_config', None)) |
| 512 | return dict(self._DEFAULTS, |
| 513 | control_file=control_file_contents, |
| 514 | is_server=cf_info['is_server'], |
| 515 | dependencies=cf_info['dependencies'], |
| 516 | machines_per_execution=cf_info['synch_count']) |
| 517 | |
| 518 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 519 | def handle_request(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 520 | result = self.link() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 521 | result['execution_info'] = self._get_execution_info( |
| 522 | self._request.REQUEST) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 523 | return self._basic_response(result) |
| 524 | |
| 525 | |
| 526 | class QueueEntriesRequest(resource_lib.Resource): |
| 527 | _permitted_methods = ('GET',) |
| 528 | |
| 529 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 530 | def _query_parameters_accepted(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 531 | return (('hosts', 'Comma-separated list of hostnames'), |
| 532 | ('one_time_hosts', |
| 533 | 'Comma-separated list of hostnames not already in the ' |
| 534 | 'Autotest system'), |
| 535 | ('meta_hosts', |
| 536 | 'Comma-separated list of label names; for each one, an entry ' |
| 537 | 'will be created and assigned at runtime to an available host ' |
| 538 | 'with that label'), |
| 539 | ('atomic_group_class', 'TODO')) |
| 540 | |
| 541 | |
| 542 | def _read_list(self, list_string): |
| 543 | if list_string: |
| 544 | return list_string.split(',') |
| 545 | return [] |
| 546 | |
| 547 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 548 | def handle_request(self): |
| 549 | request_dict = self._request.REQUEST |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 550 | hosts = self._read_list(request_dict.get('hosts')) |
| 551 | one_time_hosts = self._read_list(request_dict.get('one_time_hosts')) |
| 552 | meta_hosts = self._read_list(request_dict.get('meta_hosts')) |
| 553 | atomic_group_class = request_dict.get('atomic_group_class') |
| 554 | |
| 555 | # TODO: bring in all the atomic groups magic from create_job() |
| 556 | |
| 557 | entries = [] |
| 558 | for hostname in one_time_hosts: |
| 559 | models.Host.create_one_time_host(hostname) |
| 560 | for hostname in hosts: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 561 | entry = Host.from_uri_args(self._request, hostname) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 562 | entries.append({'host': entry.link()}) |
| 563 | for label_name in meta_hosts: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 564 | entry = Label.from_uri_args(self._request, label_name) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 565 | entries.append({'meta_host': entry.link()}) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 566 | if atomic_group_class: |
| 567 | entries.append({'atomic_group_class': atomic_group_class}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 568 | |
| 569 | result = self.link() |
| 570 | result['queue_entries'] = entries |
| 571 | return self._basic_response(result) |
| 572 | |
| 573 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 574 | class Job(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 575 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 576 | model = models.Job |
| 577 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 578 | |
| 579 | class _StatusConstraint(query_lib.Constraint): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 580 | def apply_constraint(self, queryset, value, comparison_type, |
| 581 | is_inverse): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 582 | if comparison_type != 'equals' or is_inverse: |
| 583 | raise query_lib.ConstraintError('Can only use this selector ' |
| 584 | 'with equals') |
| 585 | non_queued_statuses = [ |
| 586 | status for status, _ |
| 587 | in models.HostQueueEntry.Status.choices() |
| 588 | if status != models.HostQueueEntry.Status.QUEUED] |
| 589 | if value == 'queued': |
| 590 | return queryset.exclude( |
| 591 | hostqueueentry__status__in=non_queued_statuses) |
| 592 | elif value == 'active': |
| 593 | return queryset.filter( |
| 594 | hostqueueentry__status__in=non_queued_statuses).filter( |
| 595 | hostqueueentry__complete=False).distinct() |
| 596 | elif value == 'complete': |
| 597 | return queryset.exclude(hostqueueentry__complete=False) |
| 598 | else: |
| 599 | raise query_lib.ConstraintError('Value must be one of queued, ' |
| 600 | 'active or complete') |
| 601 | |
| 602 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 603 | @classmethod |
| 604 | def add_query_selectors(cls, query_processor): |
| 605 | query_processor.add_field_selector('id') |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 606 | query_processor.add_field_selector('name') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 607 | query_processor.add_selector( |
| 608 | query_lib.Selector('status', |
| 609 | doc='One of queued, active or complete'), |
| 610 | Job._StatusConstraint()) |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 611 | query_processor.add_keyval_selector('has_keyval', models.JobKeyval, |
| 612 | 'key', 'value') |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 613 | |
| 614 | |
| 615 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 616 | def from_uri_args(cls, request, job_id, **kwargs): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 617 | return cls(request, models.Job.objects.get(id=job_id)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 618 | |
| 619 | |
| 620 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 621 | return {'job_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 622 | |
| 623 | |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 624 | @classmethod |
| 625 | def _do_prepare_for_full_representation(cls, instances): |
| 626 | models.Job.objects.populate_relationships(instances, models.JobKeyval, |
| 627 | 'keyvals') |
| 628 | |
| 629 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 630 | def short_representation(self): |
| 631 | rep = super(Job, self).short_representation() |
| 632 | rep.update({'id': self.instance.id, |
| 633 | 'owner': self.instance.owner, |
| 634 | 'name': self.instance.name, |
| 635 | 'priority': |
| 636 | models.Job.Priority.get_string(self.instance.priority), |
| 637 | 'created_on': |
| 638 | self._format_datetime(self.instance.created_on), |
| 639 | }) |
| 640 | return rep |
| 641 | |
| 642 | |
| 643 | def full_representation(self): |
| 644 | rep = super(Job, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 645 | queue_entries = QueueEntryCollection(self._request) |
| 646 | queue_entries.set_query_parameters(job=self.instance.id) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 647 | drone_set = self.instance.drone_set and self.instance.drone_set.name |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 648 | rep.update({'email_list': self.instance.email_list, |
| 649 | 'parse_failed_repair': |
| 650 | bool(self.instance.parse_failed_repair), |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 651 | 'drone_set': drone_set, |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 652 | 'execution_info': |
| 653 | ExecutionInfo.execution_info_from_job(self.instance), |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 654 | 'queue_entries': queue_entries.link(), |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 655 | 'keyvals': dict((keyval.key, keyval.value) |
| 656 | for keyval in self.instance.keyvals) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 657 | }) |
| 658 | return rep |
| 659 | |
| 660 | |
| 661 | @classmethod |
| 662 | def create_instance(cls, input_dict, containing_collection): |
jamesren | e38a0a7 | 2010-04-19 18:05:31 +0000 | [diff] [blame] | 663 | owner = input_dict.get('owner') |
| 664 | if not owner: |
| 665 | owner = models.User.current_user().login |
| 666 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 667 | cls._check_for_required_fields(input_dict, ('name', 'execution_info', |
| 668 | 'queue_entries')) |
| 669 | execution_info = input_dict['execution_info'] |
| 670 | cls._check_for_required_fields(execution_info, ('control_file', |
| 671 | 'is_server')) |
| 672 | |
| 673 | if execution_info['is_server']: |
| 674 | control_type = models.Job.ControlType.SERVER |
| 675 | else: |
| 676 | control_type = models.Job.ControlType.CLIENT |
| 677 | options = dict( |
| 678 | name=input_dict['name'], |
| 679 | priority=input_dict.get('priority', None), |
| 680 | control_file=execution_info['control_file'], |
| 681 | control_type=control_type, |
| 682 | is_template=input_dict.get('is_template', None), |
| 683 | timeout=execution_info.get('timeout_hrs'), |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame^] | 684 | max_runtime_mins=execution_info.get('maximum_runtime_mins'), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 685 | synch_count=execution_info.get('machines_per_execution'), |
| 686 | run_verify=execution_info.get('run_verify'), |
| 687 | email_list=input_dict.get('email_list', None), |
| 688 | dependencies=execution_info.get('dependencies', ()), |
| 689 | reboot_before=execution_info.get('cleanup_before_job'), |
| 690 | reboot_after=execution_info.get('cleanup_after_job'), |
| 691 | parse_failed_repair=input_dict.get('parse_failed_repair', None), |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 692 | drone_set=input_dict.get('drone_set', None), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 693 | keyvals=input_dict.get('keyvals', None)) |
| 694 | |
| 695 | host_objects, metahost_label_objects, atomic_group = [], [], None |
| 696 | for queue_entry in input_dict['queue_entries']: |
| 697 | if 'host' in queue_entry: |
| 698 | host = queue_entry['host'] |
| 699 | if host: # can be None, indicated a hostless job |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 700 | host_entry = containing_collection.resolve_link(host) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 701 | host_objects.append(host_entry.instance) |
| 702 | elif 'meta_host' in queue_entry: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 703 | label_entry = containing_collection.resolve_link( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 704 | queue_entry['meta_host']) |
| 705 | metahost_label_objects.append(label_entry.instance) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 706 | if 'atomic_group_class' in queue_entry: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 707 | atomic_group_entry = containing_collection.resolve_link( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 708 | queue_entry['atomic_group_class']) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 709 | if atomic_group: |
| 710 | assert atomic_group_entry.instance.id == atomic_group.id |
| 711 | else: |
| 712 | atomic_group = atomic_group_entry.instance |
| 713 | |
| 714 | job_id = rpc_utils.create_new_job( |
jamesren | e38a0a7 | 2010-04-19 18:05:31 +0000 | [diff] [blame] | 715 | owner=owner, |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 716 | options=options, |
| 717 | host_objects=host_objects, |
| 718 | metahost_objects=metahost_label_objects, |
| 719 | atomic_group=atomic_group) |
| 720 | return models.Job.objects.get(id=job_id) |
| 721 | |
| 722 | |
| 723 | def update(self, input_dict): |
| 724 | # Required for POST, doesn't actually support PUT |
| 725 | pass |
| 726 | |
| 727 | |
| 728 | class JobCollection(resource_lib.Collection): |
| 729 | queryset = models.Job.objects.order_by('-id') |
| 730 | entry_class = Job |
| 731 | |
| 732 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 733 | class QueueEntry(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 734 | _permitted_methods = ('GET', 'PUT') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 735 | model = models.HostQueueEntry |
| 736 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 737 | |
| 738 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 739 | def add_query_selectors(cls, query_processor): |
| 740 | query_processor.add_field_selector('host', field='host__hostname') |
| 741 | query_processor.add_field_selector('job', field='job__id') |
| 742 | |
| 743 | |
| 744 | @classmethod |
| 745 | def from_uri_args(cls, request, queue_entry_id): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 746 | instance = models.HostQueueEntry.objects.get(id=queue_entry_id) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 747 | return cls(request, instance) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 748 | |
| 749 | |
| 750 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 751 | return {'queue_entry_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 752 | |
| 753 | |
| 754 | def short_representation(self): |
| 755 | rep = super(QueueEntry, self).short_representation() |
| 756 | if self.instance.host: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 757 | host = (Host(self._request, self.instance.host) |
| 758 | .short_representation()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 759 | else: |
| 760 | host = None |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 761 | job = Job(self._request, self.instance.job) |
| 762 | host = Host.from_optional_instance(self._request, self.instance.host) |
| 763 | label = Label.from_optional_instance(self._request, |
| 764 | self.instance.meta_host) |
| 765 | atomic_group_class = AtomicGroupClass.from_optional_instance( |
| 766 | self._request, self.instance.atomic_group) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 767 | rep.update( |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 768 | {'job': job.short_representation(), |
| 769 | 'host': host.short_representation(), |
| 770 | 'label': label.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 771 | 'atomic_group_class': |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 772 | atomic_group_class.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 773 | 'status': self.instance.status, |
| 774 | 'execution_path': self.instance.execution_subdir, |
| 775 | 'started_on': self._format_datetime(self.instance.started_on), |
| 776 | 'aborted': bool(self.instance.aborted)}) |
| 777 | return rep |
| 778 | |
| 779 | |
| 780 | def update(self, input_dict): |
| 781 | if 'aborted' in input_dict: |
| 782 | if input_dict['aborted'] != True: |
Eric Li | 0a99391 | 2011-05-17 12:56:25 -0700 | [diff] [blame] | 783 | raise exceptions.BadRequest('"aborted" can only be set to true') |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 784 | query = models.HostQueueEntry.objects.filter(pk=self.instance.pk) |
| 785 | models.AclGroup.check_abort_permissions(query) |
| 786 | rpc_utils.check_abort_synchronous_jobs(query) |
| 787 | self.instance.abort(thread_local.get_user()) |
| 788 | |
| 789 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 790 | class QueueEntryCollection(resource_lib.Collection): |
| 791 | queryset = models.HostQueueEntry.objects.order_by('-id') |
| 792 | entry_class = QueueEntry |
| 793 | |
| 794 | |
| 795 | class HealthTask(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 796 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 797 | model = models.SpecialTask |
| 798 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 799 | |
| 800 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 801 | def add_query_selectors(cls, query_processor): |
| 802 | query_processor.add_field_selector('host', field='host__hostname') |
| 803 | |
| 804 | |
| 805 | @classmethod |
| 806 | def from_uri_args(cls, request, task_id): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 807 | instance = models.SpecialTask.objects.get(id=task_id) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 808 | return cls(request, instance) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 809 | |
| 810 | |
| 811 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 812 | return {'task_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 813 | |
| 814 | |
| 815 | def short_representation(self): |
| 816 | rep = super(HealthTask, self).short_representation() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 817 | host = Host(self._request, self.instance.host) |
| 818 | queue_entry = QueueEntry.from_optional_instance( |
| 819 | self._request, self.instance.queue_entry) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 820 | rep.update( |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 821 | {'host': host.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 822 | 'task_type': self.instance.task, |
| 823 | 'started_on': |
| 824 | self._format_datetime(self.instance.time_started), |
| 825 | 'status': self.instance.status, |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 826 | 'queue_entry': queue_entry.short_representation() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 827 | }) |
| 828 | return rep |
| 829 | |
| 830 | |
| 831 | @classmethod |
| 832 | def create_instance(cls, input_dict, containing_collection): |
| 833 | cls._check_for_required_fields(input_dict, ('task_type',)) |
| 834 | host = containing_collection.base_entry.instance |
| 835 | models.AclGroup.check_for_acl_violation_hosts((host,)) |
| 836 | return models.SpecialTask.schedule_special_task(host, |
| 837 | input_dict['task_type']) |
| 838 | |
| 839 | |
| 840 | def update(self, input_dict): |
| 841 | # Required for POST, doesn't actually support PUT |
| 842 | pass |
| 843 | |
| 844 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 845 | class HealthTaskCollection(resource_lib.Collection): |
| 846 | entry_class = HealthTask |
| 847 | |
| 848 | |
| 849 | def _fresh_queryset(self): |
| 850 | return models.SpecialTask.objects.order_by('-id') |
| 851 | |
| 852 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 853 | class ResourceDirectory(resource_lib.Resource): |
| 854 | _permitted_methods = ('GET',) |
| 855 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 856 | def handle_request(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 857 | result = self.link() |
| 858 | result.update({ |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 859 | 'atomic_group_classes': |
| 860 | AtomicGroupClassCollection(self._request).link(), |
| 861 | 'labels': LabelCollection(self._request).link(), |
| 862 | 'users': UserCollection(self._request).link(), |
| 863 | 'acl_groups': AclCollection(self._request).link(), |
| 864 | 'hosts': HostCollection(self._request).link(), |
| 865 | 'tests': TestCollection(self._request).link(), |
| 866 | 'execution_info': ExecutionInfo(self._request).link(), |
| 867 | 'queue_entries_request': |
| 868 | QueueEntriesRequest(self._request).link(), |
| 869 | 'jobs': JobCollection(self._request).link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 870 | }) |
| 871 | return self._basic_response(result) |