blob: 79c0dec964206d5a9b0095250fc34f13bd48e598 [file] [log] [blame]
Richard Barnette7983b632016-05-09 09:02:57 -07001# pylint: disable=C0111
Aviv Keshet0b9cfc92013-02-05 11:36:02 -08002
Fang Deng2b79fd72015-05-20 19:11:36 -07003import logging
showardfb2a7fa2008-07-17 17:04:12 +00004from datetime import datetime
Aviv Keshetfa199002013-05-09 13:31:46 -07005import django.core
6try:
7 from django.db import models as dbmodels, connection
8except django.core.exceptions.ImproperlyConfigured:
9 raise ImportError('Django database not yet configured. Import either '
10 'setup_django_environment or '
11 'setup_django_lite_environment from '
12 'autotest_lib.frontend before any imports that '
13 'depend on django models.')
jamesren35a70222010-02-16 19:30:46 +000014from xml.sax import saxutils
showardcafd16e2009-05-29 18:37:49 +000015import common
jamesrendd855242010-03-02 22:23:44 +000016from autotest_lib.frontend.afe import model_logic, model_attributes
Prashanth B489b91d2014-03-15 12:17:16 -070017from autotest_lib.frontend.afe import rdb_model_extensions
showardcafd16e2009-05-29 18:37:49 +000018from autotest_lib.frontend import settings, thread_local
Jakob Juelicha94efe62014-09-18 16:02:49 -070019from autotest_lib.client.common_lib import enum, error, host_protections
20from autotest_lib.client.common_lib import global_config
showardeaa408e2009-09-11 18:45:31 +000021from autotest_lib.client.common_lib import host_queue_entry_states
Jakob Juelicha94efe62014-09-18 16:02:49 -070022from autotest_lib.client.common_lib import control_data, priorities, decorators
Prashanth Balasubramanian6edaaf92014-11-24 16:36:25 -080023from autotest_lib.client.common_lib import site_utils
Gabe Blackb72f4fb2015-01-20 16:47:13 -080024from autotest_lib.client.common_lib.cros.graphite import autotest_es
MK Ryu0c1a37d2015-04-30 12:00:55 -070025from autotest_lib.server import utils as server_utils
mblighe8819cd2008-02-15 16:48:40 +000026
showard0fc38302008-10-23 00:44:07 +000027# job options and user preferences
jamesrendd855242010-03-02 22:23:44 +000028DEFAULT_REBOOT_BEFORE = model_attributes.RebootBefore.IF_DIRTY
Dan Shi07e09af2013-04-12 09:31:29 -070029DEFAULT_REBOOT_AFTER = model_attributes.RebootBefore.NEVER
mblighe8819cd2008-02-15 16:48:40 +000030
showard89f84db2009-03-12 20:39:13 +000031
mblighe8819cd2008-02-15 16:48:40 +000032class AclAccessViolation(Exception):
jadmanski0afbb632008-06-06 21:10:57 +000033 """\
34 Raised when an operation is attempted with proper permissions as
35 dictated by ACLs.
36 """
mblighe8819cd2008-02-15 16:48:40 +000037
38
showard205fd602009-03-21 00:17:35 +000039class AtomicGroup(model_logic.ModelWithInvalid, dbmodels.Model):
showard89f84db2009-03-12 20:39:13 +000040 """\
41 An atomic group defines a collection of hosts which must only be scheduled
42 all at once. Any host with a label having an atomic group will only be
43 scheduled for a job at the same time as other hosts sharing that label.
44
45 Required:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -080046 name: A name for this atomic group, e.g. 'rack23' or 'funky_net'.
showard89f84db2009-03-12 20:39:13 +000047 max_number_of_machines: The maximum number of machines that will be
48 scheduled at once when scheduling jobs to this atomic group.
49 The job.synch_count is considered the minimum.
50
51 Optional:
52 description: Arbitrary text description of this group's purpose.
53 """
showarda5288b42009-07-28 20:06:08 +000054 name = dbmodels.CharField(max_length=255, unique=True)
showard89f84db2009-03-12 20:39:13 +000055 description = dbmodels.TextField(blank=True)
showarde9450c92009-06-30 01:58:52 +000056 # This magic value is the default to simplify the scheduler logic.
57 # It must be "large". The common use of atomic groups is to want all
58 # machines in the group to be used, limits on which subset used are
59 # often chosen via dependency labels.
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -080060 # TODO(dennisjeffrey): Revisit this so we don't have to assume that
61 # "infinity" is around 3.3 million.
showarde9450c92009-06-30 01:58:52 +000062 INFINITE_MACHINES = 333333333
63 max_number_of_machines = dbmodels.IntegerField(default=INFINITE_MACHINES)
showard205fd602009-03-21 00:17:35 +000064 invalid = dbmodels.BooleanField(default=False,
showarda5288b42009-07-28 20:06:08 +000065 editable=settings.FULL_ADMIN)
showard89f84db2009-03-12 20:39:13 +000066
showard89f84db2009-03-12 20:39:13 +000067 name_field = 'name'
jamesrene3656232010-03-02 00:00:30 +000068 objects = model_logic.ModelWithInvalidManager()
showard205fd602009-03-21 00:17:35 +000069 valid_objects = model_logic.ValidObjectsManager()
70
71
showard29f7cd22009-04-29 21:16:24 +000072 def enqueue_job(self, job, is_template=False):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -080073 """Enqueue a job on an associated atomic group of hosts.
74
75 @param job: A job to enqueue.
76 @param is_template: Whether the status should be "Template".
77 """
showard29f7cd22009-04-29 21:16:24 +000078 queue_entry = HostQueueEntry.create(atomic_group=self, job=job,
79 is_template=is_template)
showardc92da832009-04-07 18:14:34 +000080 queue_entry.save()
81
82
showard205fd602009-03-21 00:17:35 +000083 def clean_object(self):
84 self.label_set.clear()
showard89f84db2009-03-12 20:39:13 +000085
86
87 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -080088 """Metadata for class AtomicGroup."""
showardeab66ce2009-12-23 00:03:56 +000089 db_table = 'afe_atomic_groups'
showard89f84db2009-03-12 20:39:13 +000090
showard205fd602009-03-21 00:17:35 +000091
showarda5288b42009-07-28 20:06:08 +000092 def __unicode__(self):
93 return unicode(self.name)
showard89f84db2009-03-12 20:39:13 +000094
95
showard7c785282008-05-29 19:45:12 +000096class Label(model_logic.ModelWithInvalid, dbmodels.Model):
jadmanski0afbb632008-06-06 21:10:57 +000097 """\
98 Required:
showard89f84db2009-03-12 20:39:13 +000099 name: label name
mblighe8819cd2008-02-15 16:48:40 +0000100
jadmanski0afbb632008-06-06 21:10:57 +0000101 Optional:
showard89f84db2009-03-12 20:39:13 +0000102 kernel_config: URL/path to kernel config for jobs run on this label.
103 platform: If True, this is a platform label (defaults to False).
104 only_if_needed: If True, a Host with this label can only be used if that
105 label is requested by the job/test (either as the meta_host or
106 in the job_dependencies).
107 atomic_group: The atomic group associated with this label.
jadmanski0afbb632008-06-06 21:10:57 +0000108 """
showarda5288b42009-07-28 20:06:08 +0000109 name = dbmodels.CharField(max_length=255, unique=True)
110 kernel_config = dbmodels.CharField(max_length=255, blank=True)
jadmanski0afbb632008-06-06 21:10:57 +0000111 platform = dbmodels.BooleanField(default=False)
112 invalid = dbmodels.BooleanField(default=False,
113 editable=settings.FULL_ADMIN)
showardb1e51872008-10-07 11:08:18 +0000114 only_if_needed = dbmodels.BooleanField(default=False)
mblighe8819cd2008-02-15 16:48:40 +0000115
jadmanski0afbb632008-06-06 21:10:57 +0000116 name_field = 'name'
jamesrene3656232010-03-02 00:00:30 +0000117 objects = model_logic.ModelWithInvalidManager()
jadmanski0afbb632008-06-06 21:10:57 +0000118 valid_objects = model_logic.ValidObjectsManager()
showard89f84db2009-03-12 20:39:13 +0000119 atomic_group = dbmodels.ForeignKey(AtomicGroup, null=True, blank=True)
120
mbligh5244cbb2008-04-24 20:39:52 +0000121
jadmanski0afbb632008-06-06 21:10:57 +0000122 def clean_object(self):
123 self.host_set.clear()
showard01a51672009-05-29 18:42:37 +0000124 self.test_set.clear()
mblighe8819cd2008-02-15 16:48:40 +0000125
126
showard29f7cd22009-04-29 21:16:24 +0000127 def enqueue_job(self, job, atomic_group=None, is_template=False):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800128 """Enqueue a job on any host of this label.
129
130 @param job: A job to enqueue.
131 @param atomic_group: The associated atomic group.
132 @param is_template: Whether the status should be "Template".
133 """
showard29f7cd22009-04-29 21:16:24 +0000134 queue_entry = HostQueueEntry.create(meta_host=self, job=job,
135 is_template=is_template,
136 atomic_group=atomic_group)
jadmanski0afbb632008-06-06 21:10:57 +0000137 queue_entry.save()
mblighe8819cd2008-02-15 16:48:40 +0000138
139
Fang Dengff361592015-02-02 15:27:34 -0800140
jadmanski0afbb632008-06-06 21:10:57 +0000141 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800142 """Metadata for class Label."""
showardeab66ce2009-12-23 00:03:56 +0000143 db_table = 'afe_labels'
mblighe8819cd2008-02-15 16:48:40 +0000144
Fang Dengff361592015-02-02 15:27:34 -0800145
showarda5288b42009-07-28 20:06:08 +0000146 def __unicode__(self):
147 return unicode(self.name)
mblighe8819cd2008-02-15 16:48:40 +0000148
149
Jakob Jülich92c06332014-08-25 19:06:57 +0000150class Shard(dbmodels.Model, model_logic.ModelExtensions):
151
Jakob Juelichde2b9a92014-09-02 15:29:28 -0700152 hostname = dbmodels.CharField(max_length=255, unique=True)
153
154 name_field = 'hostname'
155
Jakob Jülich92c06332014-08-25 19:06:57 +0000156 labels = dbmodels.ManyToManyField(Label, blank=True,
157 db_table='afe_shards_labels')
158
159 class Meta:
160 """Metadata for class ParameterizedJob."""
161 db_table = 'afe_shards'
162
163
Prashanth Balasubramanian6edaaf92014-11-24 16:36:25 -0800164 def rpc_hostname(self):
165 """Get the rpc hostname of the shard.
166
167 @return: Just the shard hostname for all non-testing environments.
168 The address of the default gateway for vm testing environments.
169 """
170 # TODO: Figure out a better solution for testing. Since no 2 shards
171 # can run on the same host, if the shard hostname is localhost we
172 # conclude that it must be a vm in a test cluster. In such situations
173 # a name of localhost:<port> is necessary to achieve the correct
174 # afe links/redirection from the frontend (this happens through the
175 # host), but for rpcs that are performed *on* the shard, they need to
176 # use the address of the gateway.
MK Ryu8f8cdb42015-05-11 17:41:14 -0700177 # In the virtual machine testing environment (i.e., puppylab), each
178 # shard VM has a hostname like localhost:<port>. In the real cluster
179 # environment, a shard node does not have 'localhost' for its hostname.
180 # The following hostname substitution is needed only for the VM
181 # in puppylab.
182 # The 'hostname' should not be replaced in the case of real cluster.
183 if site_utils.is_puppylab_vm(self.hostname):
184 hostname = self.hostname.split(':')[0]
Prashanth Balasubramanian6edaaf92014-11-24 16:36:25 -0800185 return self.hostname.replace(
186 hostname, site_utils.DEFAULT_VM_GATEWAY)
187 return self.hostname
188
189
jamesren76fcf192010-04-21 20:39:50 +0000190class Drone(dbmodels.Model, model_logic.ModelExtensions):
191 """
192 A scheduler drone
193
194 hostname: the drone's hostname
195 """
196 hostname = dbmodels.CharField(max_length=255, unique=True)
197
198 name_field = 'hostname'
199 objects = model_logic.ExtendedManager()
200
201
202 def save(self, *args, **kwargs):
203 if not User.current_user().is_superuser():
204 raise Exception('Only superusers may edit drones')
205 super(Drone, self).save(*args, **kwargs)
206
207
208 def delete(self):
209 if not User.current_user().is_superuser():
210 raise Exception('Only superusers may delete drones')
211 super(Drone, self).delete()
212
213
214 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800215 """Metadata for class Drone."""
jamesren76fcf192010-04-21 20:39:50 +0000216 db_table = 'afe_drones'
217
218 def __unicode__(self):
219 return unicode(self.hostname)
220
221
222class DroneSet(dbmodels.Model, model_logic.ModelExtensions):
223 """
224 A set of scheduler drones
225
226 These will be used by the scheduler to decide what drones a job is allowed
227 to run on.
228
229 name: the drone set's name
230 drones: the drones that are part of the set
231 """
232 DRONE_SETS_ENABLED = global_config.global_config.get_config_value(
233 'SCHEDULER', 'drone_sets_enabled', type=bool, default=False)
234 DEFAULT_DRONE_SET_NAME = global_config.global_config.get_config_value(
235 'SCHEDULER', 'default_drone_set_name', default=None)
236
237 name = dbmodels.CharField(max_length=255, unique=True)
238 drones = dbmodels.ManyToManyField(Drone, db_table='afe_drone_sets_drones')
239
240 name_field = 'name'
241 objects = model_logic.ExtendedManager()
242
243
244 def save(self, *args, **kwargs):
245 if not User.current_user().is_superuser():
246 raise Exception('Only superusers may edit drone sets')
247 super(DroneSet, self).save(*args, **kwargs)
248
249
250 def delete(self):
251 if not User.current_user().is_superuser():
252 raise Exception('Only superusers may delete drone sets')
253 super(DroneSet, self).delete()
254
255
256 @classmethod
257 def drone_sets_enabled(cls):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800258 """Returns whether drone sets are enabled.
259
260 @param cls: Implicit class object.
261 """
jamesren76fcf192010-04-21 20:39:50 +0000262 return cls.DRONE_SETS_ENABLED
263
264
265 @classmethod
266 def default_drone_set_name(cls):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800267 """Returns the default drone set name.
268
269 @param cls: Implicit class object.
270 """
jamesren76fcf192010-04-21 20:39:50 +0000271 return cls.DEFAULT_DRONE_SET_NAME
272
273
274 @classmethod
275 def get_default(cls):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800276 """Gets the default drone set name, compatible with Job.add_object.
277
278 @param cls: Implicit class object.
279 """
jamesren76fcf192010-04-21 20:39:50 +0000280 return cls.smart_get(cls.DEFAULT_DRONE_SET_NAME)
281
282
283 @classmethod
284 def resolve_name(cls, drone_set_name):
285 """
286 Returns the name of one of these, if not None, in order of preference:
287 1) the drone set given,
288 2) the current user's default drone set, or
289 3) the global default drone set
290
291 or returns None if drone sets are disabled
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800292
293 @param cls: Implicit class object.
294 @param drone_set_name: A drone set name.
jamesren76fcf192010-04-21 20:39:50 +0000295 """
296 if not cls.drone_sets_enabled():
297 return None
298
299 user = User.current_user()
300 user_drone_set_name = user.drone_set and user.drone_set.name
301
302 return drone_set_name or user_drone_set_name or cls.get_default().name
303
304
305 def get_drone_hostnames(self):
306 """
307 Gets the hostnames of all drones in this drone set
308 """
309 return set(self.drones.all().values_list('hostname', flat=True))
310
311
312 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800313 """Metadata for class DroneSet."""
jamesren76fcf192010-04-21 20:39:50 +0000314 db_table = 'afe_drone_sets'
315
316 def __unicode__(self):
317 return unicode(self.name)
318
319
showardfb2a7fa2008-07-17 17:04:12 +0000320class User(dbmodels.Model, model_logic.ModelExtensions):
321 """\
322 Required:
323 login :user login name
324
325 Optional:
326 access_level: 0=User (default), 1=Admin, 100=Root
327 """
328 ACCESS_ROOT = 100
329 ACCESS_ADMIN = 1
330 ACCESS_USER = 0
331
showard64a95952010-01-13 21:27:16 +0000332 AUTOTEST_SYSTEM = 'autotest_system'
333
showarda5288b42009-07-28 20:06:08 +0000334 login = dbmodels.CharField(max_length=255, unique=True)
showardfb2a7fa2008-07-17 17:04:12 +0000335 access_level = dbmodels.IntegerField(default=ACCESS_USER, blank=True)
336
showard0fc38302008-10-23 00:44:07 +0000337 # user preferences
jamesrendd855242010-03-02 22:23:44 +0000338 reboot_before = dbmodels.SmallIntegerField(
339 choices=model_attributes.RebootBefore.choices(), blank=True,
340 default=DEFAULT_REBOOT_BEFORE)
341 reboot_after = dbmodels.SmallIntegerField(
342 choices=model_attributes.RebootAfter.choices(), blank=True,
343 default=DEFAULT_REBOOT_AFTER)
jamesren76fcf192010-04-21 20:39:50 +0000344 drone_set = dbmodels.ForeignKey(DroneSet, null=True, blank=True)
showard97db5ba2008-11-12 18:18:02 +0000345 show_experimental = dbmodels.BooleanField(default=False)
showard0fc38302008-10-23 00:44:07 +0000346
showardfb2a7fa2008-07-17 17:04:12 +0000347 name_field = 'login'
348 objects = model_logic.ExtendedManager()
349
350
showarda5288b42009-07-28 20:06:08 +0000351 def save(self, *args, **kwargs):
showardfb2a7fa2008-07-17 17:04:12 +0000352 # is this a new object being saved for the first time?
353 first_time = (self.id is None)
354 user = thread_local.get_user()
showard0fc38302008-10-23 00:44:07 +0000355 if user and not user.is_superuser() and user.login != self.login:
356 raise AclAccessViolation("You cannot modify user " + self.login)
showarda5288b42009-07-28 20:06:08 +0000357 super(User, self).save(*args, **kwargs)
showardfb2a7fa2008-07-17 17:04:12 +0000358 if first_time:
359 everyone = AclGroup.objects.get(name='Everyone')
360 everyone.users.add(self)
361
362
363 def is_superuser(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800364 """Returns whether the user has superuser access."""
showardfb2a7fa2008-07-17 17:04:12 +0000365 return self.access_level >= self.ACCESS_ROOT
366
367
showard64a95952010-01-13 21:27:16 +0000368 @classmethod
369 def current_user(cls):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800370 """Returns the current user.
371
372 @param cls: Implicit class object.
373 """
showard64a95952010-01-13 21:27:16 +0000374 user = thread_local.get_user()
375 if user is None:
showardcfcdd802010-01-15 00:16:33 +0000376 user, _ = cls.objects.get_or_create(login=cls.AUTOTEST_SYSTEM)
showard64a95952010-01-13 21:27:16 +0000377 user.access_level = cls.ACCESS_ROOT
378 user.save()
379 return user
380
381
Prashanth Balasubramanianaf516642014-12-12 18:16:32 -0800382 @classmethod
383 def get_record(cls, data):
384 """Check the database for an identical record.
385
386 Check for a record with matching id and login. If one exists,
387 return it. If one does not exist there is a possibility that
388 the following cases have happened:
389 1. Same id, different login
390 We received: "1 chromeos-test"
391 And we have: "1 debug-user"
392 In this case we need to delete "1 debug_user" and insert
393 "1 chromeos-test".
394
395 2. Same login, different id:
396 We received: "1 chromeos-test"
397 And we have: "2 chromeos-test"
398 In this case we need to delete "2 chromeos-test" and insert
399 "1 chromeos-test".
400
401 As long as this method deletes bad records and raises the
402 DoesNotExist exception the caller will handle creating the
403 new record.
404
405 @raises: DoesNotExist, if a record with the matching login and id
406 does not exist.
407 """
408
409 # Both the id and login should be uniqe but there are cases when
410 # we might already have a user with the same login/id because
411 # current_user will proactively create a user record if it doesn't
412 # exist. Since we want to avoid conflict between the master and
413 # shard, just delete any existing user records that don't match
414 # what we're about to deserialize from the master.
415 try:
416 return cls.objects.get(login=data['login'], id=data['id'])
417 except cls.DoesNotExist:
418 cls.delete_matching_record(login=data['login'])
419 cls.delete_matching_record(id=data['id'])
420 raise
421
422
showardfb2a7fa2008-07-17 17:04:12 +0000423 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800424 """Metadata for class User."""
showardeab66ce2009-12-23 00:03:56 +0000425 db_table = 'afe_users'
showardfb2a7fa2008-07-17 17:04:12 +0000426
showarda5288b42009-07-28 20:06:08 +0000427 def __unicode__(self):
428 return unicode(self.login)
showardfb2a7fa2008-07-17 17:04:12 +0000429
430
Prashanth B489b91d2014-03-15 12:17:16 -0700431class Host(model_logic.ModelWithInvalid, rdb_model_extensions.AbstractHostModel,
showardf8b19042009-05-12 17:22:49 +0000432 model_logic.ModelWithAttributes):
jadmanski0afbb632008-06-06 21:10:57 +0000433 """\
434 Required:
435 hostname
mblighe8819cd2008-02-15 16:48:40 +0000436
jadmanski0afbb632008-06-06 21:10:57 +0000437 optional:
showard21baa452008-10-21 00:08:39 +0000438 locked: if true, host is locked and will not be queued
mblighe8819cd2008-02-15 16:48:40 +0000439
jadmanski0afbb632008-06-06 21:10:57 +0000440 Internal:
Prashanth B489b91d2014-03-15 12:17:16 -0700441 From AbstractHostModel:
442 synch_id: currently unused
443 status: string describing status of host
444 invalid: true if the host has been deleted
445 protection: indicates what can be done to this host during repair
446 lock_time: DateTime at which the host was locked
447 dirty: true if the host has been used without being rebooted
448 Local:
449 locked_by: user that locked the host, or null if the host is unlocked
jadmanski0afbb632008-06-06 21:10:57 +0000450 """
mblighe8819cd2008-02-15 16:48:40 +0000451
Jakob Juelich3bb7c802014-09-02 16:31:11 -0700452 SERIALIZATION_LINKS_TO_FOLLOW = set(['aclgroup_set',
453 'hostattribute_set',
454 'labels',
455 'shard'])
Prashanth Balasubramanian5949b4a2014-11-23 12:58:30 -0800456 SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['invalid'])
Jakob Juelich3bb7c802014-09-02 16:31:11 -0700457
Jakob Juelichf88fa932014-09-03 17:58:04 -0700458
459 def custom_deserialize_relation(self, link, data):
Jakob Juelich116ff0f2014-09-17 18:25:16 -0700460 assert link == 'shard', 'Link %s should not be deserialized' % link
Jakob Juelichf88fa932014-09-03 17:58:04 -0700461 self.shard = Shard.deserialize(data)
462
463
Prashanth B489b91d2014-03-15 12:17:16 -0700464 # Note: Only specify foreign keys here, specify all native host columns in
465 # rdb_model_extensions instead.
466 Protection = host_protections.Protection
showardeab66ce2009-12-23 00:03:56 +0000467 labels = dbmodels.ManyToManyField(Label, blank=True,
468 db_table='afe_hosts_labels')
showardfb2a7fa2008-07-17 17:04:12 +0000469 locked_by = dbmodels.ForeignKey(User, null=True, blank=True, editable=False)
jadmanski0afbb632008-06-06 21:10:57 +0000470 name_field = 'hostname'
jamesrene3656232010-03-02 00:00:30 +0000471 objects = model_logic.ModelWithInvalidManager()
jadmanski0afbb632008-06-06 21:10:57 +0000472 valid_objects = model_logic.ValidObjectsManager()
beepscc9fc702013-12-02 12:45:38 -0800473 leased_objects = model_logic.LeasedHostManager()
mbligh5244cbb2008-04-24 20:39:52 +0000474
Jakob Juelichde2b9a92014-09-02 15:29:28 -0700475 shard = dbmodels.ForeignKey(Shard, blank=True, null=True)
showard2bab8f42008-11-12 18:15:22 +0000476
477 def __init__(self, *args, **kwargs):
478 super(Host, self).__init__(*args, **kwargs)
479 self._record_attributes(['status'])
480
481
showardb8471e32008-07-03 19:51:08 +0000482 @staticmethod
483 def create_one_time_host(hostname):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800484 """Creates a one-time host.
485
486 @param hostname: The name for the host.
487 """
showardb8471e32008-07-03 19:51:08 +0000488 query = Host.objects.filter(hostname=hostname)
489 if query.count() == 0:
490 host = Host(hostname=hostname, invalid=True)
showarda8411af2008-08-07 22:35:58 +0000491 host.do_validate()
showardb8471e32008-07-03 19:51:08 +0000492 else:
493 host = query[0]
494 if not host.invalid:
495 raise model_logic.ValidationError({
mblighb5b7b5d2009-02-03 17:47:15 +0000496 'hostname' : '%s already exists in the autotest DB. '
497 'Select it rather than entering it as a one time '
498 'host.' % hostname
showardb8471e32008-07-03 19:51:08 +0000499 })
showard1ab512b2008-07-30 23:39:04 +0000500 host.protection = host_protections.Protection.DO_NOT_REPAIR
showard946a7af2009-04-15 21:53:23 +0000501 host.locked = False
showardb8471e32008-07-03 19:51:08 +0000502 host.save()
showard2924b0a2009-06-18 23:16:15 +0000503 host.clean_object()
showardb8471e32008-07-03 19:51:08 +0000504 return host
mbligh5244cbb2008-04-24 20:39:52 +0000505
showard1ff7b2e2009-05-15 23:17:18 +0000506
Jakob Juelich59cfe542014-09-02 16:37:46 -0700507 @classmethod
Jakob Juelich1b525742014-09-30 13:08:07 -0700508 def assign_to_shard(cls, shard, known_ids):
Jakob Juelich59cfe542014-09-02 16:37:46 -0700509 """Assigns hosts to a shard.
510
MK Ryu638a6cf2015-05-08 14:49:43 -0700511 For all labels that have been assigned to a shard, all hosts that
512 have at least one of the shard's labels are assigned to the shard.
Jakob Juelich1b525742014-09-30 13:08:07 -0700513 Hosts that are assigned to the shard but aren't already present on the
514 shard are returned.
Jakob Juelich59cfe542014-09-02 16:37:46 -0700515
MK Ryu638a6cf2015-05-08 14:49:43 -0700516 Board to shard mapping is many-to-one. Many different boards can be
517 hosted in a shard. However, DUTs of a single board cannot be distributed
518 into more than one shard.
519
Jakob Juelich59cfe542014-09-02 16:37:46 -0700520 @param shard: The shard object to assign labels/hosts for.
Jakob Juelich1b525742014-09-30 13:08:07 -0700521 @param known_ids: List of all host-ids the shard already knows.
522 This is used to figure out which hosts should be sent
523 to the shard. If shard_ids were used instead, hosts
524 would only be transferred once, even if the client
525 failed persisting them.
526 The number of hosts usually lies in O(100), so the
527 overhead is acceptable.
528
Jakob Juelich59cfe542014-09-02 16:37:46 -0700529 @returns the hosts objects that should be sent to the shard.
530 """
531
532 # Disclaimer: concurrent heartbeats should theoretically not occur in
533 # the current setup. As they may be introduced in the near future,
534 # this comment will be left here.
535
536 # Sending stuff twice is acceptable, but forgetting something isn't.
537 # Detecting duplicates on the client is easy, but here it's harder. The
538 # following options were considered:
539 # - SELECT ... WHERE and then UPDATE ... WHERE: Update might update more
540 # than select returned, as concurrently more hosts might have been
541 # inserted
542 # - UPDATE and then SELECT WHERE shard=shard: select always returns all
543 # hosts for the shard, this is overhead
544 # - SELECT and then UPDATE only selected without requerying afterwards:
545 # returns the old state of the records.
MK Ryu638a6cf2015-05-08 14:49:43 -0700546 host_ids = set(Host.objects.filter(
547 labels__in=shard.labels.all(),
Jakob Juelich59cfe542014-09-02 16:37:46 -0700548 leased=False
Jakob Juelich1b525742014-09-30 13:08:07 -0700549 ).exclude(
550 id__in=known_ids,
Jakob Juelich59cfe542014-09-02 16:37:46 -0700551 ).values_list('pk', flat=True))
552
553 if host_ids:
554 Host.objects.filter(pk__in=host_ids).update(shard=shard)
555 return list(Host.objects.filter(pk__in=host_ids).all())
556 return []
557
showardafd97de2009-10-01 18:45:09 +0000558 def resurrect_object(self, old_object):
559 super(Host, self).resurrect_object(old_object)
560 # invalid hosts can be in use by the scheduler (as one-time hosts), so
561 # don't change the status
562 self.status = old_object.status
563
564
jadmanski0afbb632008-06-06 21:10:57 +0000565 def clean_object(self):
566 self.aclgroup_set.clear()
567 self.labels.clear()
mblighe8819cd2008-02-15 16:48:40 +0000568
569
Dan Shia0acfbc2014-10-14 15:56:23 -0700570 def record_state(self, type_str, state, value, other_metadata=None):
571 """Record metadata in elasticsearch.
572
573 @param type_str: sets the _type field in elasticsearch db.
574 @param state: string representing what state we are recording,
575 e.g. 'locked'
576 @param value: value of the state, e.g. True
577 @param other_metadata: Other metadata to store in metaDB.
578 """
579 metadata = {
580 state: value,
581 'hostname': self.hostname,
582 }
583 if other_metadata:
584 metadata = dict(metadata.items() + other_metadata.items())
Matthew Sartori68186332015-04-27 17:19:53 -0700585 autotest_es.post(use_http=True, type_str=type_str, metadata=metadata)
Dan Shia0acfbc2014-10-14 15:56:23 -0700586
587
showarda5288b42009-07-28 20:06:08 +0000588 def save(self, *args, **kwargs):
jadmanski0afbb632008-06-06 21:10:57 +0000589 # extra spaces in the hostname can be a sneaky source of errors
590 self.hostname = self.hostname.strip()
591 # is this a new object being saved for the first time?
592 first_time = (self.id is None)
showard3dd47c22008-07-10 00:41:36 +0000593 if not first_time:
594 AclGroup.check_for_acl_violation_hosts([self])
Dan Shia0acfbc2014-10-14 15:56:23 -0700595 # If locked is changed, send its status and user made the change to
596 # metaDB. Locks are important in host history because if a device is
597 # locked then we don't really care what state it is in.
showardfb2a7fa2008-07-17 17:04:12 +0000598 if self.locked and not self.locked_by:
showard64a95952010-01-13 21:27:16 +0000599 self.locked_by = User.current_user()
MK Ryud53e1492015-12-15 12:09:03 -0800600 if not self.lock_time:
601 self.lock_time = datetime.now()
Dan Shia0acfbc2014-10-14 15:56:23 -0700602 self.record_state('lock_history', 'locked', self.locked,
Matthew Sartori68186332015-04-27 17:19:53 -0700603 {'changed_by': self.locked_by.login,
604 'lock_reason': self.lock_reason})
showard21baa452008-10-21 00:08:39 +0000605 self.dirty = True
showardfb2a7fa2008-07-17 17:04:12 +0000606 elif not self.locked and self.locked_by:
Dan Shia0acfbc2014-10-14 15:56:23 -0700607 self.record_state('lock_history', 'locked', self.locked,
608 {'changed_by': self.locked_by.login})
showardfb2a7fa2008-07-17 17:04:12 +0000609 self.locked_by = None
610 self.lock_time = None
showarda5288b42009-07-28 20:06:08 +0000611 super(Host, self).save(*args, **kwargs)
jadmanski0afbb632008-06-06 21:10:57 +0000612 if first_time:
613 everyone = AclGroup.objects.get(name='Everyone')
614 everyone.hosts.add(self)
showard2bab8f42008-11-12 18:15:22 +0000615 self._check_for_updated_attributes()
616
mblighe8819cd2008-02-15 16:48:40 +0000617
showardb8471e32008-07-03 19:51:08 +0000618 def delete(self):
showard3dd47c22008-07-10 00:41:36 +0000619 AclGroup.check_for_acl_violation_hosts([self])
showardb8471e32008-07-03 19:51:08 +0000620 for queue_entry in self.hostqueueentry_set.all():
621 queue_entry.deleted = True
showard64a95952010-01-13 21:27:16 +0000622 queue_entry.abort()
showardb8471e32008-07-03 19:51:08 +0000623 super(Host, self).delete()
624
mblighe8819cd2008-02-15 16:48:40 +0000625
showard2bab8f42008-11-12 18:15:22 +0000626 def on_attribute_changed(self, attribute, old_value):
627 assert attribute == 'status'
showardf1175bb2009-06-17 19:34:36 +0000628 logging.info(self.hostname + ' -> ' + self.status)
showard2bab8f42008-11-12 18:15:22 +0000629
630
showard29f7cd22009-04-29 21:16:24 +0000631 def enqueue_job(self, job, atomic_group=None, is_template=False):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800632 """Enqueue a job on this host.
633
634 @param job: A job to enqueue.
635 @param atomic_group: The associated atomic group.
636 @param is_template: Whther the status should be "Template".
637 """
showard29f7cd22009-04-29 21:16:24 +0000638 queue_entry = HostQueueEntry.create(host=self, job=job,
639 is_template=is_template,
640 atomic_group=atomic_group)
jadmanski0afbb632008-06-06 21:10:57 +0000641 # allow recovery of dead hosts from the frontend
642 if not self.active_queue_entry() and self.is_dead():
643 self.status = Host.Status.READY
644 self.save()
645 queue_entry.save()
mblighe8819cd2008-02-15 16:48:40 +0000646
showard08f981b2008-06-24 21:59:03 +0000647 block = IneligibleHostQueue(job=job, host=self)
648 block.save()
649
mblighe8819cd2008-02-15 16:48:40 +0000650
jadmanski0afbb632008-06-06 21:10:57 +0000651 def platform(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800652 """The platform of the host."""
jadmanski0afbb632008-06-06 21:10:57 +0000653 # TODO(showard): slighly hacky?
654 platforms = self.labels.filter(platform=True)
655 if len(platforms) == 0:
656 return None
657 return platforms[0]
658 platform.short_description = 'Platform'
mblighe8819cd2008-02-15 16:48:40 +0000659
660
showardcafd16e2009-05-29 18:37:49 +0000661 @classmethod
662 def check_no_platform(cls, hosts):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800663 """Verify the specified hosts have no associated platforms.
664
665 @param cls: Implicit class object.
666 @param hosts: The hosts to verify.
667 @raises model_logic.ValidationError if any hosts already have a
668 platform.
669 """
showardcafd16e2009-05-29 18:37:49 +0000670 Host.objects.populate_relationships(hosts, Label, 'label_list')
671 errors = []
672 for host in hosts:
673 platforms = [label.name for label in host.label_list
674 if label.platform]
675 if platforms:
676 # do a join, just in case this host has multiple platforms,
677 # we'll be able to see it
678 errors.append('Host %s already has a platform: %s' % (
679 host.hostname, ', '.join(platforms)))
680 if errors:
681 raise model_logic.ValidationError({'labels': '; '.join(errors)})
682
683
jadmanski0afbb632008-06-06 21:10:57 +0000684 def is_dead(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800685 """Returns whether the host is dead (has status repair failed)."""
jadmanski0afbb632008-06-06 21:10:57 +0000686 return self.status == Host.Status.REPAIR_FAILED
mbligh3cab4a72008-03-05 23:19:09 +0000687
688
jadmanski0afbb632008-06-06 21:10:57 +0000689 def active_queue_entry(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800690 """Returns the active queue entry for this host, or None if none."""
jadmanski0afbb632008-06-06 21:10:57 +0000691 active = list(self.hostqueueentry_set.filter(active=True))
692 if not active:
693 return None
694 assert len(active) == 1, ('More than one active entry for '
695 'host ' + self.hostname)
696 return active[0]
mblighe8819cd2008-02-15 16:48:40 +0000697
698
showardf8b19042009-05-12 17:22:49 +0000699 def _get_attribute_model_and_args(self, attribute):
700 return HostAttribute, dict(host=self, attribute=attribute)
showard0957a842009-05-11 19:25:08 +0000701
702
Fang Dengff361592015-02-02 15:27:34 -0800703 @classmethod
704 def get_attribute_model(cls):
705 """Return the attribute model.
706
707 Override method in parent class. See ModelExtensions for details.
708 @returns: The attribute model of Host.
709 """
710 return HostAttribute
711
712
jadmanski0afbb632008-06-06 21:10:57 +0000713 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800714 """Metadata for the Host class."""
showardeab66ce2009-12-23 00:03:56 +0000715 db_table = 'afe_hosts'
mblighe8819cd2008-02-15 16:48:40 +0000716
Fang Dengff361592015-02-02 15:27:34 -0800717
showarda5288b42009-07-28 20:06:08 +0000718 def __unicode__(self):
719 return unicode(self.hostname)
mblighe8819cd2008-02-15 16:48:40 +0000720
721
MK Ryuacf35922014-10-03 14:56:49 -0700722class HostAttribute(dbmodels.Model, model_logic.ModelExtensions):
showard0957a842009-05-11 19:25:08 +0000723 """Arbitrary keyvals associated with hosts."""
Fang Deng86248502014-12-18 16:38:00 -0800724
725 SERIALIZATION_LINKS_TO_KEEP = set(['host'])
Fang Dengff361592015-02-02 15:27:34 -0800726 SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['value'])
showard0957a842009-05-11 19:25:08 +0000727 host = dbmodels.ForeignKey(Host)
showarda5288b42009-07-28 20:06:08 +0000728 attribute = dbmodels.CharField(max_length=90)
729 value = dbmodels.CharField(max_length=300)
showard0957a842009-05-11 19:25:08 +0000730
731 objects = model_logic.ExtendedManager()
732
733 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800734 """Metadata for the HostAttribute class."""
showardeab66ce2009-12-23 00:03:56 +0000735 db_table = 'afe_host_attributes'
showard0957a842009-05-11 19:25:08 +0000736
737
Fang Dengff361592015-02-02 15:27:34 -0800738 @classmethod
739 def get_record(cls, data):
740 """Check the database for an identical record.
741
742 Use host_id and attribute to search for a existing record.
743
744 @raises: DoesNotExist, if no record found
745 @raises: MultipleObjectsReturned if multiple records found.
746 """
747 # TODO(fdeng): We should use host_id and attribute together as
748 # a primary key in the db.
749 return cls.objects.get(host_id=data['host_id'],
750 attribute=data['attribute'])
751
752
753 @classmethod
754 def deserialize(cls, data):
755 """Override deserialize in parent class.
756
757 Do not deserialize id as id is not kept consistent on master and shards.
758
759 @param data: A dictionary of data to deserialize.
760
761 @returns: A HostAttribute object.
762 """
763 if data:
764 data.pop('id')
765 return super(HostAttribute, cls).deserialize(data)
766
767
showard7c785282008-05-29 19:45:12 +0000768class Test(dbmodels.Model, model_logic.ModelExtensions):
jadmanski0afbb632008-06-06 21:10:57 +0000769 """\
770 Required:
showard909c7a62008-07-15 21:52:38 +0000771 author: author name
772 description: description of the test
jadmanski0afbb632008-06-06 21:10:57 +0000773 name: test name
showard909c7a62008-07-15 21:52:38 +0000774 time: short, medium, long
775 test_class: This describes the class for your the test belongs in.
776 test_category: This describes the category for your tests
jadmanski0afbb632008-06-06 21:10:57 +0000777 test_type: Client or Server
778 path: path to pass to run_test()
showard909c7a62008-07-15 21:52:38 +0000779 sync_count: is a number >=1 (1 being the default). If it's 1, then it's an
780 async job. If it's >1 it's sync job for that number of machines
showard2bab8f42008-11-12 18:15:22 +0000781 i.e. if sync_count = 2 it is a sync job that requires two
782 machines.
jadmanski0afbb632008-06-06 21:10:57 +0000783 Optional:
showard909c7a62008-07-15 21:52:38 +0000784 dependencies: What the test requires to run. Comma deliminated list
showard989f25d2008-10-01 11:38:11 +0000785 dependency_labels: many-to-many relationship with labels corresponding to
786 test dependencies.
showard909c7a62008-07-15 21:52:38 +0000787 experimental: If this is set to True production servers will ignore the test
788 run_verify: Whether or not the scheduler should run the verify stage
Dan Shi07e09af2013-04-12 09:31:29 -0700789 run_reset: Whether or not the scheduler should run the reset stage
Aviv Keshet9af96d32013-03-05 12:56:24 -0800790 test_retry: Number of times to retry test if the test did not complete
791 successfully. (optional, default: 0)
jadmanski0afbb632008-06-06 21:10:57 +0000792 """
showard909c7a62008-07-15 21:52:38 +0000793 TestTime = enum.Enum('SHORT', 'MEDIUM', 'LONG', start_value=1)
mblighe8819cd2008-02-15 16:48:40 +0000794
showarda5288b42009-07-28 20:06:08 +0000795 name = dbmodels.CharField(max_length=255, unique=True)
796 author = dbmodels.CharField(max_length=255)
797 test_class = dbmodels.CharField(max_length=255)
798 test_category = dbmodels.CharField(max_length=255)
799 dependencies = dbmodels.CharField(max_length=255, blank=True)
jadmanski0afbb632008-06-06 21:10:57 +0000800 description = dbmodels.TextField(blank=True)
showard909c7a62008-07-15 21:52:38 +0000801 experimental = dbmodels.BooleanField(default=True)
Dan Shi07e09af2013-04-12 09:31:29 -0700802 run_verify = dbmodels.BooleanField(default=False)
showard909c7a62008-07-15 21:52:38 +0000803 test_time = dbmodels.SmallIntegerField(choices=TestTime.choices(),
804 default=TestTime.MEDIUM)
Aviv Keshet3dd8beb2013-05-13 17:36:04 -0700805 test_type = dbmodels.SmallIntegerField(
806 choices=control_data.CONTROL_TYPE.choices())
showard909c7a62008-07-15 21:52:38 +0000807 sync_count = dbmodels.IntegerField(default=1)
showarda5288b42009-07-28 20:06:08 +0000808 path = dbmodels.CharField(max_length=255, unique=True)
Aviv Keshet9af96d32013-03-05 12:56:24 -0800809 test_retry = dbmodels.IntegerField(blank=True, default=0)
Dan Shi07e09af2013-04-12 09:31:29 -0700810 run_reset = dbmodels.BooleanField(default=True)
mblighe8819cd2008-02-15 16:48:40 +0000811
showardeab66ce2009-12-23 00:03:56 +0000812 dependency_labels = (
813 dbmodels.ManyToManyField(Label, blank=True,
814 db_table='afe_autotests_dependency_labels'))
jadmanski0afbb632008-06-06 21:10:57 +0000815 name_field = 'name'
816 objects = model_logic.ExtendedManager()
mblighe8819cd2008-02-15 16:48:40 +0000817
818
jamesren35a70222010-02-16 19:30:46 +0000819 def admin_description(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800820 """Returns a string representing the admin description."""
jamesren35a70222010-02-16 19:30:46 +0000821 escaped_description = saxutils.escape(self.description)
822 return '<span style="white-space:pre">%s</span>' % escaped_description
823 admin_description.allow_tags = True
jamesrencae88c62010-02-19 00:12:28 +0000824 admin_description.short_description = 'Description'
jamesren35a70222010-02-16 19:30:46 +0000825
826
jadmanski0afbb632008-06-06 21:10:57 +0000827 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800828 """Metadata for class Test."""
showardeab66ce2009-12-23 00:03:56 +0000829 db_table = 'afe_autotests'
mblighe8819cd2008-02-15 16:48:40 +0000830
showarda5288b42009-07-28 20:06:08 +0000831 def __unicode__(self):
832 return unicode(self.name)
mblighe8819cd2008-02-15 16:48:40 +0000833
834
jamesren4a41e012010-07-16 22:33:48 +0000835class TestParameter(dbmodels.Model):
836 """
837 A declared parameter of a test
838 """
839 test = dbmodels.ForeignKey(Test)
840 name = dbmodels.CharField(max_length=255)
841
842 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800843 """Metadata for class TestParameter."""
jamesren4a41e012010-07-16 22:33:48 +0000844 db_table = 'afe_test_parameters'
845 unique_together = ('test', 'name')
846
847 def __unicode__(self):
Eric Li0a993912011-05-17 12:56:25 -0700848 return u'%s (%s)' % (self.name, self.test.name)
jamesren4a41e012010-07-16 22:33:48 +0000849
850
showard2b9a88b2008-06-13 20:55:03 +0000851class Profiler(dbmodels.Model, model_logic.ModelExtensions):
852 """\
853 Required:
854 name: profiler name
855 test_type: Client or Server
856
857 Optional:
858 description: arbirary text description
859 """
showarda5288b42009-07-28 20:06:08 +0000860 name = dbmodels.CharField(max_length=255, unique=True)
showard2b9a88b2008-06-13 20:55:03 +0000861 description = dbmodels.TextField(blank=True)
862
863 name_field = 'name'
864 objects = model_logic.ExtendedManager()
865
866
867 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800868 """Metadata for class Profiler."""
showardeab66ce2009-12-23 00:03:56 +0000869 db_table = 'afe_profilers'
showard2b9a88b2008-06-13 20:55:03 +0000870
showarda5288b42009-07-28 20:06:08 +0000871 def __unicode__(self):
872 return unicode(self.name)
showard2b9a88b2008-06-13 20:55:03 +0000873
874
showard7c785282008-05-29 19:45:12 +0000875class AclGroup(dbmodels.Model, model_logic.ModelExtensions):
jadmanski0afbb632008-06-06 21:10:57 +0000876 """\
877 Required:
878 name: name of ACL group
mblighe8819cd2008-02-15 16:48:40 +0000879
jadmanski0afbb632008-06-06 21:10:57 +0000880 Optional:
881 description: arbitrary description of group
882 """
Jakob Juelich3bb7c802014-09-02 16:31:11 -0700883
884 SERIALIZATION_LINKS_TO_FOLLOW = set(['users'])
885
showarda5288b42009-07-28 20:06:08 +0000886 name = dbmodels.CharField(max_length=255, unique=True)
887 description = dbmodels.CharField(max_length=255, blank=True)
showardeab66ce2009-12-23 00:03:56 +0000888 users = dbmodels.ManyToManyField(User, blank=False,
889 db_table='afe_acl_groups_users')
890 hosts = dbmodels.ManyToManyField(Host, blank=True,
891 db_table='afe_acl_groups_hosts')
mblighe8819cd2008-02-15 16:48:40 +0000892
jadmanski0afbb632008-06-06 21:10:57 +0000893 name_field = 'name'
894 objects = model_logic.ExtendedManager()
showardeb3be4d2008-04-21 20:59:26 +0000895
showard08f981b2008-06-24 21:59:03 +0000896 @staticmethod
showard3dd47c22008-07-10 00:41:36 +0000897 def check_for_acl_violation_hosts(hosts):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800898 """Verify the current user has access to the specified hosts.
899
900 @param hosts: The hosts to verify against.
901 @raises AclAccessViolation if the current user doesn't have access
902 to a host.
903 """
showard64a95952010-01-13 21:27:16 +0000904 user = User.current_user()
showard3dd47c22008-07-10 00:41:36 +0000905 if user.is_superuser():
showard9dbdcda2008-10-14 17:34:36 +0000906 return
showard3dd47c22008-07-10 00:41:36 +0000907 accessible_host_ids = set(
showardd9ac4452009-02-07 02:04:37 +0000908 host.id for host in Host.objects.filter(aclgroup__users=user))
showard3dd47c22008-07-10 00:41:36 +0000909 for host in hosts:
910 # Check if the user has access to this host,
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800911 # but only if it is not a metahost or a one-time-host.
showard98ead172009-06-22 18:13:24 +0000912 no_access = (isinstance(host, Host)
913 and not host.invalid
914 and int(host.id) not in accessible_host_ids)
915 if no_access:
showardeaa408e2009-09-11 18:45:31 +0000916 raise AclAccessViolation("%s does not have access to %s" %
917 (str(user), str(host)))
showard3dd47c22008-07-10 00:41:36 +0000918
showard9dbdcda2008-10-14 17:34:36 +0000919
920 @staticmethod
showarddc817512008-11-12 18:16:41 +0000921 def check_abort_permissions(queue_entries):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800922 """Look for queue entries that aren't abortable by the current user.
923
924 An entry is not abortable if:
925 * the job isn't owned by this user, and
showarddc817512008-11-12 18:16:41 +0000926 * the machine isn't ACL-accessible, or
927 * the machine is in the "Everyone" ACL
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800928
929 @param queue_entries: The queue entries to check.
930 @raises AclAccessViolation if a queue entry is not abortable by the
931 current user.
showarddc817512008-11-12 18:16:41 +0000932 """
showard64a95952010-01-13 21:27:16 +0000933 user = User.current_user()
showard9dbdcda2008-10-14 17:34:36 +0000934 if user.is_superuser():
935 return
showarddc817512008-11-12 18:16:41 +0000936 not_owned = queue_entries.exclude(job__owner=user.login)
937 # I do this using ID sets instead of just Django filters because
showarda5288b42009-07-28 20:06:08 +0000938 # filtering on M2M dbmodels is broken in Django 0.96. It's better in
939 # 1.0.
940 # TODO: Use Django filters, now that we're using 1.0.
showarddc817512008-11-12 18:16:41 +0000941 accessible_ids = set(
942 entry.id for entry
showardd9ac4452009-02-07 02:04:37 +0000943 in not_owned.filter(host__aclgroup__users__login=user.login))
showarddc817512008-11-12 18:16:41 +0000944 public_ids = set(entry.id for entry
showardd9ac4452009-02-07 02:04:37 +0000945 in not_owned.filter(host__aclgroup__name='Everyone'))
showarddc817512008-11-12 18:16:41 +0000946 cannot_abort = [entry for entry in not_owned.select_related()
947 if entry.id not in accessible_ids
948 or entry.id in public_ids]
949 if len(cannot_abort) == 0:
950 return
951 entry_names = ', '.join('%s-%s/%s' % (entry.job.id, entry.job.owner,
showard3f15eed2008-11-14 22:40:48 +0000952 entry.host_or_metahost_name())
showarddc817512008-11-12 18:16:41 +0000953 for entry in cannot_abort)
954 raise AclAccessViolation('You cannot abort the following job entries: '
955 + entry_names)
showard9dbdcda2008-10-14 17:34:36 +0000956
957
showard3dd47c22008-07-10 00:41:36 +0000958 def check_for_acl_violation_acl_group(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800959 """Verifies the current user has acces to this ACL group.
960
961 @raises AclAccessViolation if the current user doesn't have access to
962 this ACL group.
963 """
showard64a95952010-01-13 21:27:16 +0000964 user = User.current_user()
showard3dd47c22008-07-10 00:41:36 +0000965 if user.is_superuser():
showard8cbaf1e2009-09-08 16:27:04 +0000966 return
967 if self.name == 'Everyone':
968 raise AclAccessViolation("You cannot modify 'Everyone'!")
showard3dd47c22008-07-10 00:41:36 +0000969 if not user in self.users.all():
970 raise AclAccessViolation("You do not have access to %s"
971 % self.name)
972
973 @staticmethod
showard08f981b2008-06-24 21:59:03 +0000974 def on_host_membership_change():
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -0800975 """Invoked when host membership changes."""
showard08f981b2008-06-24 21:59:03 +0000976 everyone = AclGroup.objects.get(name='Everyone')
977
showard3dd47c22008-07-10 00:41:36 +0000978 # find hosts that aren't in any ACL group and add them to Everyone
showard08f981b2008-06-24 21:59:03 +0000979 # TODO(showard): this is a bit of a hack, since the fact that this query
980 # works is kind of a coincidence of Django internals. This trick
981 # doesn't work in general (on all foreign key relationships). I'll
982 # replace it with a better technique when the need arises.
showardd9ac4452009-02-07 02:04:37 +0000983 orphaned_hosts = Host.valid_objects.filter(aclgroup__id__isnull=True)
showard08f981b2008-06-24 21:59:03 +0000984 everyone.hosts.add(*orphaned_hosts.distinct())
985
986 # find hosts in both Everyone and another ACL group, and remove them
987 # from Everyone
showarda5288b42009-07-28 20:06:08 +0000988 hosts_in_everyone = Host.valid_objects.filter(aclgroup__name='Everyone')
989 acled_hosts = set()
990 for host in hosts_in_everyone:
991 # Has an ACL group other than Everyone
992 if host.aclgroup_set.count() > 1:
993 acled_hosts.add(host)
994 everyone.hosts.remove(*acled_hosts)
showard08f981b2008-06-24 21:59:03 +0000995
996
997 def delete(self):
showard3dd47c22008-07-10 00:41:36 +0000998 if (self.name == 'Everyone'):
999 raise AclAccessViolation("You cannot delete 'Everyone'!")
1000 self.check_for_acl_violation_acl_group()
showard08f981b2008-06-24 21:59:03 +00001001 super(AclGroup, self).delete()
1002 self.on_host_membership_change()
1003
1004
showard04f2cd82008-07-25 20:53:31 +00001005 def add_current_user_if_empty(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001006 """Adds the current user if the set of users is empty."""
showard04f2cd82008-07-25 20:53:31 +00001007 if not self.users.count():
showard64a95952010-01-13 21:27:16 +00001008 self.users.add(User.current_user())
showard04f2cd82008-07-25 20:53:31 +00001009
1010
showard8cbaf1e2009-09-08 16:27:04 +00001011 def perform_after_save(self, change):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001012 """Called after a save.
1013
1014 @param change: Whether there was a change.
1015 """
showard8cbaf1e2009-09-08 16:27:04 +00001016 if not change:
showard64a95952010-01-13 21:27:16 +00001017 self.users.add(User.current_user())
showard8cbaf1e2009-09-08 16:27:04 +00001018 self.add_current_user_if_empty()
1019 self.on_host_membership_change()
1020
1021
1022 def save(self, *args, **kwargs):
Jakob Juelich116ff0f2014-09-17 18:25:16 -07001023 change = bool(self.id)
1024 if change:
showard8cbaf1e2009-09-08 16:27:04 +00001025 # Check the original object for an ACL violation
Jakob Juelich116ff0f2014-09-17 18:25:16 -07001026 AclGroup.objects.get(id=self.id).check_for_acl_violation_acl_group()
showard8cbaf1e2009-09-08 16:27:04 +00001027 super(AclGroup, self).save(*args, **kwargs)
Jakob Juelich116ff0f2014-09-17 18:25:16 -07001028 self.perform_after_save(change)
showard8cbaf1e2009-09-08 16:27:04 +00001029
showardeb3be4d2008-04-21 20:59:26 +00001030
jadmanski0afbb632008-06-06 21:10:57 +00001031 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001032 """Metadata for class AclGroup."""
showardeab66ce2009-12-23 00:03:56 +00001033 db_table = 'afe_acl_groups'
mblighe8819cd2008-02-15 16:48:40 +00001034
showarda5288b42009-07-28 20:06:08 +00001035 def __unicode__(self):
1036 return unicode(self.name)
mblighe8819cd2008-02-15 16:48:40 +00001037
mblighe8819cd2008-02-15 16:48:40 +00001038
jamesren4a41e012010-07-16 22:33:48 +00001039class ParameterizedJob(dbmodels.Model):
1040 """
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001041 Auxiliary configuration for a parameterized job.
jamesren4a41e012010-07-16 22:33:48 +00001042 """
1043 test = dbmodels.ForeignKey(Test)
1044 label = dbmodels.ForeignKey(Label, null=True)
1045 use_container = dbmodels.BooleanField(default=False)
1046 profile_only = dbmodels.BooleanField(default=False)
1047 upload_kernel_config = dbmodels.BooleanField(default=False)
1048
jamesren4a41e012010-07-16 22:33:48 +00001049 profilers = dbmodels.ManyToManyField(
1050 Profiler, through='ParameterizedJobProfiler')
1051
1052
1053 @classmethod
1054 def smart_get(cls, id_or_name, *args, **kwargs):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001055 """For compatibility with Job.add_object.
1056
1057 @param cls: Implicit class object.
1058 @param id_or_name: The ID or name to get.
1059 @param args: Non-keyword arguments.
1060 @param kwargs: Keyword arguments.
1061 """
jamesren4a41e012010-07-16 22:33:48 +00001062 return cls.objects.get(pk=id_or_name)
1063
1064
1065 def job(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001066 """Returns the job if it exists, or else None."""
jamesren4a41e012010-07-16 22:33:48 +00001067 jobs = self.job_set.all()
1068 assert jobs.count() <= 1
1069 return jobs and jobs[0] or None
1070
1071
1072 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001073 """Metadata for class ParameterizedJob."""
jamesren4a41e012010-07-16 22:33:48 +00001074 db_table = 'afe_parameterized_jobs'
1075
1076 def __unicode__(self):
1077 return u'%s (parameterized) - %s' % (self.test.name, self.job())
1078
1079
1080class ParameterizedJobProfiler(dbmodels.Model):
1081 """
1082 A profiler to run on a parameterized job
1083 """
1084 parameterized_job = dbmodels.ForeignKey(ParameterizedJob)
1085 profiler = dbmodels.ForeignKey(Profiler)
1086
1087 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001088 """Metedata for class ParameterizedJobProfiler."""
jamesren4a41e012010-07-16 22:33:48 +00001089 db_table = 'afe_parameterized_jobs_profilers'
1090 unique_together = ('parameterized_job', 'profiler')
1091
1092
showard7c785282008-05-29 19:45:12 +00001093class JobManager(model_logic.ExtendedManager):
jadmanski0afbb632008-06-06 21:10:57 +00001094 'Custom manager to provide efficient status counts querying.'
1095 def get_status_counts(self, job_ids):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001096 """Returns a dict mapping the given job IDs to their status count dicts.
1097
1098 @param job_ids: A list of job IDs.
jadmanski0afbb632008-06-06 21:10:57 +00001099 """
1100 if not job_ids:
1101 return {}
1102 id_list = '(%s)' % ','.join(str(job_id) for job_id in job_ids)
1103 cursor = connection.cursor()
1104 cursor.execute("""
showardd3dc1992009-04-22 21:01:40 +00001105 SELECT job_id, status, aborted, complete, COUNT(*)
showardeab66ce2009-12-23 00:03:56 +00001106 FROM afe_host_queue_entries
jadmanski0afbb632008-06-06 21:10:57 +00001107 WHERE job_id IN %s
showardd3dc1992009-04-22 21:01:40 +00001108 GROUP BY job_id, status, aborted, complete
jadmanski0afbb632008-06-06 21:10:57 +00001109 """ % id_list)
showard25aaf3f2009-06-08 23:23:40 +00001110 all_job_counts = dict((job_id, {}) for job_id in job_ids)
showardd3dc1992009-04-22 21:01:40 +00001111 for job_id, status, aborted, complete, count in cursor.fetchall():
showard25aaf3f2009-06-08 23:23:40 +00001112 job_dict = all_job_counts[job_id]
showardd3dc1992009-04-22 21:01:40 +00001113 full_status = HostQueueEntry.compute_full_status(status, aborted,
1114 complete)
showardb6d16622009-05-26 19:35:29 +00001115 job_dict.setdefault(full_status, 0)
showard25aaf3f2009-06-08 23:23:40 +00001116 job_dict[full_status] += count
jadmanski0afbb632008-06-06 21:10:57 +00001117 return all_job_counts
mblighe8819cd2008-02-15 16:48:40 +00001118
1119
showard7c785282008-05-29 19:45:12 +00001120class Job(dbmodels.Model, model_logic.ModelExtensions):
jadmanski0afbb632008-06-06 21:10:57 +00001121 """\
1122 owner: username of job owner
1123 name: job name (does not have to be unique)
Alex Miller7d658cf2013-09-04 16:00:35 -07001124 priority: Integer priority value. Higher is more important.
jadmanski0afbb632008-06-06 21:10:57 +00001125 control_file: contents of control file
1126 control_type: Client or Server
1127 created_on: date of job creation
1128 submitted_on: date of job submission
showard2bab8f42008-11-12 18:15:22 +00001129 synch_count: how many hosts should be used per autoserv execution
showard909c7a62008-07-15 21:52:38 +00001130 run_verify: Whether or not to run the verify phase
Dan Shi07e09af2013-04-12 09:31:29 -07001131 run_reset: Whether or not to run the reset phase
Simran Basi94dc0032013-11-12 14:09:46 -08001132 timeout: DEPRECATED - hours from queuing time until job times out
1133 timeout_mins: minutes from job queuing time until the job times out
Simran Basi34217022012-11-06 13:43:15 -08001134 max_runtime_hrs: DEPRECATED - hours from job starting time until job
1135 times out
1136 max_runtime_mins: minutes from job starting time until job times out
showard542e8402008-09-19 20:16:18 +00001137 email_list: list of people to email on completion delimited by any of:
1138 white space, ',', ':', ';'
showard989f25d2008-10-01 11:38:11 +00001139 dependency_labels: many-to-many relationship with labels corresponding to
1140 job dependencies
showard21baa452008-10-21 00:08:39 +00001141 reboot_before: Never, If dirty, or Always
1142 reboot_after: Never, If all tests passed, or Always
showarda1e74b32009-05-12 17:32:04 +00001143 parse_failed_repair: if True, a failed repair launched by this job will have
1144 its results parsed as part of the job.
jamesren76fcf192010-04-21 20:39:50 +00001145 drone_set: The set of drones to run this job on
Aviv Keshet0b9cfc92013-02-05 11:36:02 -08001146 parent_job: Parent job (optional)
Aviv Keshetcd1ff9b2013-03-01 14:55:19 -08001147 test_retry: Number of times to retry test if the test did not complete
1148 successfully. (optional, default: 0)
Dan Shic9e17142015-02-19 11:50:55 -08001149 require_ssp: Require server-side packaging unless require_ssp is set to
1150 False. (optional, default: None)
jadmanski0afbb632008-06-06 21:10:57 +00001151 """
Jakob Juelich3bb7c802014-09-02 16:31:11 -07001152
1153 # TODO: Investigate, if jobkeyval_set is really needed.
1154 # dynamic_suite will write them into an attached file for the drone, but
1155 # it doesn't seem like they are actually used. If they aren't used, remove
1156 # jobkeyval_set here.
1157 SERIALIZATION_LINKS_TO_FOLLOW = set(['dependency_labels',
1158 'hostqueueentry_set',
1159 'jobkeyval_set',
1160 'shard'])
1161
Fang Deng2b79fd72015-05-20 19:11:36 -07001162 # SQL for selecting jobs that should be sent to shard.
1163 # We use raw sql as django filters were not optimized.
1164 # The following jobs are excluded by the SQL.
1165 # - Non-aborted jobs known to shard as specified in |known_ids|.
1166 # Note for jobs aborted on master, even if already known to shard,
1167 # will be sent to shard again so that shard can abort them.
1168 # - Completed jobs
1169 # - Active jobs
1170 # - Jobs without host_queue_entries
1171 NON_ABORTED_KNOWN_JOBS = '(t2.aborted = 0 AND t1.id IN (%(known_ids)s))'
1172
1173 SQL_SHARD_JOBS = (
MK Ryu5c7ef302015-07-10 17:23:08 -07001174 'SELECT DISTINCT(t1.id) FROM afe_jobs t1 '
Fang Deng2b79fd72015-05-20 19:11:36 -07001175 'INNER JOIN afe_host_queue_entries t2 ON '
MK Ryu5c7ef302015-07-10 17:23:08 -07001176 ' (t1.id = t2.job_id AND t2.complete != 1 AND t2.active != 1 '
Fang Deng2b79fd72015-05-20 19:11:36 -07001177 ' %(check_known_jobs)s) '
MK Ryu5c7ef302015-07-10 17:23:08 -07001178 'LEFT OUTER JOIN afe_jobs_dependency_labels t3 ON (t1.id = t3.job_id) '
Fang Deng2b79fd72015-05-20 19:11:36 -07001179 'WHERE (t3.label_id IN '
1180 ' (SELECT label_id FROM afe_shards_labels '
1181 ' WHERE shard_id = %(shard_id)s) '
MK Ryu5c7ef302015-07-10 17:23:08 -07001182 ' OR t2.meta_host IN '
1183 ' (SELECT label_id FROM afe_shards_labels '
1184 ' WHERE shard_id = %(shard_id)s))'
Fang Deng2b79fd72015-05-20 19:11:36 -07001185 )
1186
MK Ryu5c7ef302015-07-10 17:23:08 -07001187 # Jobs can be created with assigned hosts and have no dependency
1188 # labels nor meta_host.
Fang Deng2b79fd72015-05-20 19:11:36 -07001189 # We are looking for:
MK Ryu5c7ef302015-07-10 17:23:08 -07001190 # - a job whose hqe's meta_host is null
Fang Deng2b79fd72015-05-20 19:11:36 -07001191 # - a job whose hqe has a host
1192 # - one of the host's labels matches the shard's label.
1193 # Non-aborted known jobs, completed jobs, active jobs, jobs
MK Ryu5c7ef302015-07-10 17:23:08 -07001194 # without hqe are exluded as we do with SQL_SHARD_JOBS.
1195 SQL_SHARD_JOBS_WITH_HOSTS = (
1196 'SELECT DISTINCT(t1.id) FROM afe_jobs t1 '
Fang Deng2b79fd72015-05-20 19:11:36 -07001197 'INNER JOIN afe_host_queue_entries t2 ON '
1198 ' (t1.id = t2.job_id AND t2.complete != 1 AND t2.active != 1 '
1199 ' AND t2.meta_host IS NULL AND t2.host_id IS NOT NULL '
1200 ' %(check_known_jobs)s) '
1201 'LEFT OUTER JOIN afe_hosts_labels t3 ON (t2.host_id = t3.host_id) '
1202 'WHERE (t3.label_id IN '
1203 ' (SELECT label_id FROM afe_shards_labels '
1204 ' WHERE shard_id = %(shard_id)s))'
1205 )
MK Ryu5c7ef302015-07-10 17:23:08 -07001206
Fang Deng2b79fd72015-05-20 19:11:36 -07001207 # Even if we had filters about complete, active and aborted
MK Ryu5c7ef302015-07-10 17:23:08 -07001208 # bits in the above two SQLs, there is a chance that
Fang Deng2b79fd72015-05-20 19:11:36 -07001209 # the result may still contain a job with an hqe with 'complete=1'
1210 # or 'active=1' or 'aborted=0 and afe_job.id in known jobs.'
1211 # This happens when a job has two (or more) hqes and at least
1212 # one hqe has different bits than others.
1213 # We use a second sql to ensure we exclude all un-desired jobs.
1214 SQL_JOBS_TO_EXCLUDE =(
1215 'SELECT t1.id FROM afe_jobs t1 '
1216 'INNER JOIN afe_host_queue_entries t2 ON '
1217 ' (t1.id = t2.job_id) '
1218 'WHERE (t1.id in (%(candidates)s) '
1219 ' AND (t2.complete=1 OR t2.active=1 '
1220 ' %(check_known_jobs)s))'
1221 )
Jakob Juelich3bb7c802014-09-02 16:31:11 -07001222
Jakob Juelichf88fa932014-09-03 17:58:04 -07001223 def _deserialize_relation(self, link, data):
1224 if link in ['hostqueueentry_set', 'jobkeyval_set']:
1225 for obj in data:
1226 obj['job_id'] = self.id
1227
1228 super(Job, self)._deserialize_relation(link, data)
1229
1230
1231 def custom_deserialize_relation(self, link, data):
Jakob Juelich116ff0f2014-09-17 18:25:16 -07001232 assert link == 'shard', 'Link %s should not be deserialized' % link
Jakob Juelichf88fa932014-09-03 17:58:04 -07001233 self.shard = Shard.deserialize(data)
1234
1235
Jakob Juelicha94efe62014-09-18 16:02:49 -07001236 def sanity_check_update_from_shard(self, shard, updated_serialized):
Jakob Juelich02e61292014-10-17 12:36:55 -07001237 # If the job got aborted on the master after the client fetched it
1238 # no shard_id will be set. The shard might still push updates though,
1239 # as the job might complete before the abort bit syncs to the shard.
1240 # Alternative considered: The master scheduler could be changed to not
1241 # set aborted jobs to completed that are sharded out. But that would
1242 # require database queries and seemed more complicated to implement.
1243 # This seems safe to do, as there won't be updates pushed from the wrong
1244 # shards should be powered off and wiped hen they are removed from the
1245 # master.
1246 if self.shard_id and self.shard_id != shard.id:
Jakob Juelicha94efe62014-09-18 16:02:49 -07001247 raise error.UnallowedRecordsSentToMaster(
1248 'Job id=%s is assigned to shard (%s). Cannot update it with %s '
1249 'from shard %s.' % (self.id, self.shard_id, updated_serialized,
1250 shard.id))
1251
1252
Simran Basi94dc0032013-11-12 14:09:46 -08001253 # TIMEOUT is deprecated.
showardb1e51872008-10-07 11:08:18 +00001254 DEFAULT_TIMEOUT = global_config.global_config.get_config_value(
Simran Basi94dc0032013-11-12 14:09:46 -08001255 'AUTOTEST_WEB', 'job_timeout_default', default=24)
1256 DEFAULT_TIMEOUT_MINS = global_config.global_config.get_config_value(
1257 'AUTOTEST_WEB', 'job_timeout_mins_default', default=24*60)
Simran Basi34217022012-11-06 13:43:15 -08001258 # MAX_RUNTIME_HRS is deprecated. Will be removed after switch to mins is
1259 # completed.
showard12f3e322009-05-13 21:27:42 +00001260 DEFAULT_MAX_RUNTIME_HRS = global_config.global_config.get_config_value(
1261 'AUTOTEST_WEB', 'job_max_runtime_hrs_default', default=72)
Simran Basi34217022012-11-06 13:43:15 -08001262 DEFAULT_MAX_RUNTIME_MINS = global_config.global_config.get_config_value(
1263 'AUTOTEST_WEB', 'job_max_runtime_mins_default', default=72*60)
showarda1e74b32009-05-12 17:32:04 +00001264 DEFAULT_PARSE_FAILED_REPAIR = global_config.global_config.get_config_value(
1265 'AUTOTEST_WEB', 'parse_failed_repair_default', type=bool,
1266 default=False)
showardb1e51872008-10-07 11:08:18 +00001267
showarda5288b42009-07-28 20:06:08 +00001268 owner = dbmodels.CharField(max_length=255)
1269 name = dbmodels.CharField(max_length=255)
Alex Miller7d658cf2013-09-04 16:00:35 -07001270 priority = dbmodels.SmallIntegerField(default=priorities.Priority.DEFAULT)
jamesren4a41e012010-07-16 22:33:48 +00001271 control_file = dbmodels.TextField(null=True, blank=True)
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001272 control_type = dbmodels.SmallIntegerField(
1273 choices=control_data.CONTROL_TYPE.choices(),
1274 blank=True, # to allow 0
1275 default=control_data.CONTROL_TYPE.CLIENT)
showard68c7aa02008-10-09 16:49:11 +00001276 created_on = dbmodels.DateTimeField()
Simran Basi8d89b642014-05-02 17:33:20 -07001277 synch_count = dbmodels.IntegerField(blank=True, default=0)
showardb1e51872008-10-07 11:08:18 +00001278 timeout = dbmodels.IntegerField(default=DEFAULT_TIMEOUT)
Dan Shi07e09af2013-04-12 09:31:29 -07001279 run_verify = dbmodels.BooleanField(default=False)
showarda5288b42009-07-28 20:06:08 +00001280 email_list = dbmodels.CharField(max_length=250, blank=True)
showardeab66ce2009-12-23 00:03:56 +00001281 dependency_labels = (
1282 dbmodels.ManyToManyField(Label, blank=True,
1283 db_table='afe_jobs_dependency_labels'))
jamesrendd855242010-03-02 22:23:44 +00001284 reboot_before = dbmodels.SmallIntegerField(
1285 choices=model_attributes.RebootBefore.choices(), blank=True,
1286 default=DEFAULT_REBOOT_BEFORE)
1287 reboot_after = dbmodels.SmallIntegerField(
1288 choices=model_attributes.RebootAfter.choices(), blank=True,
1289 default=DEFAULT_REBOOT_AFTER)
showarda1e74b32009-05-12 17:32:04 +00001290 parse_failed_repair = dbmodels.BooleanField(
1291 default=DEFAULT_PARSE_FAILED_REPAIR)
Simran Basi34217022012-11-06 13:43:15 -08001292 # max_runtime_hrs is deprecated. Will be removed after switch to mins is
1293 # completed.
showard12f3e322009-05-13 21:27:42 +00001294 max_runtime_hrs = dbmodels.IntegerField(default=DEFAULT_MAX_RUNTIME_HRS)
Simran Basi34217022012-11-06 13:43:15 -08001295 max_runtime_mins = dbmodels.IntegerField(default=DEFAULT_MAX_RUNTIME_MINS)
jamesren76fcf192010-04-21 20:39:50 +00001296 drone_set = dbmodels.ForeignKey(DroneSet, null=True, blank=True)
mblighe8819cd2008-02-15 16:48:40 +00001297
jamesren4a41e012010-07-16 22:33:48 +00001298 parameterized_job = dbmodels.ForeignKey(ParameterizedJob, null=True,
1299 blank=True)
1300
Aviv Keshet0b9cfc92013-02-05 11:36:02 -08001301 parent_job = dbmodels.ForeignKey('self', blank=True, null=True)
mblighe8819cd2008-02-15 16:48:40 +00001302
Aviv Keshetcd1ff9b2013-03-01 14:55:19 -08001303 test_retry = dbmodels.IntegerField(blank=True, default=0)
1304
Dan Shi07e09af2013-04-12 09:31:29 -07001305 run_reset = dbmodels.BooleanField(default=True)
1306
Simran Basi94dc0032013-11-12 14:09:46 -08001307 timeout_mins = dbmodels.IntegerField(default=DEFAULT_TIMEOUT_MINS)
1308
Jakob Juelich8421d592014-09-17 15:27:06 -07001309 # If this is None on the master, a slave should be found.
1310 # If this is None on a slave, it should be synced back to the master
Jakob Jülich92c06332014-08-25 19:06:57 +00001311 shard = dbmodels.ForeignKey(Shard, blank=True, null=True)
1312
Dan Shic9e17142015-02-19 11:50:55 -08001313 # If this is None, server-side packaging will be used for server side test,
1314 # unless it's disabled in global config AUTOSERV/enable_ssp_container.
1315 require_ssp = dbmodels.NullBooleanField(default=None, blank=True, null=True)
1316
jadmanski0afbb632008-06-06 21:10:57 +00001317 # custom manager
1318 objects = JobManager()
mblighe8819cd2008-02-15 16:48:40 +00001319
1320
Alex Millerec212252014-02-28 16:48:34 -08001321 @decorators.cached_property
1322 def labels(self):
1323 """All the labels of this job"""
1324 # We need to convert dependency_labels to a list, because all() gives us
1325 # back an iterator, and storing/caching an iterator means we'd only be
1326 # able to read from it once.
1327 return list(self.dependency_labels.all())
1328
1329
jadmanski0afbb632008-06-06 21:10:57 +00001330 def is_server_job(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001331 """Returns whether this job is of type server."""
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001332 return self.control_type == control_data.CONTROL_TYPE.SERVER
mblighe8819cd2008-02-15 16:48:40 +00001333
1334
jadmanski0afbb632008-06-06 21:10:57 +00001335 @classmethod
jamesren4a41e012010-07-16 22:33:48 +00001336 def parameterized_jobs_enabled(cls):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001337 """Returns whether parameterized jobs are enabled.
1338
1339 @param cls: Implicit class object.
1340 """
jamesren4a41e012010-07-16 22:33:48 +00001341 return global_config.global_config.get_config_value(
1342 'AUTOTEST_WEB', 'parameterized_jobs', type=bool)
1343
1344
1345 @classmethod
1346 def check_parameterized_job(cls, control_file, parameterized_job):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001347 """Checks that the job is valid given the global config settings.
jamesren4a41e012010-07-16 22:33:48 +00001348
1349 First, either control_file must be set, or parameterized_job must be
1350 set, but not both. Second, parameterized_job must be set if and only if
1351 the parameterized_jobs option in the global config is set to True.
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001352
1353 @param cls: Implict class object.
1354 @param control_file: A control file.
1355 @param parameterized_job: A parameterized job.
jamesren4a41e012010-07-16 22:33:48 +00001356 """
1357 if not (bool(control_file) ^ bool(parameterized_job)):
1358 raise Exception('Job must have either control file or '
1359 'parameterization, but not both')
1360
1361 parameterized_jobs_enabled = cls.parameterized_jobs_enabled()
1362 if control_file and parameterized_jobs_enabled:
1363 raise Exception('Control file specified, but parameterized jobs '
1364 'are enabled')
1365 if parameterized_job and not parameterized_jobs_enabled:
1366 raise Exception('Parameterized job specified, but parameterized '
1367 'jobs are not enabled')
1368
1369
1370 @classmethod
showarda1e74b32009-05-12 17:32:04 +00001371 def create(cls, owner, options, hosts):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001372 """Creates a job.
1373
1374 The job is created by taking some information (the listed args) and
1375 filling in the rest of the necessary information.
1376
1377 @param cls: Implicit class object.
1378 @param owner: The owner for the job.
1379 @param options: An options object.
1380 @param hosts: The hosts to use.
jadmanski0afbb632008-06-06 21:10:57 +00001381 """
showard3dd47c22008-07-10 00:41:36 +00001382 AclGroup.check_for_acl_violation_hosts(hosts)
showard4d233752010-01-20 19:06:40 +00001383
jamesren4a41e012010-07-16 22:33:48 +00001384 control_file = options.get('control_file')
1385 parameterized_job = options.get('parameterized_job')
jamesren4a41e012010-07-16 22:33:48 +00001386
Paul Pendlebury5a8c6ad2011-02-01 07:20:17 -08001387 # The current implementation of parameterized jobs requires that only
1388 # control files or parameterized jobs are used. Using the image
1389 # parameter on autoupdate_ParameterizedJob doesn't mix pure
1390 # parameterized jobs and control files jobs, it does muck enough with
1391 # normal jobs by adding a parameterized id to them that this check will
1392 # fail. So for now we just skip this check.
1393 # cls.check_parameterized_job(control_file=control_file,
1394 # parameterized_job=parameterized_job)
showard4d233752010-01-20 19:06:40 +00001395 user = User.current_user()
1396 if options.get('reboot_before') is None:
1397 options['reboot_before'] = user.get_reboot_before_display()
1398 if options.get('reboot_after') is None:
1399 options['reboot_after'] = user.get_reboot_after_display()
1400
jamesren76fcf192010-04-21 20:39:50 +00001401 drone_set = DroneSet.resolve_name(options.get('drone_set'))
1402
Simran Basi94dc0032013-11-12 14:09:46 -08001403 if options.get('timeout_mins') is None and options.get('timeout'):
1404 options['timeout_mins'] = options['timeout'] * 60
1405
jadmanski0afbb632008-06-06 21:10:57 +00001406 job = cls.add_object(
showarda1e74b32009-05-12 17:32:04 +00001407 owner=owner,
1408 name=options['name'],
1409 priority=options['priority'],
jamesren4a41e012010-07-16 22:33:48 +00001410 control_file=control_file,
showarda1e74b32009-05-12 17:32:04 +00001411 control_type=options['control_type'],
1412 synch_count=options.get('synch_count'),
Simran Basi94dc0032013-11-12 14:09:46 -08001413 # timeout needs to be deleted in the future.
showarda1e74b32009-05-12 17:32:04 +00001414 timeout=options.get('timeout'),
Simran Basi94dc0032013-11-12 14:09:46 -08001415 timeout_mins=options.get('timeout_mins'),
Simran Basi34217022012-11-06 13:43:15 -08001416 max_runtime_mins=options.get('max_runtime_mins'),
showarda1e74b32009-05-12 17:32:04 +00001417 run_verify=options.get('run_verify'),
1418 email_list=options.get('email_list'),
1419 reboot_before=options.get('reboot_before'),
1420 reboot_after=options.get('reboot_after'),
1421 parse_failed_repair=options.get('parse_failed_repair'),
jamesren76fcf192010-04-21 20:39:50 +00001422 created_on=datetime.now(),
jamesren4a41e012010-07-16 22:33:48 +00001423 drone_set=drone_set,
Aviv Keshet18308922013-02-19 17:49:49 -08001424 parameterized_job=parameterized_job,
Aviv Keshetcd1ff9b2013-03-01 14:55:19 -08001425 parent_job=options.get('parent_job_id'),
Dan Shi07e09af2013-04-12 09:31:29 -07001426 test_retry=options.get('test_retry'),
Dan Shic9e17142015-02-19 11:50:55 -08001427 run_reset=options.get('run_reset'),
1428 require_ssp=options.get('require_ssp'))
mblighe8819cd2008-02-15 16:48:40 +00001429
showarda1e74b32009-05-12 17:32:04 +00001430 job.dependency_labels = options['dependencies']
showardc1a98d12010-01-15 00:22:22 +00001431
jamesrend8b6e172010-04-16 23:45:00 +00001432 if options.get('keyvals'):
showardc1a98d12010-01-15 00:22:22 +00001433 for key, value in options['keyvals'].iteritems():
1434 JobKeyval.objects.create(job=job, key=key, value=value)
1435
jadmanski0afbb632008-06-06 21:10:57 +00001436 return job
mblighe8819cd2008-02-15 16:48:40 +00001437
1438
Jakob Juelich59cfe542014-09-02 16:37:46 -07001439 @classmethod
Jakob Juelich1b525742014-09-30 13:08:07 -07001440 def assign_to_shard(cls, shard, known_ids):
Jakob Juelich59cfe542014-09-02 16:37:46 -07001441 """Assigns unassigned jobs to a shard.
1442
Jakob Juelich1b525742014-09-30 13:08:07 -07001443 For all labels that have been assigned to this shard, all jobs that
1444 have this label, are assigned to this shard.
1445
1446 Jobs that are assigned to the shard but aren't already present on the
1447 shard are returned.
1448
Jakob Juelich59cfe542014-09-02 16:37:46 -07001449 @param shard: The shard to assign jobs to.
Jakob Juelich1b525742014-09-30 13:08:07 -07001450 @param known_ids: List of all ids of incomplete jobs, the shard already
1451 knows about.
1452 This is used to figure out which jobs should be sent
1453 to the shard. If shard_ids were used instead, jobs
1454 would only be transferred once, even if the client
1455 failed persisting them.
1456 The number of unfinished jobs usually lies in O(1000).
1457 Assuming one id takes 8 chars in the json, this means
1458 overhead that lies in the lower kilobyte range.
1459 A not in query with 5000 id's takes about 30ms.
1460
Jakob Juelich59cfe542014-09-02 16:37:46 -07001461 @returns The job objects that should be sent to the shard.
1462 """
1463 # Disclaimer: Concurrent heartbeats should not occur in today's setup.
1464 # If this changes or they are triggered manually, this applies:
1465 # Jobs may be returned more than once by concurrent calls of this
1466 # function, as there is a race condition between SELECT and UPDATE.
Fang Deng2b79fd72015-05-20 19:11:36 -07001467 job_ids = set([])
1468 check_known_jobs_exclude = ''
1469 check_known_jobs_include = ''
Prashanth Balasubramanian8c98ac12014-12-23 11:26:44 -08001470
Fang Deng2b79fd72015-05-20 19:11:36 -07001471 if known_ids:
1472 check_known_jobs = (
1473 cls.NON_ABORTED_KNOWN_JOBS %
1474 {'known_ids': ','.join([str(i) for i in known_ids])})
1475 check_known_jobs_exclude = 'AND NOT ' + check_known_jobs
1476 check_known_jobs_include = 'OR ' + check_known_jobs
1477
MK Ryu5c7ef302015-07-10 17:23:08 -07001478 for sql in [cls.SQL_SHARD_JOBS, cls.SQL_SHARD_JOBS_WITH_HOSTS]:
MK Ryu2e3e6052015-07-09 16:15:18 -07001479 query = Job.objects.raw(sql % {
1480 'check_known_jobs': check_known_jobs_exclude,
1481 'shard_id': shard.id})
Fang Deng2b79fd72015-05-20 19:11:36 -07001482 job_ids |= set([j.id for j in query])
1483
1484 if job_ids:
1485 query = Job.objects.raw(
1486 cls.SQL_JOBS_TO_EXCLUDE %
1487 {'check_known_jobs': check_known_jobs_include,
1488 'candidates': ','.join([str(i) for i in job_ids])})
1489 job_ids -= set([j.id for j in query])
1490
Jakob Juelich59cfe542014-09-02 16:37:46 -07001491 if job_ids:
1492 Job.objects.filter(pk__in=job_ids).update(shard=shard)
1493 return list(Job.objects.filter(pk__in=job_ids).all())
1494 return []
1495
1496
jamesren4a41e012010-07-16 22:33:48 +00001497 def save(self, *args, **kwargs):
Paul Pendlebury5a8c6ad2011-02-01 07:20:17 -08001498 # The current implementation of parameterized jobs requires that only
1499 # control files or parameterized jobs are used. Using the image
1500 # parameter on autoupdate_ParameterizedJob doesn't mix pure
1501 # parameterized jobs and control files jobs, it does muck enough with
1502 # normal jobs by adding a parameterized id to them that this check will
1503 # fail. So for now we just skip this check.
1504 # cls.check_parameterized_job(control_file=self.control_file,
1505 # parameterized_job=self.parameterized_job)
jamesren4a41e012010-07-16 22:33:48 +00001506 super(Job, self).save(*args, **kwargs)
1507
1508
showard29f7cd22009-04-29 21:16:24 +00001509 def queue(self, hosts, atomic_group=None, is_template=False):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001510 """Enqueue a job on the given hosts.
1511
1512 @param hosts: The hosts to use.
1513 @param atomic_group: The associated atomic group.
1514 @param is_template: Whether the status should be "Template".
1515 """
showarda9545c02009-12-18 22:44:26 +00001516 if not hosts:
1517 if atomic_group:
1518 # No hosts or labels are required to queue an atomic group
1519 # Job. However, if they are given, we respect them below.
1520 atomic_group.enqueue_job(self, is_template=is_template)
1521 else:
1522 # hostless job
1523 entry = HostQueueEntry.create(job=self, is_template=is_template)
1524 entry.save()
1525 return
1526
jadmanski0afbb632008-06-06 21:10:57 +00001527 for host in hosts:
showard29f7cd22009-04-29 21:16:24 +00001528 host.enqueue_job(self, atomic_group=atomic_group,
1529 is_template=is_template)
1530
1531
1532 def create_recurring_job(self, start_date, loop_period, loop_count, owner):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001533 """Creates a recurring job.
1534
1535 @param start_date: The starting date of the job.
1536 @param loop_period: How often to re-run the job, in seconds.
1537 @param loop_count: The re-run count.
1538 @param owner: The owner of the job.
1539 """
showard29f7cd22009-04-29 21:16:24 +00001540 rec = RecurringRun(job=self, start_date=start_date,
1541 loop_period=loop_period,
1542 loop_count=loop_count,
1543 owner=User.objects.get(login=owner))
1544 rec.save()
1545 return rec.id
mblighe8819cd2008-02-15 16:48:40 +00001546
1547
jadmanski0afbb632008-06-06 21:10:57 +00001548 def user(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001549 """Gets the user of this job, or None if it doesn't exist."""
jadmanski0afbb632008-06-06 21:10:57 +00001550 try:
1551 return User.objects.get(login=self.owner)
1552 except self.DoesNotExist:
1553 return None
mblighe8819cd2008-02-15 16:48:40 +00001554
1555
showard64a95952010-01-13 21:27:16 +00001556 def abort(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001557 """Aborts this job."""
showard98863972008-10-29 21:14:56 +00001558 for queue_entry in self.hostqueueentry_set.all():
showard64a95952010-01-13 21:27:16 +00001559 queue_entry.abort()
showard98863972008-10-29 21:14:56 +00001560
1561
showardd1195652009-12-08 22:21:02 +00001562 def tag(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001563 """Returns a string tag for this job."""
MK Ryu0c1a37d2015-04-30 12:00:55 -07001564 return server_utils.get_job_tag(self.id, self.owner)
showardd1195652009-12-08 22:21:02 +00001565
1566
showardc1a98d12010-01-15 00:22:22 +00001567 def keyval_dict(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001568 """Returns all keyvals for this job as a dictionary."""
showardc1a98d12010-01-15 00:22:22 +00001569 return dict((keyval.key, keyval.value)
1570 for keyval in self.jobkeyval_set.all())
1571
1572
Fang Dengff361592015-02-02 15:27:34 -08001573 @classmethod
1574 def get_attribute_model(cls):
1575 """Return the attribute model.
1576
1577 Override method in parent class. This class is called when
1578 deserializing the one-to-many relationship betwen Job and JobKeyval.
1579 On deserialization, we will try to clear any existing job keyvals
1580 associated with a job to avoid any inconsistency.
1581 Though Job doesn't implement ModelWithAttribute, we still treat
1582 it as an attribute model for this purpose.
1583
1584 @returns: The attribute model of Job.
1585 """
1586 return JobKeyval
1587
1588
jadmanski0afbb632008-06-06 21:10:57 +00001589 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001590 """Metadata for class Job."""
showardeab66ce2009-12-23 00:03:56 +00001591 db_table = 'afe_jobs'
mblighe8819cd2008-02-15 16:48:40 +00001592
showarda5288b42009-07-28 20:06:08 +00001593 def __unicode__(self):
1594 return u'%s (%s-%s)' % (self.name, self.id, self.owner)
mblighe8819cd2008-02-15 16:48:40 +00001595
1596
showardc1a98d12010-01-15 00:22:22 +00001597class JobKeyval(dbmodels.Model, model_logic.ModelExtensions):
1598 """Keyvals associated with jobs"""
Fang Dengff361592015-02-02 15:27:34 -08001599
1600 SERIALIZATION_LINKS_TO_KEEP = set(['job'])
1601 SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['value'])
1602
showardc1a98d12010-01-15 00:22:22 +00001603 job = dbmodels.ForeignKey(Job)
1604 key = dbmodels.CharField(max_length=90)
1605 value = dbmodels.CharField(max_length=300)
1606
1607 objects = model_logic.ExtendedManager()
1608
Fang Dengff361592015-02-02 15:27:34 -08001609
1610 @classmethod
1611 def get_record(cls, data):
1612 """Check the database for an identical record.
1613
1614 Use job_id and key to search for a existing record.
1615
1616 @raises: DoesNotExist, if no record found
1617 @raises: MultipleObjectsReturned if multiple records found.
1618 """
1619 # TODO(fdeng): We should use job_id and key together as
1620 # a primary key in the db.
1621 return cls.objects.get(job_id=data['job_id'], key=data['key'])
1622
1623
1624 @classmethod
1625 def deserialize(cls, data):
1626 """Override deserialize in parent class.
1627
1628 Do not deserialize id as id is not kept consistent on master and shards.
1629
1630 @param data: A dictionary of data to deserialize.
1631
1632 @returns: A JobKeyval object.
1633 """
1634 if data:
1635 data.pop('id')
1636 return super(JobKeyval, cls).deserialize(data)
1637
1638
showardc1a98d12010-01-15 00:22:22 +00001639 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001640 """Metadata for class JobKeyval."""
showardc1a98d12010-01-15 00:22:22 +00001641 db_table = 'afe_job_keyvals'
1642
1643
showard7c785282008-05-29 19:45:12 +00001644class IneligibleHostQueue(dbmodels.Model, model_logic.ModelExtensions):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001645 """Represents an ineligible host queue."""
jadmanski0afbb632008-06-06 21:10:57 +00001646 job = dbmodels.ForeignKey(Job)
1647 host = dbmodels.ForeignKey(Host)
mblighe8819cd2008-02-15 16:48:40 +00001648
jadmanski0afbb632008-06-06 21:10:57 +00001649 objects = model_logic.ExtendedManager()
showardeb3be4d2008-04-21 20:59:26 +00001650
jadmanski0afbb632008-06-06 21:10:57 +00001651 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001652 """Metadata for class IneligibleHostQueue."""
showardeab66ce2009-12-23 00:03:56 +00001653 db_table = 'afe_ineligible_host_queues'
mblighe8819cd2008-02-15 16:48:40 +00001654
mblighe8819cd2008-02-15 16:48:40 +00001655
showard7c785282008-05-29 19:45:12 +00001656class HostQueueEntry(dbmodels.Model, model_logic.ModelExtensions):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001657 """Represents a host queue entry."""
Jakob Juelich3bb7c802014-09-02 16:31:11 -07001658
1659 SERIALIZATION_LINKS_TO_FOLLOW = set(['meta_host'])
Prashanth Balasubramanian8c98ac12014-12-23 11:26:44 -08001660 SERIALIZATION_LINKS_TO_KEEP = set(['host'])
Jakob Juelichf865d332014-09-29 10:47:49 -07001661 SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['aborted'])
Jakob Juelich3bb7c802014-09-02 16:31:11 -07001662
Jakob Juelichf88fa932014-09-03 17:58:04 -07001663
1664 def custom_deserialize_relation(self, link, data):
1665 assert link == 'meta_host'
1666 self.meta_host = Label.deserialize(data)
1667
1668
Jakob Juelicha94efe62014-09-18 16:02:49 -07001669 def sanity_check_update_from_shard(self, shard, updated_serialized,
1670 job_ids_sent):
1671 if self.job_id not in job_ids_sent:
1672 raise error.UnallowedRecordsSentToMaster(
1673 'Sent HostQueueEntry without corresponding '
1674 'job entry: %s' % updated_serialized)
1675
1676
showardeaa408e2009-09-11 18:45:31 +00001677 Status = host_queue_entry_states.Status
1678 ACTIVE_STATUSES = host_queue_entry_states.ACTIVE_STATUSES
showardeab66ce2009-12-23 00:03:56 +00001679 COMPLETE_STATUSES = host_queue_entry_states.COMPLETE_STATUSES
showarda3ab0d52008-11-03 19:03:47 +00001680
jadmanski0afbb632008-06-06 21:10:57 +00001681 job = dbmodels.ForeignKey(Job)
1682 host = dbmodels.ForeignKey(Host, blank=True, null=True)
showarda5288b42009-07-28 20:06:08 +00001683 status = dbmodels.CharField(max_length=255)
jadmanski0afbb632008-06-06 21:10:57 +00001684 meta_host = dbmodels.ForeignKey(Label, blank=True, null=True,
1685 db_column='meta_host')
1686 active = dbmodels.BooleanField(default=False)
1687 complete = dbmodels.BooleanField(default=False)
showardb8471e32008-07-03 19:51:08 +00001688 deleted = dbmodels.BooleanField(default=False)
showarda5288b42009-07-28 20:06:08 +00001689 execution_subdir = dbmodels.CharField(max_length=255, blank=True,
1690 default='')
showard89f84db2009-03-12 20:39:13 +00001691 # If atomic_group is set, this is a virtual HostQueueEntry that will
1692 # be expanded into many actual hosts within the group at schedule time.
1693 atomic_group = dbmodels.ForeignKey(AtomicGroup, blank=True, null=True)
showardd3dc1992009-04-22 21:01:40 +00001694 aborted = dbmodels.BooleanField(default=False)
showardd3771cc2009-10-07 20:48:22 +00001695 started_on = dbmodels.DateTimeField(null=True, blank=True)
Fang Deng51599032014-06-23 17:24:27 -07001696 finished_on = dbmodels.DateTimeField(null=True, blank=True)
mblighe8819cd2008-02-15 16:48:40 +00001697
jadmanski0afbb632008-06-06 21:10:57 +00001698 objects = model_logic.ExtendedManager()
showardeb3be4d2008-04-21 20:59:26 +00001699
mblighe8819cd2008-02-15 16:48:40 +00001700
showard2bab8f42008-11-12 18:15:22 +00001701 def __init__(self, *args, **kwargs):
1702 super(HostQueueEntry, self).__init__(*args, **kwargs)
1703 self._record_attributes(['status'])
1704
1705
showard29f7cd22009-04-29 21:16:24 +00001706 @classmethod
1707 def create(cls, job, host=None, meta_host=None, atomic_group=None,
1708 is_template=False):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001709 """Creates a new host queue entry.
1710
1711 @param cls: Implicit class object.
1712 @param job: The associated job.
1713 @param host: The associated host.
1714 @param meta_host: The associated meta host.
1715 @param atomic_group: The associated atomic group.
1716 @param is_template: Whether the status should be "Template".
1717 """
showard29f7cd22009-04-29 21:16:24 +00001718 if is_template:
1719 status = cls.Status.TEMPLATE
1720 else:
1721 status = cls.Status.QUEUED
1722
1723 return cls(job=job, host=host, meta_host=meta_host,
1724 atomic_group=atomic_group, status=status)
1725
1726
showarda5288b42009-07-28 20:06:08 +00001727 def save(self, *args, **kwargs):
showard2bab8f42008-11-12 18:15:22 +00001728 self._set_active_and_complete()
showarda5288b42009-07-28 20:06:08 +00001729 super(HostQueueEntry, self).save(*args, **kwargs)
showard2bab8f42008-11-12 18:15:22 +00001730 self._check_for_updated_attributes()
1731
1732
showardc0ac3a72009-07-08 21:14:45 +00001733 def execution_path(self):
1734 """
1735 Path to this entry's results (relative to the base results directory).
1736 """
MK Ryu0c1a37d2015-04-30 12:00:55 -07001737 return server_utils.get_hqe_exec_path(self.job.tag(),
1738 self.execution_subdir)
showardc0ac3a72009-07-08 21:14:45 +00001739
1740
showard3f15eed2008-11-14 22:40:48 +00001741 def host_or_metahost_name(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001742 """Returns the first non-None name found in priority order.
1743
1744 The priority order checked is: (1) host name; (2) meta host name; and
1745 (3) atomic group name.
1746 """
showard3f15eed2008-11-14 22:40:48 +00001747 if self.host:
1748 return self.host.hostname
showard7890e792009-07-28 20:10:20 +00001749 elif self.meta_host:
showard3f15eed2008-11-14 22:40:48 +00001750 return self.meta_host.name
showard7890e792009-07-28 20:10:20 +00001751 else:
1752 assert self.atomic_group, "no host, meta_host or atomic group!"
1753 return self.atomic_group.name
showard3f15eed2008-11-14 22:40:48 +00001754
1755
showard2bab8f42008-11-12 18:15:22 +00001756 def _set_active_and_complete(self):
showardd3dc1992009-04-22 21:01:40 +00001757 if self.status in self.ACTIVE_STATUSES:
showard2bab8f42008-11-12 18:15:22 +00001758 self.active, self.complete = True, False
1759 elif self.status in self.COMPLETE_STATUSES:
1760 self.active, self.complete = False, True
1761 else:
1762 self.active, self.complete = False, False
1763
1764
1765 def on_attribute_changed(self, attribute, old_value):
1766 assert attribute == 'status'
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001767 logging.info('%s/%d (%d) -> %s', self.host, self.job.id, self.id,
1768 self.status)
showard2bab8f42008-11-12 18:15:22 +00001769
1770
jadmanski0afbb632008-06-06 21:10:57 +00001771 def is_meta_host_entry(self):
1772 'True if this is a entry has a meta_host instead of a host.'
1773 return self.host is None and self.meta_host is not None
mblighe8819cd2008-02-15 16:48:40 +00001774
showarda3ab0d52008-11-03 19:03:47 +00001775
Simran Basic1b26762013-06-26 14:23:21 -07001776 # This code is shared between rpc_interface and models.HostQueueEntry.
1777 # Sadly due to circular imports between the 2 (crbug.com/230100) making it
1778 # a class method was the best way to refactor it. Attempting to put it in
1779 # rpc_utils or a new utils module failed as that would require us to import
1780 # models.py but to call it from here we would have to import the utils.py
1781 # thus creating a cycle.
1782 @classmethod
1783 def abort_host_queue_entries(cls, host_queue_entries):
1784 """Aborts a collection of host_queue_entries.
1785
1786 Abort these host queue entry and all host queue entries of jobs created
1787 by them.
1788
1789 @param host_queue_entries: List of host queue entries we want to abort.
1790 """
1791 # This isn't completely immune to race conditions since it's not atomic,
1792 # but it should be safe given the scheduler's behavior.
1793
1794 # TODO(milleral): crbug.com/230100
1795 # The |abort_host_queue_entries| rpc does nearly exactly this,
1796 # however, trying to re-use the code generates some horrible
1797 # circular import error. I'd be nice to refactor things around
1798 # sometime so the code could be reused.
1799
1800 # Fixpoint algorithm to find the whole tree of HQEs to abort to
1801 # minimize the total number of database queries:
1802 children = set()
1803 new_children = set(host_queue_entries)
1804 while new_children:
1805 children.update(new_children)
1806 new_child_ids = [hqe.job_id for hqe in new_children]
1807 new_children = HostQueueEntry.objects.filter(
1808 job__parent_job__in=new_child_ids,
1809 complete=False, aborted=False).all()
1810 # To handle circular parental relationships
1811 new_children = set(new_children) - children
1812
1813 # Associate a user with the host queue entries that we're about
1814 # to abort so that we can look up who to blame for the aborts.
1815 now = datetime.now()
1816 user = User.current_user()
1817 aborted_hqes = [AbortedHostQueueEntry(queue_entry=hqe,
1818 aborted_by=user, aborted_on=now) for hqe in children]
1819 AbortedHostQueueEntry.objects.bulk_create(aborted_hqes)
1820 # Bulk update all of the HQEs to set the abort bit.
1821 child_ids = [hqe.id for hqe in children]
1822 HostQueueEntry.objects.filter(id__in=child_ids).update(aborted=True)
1823
1824
Scott Zawalski23041432013-04-17 07:39:09 -07001825 def abort(self):
Alex Millerdea67042013-04-22 17:23:34 -07001826 """ Aborts this host queue entry.
Simran Basic1b26762013-06-26 14:23:21 -07001827
Alex Millerdea67042013-04-22 17:23:34 -07001828 Abort this host queue entry and all host queue entries of jobs created by
1829 this one.
1830
1831 """
showardd3dc1992009-04-22 21:01:40 +00001832 if not self.complete and not self.aborted:
Simran Basi97582a22013-06-27 12:03:21 -07001833 HostQueueEntry.abort_host_queue_entries([self])
mblighe8819cd2008-02-15 16:48:40 +00001834
showardd3dc1992009-04-22 21:01:40 +00001835
1836 @classmethod
1837 def compute_full_status(cls, status, aborted, complete):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001838 """Returns a modified status msg if the host queue entry was aborted.
1839
1840 @param cls: Implicit class object.
1841 @param status: The original status message.
1842 @param aborted: Whether the host queue entry was aborted.
1843 @param complete: Whether the host queue entry was completed.
1844 """
showardd3dc1992009-04-22 21:01:40 +00001845 if aborted and not complete:
1846 return 'Aborted (%s)' % status
1847 return status
1848
1849
1850 def full_status(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001851 """Returns the full status of this host queue entry, as a string."""
showardd3dc1992009-04-22 21:01:40 +00001852 return self.compute_full_status(self.status, self.aborted,
1853 self.complete)
1854
1855
1856 def _postprocess_object_dict(self, object_dict):
1857 object_dict['full_status'] = self.full_status()
1858
1859
jadmanski0afbb632008-06-06 21:10:57 +00001860 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001861 """Metadata for class HostQueueEntry."""
showardeab66ce2009-12-23 00:03:56 +00001862 db_table = 'afe_host_queue_entries'
mblighe8819cd2008-02-15 16:48:40 +00001863
showard12f3e322009-05-13 21:27:42 +00001864
showard4c119042008-09-29 19:16:18 +00001865
showarda5288b42009-07-28 20:06:08 +00001866 def __unicode__(self):
showard12f3e322009-05-13 21:27:42 +00001867 hostname = None
1868 if self.host:
1869 hostname = self.host.hostname
showarda5288b42009-07-28 20:06:08 +00001870 return u"%s/%d (%d)" % (hostname, self.job.id, self.id)
showard12f3e322009-05-13 21:27:42 +00001871
1872
showard4c119042008-09-29 19:16:18 +00001873class AbortedHostQueueEntry(dbmodels.Model, model_logic.ModelExtensions):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001874 """Represents an aborted host queue entry."""
showard4c119042008-09-29 19:16:18 +00001875 queue_entry = dbmodels.OneToOneField(HostQueueEntry, primary_key=True)
1876 aborted_by = dbmodels.ForeignKey(User)
showard68c7aa02008-10-09 16:49:11 +00001877 aborted_on = dbmodels.DateTimeField()
showard4c119042008-09-29 19:16:18 +00001878
1879 objects = model_logic.ExtendedManager()
1880
showard68c7aa02008-10-09 16:49:11 +00001881
showarda5288b42009-07-28 20:06:08 +00001882 def save(self, *args, **kwargs):
showard68c7aa02008-10-09 16:49:11 +00001883 self.aborted_on = datetime.now()
showarda5288b42009-07-28 20:06:08 +00001884 super(AbortedHostQueueEntry, self).save(*args, **kwargs)
showard68c7aa02008-10-09 16:49:11 +00001885
showard4c119042008-09-29 19:16:18 +00001886 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001887 """Metadata for class AbortedHostQueueEntry."""
showardeab66ce2009-12-23 00:03:56 +00001888 db_table = 'afe_aborted_host_queue_entries'
showard29f7cd22009-04-29 21:16:24 +00001889
1890
1891class RecurringRun(dbmodels.Model, model_logic.ModelExtensions):
1892 """\
1893 job: job to use as a template
1894 owner: owner of the instantiated template
1895 start_date: Run the job at scheduled date
1896 loop_period: Re-run (loop) the job periodically
1897 (in every loop_period seconds)
1898 loop_count: Re-run (loop) count
1899 """
1900
1901 job = dbmodels.ForeignKey(Job)
1902 owner = dbmodels.ForeignKey(User)
1903 start_date = dbmodels.DateTimeField()
1904 loop_period = dbmodels.IntegerField(blank=True)
1905 loop_count = dbmodels.IntegerField(blank=True)
1906
1907 objects = model_logic.ExtendedManager()
1908
1909 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001910 """Metadata for class RecurringRun."""
showardeab66ce2009-12-23 00:03:56 +00001911 db_table = 'afe_recurring_run'
showard29f7cd22009-04-29 21:16:24 +00001912
showarda5288b42009-07-28 20:06:08 +00001913 def __unicode__(self):
1914 return u'RecurringRun(job %s, start %s, period %s, count %s)' % (
showard29f7cd22009-04-29 21:16:24 +00001915 self.job.id, self.start_date, self.loop_period, self.loop_count)
showard6d7b2ff2009-06-10 00:16:47 +00001916
1917
1918class SpecialTask(dbmodels.Model, model_logic.ModelExtensions):
1919 """\
1920 Tasks to run on hosts at the next time they are in the Ready state. Use this
1921 for high-priority tasks, such as forced repair or forced reinstall.
1922
1923 host: host to run this task on
showard2fe3f1d2009-07-06 20:19:11 +00001924 task: special task to run
showard6d7b2ff2009-06-10 00:16:47 +00001925 time_requested: date and time the request for this task was made
1926 is_active: task is currently running
1927 is_complete: task has finished running
beeps8bb1f7d2013-08-05 01:30:09 -07001928 is_aborted: task was aborted
showard2fe3f1d2009-07-06 20:19:11 +00001929 time_started: date and time the task started
Dan Shid0725542014-06-23 15:34:27 -07001930 time_finished: date and time the task finished
showard2fe3f1d2009-07-06 20:19:11 +00001931 queue_entry: Host queue entry waiting on this task (or None, if task was not
1932 started in preparation of a job)
showard6d7b2ff2009-06-10 00:16:47 +00001933 """
Alex Millerdfff2fd2013-05-28 13:05:06 -07001934 Task = enum.Enum('Verify', 'Cleanup', 'Repair', 'Reset', 'Provision',
Dan Shi07e09af2013-04-12 09:31:29 -07001935 string_values=True)
showard6d7b2ff2009-06-10 00:16:47 +00001936
1937 host = dbmodels.ForeignKey(Host, blank=False, null=False)
showarda5288b42009-07-28 20:06:08 +00001938 task = dbmodels.CharField(max_length=64, choices=Task.choices(),
showard6d7b2ff2009-06-10 00:16:47 +00001939 blank=False, null=False)
jamesren76fcf192010-04-21 20:39:50 +00001940 requested_by = dbmodels.ForeignKey(User)
showard6d7b2ff2009-06-10 00:16:47 +00001941 time_requested = dbmodels.DateTimeField(auto_now_add=True, blank=False,
1942 null=False)
1943 is_active = dbmodels.BooleanField(default=False, blank=False, null=False)
1944 is_complete = dbmodels.BooleanField(default=False, blank=False, null=False)
beeps8bb1f7d2013-08-05 01:30:09 -07001945 is_aborted = dbmodels.BooleanField(default=False, blank=False, null=False)
showardc0ac3a72009-07-08 21:14:45 +00001946 time_started = dbmodels.DateTimeField(null=True, blank=True)
showard2fe3f1d2009-07-06 20:19:11 +00001947 queue_entry = dbmodels.ForeignKey(HostQueueEntry, blank=True, null=True)
showarde60e44e2009-11-13 20:45:38 +00001948 success = dbmodels.BooleanField(default=False, blank=False, null=False)
Dan Shid0725542014-06-23 15:34:27 -07001949 time_finished = dbmodels.DateTimeField(null=True, blank=True)
showard6d7b2ff2009-06-10 00:16:47 +00001950
1951 objects = model_logic.ExtendedManager()
1952
1953
showard9bb960b2009-11-19 01:02:11 +00001954 def save(self, **kwargs):
1955 if self.queue_entry:
1956 self.requested_by = User.objects.get(
1957 login=self.queue_entry.job.owner)
1958 super(SpecialTask, self).save(**kwargs)
1959
1960
showarded2afea2009-07-07 20:54:07 +00001961 def execution_path(self):
MK Ryu0c1a37d2015-04-30 12:00:55 -07001962 """Returns the execution path for a special task."""
1963 return server_utils.get_special_task_exec_path(
1964 self.host.hostname, self.id, self.task, self.time_requested)
showarded2afea2009-07-07 20:54:07 +00001965
1966
showardc0ac3a72009-07-08 21:14:45 +00001967 # property to emulate HostQueueEntry.status
1968 @property
1969 def status(self):
MK Ryu0c1a37d2015-04-30 12:00:55 -07001970 """Returns a host queue entry status appropriate for a speical task."""
1971 return server_utils.get_special_task_status(
1972 self.is_complete, self.success, self.is_active)
showardc0ac3a72009-07-08 21:14:45 +00001973
1974
1975 # property to emulate HostQueueEntry.started_on
1976 @property
1977 def started_on(self):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001978 """Returns the time at which this special task started."""
showardc0ac3a72009-07-08 21:14:45 +00001979 return self.time_started
1980
1981
showard6d7b2ff2009-06-10 00:16:47 +00001982 @classmethod
showardc5103442010-01-15 00:20:26 +00001983 def schedule_special_task(cls, host, task):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08001984 """Schedules a special task on a host if not already scheduled.
1985
1986 @param cls: Implicit class object.
1987 @param host: The host to use.
1988 @param task: The task to schedule.
showard6d7b2ff2009-06-10 00:16:47 +00001989 """
showardc5103442010-01-15 00:20:26 +00001990 existing_tasks = SpecialTask.objects.filter(host__id=host.id, task=task,
1991 is_active=False,
1992 is_complete=False)
1993 if existing_tasks:
1994 return existing_tasks[0]
1995
1996 special_task = SpecialTask(host=host, task=task,
1997 requested_by=User.current_user())
1998 special_task.save()
1999 return special_task
showard2fe3f1d2009-07-06 20:19:11 +00002000
2001
beeps8bb1f7d2013-08-05 01:30:09 -07002002 def abort(self):
2003 """ Abort this special task."""
2004 self.is_aborted = True
2005 self.save()
2006
2007
showarded2afea2009-07-07 20:54:07 +00002008 def activate(self):
showard474d1362009-08-20 23:32:01 +00002009 """
2010 Sets a task as active and sets the time started to the current time.
showard2fe3f1d2009-07-06 20:19:11 +00002011 """
showard97446882009-07-20 22:37:28 +00002012 logging.info('Starting: %s', self)
showard2fe3f1d2009-07-06 20:19:11 +00002013 self.is_active = True
2014 self.time_started = datetime.now()
2015 self.save()
2016
2017
showarde60e44e2009-11-13 20:45:38 +00002018 def finish(self, success):
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08002019 """Sets a task as completed.
2020
2021 @param success: Whether or not the task was successful.
showard2fe3f1d2009-07-06 20:19:11 +00002022 """
showard97446882009-07-20 22:37:28 +00002023 logging.info('Finished: %s', self)
showarded2afea2009-07-07 20:54:07 +00002024 self.is_active = False
showard2fe3f1d2009-07-06 20:19:11 +00002025 self.is_complete = True
showarde60e44e2009-11-13 20:45:38 +00002026 self.success = success
Dan Shid85d6112014-07-14 10:32:55 -07002027 if self.time_started:
2028 self.time_finished = datetime.now()
showard2fe3f1d2009-07-06 20:19:11 +00002029 self.save()
showard6d7b2ff2009-06-10 00:16:47 +00002030
2031
2032 class Meta:
Dennis Jeffrey7db38ba2013-02-13 10:03:17 -08002033 """Metadata for class SpecialTask."""
showardeab66ce2009-12-23 00:03:56 +00002034 db_table = 'afe_special_tasks'
showard6d7b2ff2009-06-10 00:16:47 +00002035
showard474d1362009-08-20 23:32:01 +00002036
showarda5288b42009-07-28 20:06:08 +00002037 def __unicode__(self):
2038 result = u'Special Task %s (host %s, task %s, time %s)' % (
showarded2afea2009-07-07 20:54:07 +00002039 self.id, self.host, self.task, self.time_requested)
showard6d7b2ff2009-06-10 00:16:47 +00002040 if self.is_complete:
showarda5288b42009-07-28 20:06:08 +00002041 result += u' (completed)'
showard6d7b2ff2009-06-10 00:16:47 +00002042 elif self.is_active:
showarda5288b42009-07-28 20:06:08 +00002043 result += u' (active)'
showard6d7b2ff2009-06-10 00:16:47 +00002044
2045 return result
Dan Shi6964fa52014-12-18 11:04:27 -08002046
2047
2048class StableVersion(dbmodels.Model, model_logic.ModelExtensions):
2049
2050 board = dbmodels.CharField(max_length=255, unique=True)
2051 version = dbmodels.CharField(max_length=255)
2052
2053 class Meta:
2054 """Metadata for class StableVersion."""
Fang Deng86248502014-12-18 16:38:00 -08002055 db_table = 'afe_stable_versions'