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()}) |
| 557 | |
| 558 | result = self.link() |
| 559 | result['queue_entries'] = entries |
| 560 | return self._basic_response(result) |
| 561 | |
| 562 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 563 | class Job(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 564 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 565 | model = models.Job |
| 566 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 567 | |
| 568 | class _StatusConstraint(query_lib.Constraint): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 569 | def apply_constraint(self, queryset, value, comparison_type, |
| 570 | is_inverse): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 571 | if comparison_type != 'equals' or is_inverse: |
| 572 | raise query_lib.ConstraintError('Can only use this selector ' |
| 573 | 'with equals') |
| 574 | non_queued_statuses = [ |
| 575 | status for status, _ |
| 576 | in models.HostQueueEntry.Status.choices() |
| 577 | if status != models.HostQueueEntry.Status.QUEUED] |
| 578 | if value == 'queued': |
| 579 | return queryset.exclude( |
| 580 | hostqueueentry__status__in=non_queued_statuses) |
| 581 | elif value == 'active': |
| 582 | return queryset.filter( |
| 583 | hostqueueentry__status__in=non_queued_statuses).filter( |
| 584 | hostqueueentry__complete=False).distinct() |
| 585 | elif value == 'complete': |
| 586 | return queryset.exclude(hostqueueentry__complete=False) |
| 587 | else: |
| 588 | raise query_lib.ConstraintError('Value must be one of queued, ' |
| 589 | 'active or complete') |
| 590 | |
| 591 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 592 | @classmethod |
| 593 | def add_query_selectors(cls, query_processor): |
| 594 | query_processor.add_field_selector('id') |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 595 | query_processor.add_field_selector('name') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 596 | query_processor.add_selector( |
| 597 | query_lib.Selector('status', |
| 598 | doc='One of queued, active or complete'), |
| 599 | Job._StatusConstraint()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 600 | |
| 601 | |
| 602 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 603 | def from_uri_args(cls, request, job_id, **kwargs): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 604 | return cls(request, models.Job.objects.get(id=job_id)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 605 | |
| 606 | |
| 607 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 608 | return {'job_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 609 | |
| 610 | |
| 611 | def short_representation(self): |
| 612 | rep = super(Job, self).short_representation() |
| 613 | rep.update({'id': self.instance.id, |
| 614 | 'owner': self.instance.owner, |
| 615 | 'name': self.instance.name, |
| 616 | 'priority': |
| 617 | models.Job.Priority.get_string(self.instance.priority), |
| 618 | 'created_on': |
| 619 | self._format_datetime(self.instance.created_on), |
| 620 | }) |
| 621 | return rep |
| 622 | |
| 623 | |
| 624 | def full_representation(self): |
| 625 | rep = super(Job, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 626 | queue_entries = QueueEntryCollection(self._request) |
| 627 | queue_entries.set_query_parameters(job=self.instance.id) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 628 | rep.update({'email_list': self.instance.email_list, |
| 629 | 'parse_failed_repair': |
| 630 | bool(self.instance.parse_failed_repair), |
| 631 | 'execution_info': |
| 632 | ExecutionInfo.execution_info_from_job(self.instance), |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 633 | 'queue_entries': queue_entries.link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 634 | }) |
| 635 | return rep |
| 636 | |
| 637 | |
| 638 | @classmethod |
| 639 | def create_instance(cls, input_dict, containing_collection): |
| 640 | cls._check_for_required_fields(input_dict, ('name', 'execution_info', |
| 641 | 'queue_entries')) |
| 642 | execution_info = input_dict['execution_info'] |
| 643 | cls._check_for_required_fields(execution_info, ('control_file', |
| 644 | 'is_server')) |
| 645 | |
| 646 | if execution_info['is_server']: |
| 647 | control_type = models.Job.ControlType.SERVER |
| 648 | else: |
| 649 | control_type = models.Job.ControlType.CLIENT |
| 650 | options = dict( |
| 651 | name=input_dict['name'], |
| 652 | priority=input_dict.get('priority', None), |
| 653 | control_file=execution_info['control_file'], |
| 654 | control_type=control_type, |
| 655 | is_template=input_dict.get('is_template', None), |
| 656 | timeout=execution_info.get('timeout_hrs'), |
| 657 | max_runtime_hrs=execution_info.get('maximum_runtime_hrs'), |
| 658 | synch_count=execution_info.get('machines_per_execution'), |
| 659 | run_verify=execution_info.get('run_verify'), |
| 660 | email_list=input_dict.get('email_list', None), |
| 661 | dependencies=execution_info.get('dependencies', ()), |
| 662 | reboot_before=execution_info.get('cleanup_before_job'), |
| 663 | reboot_after=execution_info.get('cleanup_after_job'), |
| 664 | parse_failed_repair=input_dict.get('parse_failed_repair', None), |
| 665 | keyvals=input_dict.get('keyvals', None)) |
| 666 | |
| 667 | host_objects, metahost_label_objects, atomic_group = [], [], None |
| 668 | for queue_entry in input_dict['queue_entries']: |
| 669 | if 'host' in queue_entry: |
| 670 | host = queue_entry['host'] |
| 671 | if host: # can be None, indicated a hostless job |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 672 | host_entry = containing_collection.resolve_link(host) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 673 | host_objects.append(host_entry.instance) |
| 674 | elif 'meta_host' in queue_entry: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 675 | label_entry = containing_collection.resolve_link( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 676 | queue_entry['meta_host']) |
| 677 | metahost_label_objects.append(label_entry.instance) |
| 678 | if 'atomic_group' in queue_entry: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 679 | atomic_group_entry = containing_collection.resolve_link( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 680 | queue_entry['atomic_group']) |
| 681 | if atomic_group: |
| 682 | assert atomic_group_entry.instance.id == atomic_group.id |
| 683 | else: |
| 684 | atomic_group = atomic_group_entry.instance |
| 685 | |
| 686 | job_id = rpc_utils.create_new_job( |
| 687 | owner=models.User.current_user().login, |
| 688 | options=options, |
| 689 | host_objects=host_objects, |
| 690 | metahost_objects=metahost_label_objects, |
| 691 | atomic_group=atomic_group) |
| 692 | return models.Job.objects.get(id=job_id) |
| 693 | |
| 694 | |
| 695 | def update(self, input_dict): |
| 696 | # Required for POST, doesn't actually support PUT |
| 697 | pass |
| 698 | |
| 699 | |
| 700 | class JobCollection(resource_lib.Collection): |
| 701 | queryset = models.Job.objects.order_by('-id') |
| 702 | entry_class = Job |
| 703 | |
| 704 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 705 | class QueueEntry(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 706 | _permitted_methods = ('GET', 'PUT') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 707 | model = models.HostQueueEntry |
| 708 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 709 | |
| 710 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 711 | def add_query_selectors(cls, query_processor): |
| 712 | query_processor.add_field_selector('host', field='host__hostname') |
| 713 | query_processor.add_field_selector('job', field='job__id') |
| 714 | |
| 715 | |
| 716 | @classmethod |
| 717 | def from_uri_args(cls, request, queue_entry_id): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 718 | instance = models.HostQueueEntry.objects.get(id=queue_entry_id) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 719 | return cls(request, instance) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 720 | |
| 721 | |
| 722 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 723 | return {'queue_entry_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 724 | |
| 725 | |
| 726 | def short_representation(self): |
| 727 | rep = super(QueueEntry, self).short_representation() |
| 728 | if self.instance.host: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 729 | host = (Host(self._request, self.instance.host) |
| 730 | .short_representation()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 731 | else: |
| 732 | host = None |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 733 | job = Job(self._request, self.instance.job) |
| 734 | host = Host.from_optional_instance(self._request, self.instance.host) |
| 735 | label = Label.from_optional_instance(self._request, |
| 736 | self.instance.meta_host) |
| 737 | atomic_group_class = AtomicGroupClass.from_optional_instance( |
| 738 | self._request, self.instance.atomic_group) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 739 | rep.update( |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 740 | {'job': job.short_representation(), |
| 741 | 'host': host.short_representation(), |
| 742 | 'label': label.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 743 | 'atomic_group_class': |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 744 | atomic_group_class.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 745 | 'status': self.instance.status, |
| 746 | 'execution_path': self.instance.execution_subdir, |
| 747 | 'started_on': self._format_datetime(self.instance.started_on), |
| 748 | 'aborted': bool(self.instance.aborted)}) |
| 749 | return rep |
| 750 | |
| 751 | |
| 752 | def update(self, input_dict): |
| 753 | if 'aborted' in input_dict: |
| 754 | if input_dict['aborted'] != True: |
| 755 | raise BadRequest('"aborted" can only be set to true') |
| 756 | query = models.HostQueueEntry.objects.filter(pk=self.instance.pk) |
| 757 | models.AclGroup.check_abort_permissions(query) |
| 758 | rpc_utils.check_abort_synchronous_jobs(query) |
| 759 | self.instance.abort(thread_local.get_user()) |
| 760 | |
| 761 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 762 | class QueueEntryCollection(resource_lib.Collection): |
| 763 | queryset = models.HostQueueEntry.objects.order_by('-id') |
| 764 | entry_class = QueueEntry |
| 765 | |
| 766 | |
| 767 | class HealthTask(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 768 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 769 | model = models.SpecialTask |
| 770 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 771 | |
| 772 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 773 | def add_query_selectors(cls, query_processor): |
| 774 | query_processor.add_field_selector('host', field='host__hostname') |
| 775 | |
| 776 | |
| 777 | @classmethod |
| 778 | def from_uri_args(cls, request, task_id): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 779 | instance = models.SpecialTask.objects.get(id=task_id) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 780 | return cls(request, instance) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 781 | |
| 782 | |
| 783 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 784 | return {'task_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 785 | |
| 786 | |
| 787 | def short_representation(self): |
| 788 | rep = super(HealthTask, self).short_representation() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 789 | host = Host(self._request, self.instance.host) |
| 790 | queue_entry = QueueEntry.from_optional_instance( |
| 791 | self._request, self.instance.queue_entry) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 792 | rep.update( |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 793 | {'host': host.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 794 | 'task_type': self.instance.task, |
| 795 | 'started_on': |
| 796 | self._format_datetime(self.instance.time_started), |
| 797 | 'status': self.instance.status, |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 798 | 'queue_entry': queue_entry.short_representation() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 799 | }) |
| 800 | return rep |
| 801 | |
| 802 | |
| 803 | @classmethod |
| 804 | def create_instance(cls, input_dict, containing_collection): |
| 805 | cls._check_for_required_fields(input_dict, ('task_type',)) |
| 806 | host = containing_collection.base_entry.instance |
| 807 | models.AclGroup.check_for_acl_violation_hosts((host,)) |
| 808 | return models.SpecialTask.schedule_special_task(host, |
| 809 | input_dict['task_type']) |
| 810 | |
| 811 | |
| 812 | def update(self, input_dict): |
| 813 | # Required for POST, doesn't actually support PUT |
| 814 | pass |
| 815 | |
| 816 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame] | 817 | class HealthTaskCollection(resource_lib.Collection): |
| 818 | entry_class = HealthTask |
| 819 | |
| 820 | |
| 821 | def _fresh_queryset(self): |
| 822 | return models.SpecialTask.objects.order_by('-id') |
| 823 | |
| 824 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 825 | class ResourceDirectory(resource_lib.Resource): |
| 826 | _permitted_methods = ('GET',) |
| 827 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 828 | def handle_request(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 829 | result = self.link() |
| 830 | result.update({ |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 831 | 'atomic_group_classes': |
| 832 | AtomicGroupClassCollection(self._request).link(), |
| 833 | 'labels': LabelCollection(self._request).link(), |
| 834 | 'users': UserCollection(self._request).link(), |
| 835 | 'acl_groups': AclCollection(self._request).link(), |
| 836 | 'hosts': HostCollection(self._request).link(), |
| 837 | 'tests': TestCollection(self._request).link(), |
| 838 | 'execution_info': ExecutionInfo(self._request).link(), |
| 839 | 'queue_entries_request': |
| 840 | QueueEntriesRequest(self._request).link(), |
| 841 | 'jobs': JobCollection(self._request).link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 842 | }) |
| 843 | return self._basic_response(result) |