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