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