Richard Barnette | b28def3 | 2017-03-07 13:23:08 -0800 | [diff] [blame] | 1 | # pylint: disable=missing-docstring |
Aviv Keshet | 0b9cfc9 | 2013-02-05 11:36:02 -0800 | [diff] [blame] | 2 | |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 3 | import logging |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 4 | from datetime import datetime |
Aviv Keshet | fa19900 | 2013-05-09 13:31:46 -0700 | [diff] [blame] | 5 | import django.core |
| 6 | try: |
| 7 | from django.db import models as dbmodels, connection |
| 8 | except 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.') |
jamesren | 35a7022 | 2010-02-16 19:30:46 +0000 | [diff] [blame] | 14 | from xml.sax import saxutils |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 15 | import common |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 16 | from autotest_lib.frontend.afe import model_logic, model_attributes |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 17 | from autotest_lib.frontend.afe import rdb_model_extensions |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 18 | from autotest_lib.frontend import settings, thread_local |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 19 | from autotest_lib.client.common_lib import enum, error, host_protections |
| 20 | from autotest_lib.client.common_lib import global_config |
showard | eaa408e | 2009-09-11 18:45:31 +0000 | [diff] [blame] | 21 | from autotest_lib.client.common_lib import host_queue_entry_states |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 22 | from autotest_lib.client.common_lib import control_data, priorities, decorators |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 23 | from autotest_lib.server import utils as server_utils |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 24 | |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 25 | # job options and user preferences |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 26 | DEFAULT_REBOOT_BEFORE = model_attributes.RebootBefore.IF_DIRTY |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 27 | DEFAULT_REBOOT_AFTER = model_attributes.RebootBefore.NEVER |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 28 | |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 29 | RESPECT_STATIC_LABELS = global_config.global_config.get_config_value( |
| 30 | 'SKYLAB', 'respect_static_labels', type=bool, default=False) |
| 31 | |
Xixuan Wu | c00f603 | 2018-02-22 12:41:41 -0800 | [diff] [blame] | 32 | RESPECT_STATIC_ATTRIBUTES = global_config.global_config.get_config_value( |
| 33 | 'SKYLAB', 'respect_static_attributes', type=bool, default=False) |
| 34 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 35 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 36 | class AclAccessViolation(Exception): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 37 | """\ |
| 38 | Raised when an operation is attempted with proper permissions as |
| 39 | dictated by ACLs. |
| 40 | """ |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 41 | |
| 42 | |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 43 | class AtomicGroup(model_logic.ModelWithInvalid, dbmodels.Model): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 44 | """\ |
| 45 | An atomic group defines a collection of hosts which must only be scheduled |
| 46 | all at once. Any host with a label having an atomic group will only be |
| 47 | scheduled for a job at the same time as other hosts sharing that label. |
| 48 | |
| 49 | Required: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 50 | name: A name for this atomic group, e.g. 'rack23' or 'funky_net'. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 51 | max_number_of_machines: The maximum number of machines that will be |
| 52 | scheduled at once when scheduling jobs to this atomic group. |
| 53 | The job.synch_count is considered the minimum. |
| 54 | |
| 55 | Optional: |
| 56 | description: Arbitrary text description of this group's purpose. |
| 57 | """ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 58 | name = dbmodels.CharField(max_length=255, unique=True) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 59 | description = dbmodels.TextField(blank=True) |
showard | e9450c9 | 2009-06-30 01:58:52 +0000 | [diff] [blame] | 60 | # This magic value is the default to simplify the scheduler logic. |
| 61 | # It must be "large". The common use of atomic groups is to want all |
| 62 | # machines in the group to be used, limits on which subset used are |
| 63 | # often chosen via dependency labels. |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 64 | # TODO(dennisjeffrey): Revisit this so we don't have to assume that |
| 65 | # "infinity" is around 3.3 million. |
showard | e9450c9 | 2009-06-30 01:58:52 +0000 | [diff] [blame] | 66 | INFINITE_MACHINES = 333333333 |
| 67 | max_number_of_machines = dbmodels.IntegerField(default=INFINITE_MACHINES) |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 68 | invalid = dbmodels.BooleanField(default=False, |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 69 | editable=settings.FULL_ADMIN) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 70 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 71 | name_field = 'name' |
jamesren | e365623 | 2010-03-02 00:00:30 +0000 | [diff] [blame] | 72 | objects = model_logic.ModelWithInvalidManager() |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 73 | valid_objects = model_logic.ValidObjectsManager() |
| 74 | |
| 75 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 76 | def enqueue_job(self, job, is_template=False): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 77 | """Enqueue a job on an associated atomic group of hosts. |
| 78 | |
| 79 | @param job: A job to enqueue. |
| 80 | @param is_template: Whether the status should be "Template". |
| 81 | """ |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 82 | queue_entry = HostQueueEntry.create(atomic_group=self, job=job, |
| 83 | is_template=is_template) |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 84 | queue_entry.save() |
| 85 | |
| 86 | |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 87 | def clean_object(self): |
| 88 | self.label_set.clear() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 92 | """Metadata for class AtomicGroup.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 93 | db_table = 'afe_atomic_groups' |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 94 | |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 95 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 96 | def __unicode__(self): |
| 97 | return unicode(self.name) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 98 | |
| 99 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 100 | class Label(model_logic.ModelWithInvalid, dbmodels.Model): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 101 | """\ |
| 102 | Required: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 103 | name: label name |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 104 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 105 | Optional: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 106 | kernel_config: URL/path to kernel config for jobs run on this label. |
| 107 | platform: If True, this is a platform label (defaults to False). |
| 108 | only_if_needed: If True, a Host with this label can only be used if that |
| 109 | label is requested by the job/test (either as the meta_host or |
| 110 | in the job_dependencies). |
| 111 | atomic_group: The atomic group associated with this label. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 112 | """ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 113 | name = dbmodels.CharField(max_length=255, unique=True) |
| 114 | kernel_config = dbmodels.CharField(max_length=255, blank=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 115 | platform = dbmodels.BooleanField(default=False) |
| 116 | invalid = dbmodels.BooleanField(default=False, |
| 117 | editable=settings.FULL_ADMIN) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 118 | only_if_needed = dbmodels.BooleanField(default=False) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 119 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 120 | name_field = 'name' |
jamesren | e365623 | 2010-03-02 00:00:30 +0000 | [diff] [blame] | 121 | objects = model_logic.ModelWithInvalidManager() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 122 | valid_objects = model_logic.ValidObjectsManager() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 123 | atomic_group = dbmodels.ForeignKey(AtomicGroup, null=True, blank=True) |
| 124 | |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 125 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 126 | def clean_object(self): |
| 127 | self.host_set.clear() |
showard | 01a5167 | 2009-05-29 18:42:37 +0000 | [diff] [blame] | 128 | self.test_set.clear() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 129 | |
| 130 | |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 131 | def enqueue_job(self, job, is_template=False): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 132 | """Enqueue a job on any host of this label. |
| 133 | |
| 134 | @param job: A job to enqueue. |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 135 | @param is_template: Whether the status should be "Template". |
| 136 | """ |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 137 | queue_entry = HostQueueEntry.create(meta_host=self, job=job, |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 138 | is_template=is_template) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 139 | queue_entry.save() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 140 | |
| 141 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 142 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 143 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 144 | """Metadata for class Label.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 145 | db_table = 'afe_labels' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 146 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 147 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 148 | def __unicode__(self): |
| 149 | return unicode(self.name) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 150 | |
| 151 | |
Xixuan Wu | 040fc8b | 2018-01-12 12:52:14 -0800 | [diff] [blame] | 152 | def is_replaced_by_static(self): |
| 153 | """Detect whether a label is replaced by a static label. |
| 154 | |
| 155 | 'Static' means it can only be modified by skylab inventory tools. |
| 156 | """ |
| 157 | if RESPECT_STATIC_LABELS: |
| 158 | replaced = ReplacedLabel.objects.filter(label__id=self.id) |
| 159 | if len(replaced) > 0: |
| 160 | return True |
| 161 | |
| 162 | return False |
| 163 | |
| 164 | |
Xixuan Wu | cc12f3e | 2018-01-02 15:50:14 -0800 | [diff] [blame] | 165 | class StaticLabel(model_logic.ModelWithInvalid, dbmodels.Model): |
| 166 | """\ |
| 167 | Required: |
| 168 | name: label name |
| 169 | |
| 170 | Optional: |
| 171 | kernel_config: URL/path to kernel config for jobs run on this label. |
| 172 | platform: If True, this is a platform label (defaults to False). |
| 173 | only_if_needed: Deprecated. This is always False. |
| 174 | atomic_group: Deprecated. This is always NULL. |
| 175 | """ |
| 176 | name = dbmodels.CharField(max_length=255, unique=True) |
| 177 | kernel_config = dbmodels.CharField(max_length=255, blank=True) |
| 178 | platform = dbmodels.BooleanField(default=False) |
| 179 | invalid = dbmodels.BooleanField(default=False, |
| 180 | editable=settings.FULL_ADMIN) |
| 181 | only_if_needed = dbmodels.BooleanField(default=False) |
| 182 | |
| 183 | name_field = 'name' |
| 184 | objects = model_logic.ModelWithInvalidManager() |
| 185 | valid_objects = model_logic.ValidObjectsManager() |
| 186 | atomic_group = dbmodels.ForeignKey(AtomicGroup, null=True, blank=True) |
| 187 | |
| 188 | def clean_object(self): |
| 189 | self.host_set.clear() |
| 190 | self.test_set.clear() |
| 191 | |
| 192 | |
| 193 | class Meta: |
| 194 | """Metadata for class StaticLabel.""" |
| 195 | db_table = 'afe_static_labels' |
| 196 | |
| 197 | |
| 198 | def __unicode__(self): |
| 199 | return unicode(self.name) |
| 200 | |
| 201 | |
| 202 | class ReplacedLabel(dbmodels.Model, model_logic.ModelExtensions): |
| 203 | """The tag to indicate Whether to replace labels with static labels.""" |
| 204 | label = dbmodels.ForeignKey(Label) |
| 205 | objects = model_logic.ExtendedManager() |
| 206 | |
| 207 | |
| 208 | class Meta: |
| 209 | """Metadata for class ReplacedLabel.""" |
| 210 | db_table = 'afe_replaced_labels' |
| 211 | |
| 212 | |
| 213 | def __unicode__(self): |
Xixuan Wu | 76cd058 | 2017-12-21 13:23:28 -0800 | [diff] [blame] | 214 | return unicode(self.label) |
Xixuan Wu | cc12f3e | 2018-01-02 15:50:14 -0800 | [diff] [blame] | 215 | |
| 216 | |
Jakob Jülich | 92c0633 | 2014-08-25 19:06:57 +0000 | [diff] [blame] | 217 | class Shard(dbmodels.Model, model_logic.ModelExtensions): |
| 218 | |
Jakob Juelich | de2b9a9 | 2014-09-02 15:29:28 -0700 | [diff] [blame] | 219 | hostname = dbmodels.CharField(max_length=255, unique=True) |
| 220 | |
| 221 | name_field = 'hostname' |
| 222 | |
Jakob Jülich | 92c0633 | 2014-08-25 19:06:57 +0000 | [diff] [blame] | 223 | labels = dbmodels.ManyToManyField(Label, blank=True, |
| 224 | db_table='afe_shards_labels') |
| 225 | |
| 226 | class Meta: |
| 227 | """Metadata for class ParameterizedJob.""" |
| 228 | db_table = 'afe_shards' |
| 229 | |
| 230 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 231 | class Drone(dbmodels.Model, model_logic.ModelExtensions): |
| 232 | """ |
| 233 | A scheduler drone |
| 234 | |
| 235 | hostname: the drone's hostname |
| 236 | """ |
| 237 | hostname = dbmodels.CharField(max_length=255, unique=True) |
| 238 | |
| 239 | name_field = 'hostname' |
| 240 | objects = model_logic.ExtendedManager() |
| 241 | |
| 242 | |
| 243 | def save(self, *args, **kwargs): |
| 244 | if not User.current_user().is_superuser(): |
| 245 | raise Exception('Only superusers may edit drones') |
| 246 | super(Drone, self).save(*args, **kwargs) |
| 247 | |
| 248 | |
| 249 | def delete(self): |
| 250 | if not User.current_user().is_superuser(): |
| 251 | raise Exception('Only superusers may delete drones') |
| 252 | super(Drone, self).delete() |
| 253 | |
| 254 | |
| 255 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 256 | """Metadata for class Drone.""" |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 257 | db_table = 'afe_drones' |
| 258 | |
| 259 | def __unicode__(self): |
| 260 | return unicode(self.hostname) |
| 261 | |
| 262 | |
| 263 | class DroneSet(dbmodels.Model, model_logic.ModelExtensions): |
| 264 | """ |
| 265 | A set of scheduler drones |
| 266 | |
| 267 | These will be used by the scheduler to decide what drones a job is allowed |
| 268 | to run on. |
| 269 | |
| 270 | name: the drone set's name |
| 271 | drones: the drones that are part of the set |
| 272 | """ |
| 273 | DRONE_SETS_ENABLED = global_config.global_config.get_config_value( |
| 274 | 'SCHEDULER', 'drone_sets_enabled', type=bool, default=False) |
| 275 | DEFAULT_DRONE_SET_NAME = global_config.global_config.get_config_value( |
| 276 | 'SCHEDULER', 'default_drone_set_name', default=None) |
| 277 | |
| 278 | name = dbmodels.CharField(max_length=255, unique=True) |
| 279 | drones = dbmodels.ManyToManyField(Drone, db_table='afe_drone_sets_drones') |
| 280 | |
| 281 | name_field = 'name' |
| 282 | objects = model_logic.ExtendedManager() |
| 283 | |
| 284 | |
| 285 | def save(self, *args, **kwargs): |
| 286 | if not User.current_user().is_superuser(): |
| 287 | raise Exception('Only superusers may edit drone sets') |
| 288 | super(DroneSet, self).save(*args, **kwargs) |
| 289 | |
| 290 | |
| 291 | def delete(self): |
| 292 | if not User.current_user().is_superuser(): |
| 293 | raise Exception('Only superusers may delete drone sets') |
| 294 | super(DroneSet, self).delete() |
| 295 | |
| 296 | |
| 297 | @classmethod |
| 298 | def drone_sets_enabled(cls): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 299 | """Returns whether drone sets are enabled. |
| 300 | |
| 301 | @param cls: Implicit class object. |
| 302 | """ |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 303 | return cls.DRONE_SETS_ENABLED |
| 304 | |
| 305 | |
| 306 | @classmethod |
| 307 | def default_drone_set_name(cls): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 308 | """Returns the default drone set name. |
| 309 | |
| 310 | @param cls: Implicit class object. |
| 311 | """ |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 312 | return cls.DEFAULT_DRONE_SET_NAME |
| 313 | |
| 314 | |
| 315 | @classmethod |
| 316 | def get_default(cls): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 317 | """Gets the default drone set name, compatible with Job.add_object. |
| 318 | |
| 319 | @param cls: Implicit class object. |
| 320 | """ |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 321 | return cls.smart_get(cls.DEFAULT_DRONE_SET_NAME) |
| 322 | |
| 323 | |
| 324 | @classmethod |
| 325 | def resolve_name(cls, drone_set_name): |
| 326 | """ |
| 327 | Returns the name of one of these, if not None, in order of preference: |
| 328 | 1) the drone set given, |
| 329 | 2) the current user's default drone set, or |
| 330 | 3) the global default drone set |
| 331 | |
| 332 | or returns None if drone sets are disabled |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 333 | |
| 334 | @param cls: Implicit class object. |
| 335 | @param drone_set_name: A drone set name. |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 336 | """ |
| 337 | if not cls.drone_sets_enabled(): |
| 338 | return None |
| 339 | |
| 340 | user = User.current_user() |
| 341 | user_drone_set_name = user.drone_set and user.drone_set.name |
| 342 | |
| 343 | return drone_set_name or user_drone_set_name or cls.get_default().name |
| 344 | |
| 345 | |
| 346 | def get_drone_hostnames(self): |
| 347 | """ |
| 348 | Gets the hostnames of all drones in this drone set |
| 349 | """ |
| 350 | return set(self.drones.all().values_list('hostname', flat=True)) |
| 351 | |
| 352 | |
| 353 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 354 | """Metadata for class DroneSet.""" |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 355 | db_table = 'afe_drone_sets' |
| 356 | |
| 357 | def __unicode__(self): |
| 358 | return unicode(self.name) |
| 359 | |
| 360 | |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 361 | class User(dbmodels.Model, model_logic.ModelExtensions): |
| 362 | """\ |
| 363 | Required: |
| 364 | login :user login name |
| 365 | |
| 366 | Optional: |
| 367 | access_level: 0=User (default), 1=Admin, 100=Root |
| 368 | """ |
| 369 | ACCESS_ROOT = 100 |
| 370 | ACCESS_ADMIN = 1 |
| 371 | ACCESS_USER = 0 |
| 372 | |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 373 | AUTOTEST_SYSTEM = 'autotest_system' |
| 374 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 375 | login = dbmodels.CharField(max_length=255, unique=True) |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 376 | access_level = dbmodels.IntegerField(default=ACCESS_USER, blank=True) |
| 377 | |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 378 | # user preferences |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 379 | reboot_before = dbmodels.SmallIntegerField( |
| 380 | choices=model_attributes.RebootBefore.choices(), blank=True, |
| 381 | default=DEFAULT_REBOOT_BEFORE) |
| 382 | reboot_after = dbmodels.SmallIntegerField( |
| 383 | choices=model_attributes.RebootAfter.choices(), blank=True, |
| 384 | default=DEFAULT_REBOOT_AFTER) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 385 | drone_set = dbmodels.ForeignKey(DroneSet, null=True, blank=True) |
showard | 97db5ba | 2008-11-12 18:18:02 +0000 | [diff] [blame] | 386 | show_experimental = dbmodels.BooleanField(default=False) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 387 | |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 388 | name_field = 'login' |
| 389 | objects = model_logic.ExtendedManager() |
| 390 | |
| 391 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 392 | def save(self, *args, **kwargs): |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 393 | # is this a new object being saved for the first time? |
| 394 | first_time = (self.id is None) |
| 395 | user = thread_local.get_user() |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 396 | if user and not user.is_superuser() and user.login != self.login: |
| 397 | raise AclAccessViolation("You cannot modify user " + self.login) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 398 | super(User, self).save(*args, **kwargs) |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 399 | if first_time: |
| 400 | everyone = AclGroup.objects.get(name='Everyone') |
| 401 | everyone.users.add(self) |
| 402 | |
| 403 | |
| 404 | def is_superuser(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 405 | """Returns whether the user has superuser access.""" |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 406 | return self.access_level >= self.ACCESS_ROOT |
| 407 | |
| 408 | |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 409 | @classmethod |
| 410 | def current_user(cls): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 411 | """Returns the current user. |
| 412 | |
| 413 | @param cls: Implicit class object. |
| 414 | """ |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 415 | user = thread_local.get_user() |
| 416 | if user is None: |
showard | cfcdd80 | 2010-01-15 00:16:33 +0000 | [diff] [blame] | 417 | user, _ = cls.objects.get_or_create(login=cls.AUTOTEST_SYSTEM) |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 418 | user.access_level = cls.ACCESS_ROOT |
| 419 | user.save() |
| 420 | return user |
| 421 | |
| 422 | |
Prashanth Balasubramanian | af51664 | 2014-12-12 18:16:32 -0800 | [diff] [blame] | 423 | @classmethod |
| 424 | def get_record(cls, data): |
| 425 | """Check the database for an identical record. |
| 426 | |
| 427 | Check for a record with matching id and login. If one exists, |
| 428 | return it. If one does not exist there is a possibility that |
| 429 | the following cases have happened: |
| 430 | 1. Same id, different login |
| 431 | We received: "1 chromeos-test" |
| 432 | And we have: "1 debug-user" |
| 433 | In this case we need to delete "1 debug_user" and insert |
| 434 | "1 chromeos-test". |
| 435 | |
| 436 | 2. Same login, different id: |
| 437 | We received: "1 chromeos-test" |
| 438 | And we have: "2 chromeos-test" |
| 439 | In this case we need to delete "2 chromeos-test" and insert |
| 440 | "1 chromeos-test". |
| 441 | |
| 442 | As long as this method deletes bad records and raises the |
| 443 | DoesNotExist exception the caller will handle creating the |
| 444 | new record. |
| 445 | |
| 446 | @raises: DoesNotExist, if a record with the matching login and id |
| 447 | does not exist. |
| 448 | """ |
| 449 | |
| 450 | # Both the id and login should be uniqe but there are cases when |
| 451 | # we might already have a user with the same login/id because |
| 452 | # current_user will proactively create a user record if it doesn't |
| 453 | # exist. Since we want to avoid conflict between the master and |
| 454 | # shard, just delete any existing user records that don't match |
| 455 | # what we're about to deserialize from the master. |
| 456 | try: |
| 457 | return cls.objects.get(login=data['login'], id=data['id']) |
| 458 | except cls.DoesNotExist: |
| 459 | cls.delete_matching_record(login=data['login']) |
| 460 | cls.delete_matching_record(id=data['id']) |
| 461 | raise |
| 462 | |
| 463 | |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 464 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 465 | """Metadata for class User.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 466 | db_table = 'afe_users' |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 467 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 468 | def __unicode__(self): |
| 469 | return unicode(self.login) |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 470 | |
| 471 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 472 | class Host(model_logic.ModelWithInvalid, rdb_model_extensions.AbstractHostModel, |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 473 | model_logic.ModelWithAttributes): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 474 | """\ |
| 475 | Required: |
| 476 | hostname |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 477 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 478 | optional: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 479 | locked: if true, host is locked and will not be queued |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 480 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 481 | Internal: |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 482 | From AbstractHostModel: |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 483 | status: string describing status of host |
| 484 | invalid: true if the host has been deleted |
| 485 | protection: indicates what can be done to this host during repair |
| 486 | lock_time: DateTime at which the host was locked |
| 487 | dirty: true if the host has been used without being rebooted |
| 488 | Local: |
| 489 | locked_by: user that locked the host, or null if the host is unlocked |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 490 | """ |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 491 | |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 492 | SERIALIZATION_LINKS_TO_FOLLOW = set(['aclgroup_set', |
| 493 | 'hostattribute_set', |
| 494 | 'labels', |
| 495 | 'shard']) |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 496 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['invalid']) |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 497 | |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 498 | |
| 499 | def custom_deserialize_relation(self, link, data): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 500 | assert link == 'shard', 'Link %s should not be deserialized' % link |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 501 | self.shard = Shard.deserialize(data) |
| 502 | |
| 503 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 504 | # Note: Only specify foreign keys here, specify all native host columns in |
| 505 | # rdb_model_extensions instead. |
| 506 | Protection = host_protections.Protection |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 507 | labels = dbmodels.ManyToManyField(Label, blank=True, |
| 508 | db_table='afe_hosts_labels') |
Xixuan Wu | cc12f3e | 2018-01-02 15:50:14 -0800 | [diff] [blame] | 509 | static_labels = dbmodels.ManyToManyField( |
| 510 | StaticLabel, blank=True, db_table='afe_static_hosts_labels') |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 511 | locked_by = dbmodels.ForeignKey(User, null=True, blank=True, editable=False) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 512 | name_field = 'hostname' |
jamesren | e365623 | 2010-03-02 00:00:30 +0000 | [diff] [blame] | 513 | objects = model_logic.ModelWithInvalidManager() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 514 | valid_objects = model_logic.ValidObjectsManager() |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 515 | leased_objects = model_logic.LeasedHostManager() |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 516 | |
Jakob Juelich | de2b9a9 | 2014-09-02 15:29:28 -0700 | [diff] [blame] | 517 | shard = dbmodels.ForeignKey(Shard, blank=True, null=True) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 518 | |
| 519 | def __init__(self, *args, **kwargs): |
| 520 | super(Host, self).__init__(*args, **kwargs) |
| 521 | self._record_attributes(['status']) |
| 522 | |
| 523 | |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 524 | @classmethod |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 525 | def classify_labels(cls, label_names): |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 526 | """Split labels to static & non-static. |
| 527 | |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 528 | @label_names: a list of labels (string). |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 529 | |
| 530 | @returns: a list of StaticLabel objects & a list of |
| 531 | (non-static) Label objects. |
| 532 | """ |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 533 | if not label_names: |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 534 | return [], [] |
| 535 | |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 536 | labels = Label.objects.filter(name__in=label_names) |
Xixuan Wu | 9d9549f | 2018-01-12 16:28:31 -0800 | [diff] [blame] | 537 | |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 538 | if not RESPECT_STATIC_LABELS: |
| 539 | return [], labels |
| 540 | |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 541 | return cls.classify_label_objects(labels) |
| 542 | |
| 543 | |
| 544 | @classmethod |
| 545 | def classify_label_objects(cls, label_objects): |
| 546 | replaced_labels = ReplacedLabel.objects.filter(label__in=label_objects) |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 547 | replaced_ids = [l.label.id for l in replaced_labels] |
| 548 | non_static_labels = [ |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 549 | l for l in label_objects if not l.id in replaced_ids] |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 550 | static_label_names = [ |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 551 | l.name for l in label_objects if l.id in replaced_ids] |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 552 | static_labels = StaticLabel.objects.filter(name__in=static_label_names) |
| 553 | return static_labels, non_static_labels |
| 554 | |
| 555 | |
| 556 | @classmethod |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 557 | def get_hosts_with_labels(cls, label_names, initial_query): |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 558 | """Get hosts by label filters. |
| 559 | |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 560 | @param label_names: label (string) lists for fetching hosts. |
Xixuan Wu | 9d9549f | 2018-01-12 16:28:31 -0800 | [diff] [blame] | 561 | @param initial_query: a model_logic.QuerySet of Host object, e.g. |
| 562 | |
| 563 | Host.objects.all(), Host.valid_objects.all(). |
| 564 | |
| 565 | This initial_query cannot be a sliced QuerySet, e.g. |
| 566 | |
| 567 | Host.objects.all().filter(query_limit=10) |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 568 | """ |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 569 | if not label_names: |
Xixuan Wu | 9d9549f | 2018-01-12 16:28:31 -0800 | [diff] [blame] | 570 | return initial_query |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 571 | |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 572 | static_labels, non_static_labels = cls.classify_labels(label_names) |
| 573 | if len(static_labels) + len(non_static_labels) != len(label_names): |
Xixuan Wu | 9d9549f | 2018-01-12 16:28:31 -0800 | [diff] [blame] | 574 | # Some labels don't exist in afe db, which means no hosts |
| 575 | # should be matched. |
| 576 | return set() |
Xixuan Wu | bc3e0c8 | 2017-12-18 13:39:10 -0800 | [diff] [blame] | 577 | |
| 578 | for l in static_labels: |
| 579 | initial_query = initial_query.filter(static_labels=l) |
| 580 | |
| 581 | for l in non_static_labels: |
| 582 | initial_query = initial_query.filter(labels=l) |
| 583 | |
| 584 | return initial_query |
| 585 | |
| 586 | |
Xixuan Wu | 8c7113a | 2018-01-09 17:14:38 -0800 | [diff] [blame] | 587 | @classmethod |
| 588 | def get_hosts_with_label_ids(cls, label_ids, initial_query): |
| 589 | """Get hosts by label_id filters. |
| 590 | |
| 591 | @param label_ids: label id (int) lists for fetching hosts. |
| 592 | @param initial_query: a list of Host object, e.g. |
| 593 | [<Host: 100.107.151.253>, <Host: 100.107.151.251>, ...] |
| 594 | """ |
| 595 | labels = Label.objects.filter(id__in=label_ids) |
| 596 | label_names = [l.name for l in labels] |
| 597 | return cls.get_hosts_with_labels(label_names, initial_query) |
| 598 | |
| 599 | |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 600 | @staticmethod |
| 601 | def create_one_time_host(hostname): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 602 | """Creates a one-time host. |
| 603 | |
| 604 | @param hostname: The name for the host. |
| 605 | """ |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 606 | query = Host.objects.filter(hostname=hostname) |
| 607 | if query.count() == 0: |
| 608 | host = Host(hostname=hostname, invalid=True) |
showard | a8411af | 2008-08-07 22:35:58 +0000 | [diff] [blame] | 609 | host.do_validate() |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 610 | else: |
| 611 | host = query[0] |
| 612 | if not host.invalid: |
| 613 | raise model_logic.ValidationError({ |
mbligh | b5b7b5d | 2009-02-03 17:47:15 +0000 | [diff] [blame] | 614 | 'hostname' : '%s already exists in the autotest DB. ' |
| 615 | 'Select it rather than entering it as a one time ' |
| 616 | 'host.' % hostname |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 617 | }) |
showard | 1ab512b | 2008-07-30 23:39:04 +0000 | [diff] [blame] | 618 | host.protection = host_protections.Protection.DO_NOT_REPAIR |
showard | 946a7af | 2009-04-15 21:53:23 +0000 | [diff] [blame] | 619 | host.locked = False |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 620 | host.save() |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 621 | host.clean_object() |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 622 | return host |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 623 | |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 624 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 625 | @classmethod |
Aviv Keshet | bf963c1 | 2017-04-04 10:41:55 -0700 | [diff] [blame] | 626 | def _assign_to_shard_nothing_helper(cls): |
| 627 | """Does nothing. |
| 628 | |
| 629 | This method is called in the middle of assign_to_shard, and does |
| 630 | nothing. It exists to allow integration tests to simulate a race |
| 631 | condition.""" |
| 632 | |
| 633 | |
| 634 | @classmethod |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 635 | def assign_to_shard(cls, shard, known_ids): |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 636 | """Assigns hosts to a shard. |
| 637 | |
MK Ryu | 638a6cf | 2015-05-08 14:49:43 -0700 | [diff] [blame] | 638 | For all labels that have been assigned to a shard, all hosts that |
| 639 | have at least one of the shard's labels are assigned to the shard. |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 640 | Hosts that are assigned to the shard but aren't already present on the |
| 641 | shard are returned. |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 642 | |
Aviv Keshet | 4be5a16 | 2017-03-23 08:59:06 -0700 | [diff] [blame] | 643 | Any boards that are in |known_ids| but that do not belong to the shard |
| 644 | are incorrect ids, which are also returned so that the shard can remove |
| 645 | them locally. |
| 646 | |
MK Ryu | 638a6cf | 2015-05-08 14:49:43 -0700 | [diff] [blame] | 647 | Board to shard mapping is many-to-one. Many different boards can be |
| 648 | hosted in a shard. However, DUTs of a single board cannot be distributed |
| 649 | into more than one shard. |
| 650 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 651 | @param shard: The shard object to assign labels/hosts for. |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 652 | @param known_ids: List of all host-ids the shard already knows. |
| 653 | This is used to figure out which hosts should be sent |
| 654 | to the shard. If shard_ids were used instead, hosts |
| 655 | would only be transferred once, even if the client |
| 656 | failed persisting them. |
| 657 | The number of hosts usually lies in O(100), so the |
| 658 | overhead is acceptable. |
| 659 | |
Aviv Keshet | 4be5a16 | 2017-03-23 08:59:06 -0700 | [diff] [blame] | 660 | @returns a tuple of (hosts objects that should be sent to the shard, |
| 661 | incorrect host ids that should not belong to] |
| 662 | shard) |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 663 | """ |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 664 | # Disclaimer: concurrent heartbeats should theoretically not occur in |
| 665 | # the current setup. As they may be introduced in the near future, |
| 666 | # this comment will be left here. |
| 667 | |
| 668 | # Sending stuff twice is acceptable, but forgetting something isn't. |
| 669 | # Detecting duplicates on the client is easy, but here it's harder. The |
| 670 | # following options were considered: |
| 671 | # - SELECT ... WHERE and then UPDATE ... WHERE: Update might update more |
| 672 | # than select returned, as concurrently more hosts might have been |
| 673 | # inserted |
| 674 | # - UPDATE and then SELECT WHERE shard=shard: select always returns all |
| 675 | # hosts for the shard, this is overhead |
| 676 | # - SELECT and then UPDATE only selected without requerying afterwards: |
| 677 | # returns the old state of the records. |
Aviv Keshet | bf963c1 | 2017-04-04 10:41:55 -0700 | [diff] [blame] | 678 | new_hosts = [] |
Aviv Keshet | 4be5a16 | 2017-03-23 08:59:06 -0700 | [diff] [blame] | 679 | |
Aviv Keshet | bf963c1 | 2017-04-04 10:41:55 -0700 | [diff] [blame] | 680 | possible_new_host_ids = set(Host.objects.filter( |
MK Ryu | 638a6cf | 2015-05-08 14:49:43 -0700 | [diff] [blame] | 681 | labels__in=shard.labels.all(), |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 682 | leased=False |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 683 | ).exclude( |
| 684 | id__in=known_ids, |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 685 | ).values_list('pk', flat=True)) |
| 686 | |
Aviv Keshet | bf963c1 | 2017-04-04 10:41:55 -0700 | [diff] [blame] | 687 | # No-op in production, used to simulate race condition in tests. |
| 688 | cls._assign_to_shard_nothing_helper() |
| 689 | |
| 690 | if possible_new_host_ids: |
| 691 | Host.objects.filter( |
| 692 | pk__in=possible_new_host_ids, |
| 693 | labels__in=shard.labels.all(), |
| 694 | leased=False |
| 695 | ).update(shard=shard) |
| 696 | new_hosts = list(Host.objects.filter( |
| 697 | pk__in=possible_new_host_ids, |
| 698 | shard=shard |
| 699 | ).all()) |
Aviv Keshet | 4be5a16 | 2017-03-23 08:59:06 -0700 | [diff] [blame] | 700 | |
| 701 | invalid_host_ids = list(Host.objects.filter( |
| 702 | id__in=known_ids |
| 703 | ).exclude( |
| 704 | shard=shard |
| 705 | ).values_list('pk', flat=True)) |
| 706 | |
Aviv Keshet | bf963c1 | 2017-04-04 10:41:55 -0700 | [diff] [blame] | 707 | return new_hosts, invalid_host_ids |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 708 | |
showard | afd97de | 2009-10-01 18:45:09 +0000 | [diff] [blame] | 709 | def resurrect_object(self, old_object): |
| 710 | super(Host, self).resurrect_object(old_object) |
| 711 | # invalid hosts can be in use by the scheduler (as one-time hosts), so |
| 712 | # don't change the status |
| 713 | self.status = old_object.status |
| 714 | |
| 715 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 716 | def clean_object(self): |
| 717 | self.aclgroup_set.clear() |
| 718 | self.labels.clear() |
Xixuan Wu | cc12f3e | 2018-01-02 15:50:14 -0800 | [diff] [blame] | 719 | self.static_labels.clear() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 720 | |
| 721 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 722 | def save(self, *args, **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 723 | # extra spaces in the hostname can be a sneaky source of errors |
| 724 | self.hostname = self.hostname.strip() |
| 725 | # is this a new object being saved for the first time? |
| 726 | first_time = (self.id is None) |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 727 | if not first_time: |
| 728 | AclGroup.check_for_acl_violation_hosts([self]) |
Dan Shi | a0acfbc | 2014-10-14 15:56:23 -0700 | [diff] [blame] | 729 | # If locked is changed, send its status and user made the change to |
| 730 | # metaDB. Locks are important in host history because if a device is |
| 731 | # locked then we don't really care what state it is in. |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 732 | if self.locked and not self.locked_by: |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 733 | self.locked_by = User.current_user() |
MK Ryu | d53e149 | 2015-12-15 12:09:03 -0800 | [diff] [blame] | 734 | if not self.lock_time: |
| 735 | self.lock_time = datetime.now() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 736 | self.dirty = True |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 737 | elif not self.locked and self.locked_by: |
| 738 | self.locked_by = None |
| 739 | self.lock_time = None |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 740 | super(Host, self).save(*args, **kwargs) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 741 | if first_time: |
| 742 | everyone = AclGroup.objects.get(name='Everyone') |
| 743 | everyone.hosts.add(self) |
Matt Mallett | 77545a3 | 2018-01-31 16:09:29 -0800 | [diff] [blame] | 744 | # remove attributes that may have lingered from an old host and |
| 745 | # should not be associated with a new host |
| 746 | for host_attribute in self.hostattribute_set.all(): |
| 747 | self.delete_attribute(host_attribute.attribute) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 748 | self._check_for_updated_attributes() |
| 749 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 750 | |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 751 | def delete(self): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 752 | AclGroup.check_for_acl_violation_hosts([self]) |
Aviv Keshet | a8dcf05 | 2018-02-13 16:50:19 -0800 | [diff] [blame] | 753 | logging.info('Preconditions for deleting host %s...', self.hostname) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 754 | for queue_entry in self.hostqueueentry_set.all(): |
Aviv Keshet | a8dcf05 | 2018-02-13 16:50:19 -0800 | [diff] [blame] | 755 | logging.info(' Deleting and aborting hqe %s...', queue_entry) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 756 | queue_entry.deleted = True |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 757 | queue_entry.abort() |
Aviv Keshet | a8dcf05 | 2018-02-13 16:50:19 -0800 | [diff] [blame] | 758 | logging.info(' ... done with hqe %s.', queue_entry) |
Matt Mallett | 77545a3 | 2018-01-31 16:09:29 -0800 | [diff] [blame] | 759 | for host_attribute in self.hostattribute_set.all(): |
Aviv Keshet | a8dcf05 | 2018-02-13 16:50:19 -0800 | [diff] [blame] | 760 | logging.info(' Deleting attribute %s...', host_attribute) |
Matt Mallett | 77545a3 | 2018-01-31 16:09:29 -0800 | [diff] [blame] | 761 | self.delete_attribute(host_attribute.attribute) |
Aviv Keshet | a8dcf05 | 2018-02-13 16:50:19 -0800 | [diff] [blame] | 762 | logging.info(' ... done with attribute %s.', host_attribute) |
| 763 | logging.info('... preconditions done for host %s.', self.hostname) |
| 764 | logging.info('Deleting host %s...', self.hostname) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 765 | super(Host, self).delete() |
Aviv Keshet | a8dcf05 | 2018-02-13 16:50:19 -0800 | [diff] [blame] | 766 | logging.info('... done.') |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 767 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 768 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 769 | def on_attribute_changed(self, attribute, old_value): |
| 770 | assert attribute == 'status' |
Allen Li | fc284ad | 2018-02-14 11:31:14 -0800 | [diff] [blame] | 771 | logging.info('%s -> %s', self.hostname, self.status) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 772 | |
| 773 | |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 774 | def enqueue_job(self, job, is_template=False): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 775 | """Enqueue a job on this host. |
| 776 | |
| 777 | @param job: A job to enqueue. |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 778 | @param is_template: Whther the status should be "Template". |
| 779 | """ |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 780 | queue_entry = HostQueueEntry.create(host=self, job=job, |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 781 | is_template=is_template) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 782 | # allow recovery of dead hosts from the frontend |
| 783 | if not self.active_queue_entry() and self.is_dead(): |
| 784 | self.status = Host.Status.READY |
| 785 | self.save() |
| 786 | queue_entry.save() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 787 | |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 788 | block = IneligibleHostQueue(job=job, host=self) |
| 789 | block.save() |
| 790 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 791 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 792 | def platform(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 793 | """The platform of the host.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 794 | # TODO(showard): slighly hacky? |
| 795 | platforms = self.labels.filter(platform=True) |
| 796 | if len(platforms) == 0: |
| 797 | return None |
| 798 | return platforms[0] |
| 799 | platform.short_description = 'Platform' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 800 | |
| 801 | |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 802 | @classmethod |
| 803 | def check_no_platform(cls, hosts): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 804 | """Verify the specified hosts have no associated platforms. |
| 805 | |
| 806 | @param cls: Implicit class object. |
| 807 | @param hosts: The hosts to verify. |
| 808 | @raises model_logic.ValidationError if any hosts already have a |
| 809 | platform. |
| 810 | """ |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 811 | Host.objects.populate_relationships(hosts, Label, 'label_list') |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 812 | Host.objects.populate_relationships(hosts, StaticLabel, |
| 813 | 'staticlabel_list') |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 814 | errors = [] |
| 815 | for host in hosts: |
| 816 | platforms = [label.name for label in host.label_list |
| 817 | if label.platform] |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 818 | if RESPECT_STATIC_LABELS: |
| 819 | platforms += [label.name for label in host.staticlabel_list |
| 820 | if label.platform] |
| 821 | |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 822 | if platforms: |
| 823 | # do a join, just in case this host has multiple platforms, |
| 824 | # we'll be able to see it |
| 825 | errors.append('Host %s already has a platform: %s' % ( |
| 826 | host.hostname, ', '.join(platforms))) |
| 827 | if errors: |
| 828 | raise model_logic.ValidationError({'labels': '; '.join(errors)}) |
| 829 | |
| 830 | |
Shuqian Zhao | 40e182b | 2016-10-11 11:55:11 -0700 | [diff] [blame] | 831 | @classmethod |
Dan Shi | b5b8b4f | 2016-11-02 14:04:02 -0700 | [diff] [blame] | 832 | def check_board_labels_allowed(cls, hosts, new_labels=[]): |
| 833 | """Verify the specified hosts have valid board labels and the given |
| 834 | new board labels can be added. |
Shuqian Zhao | 40e182b | 2016-10-11 11:55:11 -0700 | [diff] [blame] | 835 | |
| 836 | @param cls: Implicit class object. |
| 837 | @param hosts: The hosts to verify. |
Dan Shi | b5b8b4f | 2016-11-02 14:04:02 -0700 | [diff] [blame] | 838 | @param new_labels: A list of labels to be added to the hosts. |
| 839 | |
| 840 | @raises model_logic.ValidationError if any host has invalid board labels |
| 841 | or the given board labels cannot be added to the hsots. |
Shuqian Zhao | 40e182b | 2016-10-11 11:55:11 -0700 | [diff] [blame] | 842 | """ |
| 843 | Host.objects.populate_relationships(hosts, Label, 'label_list') |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 844 | Host.objects.populate_relationships(hosts, StaticLabel, |
| 845 | 'staticlabel_list') |
Shuqian Zhao | 40e182b | 2016-10-11 11:55:11 -0700 | [diff] [blame] | 846 | errors = [] |
| 847 | for host in hosts: |
| 848 | boards = [label.name for label in host.label_list |
| 849 | if label.name.startswith('board:')] |
Xixuan Wu | e770603 | 2017-12-21 15:57:41 -0800 | [diff] [blame] | 850 | if RESPECT_STATIC_LABELS: |
| 851 | boards += [label.name for label in host.staticlabel_list |
| 852 | if label.name.startswith('board:')] |
| 853 | |
Dan Shi | b5b8b4f | 2016-11-02 14:04:02 -0700 | [diff] [blame] | 854 | if not server_utils.board_labels_allowed(boards + new_labels): |
Shuqian Zhao | 40e182b | 2016-10-11 11:55:11 -0700 | [diff] [blame] | 855 | # do a join, just in case this host has multiple boards, |
| 856 | # we'll be able to see it |
Dan Shi | b5b8b4f | 2016-11-02 14:04:02 -0700 | [diff] [blame] | 857 | errors.append('Host %s already has board labels: %s' % ( |
Shuqian Zhao | 40e182b | 2016-10-11 11:55:11 -0700 | [diff] [blame] | 858 | host.hostname, ', '.join(boards))) |
| 859 | if errors: |
| 860 | raise model_logic.ValidationError({'labels': '; '.join(errors)}) |
| 861 | |
| 862 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 863 | def is_dead(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 864 | """Returns whether the host is dead (has status repair failed).""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 865 | return self.status == Host.Status.REPAIR_FAILED |
mbligh | 3cab4a7 | 2008-03-05 23:19:09 +0000 | [diff] [blame] | 866 | |
| 867 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 868 | def active_queue_entry(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 869 | """Returns the active queue entry for this host, or None if none.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 870 | active = list(self.hostqueueentry_set.filter(active=True)) |
| 871 | if not active: |
| 872 | return None |
| 873 | assert len(active) == 1, ('More than one active entry for ' |
| 874 | 'host ' + self.hostname) |
| 875 | return active[0] |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 876 | |
| 877 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 878 | def _get_attribute_model_and_args(self, attribute): |
| 879 | return HostAttribute, dict(host=self, attribute=attribute) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 880 | |
| 881 | |
Xixuan Wu | 903087b | 2018-02-20 15:36:30 -0800 | [diff] [blame] | 882 | def _get_static_attribute_model_and_args(self, attribute): |
| 883 | return StaticHostAttribute, dict(host=self, attribute=attribute) |
| 884 | |
| 885 | |
Xixuan Wu | c00f603 | 2018-02-22 12:41:41 -0800 | [diff] [blame] | 886 | def _is_replaced_by_static_attribute(self, attribute): |
| 887 | if RESPECT_STATIC_ATTRIBUTES: |
| 888 | model, args = self._get_static_attribute_model_and_args(attribute) |
| 889 | try: |
| 890 | static_attr = model.objects.get(**args) |
| 891 | return True |
| 892 | except StaticHostAttribute.DoesNotExist: |
| 893 | return False |
| 894 | |
| 895 | return False |
| 896 | |
| 897 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 898 | @classmethod |
| 899 | def get_attribute_model(cls): |
| 900 | """Return the attribute model. |
| 901 | |
| 902 | Override method in parent class. See ModelExtensions for details. |
| 903 | @returns: The attribute model of Host. |
| 904 | """ |
| 905 | return HostAttribute |
| 906 | |
| 907 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 908 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 909 | """Metadata for the Host class.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 910 | db_table = 'afe_hosts' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 911 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 912 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 913 | def __unicode__(self): |
| 914 | return unicode(self.hostname) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 915 | |
| 916 | |
MK Ryu | acf3592 | 2014-10-03 14:56:49 -0700 | [diff] [blame] | 917 | class HostAttribute(dbmodels.Model, model_logic.ModelExtensions): |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 918 | """Arbitrary keyvals associated with hosts.""" |
Fang Deng | 8624850 | 2014-12-18 16:38:00 -0800 | [diff] [blame] | 919 | |
| 920 | SERIALIZATION_LINKS_TO_KEEP = set(['host']) |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 921 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['value']) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 922 | host = dbmodels.ForeignKey(Host) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 923 | attribute = dbmodels.CharField(max_length=90) |
| 924 | value = dbmodels.CharField(max_length=300) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 925 | |
| 926 | objects = model_logic.ExtendedManager() |
| 927 | |
| 928 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 929 | """Metadata for the HostAttribute class.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 930 | db_table = 'afe_host_attributes' |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 931 | |
| 932 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 933 | @classmethod |
| 934 | def get_record(cls, data): |
| 935 | """Check the database for an identical record. |
| 936 | |
| 937 | Use host_id and attribute to search for a existing record. |
| 938 | |
| 939 | @raises: DoesNotExist, if no record found |
| 940 | @raises: MultipleObjectsReturned if multiple records found. |
| 941 | """ |
| 942 | # TODO(fdeng): We should use host_id and attribute together as |
| 943 | # a primary key in the db. |
| 944 | return cls.objects.get(host_id=data['host_id'], |
| 945 | attribute=data['attribute']) |
| 946 | |
| 947 | |
| 948 | @classmethod |
| 949 | def deserialize(cls, data): |
| 950 | """Override deserialize in parent class. |
| 951 | |
| 952 | Do not deserialize id as id is not kept consistent on master and shards. |
| 953 | |
| 954 | @param data: A dictionary of data to deserialize. |
| 955 | |
| 956 | @returns: A HostAttribute object. |
| 957 | """ |
| 958 | if data: |
| 959 | data.pop('id') |
| 960 | return super(HostAttribute, cls).deserialize(data) |
| 961 | |
| 962 | |
Xixuan Wu | 903087b | 2018-02-20 15:36:30 -0800 | [diff] [blame] | 963 | class StaticHostAttribute(dbmodels.Model, model_logic.ModelExtensions): |
| 964 | """Static arbitrary keyvals associated with hosts.""" |
| 965 | |
| 966 | SERIALIZATION_LINKS_TO_KEEP = set(['host']) |
| 967 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['value']) |
| 968 | host = dbmodels.ForeignKey(Host) |
| 969 | attribute = dbmodels.CharField(max_length=90) |
| 970 | value = dbmodels.CharField(max_length=300) |
| 971 | |
| 972 | objects = model_logic.ExtendedManager() |
| 973 | |
| 974 | class Meta: |
| 975 | """Metadata for the StaticHostAttribute class.""" |
| 976 | db_table = 'afe_static_host_attributes' |
| 977 | |
| 978 | |
| 979 | @classmethod |
| 980 | def get_record(cls, data): |
| 981 | """Check the database for an identical record. |
| 982 | |
| 983 | Use host_id and attribute to search for a existing record. |
| 984 | |
| 985 | @raises: DoesNotExist, if no record found |
| 986 | @raises: MultipleObjectsReturned if multiple records found. |
| 987 | """ |
| 988 | return cls.objects.get(host_id=data['host_id'], |
| 989 | attribute=data['attribute']) |
| 990 | |
| 991 | |
| 992 | @classmethod |
| 993 | def deserialize(cls, data): |
| 994 | """Override deserialize in parent class. |
| 995 | |
| 996 | Do not deserialize id as id is not kept consistent on master and shards. |
| 997 | |
| 998 | @param data: A dictionary of data to deserialize. |
| 999 | |
| 1000 | @returns: A StaticHostAttribute object. |
| 1001 | """ |
| 1002 | if data: |
| 1003 | data.pop('id') |
| 1004 | return super(StaticHostAttribute, cls).deserialize(data) |
| 1005 | |
| 1006 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1007 | class Test(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1008 | """\ |
| 1009 | Required: |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1010 | author: author name |
| 1011 | description: description of the test |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1012 | name: test name |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1013 | time: short, medium, long |
| 1014 | test_class: This describes the class for your the test belongs in. |
| 1015 | test_category: This describes the category for your tests |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1016 | test_type: Client or Server |
| 1017 | path: path to pass to run_test() |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1018 | sync_count: is a number >=1 (1 being the default). If it's 1, then it's an |
| 1019 | async job. If it's >1 it's sync job for that number of machines |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1020 | i.e. if sync_count = 2 it is a sync job that requires two |
| 1021 | machines. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1022 | Optional: |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1023 | dependencies: What the test requires to run. Comma deliminated list |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 1024 | dependency_labels: many-to-many relationship with labels corresponding to |
| 1025 | test dependencies. |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1026 | experimental: If this is set to True production servers will ignore the test |
| 1027 | run_verify: Whether or not the scheduler should run the verify stage |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1028 | run_reset: Whether or not the scheduler should run the reset stage |
Aviv Keshet | 9af96d3 | 2013-03-05 12:56:24 -0800 | [diff] [blame] | 1029 | test_retry: Number of times to retry test if the test did not complete |
| 1030 | successfully. (optional, default: 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1031 | """ |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1032 | TestTime = enum.Enum('SHORT', 'MEDIUM', 'LONG', start_value=1) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1033 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1034 | name = dbmodels.CharField(max_length=255, unique=True) |
| 1035 | author = dbmodels.CharField(max_length=255) |
| 1036 | test_class = dbmodels.CharField(max_length=255) |
| 1037 | test_category = dbmodels.CharField(max_length=255) |
| 1038 | dependencies = dbmodels.CharField(max_length=255, blank=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1039 | description = dbmodels.TextField(blank=True) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1040 | experimental = dbmodels.BooleanField(default=True) |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1041 | run_verify = dbmodels.BooleanField(default=False) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1042 | test_time = dbmodels.SmallIntegerField(choices=TestTime.choices(), |
| 1043 | default=TestTime.MEDIUM) |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 1044 | test_type = dbmodels.SmallIntegerField( |
| 1045 | choices=control_data.CONTROL_TYPE.choices()) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1046 | sync_count = dbmodels.IntegerField(default=1) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1047 | path = dbmodels.CharField(max_length=255, unique=True) |
Aviv Keshet | 9af96d3 | 2013-03-05 12:56:24 -0800 | [diff] [blame] | 1048 | test_retry = dbmodels.IntegerField(blank=True, default=0) |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1049 | run_reset = dbmodels.BooleanField(default=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1050 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1051 | dependency_labels = ( |
| 1052 | dbmodels.ManyToManyField(Label, blank=True, |
| 1053 | db_table='afe_autotests_dependency_labels')) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1054 | name_field = 'name' |
| 1055 | objects = model_logic.ExtendedManager() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1056 | |
| 1057 | |
jamesren | 35a7022 | 2010-02-16 19:30:46 +0000 | [diff] [blame] | 1058 | def admin_description(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1059 | """Returns a string representing the admin description.""" |
jamesren | 35a7022 | 2010-02-16 19:30:46 +0000 | [diff] [blame] | 1060 | escaped_description = saxutils.escape(self.description) |
| 1061 | return '<span style="white-space:pre">%s</span>' % escaped_description |
| 1062 | admin_description.allow_tags = True |
jamesren | cae88c6 | 2010-02-19 00:12:28 +0000 | [diff] [blame] | 1063 | admin_description.short_description = 'Description' |
jamesren | 35a7022 | 2010-02-16 19:30:46 +0000 | [diff] [blame] | 1064 | |
| 1065 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1066 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1067 | """Metadata for class Test.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1068 | db_table = 'afe_autotests' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1069 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1070 | def __unicode__(self): |
| 1071 | return unicode(self.name) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1072 | |
| 1073 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1074 | class TestParameter(dbmodels.Model): |
| 1075 | """ |
| 1076 | A declared parameter of a test |
| 1077 | """ |
| 1078 | test = dbmodels.ForeignKey(Test) |
| 1079 | name = dbmodels.CharField(max_length=255) |
| 1080 | |
| 1081 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1082 | """Metadata for class TestParameter.""" |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1083 | db_table = 'afe_test_parameters' |
| 1084 | unique_together = ('test', 'name') |
| 1085 | |
| 1086 | def __unicode__(self): |
Eric Li | 0a99391 | 2011-05-17 12:56:25 -0700 | [diff] [blame] | 1087 | return u'%s (%s)' % (self.name, self.test.name) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1088 | |
| 1089 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 1090 | class Profiler(dbmodels.Model, model_logic.ModelExtensions): |
| 1091 | """\ |
| 1092 | Required: |
| 1093 | name: profiler name |
| 1094 | test_type: Client or Server |
| 1095 | |
| 1096 | Optional: |
| 1097 | description: arbirary text description |
| 1098 | """ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1099 | name = dbmodels.CharField(max_length=255, unique=True) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 1100 | description = dbmodels.TextField(blank=True) |
| 1101 | |
| 1102 | name_field = 'name' |
| 1103 | objects = model_logic.ExtendedManager() |
| 1104 | |
| 1105 | |
| 1106 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1107 | """Metadata for class Profiler.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1108 | db_table = 'afe_profilers' |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 1109 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1110 | def __unicode__(self): |
| 1111 | return unicode(self.name) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 1112 | |
| 1113 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1114 | class AclGroup(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1115 | """\ |
| 1116 | Required: |
| 1117 | name: name of ACL group |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1118 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1119 | Optional: |
| 1120 | description: arbitrary description of group |
| 1121 | """ |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 1122 | |
| 1123 | SERIALIZATION_LINKS_TO_FOLLOW = set(['users']) |
| 1124 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1125 | name = dbmodels.CharField(max_length=255, unique=True) |
| 1126 | description = dbmodels.CharField(max_length=255, blank=True) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1127 | users = dbmodels.ManyToManyField(User, blank=False, |
| 1128 | db_table='afe_acl_groups_users') |
| 1129 | hosts = dbmodels.ManyToManyField(Host, blank=True, |
| 1130 | db_table='afe_acl_groups_hosts') |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1131 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1132 | name_field = 'name' |
| 1133 | objects = model_logic.ExtendedManager() |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 1134 | |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1135 | @staticmethod |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1136 | def check_for_acl_violation_hosts(hosts): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1137 | """Verify the current user has access to the specified hosts. |
| 1138 | |
| 1139 | @param hosts: The hosts to verify against. |
| 1140 | @raises AclAccessViolation if the current user doesn't have access |
| 1141 | to a host. |
| 1142 | """ |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1143 | user = User.current_user() |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1144 | if user.is_superuser(): |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 1145 | return |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1146 | accessible_host_ids = set( |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 1147 | host.id for host in Host.objects.filter(aclgroup__users=user)) |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1148 | for host in hosts: |
| 1149 | # Check if the user has access to this host, |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1150 | # but only if it is not a metahost or a one-time-host. |
showard | 98ead17 | 2009-06-22 18:13:24 +0000 | [diff] [blame] | 1151 | no_access = (isinstance(host, Host) |
| 1152 | and not host.invalid |
| 1153 | and int(host.id) not in accessible_host_ids) |
| 1154 | if no_access: |
showard | eaa408e | 2009-09-11 18:45:31 +0000 | [diff] [blame] | 1155 | raise AclAccessViolation("%s does not have access to %s" % |
| 1156 | (str(user), str(host))) |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1157 | |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 1158 | |
| 1159 | @staticmethod |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1160 | def check_abort_permissions(queue_entries): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1161 | """Look for queue entries that aren't abortable by the current user. |
| 1162 | |
| 1163 | An entry is not abortable if: |
| 1164 | * the job isn't owned by this user, and |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1165 | * the machine isn't ACL-accessible, or |
| 1166 | * the machine is in the "Everyone" ACL |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1167 | |
| 1168 | @param queue_entries: The queue entries to check. |
| 1169 | @raises AclAccessViolation if a queue entry is not abortable by the |
| 1170 | current user. |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1171 | """ |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1172 | user = User.current_user() |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 1173 | if user.is_superuser(): |
| 1174 | return |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1175 | not_owned = queue_entries.exclude(job__owner=user.login) |
| 1176 | # I do this using ID sets instead of just Django filters because |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1177 | # filtering on M2M dbmodels is broken in Django 0.96. It's better in |
| 1178 | # 1.0. |
| 1179 | # TODO: Use Django filters, now that we're using 1.0. |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1180 | accessible_ids = set( |
| 1181 | entry.id for entry |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 1182 | in not_owned.filter(host__aclgroup__users__login=user.login)) |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1183 | public_ids = set(entry.id for entry |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 1184 | in not_owned.filter(host__aclgroup__name='Everyone')) |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1185 | cannot_abort = [entry for entry in not_owned.select_related() |
| 1186 | if entry.id not in accessible_ids |
| 1187 | or entry.id in public_ids] |
| 1188 | if len(cannot_abort) == 0: |
| 1189 | return |
| 1190 | entry_names = ', '.join('%s-%s/%s' % (entry.job.id, entry.job.owner, |
showard | 3f15eed | 2008-11-14 22:40:48 +0000 | [diff] [blame] | 1191 | entry.host_or_metahost_name()) |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 1192 | for entry in cannot_abort) |
| 1193 | raise AclAccessViolation('You cannot abort the following job entries: ' |
| 1194 | + entry_names) |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 1195 | |
| 1196 | |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1197 | def check_for_acl_violation_acl_group(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1198 | """Verifies the current user has acces to this ACL group. |
| 1199 | |
| 1200 | @raises AclAccessViolation if the current user doesn't have access to |
| 1201 | this ACL group. |
| 1202 | """ |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1203 | user = User.current_user() |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1204 | if user.is_superuser(): |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1205 | return |
| 1206 | if self.name == 'Everyone': |
| 1207 | raise AclAccessViolation("You cannot modify 'Everyone'!") |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1208 | if not user in self.users.all(): |
| 1209 | raise AclAccessViolation("You do not have access to %s" |
| 1210 | % self.name) |
| 1211 | |
| 1212 | @staticmethod |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1213 | def on_host_membership_change(): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1214 | """Invoked when host membership changes.""" |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1215 | everyone = AclGroup.objects.get(name='Everyone') |
| 1216 | |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1217 | # find hosts that aren't in any ACL group and add them to Everyone |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1218 | # TODO(showard): this is a bit of a hack, since the fact that this query |
| 1219 | # works is kind of a coincidence of Django internals. This trick |
| 1220 | # doesn't work in general (on all foreign key relationships). I'll |
| 1221 | # replace it with a better technique when the need arises. |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 1222 | orphaned_hosts = Host.valid_objects.filter(aclgroup__id__isnull=True) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1223 | everyone.hosts.add(*orphaned_hosts.distinct()) |
| 1224 | |
| 1225 | # find hosts in both Everyone and another ACL group, and remove them |
| 1226 | # from Everyone |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1227 | hosts_in_everyone = Host.valid_objects.filter(aclgroup__name='Everyone') |
| 1228 | acled_hosts = set() |
| 1229 | for host in hosts_in_everyone: |
| 1230 | # Has an ACL group other than Everyone |
| 1231 | if host.aclgroup_set.count() > 1: |
| 1232 | acled_hosts.add(host) |
| 1233 | everyone.hosts.remove(*acled_hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1234 | |
| 1235 | |
| 1236 | def delete(self): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1237 | if (self.name == 'Everyone'): |
| 1238 | raise AclAccessViolation("You cannot delete 'Everyone'!") |
| 1239 | self.check_for_acl_violation_acl_group() |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 1240 | super(AclGroup, self).delete() |
| 1241 | self.on_host_membership_change() |
| 1242 | |
| 1243 | |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 1244 | def add_current_user_if_empty(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1245 | """Adds the current user if the set of users is empty.""" |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 1246 | if not self.users.count(): |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1247 | self.users.add(User.current_user()) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 1248 | |
| 1249 | |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1250 | def perform_after_save(self, change): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1251 | """Called after a save. |
| 1252 | |
| 1253 | @param change: Whether there was a change. |
| 1254 | """ |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1255 | if not change: |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1256 | self.users.add(User.current_user()) |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1257 | self.add_current_user_if_empty() |
| 1258 | self.on_host_membership_change() |
| 1259 | |
| 1260 | |
| 1261 | def save(self, *args, **kwargs): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1262 | change = bool(self.id) |
| 1263 | if change: |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1264 | # Check the original object for an ACL violation |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1265 | AclGroup.objects.get(id=self.id).check_for_acl_violation_acl_group() |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1266 | super(AclGroup, self).save(*args, **kwargs) |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1267 | self.perform_after_save(change) |
showard | 8cbaf1e | 2009-09-08 16:27:04 +0000 | [diff] [blame] | 1268 | |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 1269 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1270 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1271 | """Metadata for class AclGroup.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1272 | db_table = 'afe_acl_groups' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1273 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1274 | def __unicode__(self): |
| 1275 | return unicode(self.name) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1276 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1277 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1278 | class ParameterizedJob(dbmodels.Model): |
| 1279 | """ |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1280 | Auxiliary configuration for a parameterized job. |
Richard Barnette | fddab4a | 2017-02-02 13:23:30 -0800 | [diff] [blame] | 1281 | |
| 1282 | This class is obsolete, and ought to be dead. Due to a series of |
| 1283 | unfortunate events, it can't be deleted: |
| 1284 | * In `class Job` we're required to keep a reference to this class |
| 1285 | for the sake of the scheduler unit tests. |
| 1286 | * The existence of the reference in `Job` means that certain |
| 1287 | methods here will get called from the `get_jobs` RPC. |
| 1288 | So, the definitions below seem to be the minimum stub we can support |
| 1289 | unless/until we change the database schema. |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1290 | """ |
Richard Barnette | f616e8f | 2017-03-06 09:36:15 -0800 | [diff] [blame] | 1291 | |
| 1292 | @classmethod |
| 1293 | def smart_get(cls, id_or_name, *args, **kwargs): |
| 1294 | """For compatibility with Job.add_object. |
| 1295 | |
| 1296 | @param cls: Implicit class object. |
| 1297 | @param id_or_name: The ID or name to get. |
| 1298 | @param args: Non-keyword arguments. |
| 1299 | @param kwargs: Keyword arguments. |
| 1300 | """ |
| 1301 | return cls.objects.get(pk=id_or_name) |
| 1302 | |
| 1303 | |
| 1304 | def job(self): |
| 1305 | """Returns the job if it exists, or else None.""" |
| 1306 | jobs = self.job_set.all() |
| 1307 | assert jobs.count() <= 1 |
| 1308 | return jobs and jobs[0] or None |
| 1309 | |
| 1310 | |
| 1311 | class Meta: |
| 1312 | """Metadata for class ParameterizedJob.""" |
| 1313 | db_table = 'afe_parameterized_jobs' |
| 1314 | |
| 1315 | def __unicode__(self): |
| 1316 | return u'%s (parameterized) - %s' % (self.test.name, self.job()) |
| 1317 | |
| 1318 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1319 | class JobManager(model_logic.ExtendedManager): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1320 | 'Custom manager to provide efficient status counts querying.' |
| 1321 | def get_status_counts(self, job_ids): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1322 | """Returns a dict mapping the given job IDs to their status count dicts. |
| 1323 | |
| 1324 | @param job_ids: A list of job IDs. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1325 | """ |
| 1326 | if not job_ids: |
| 1327 | return {} |
| 1328 | id_list = '(%s)' % ','.join(str(job_id) for job_id in job_ids) |
| 1329 | cursor = connection.cursor() |
| 1330 | cursor.execute(""" |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1331 | SELECT job_id, status, aborted, complete, COUNT(*) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1332 | FROM afe_host_queue_entries |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1333 | WHERE job_id IN %s |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1334 | GROUP BY job_id, status, aborted, complete |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1335 | """ % id_list) |
showard | 25aaf3f | 2009-06-08 23:23:40 +0000 | [diff] [blame] | 1336 | all_job_counts = dict((job_id, {}) for job_id in job_ids) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1337 | for job_id, status, aborted, complete, count in cursor.fetchall(): |
showard | 25aaf3f | 2009-06-08 23:23:40 +0000 | [diff] [blame] | 1338 | job_dict = all_job_counts[job_id] |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1339 | full_status = HostQueueEntry.compute_full_status(status, aborted, |
| 1340 | complete) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 1341 | job_dict.setdefault(full_status, 0) |
showard | 25aaf3f | 2009-06-08 23:23:40 +0000 | [diff] [blame] | 1342 | job_dict[full_status] += count |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1343 | return all_job_counts |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1344 | |
| 1345 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1346 | class Job(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1347 | """\ |
| 1348 | owner: username of job owner |
| 1349 | name: job name (does not have to be unique) |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 1350 | priority: Integer priority value. Higher is more important. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1351 | control_file: contents of control file |
| 1352 | control_type: Client or Server |
| 1353 | created_on: date of job creation |
| 1354 | submitted_on: date of job submission |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1355 | synch_count: how many hosts should be used per autoserv execution |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 1356 | run_verify: Whether or not to run the verify phase |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1357 | run_reset: Whether or not to run the reset phase |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1358 | timeout: DEPRECATED - hours from queuing time until job times out |
| 1359 | timeout_mins: minutes from job queuing time until the job times out |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1360 | max_runtime_hrs: DEPRECATED - hours from job starting time until job |
| 1361 | times out |
| 1362 | max_runtime_mins: minutes from job starting time until job times out |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 1363 | email_list: list of people to email on completion delimited by any of: |
| 1364 | white space, ',', ':', ';' |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 1365 | dependency_labels: many-to-many relationship with labels corresponding to |
| 1366 | job dependencies |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1367 | reboot_before: Never, If dirty, or Always |
| 1368 | reboot_after: Never, If all tests passed, or Always |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1369 | parse_failed_repair: if True, a failed repair launched by this job will have |
| 1370 | its results parsed as part of the job. |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1371 | drone_set: The set of drones to run this job on |
Aviv Keshet | 0b9cfc9 | 2013-02-05 11:36:02 -0800 | [diff] [blame] | 1372 | parent_job: Parent job (optional) |
Aviv Keshet | cd1ff9b | 2013-03-01 14:55:19 -0800 | [diff] [blame] | 1373 | test_retry: Number of times to retry test if the test did not complete |
| 1374 | successfully. (optional, default: 0) |
Dan Shi | c9e1714 | 2015-02-19 11:50:55 -0800 | [diff] [blame] | 1375 | require_ssp: Require server-side packaging unless require_ssp is set to |
| 1376 | False. (optional, default: None) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1377 | """ |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 1378 | |
| 1379 | # TODO: Investigate, if jobkeyval_set is really needed. |
| 1380 | # dynamic_suite will write them into an attached file for the drone, but |
| 1381 | # it doesn't seem like they are actually used. If they aren't used, remove |
| 1382 | # jobkeyval_set here. |
| 1383 | SERIALIZATION_LINKS_TO_FOLLOW = set(['dependency_labels', |
| 1384 | 'hostqueueentry_set', |
| 1385 | 'jobkeyval_set', |
| 1386 | 'shard']) |
| 1387 | |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1388 | # SQL for selecting jobs that should be sent to shard. |
| 1389 | # We use raw sql as django filters were not optimized. |
| 1390 | # The following jobs are excluded by the SQL. |
| 1391 | # - Non-aborted jobs known to shard as specified in |known_ids|. |
| 1392 | # Note for jobs aborted on master, even if already known to shard, |
| 1393 | # will be sent to shard again so that shard can abort them. |
| 1394 | # - Completed jobs |
| 1395 | # - Active jobs |
| 1396 | # - Jobs without host_queue_entries |
| 1397 | NON_ABORTED_KNOWN_JOBS = '(t2.aborted = 0 AND t1.id IN (%(known_ids)s))' |
| 1398 | |
| 1399 | SQL_SHARD_JOBS = ( |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1400 | 'SELECT DISTINCT(t1.id) FROM afe_jobs t1 ' |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1401 | 'INNER JOIN afe_host_queue_entries t2 ON ' |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1402 | ' (t1.id = t2.job_id AND t2.complete != 1 AND t2.active != 1 ' |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1403 | ' %(check_known_jobs)s) ' |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1404 | 'LEFT OUTER JOIN afe_jobs_dependency_labels t3 ON (t1.id = t3.job_id) ' |
Dan Shi | 6b89f1c | 2016-09-17 22:25:53 -0700 | [diff] [blame] | 1405 | 'JOIN afe_shards_labels t4 ' |
| 1406 | ' ON (t4.label_id = t3.label_id OR t4.label_id = t2.meta_host) ' |
| 1407 | 'WHERE t4.shard_id = %(shard_id)s' |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1408 | ) |
| 1409 | |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1410 | # Jobs can be created with assigned hosts and have no dependency |
| 1411 | # labels nor meta_host. |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1412 | # We are looking for: |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1413 | # - a job whose hqe's meta_host is null |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1414 | # - a job whose hqe has a host |
| 1415 | # - one of the host's labels matches the shard's label. |
| 1416 | # Non-aborted known jobs, completed jobs, active jobs, jobs |
Prathmesh Prabhu | 2e315cf | 2018-03-02 20:32:37 +0000 | [diff] [blame] | 1417 | # without hqe are exluded as we do with SQL_SHARD_JOBS. |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1418 | SQL_SHARD_JOBS_WITH_HOSTS = ( |
| 1419 | 'SELECT DISTINCT(t1.id) FROM afe_jobs t1 ' |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1420 | 'INNER JOIN afe_host_queue_entries t2 ON ' |
| 1421 | ' (t1.id = t2.job_id AND t2.complete != 1 AND t2.active != 1 ' |
| 1422 | ' AND t2.meta_host IS NULL AND t2.host_id IS NOT NULL ' |
| 1423 | ' %(check_known_jobs)s) ' |
Xixuan Wu | 4379e93 | 2018-01-17 10:29:50 -0800 | [diff] [blame] | 1424 | 'LEFT OUTER JOIN %(host_label_table)s t3 ON (t2.host_id = t3.host_id) ' |
| 1425 | 'WHERE (t3.%(host_label_column)s IN %(label_ids)s)' |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1426 | ) |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1427 | |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1428 | # Even if we had filters about complete, active and aborted |
MK Ryu | 5c7ef30 | 2015-07-10 17:23:08 -0700 | [diff] [blame] | 1429 | # bits in the above two SQLs, there is a chance that |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1430 | # the result may still contain a job with an hqe with 'complete=1' |
| 1431 | # or 'active=1' or 'aborted=0 and afe_job.id in known jobs.' |
| 1432 | # This happens when a job has two (or more) hqes and at least |
| 1433 | # one hqe has different bits than others. |
| 1434 | # We use a second sql to ensure we exclude all un-desired jobs. |
| 1435 | SQL_JOBS_TO_EXCLUDE =( |
| 1436 | 'SELECT t1.id FROM afe_jobs t1 ' |
| 1437 | 'INNER JOIN afe_host_queue_entries t2 ON ' |
| 1438 | ' (t1.id = t2.job_id) ' |
| 1439 | 'WHERE (t1.id in (%(candidates)s) ' |
| 1440 | ' AND (t2.complete=1 OR t2.active=1 ' |
| 1441 | ' %(check_known_jobs)s))' |
| 1442 | ) |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 1443 | |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1444 | def _deserialize_relation(self, link, data): |
| 1445 | if link in ['hostqueueentry_set', 'jobkeyval_set']: |
| 1446 | for obj in data: |
| 1447 | obj['job_id'] = self.id |
| 1448 | |
| 1449 | super(Job, self)._deserialize_relation(link, data) |
| 1450 | |
| 1451 | |
| 1452 | def custom_deserialize_relation(self, link, data): |
Jakob Juelich | 116ff0f | 2014-09-17 18:25:16 -0700 | [diff] [blame] | 1453 | assert link == 'shard', 'Link %s should not be deserialized' % link |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1454 | self.shard = Shard.deserialize(data) |
| 1455 | |
| 1456 | |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1457 | def sanity_check_update_from_shard(self, shard, updated_serialized): |
Jakob Juelich | 02e6129 | 2014-10-17 12:36:55 -0700 | [diff] [blame] | 1458 | # If the job got aborted on the master after the client fetched it |
| 1459 | # no shard_id will be set. The shard might still push updates though, |
| 1460 | # as the job might complete before the abort bit syncs to the shard. |
| 1461 | # Alternative considered: The master scheduler could be changed to not |
| 1462 | # set aborted jobs to completed that are sharded out. But that would |
| 1463 | # require database queries and seemed more complicated to implement. |
| 1464 | # This seems safe to do, as there won't be updates pushed from the wrong |
| 1465 | # shards should be powered off and wiped hen they are removed from the |
| 1466 | # master. |
| 1467 | if self.shard_id and self.shard_id != shard.id: |
Aviv Keshet | 6dec0e1 | 2017-04-17 15:23:33 -0700 | [diff] [blame] | 1468 | raise error.IgnorableUnallowedRecordsSentToMaster( |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1469 | 'Job id=%s is assigned to shard (%s). Cannot update it with %s ' |
| 1470 | 'from shard %s.' % (self.id, self.shard_id, updated_serialized, |
| 1471 | shard.id)) |
| 1472 | |
| 1473 | |
Allen Li | b8b2e59 | 2017-12-14 17:41:40 -0800 | [diff] [blame] | 1474 | RebootBefore = model_attributes.RebootBefore |
| 1475 | RebootAfter = model_attributes.RebootAfter |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1476 | # TIMEOUT is deprecated. |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 1477 | DEFAULT_TIMEOUT = global_config.global_config.get_config_value( |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1478 | 'AUTOTEST_WEB', 'job_timeout_default', default=24) |
| 1479 | DEFAULT_TIMEOUT_MINS = global_config.global_config.get_config_value( |
| 1480 | 'AUTOTEST_WEB', 'job_timeout_mins_default', default=24*60) |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1481 | # MAX_RUNTIME_HRS is deprecated. Will be removed after switch to mins is |
| 1482 | # completed. |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 1483 | DEFAULT_MAX_RUNTIME_HRS = global_config.global_config.get_config_value( |
| 1484 | 'AUTOTEST_WEB', 'job_max_runtime_hrs_default', default=72) |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1485 | DEFAULT_MAX_RUNTIME_MINS = global_config.global_config.get_config_value( |
| 1486 | 'AUTOTEST_WEB', 'job_max_runtime_mins_default', default=72*60) |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1487 | DEFAULT_PARSE_FAILED_REPAIR = global_config.global_config.get_config_value( |
| 1488 | 'AUTOTEST_WEB', 'parse_failed_repair_default', type=bool, |
| 1489 | default=False) |
Jacob Kopczynski | d6a5e91 | 2018-03-01 09:52:34 -0800 | [diff] [blame^] | 1490 | FETCH_READONLY_JOBS = global_config.global_config.get_config_value( |
| 1491 | 'AUTOTEST_WEB','shard_heartbeat_use_readonly_slave', default=False) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 1492 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1493 | owner = dbmodels.CharField(max_length=255) |
| 1494 | name = dbmodels.CharField(max_length=255) |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 1495 | priority = dbmodels.SmallIntegerField(default=priorities.Priority.DEFAULT) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1496 | control_file = dbmodels.TextField(null=True, blank=True) |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 1497 | control_type = dbmodels.SmallIntegerField( |
| 1498 | choices=control_data.CONTROL_TYPE.choices(), |
| 1499 | blank=True, # to allow 0 |
| 1500 | default=control_data.CONTROL_TYPE.CLIENT) |
showard | 68c7aa0 | 2008-10-09 16:49:11 +0000 | [diff] [blame] | 1501 | created_on = dbmodels.DateTimeField() |
Simran Basi | 8d89b64 | 2014-05-02 17:33:20 -0700 | [diff] [blame] | 1502 | synch_count = dbmodels.IntegerField(blank=True, default=0) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 1503 | timeout = dbmodels.IntegerField(default=DEFAULT_TIMEOUT) |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1504 | run_verify = dbmodels.BooleanField(default=False) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1505 | email_list = dbmodels.CharField(max_length=250, blank=True) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1506 | dependency_labels = ( |
| 1507 | dbmodels.ManyToManyField(Label, blank=True, |
| 1508 | db_table='afe_jobs_dependency_labels')) |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 1509 | reboot_before = dbmodels.SmallIntegerField( |
| 1510 | choices=model_attributes.RebootBefore.choices(), blank=True, |
| 1511 | default=DEFAULT_REBOOT_BEFORE) |
| 1512 | reboot_after = dbmodels.SmallIntegerField( |
| 1513 | choices=model_attributes.RebootAfter.choices(), blank=True, |
| 1514 | default=DEFAULT_REBOOT_AFTER) |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1515 | parse_failed_repair = dbmodels.BooleanField( |
| 1516 | default=DEFAULT_PARSE_FAILED_REPAIR) |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1517 | # max_runtime_hrs is deprecated. Will be removed after switch to mins is |
| 1518 | # completed. |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 1519 | max_runtime_hrs = dbmodels.IntegerField(default=DEFAULT_MAX_RUNTIME_HRS) |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1520 | max_runtime_mins = dbmodels.IntegerField(default=DEFAULT_MAX_RUNTIME_MINS) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1521 | drone_set = dbmodels.ForeignKey(DroneSet, null=True, blank=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1522 | |
Richard Barnette | fddab4a | 2017-02-02 13:23:30 -0800 | [diff] [blame] | 1523 | # TODO(jrbarnette) We have to keep `parameterized_job` around or it |
| 1524 | # breaks the scheduler_models unit tests (and fixing the unit tests |
| 1525 | # will break the scheduler, so don't do that). |
| 1526 | # |
| 1527 | # The ultimate fix is to delete the column from the database table |
| 1528 | # at which point, you _must_ delete this. Until you're ready to do |
| 1529 | # that, DON'T MUCK WITH IT. |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1530 | parameterized_job = dbmodels.ForeignKey(ParameterizedJob, null=True, |
| 1531 | blank=True) |
| 1532 | |
Aviv Keshet | 0b9cfc9 | 2013-02-05 11:36:02 -0800 | [diff] [blame] | 1533 | parent_job = dbmodels.ForeignKey('self', blank=True, null=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1534 | |
Aviv Keshet | cd1ff9b | 2013-03-01 14:55:19 -0800 | [diff] [blame] | 1535 | test_retry = dbmodels.IntegerField(blank=True, default=0) |
| 1536 | |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1537 | run_reset = dbmodels.BooleanField(default=True) |
| 1538 | |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1539 | timeout_mins = dbmodels.IntegerField(default=DEFAULT_TIMEOUT_MINS) |
| 1540 | |
Jakob Juelich | 8421d59 | 2014-09-17 15:27:06 -0700 | [diff] [blame] | 1541 | # If this is None on the master, a slave should be found. |
| 1542 | # If this is None on a slave, it should be synced back to the master |
Jakob Jülich | 92c0633 | 2014-08-25 19:06:57 +0000 | [diff] [blame] | 1543 | shard = dbmodels.ForeignKey(Shard, blank=True, null=True) |
| 1544 | |
Dan Shi | c9e1714 | 2015-02-19 11:50:55 -0800 | [diff] [blame] | 1545 | # If this is None, server-side packaging will be used for server side test, |
| 1546 | # unless it's disabled in global config AUTOSERV/enable_ssp_container. |
| 1547 | require_ssp = dbmodels.NullBooleanField(default=None, blank=True, null=True) |
| 1548 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1549 | # custom manager |
| 1550 | objects = JobManager() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1551 | |
| 1552 | |
Alex Miller | ec21225 | 2014-02-28 16:48:34 -0800 | [diff] [blame] | 1553 | @decorators.cached_property |
| 1554 | def labels(self): |
| 1555 | """All the labels of this job""" |
| 1556 | # We need to convert dependency_labels to a list, because all() gives us |
| 1557 | # back an iterator, and storing/caching an iterator means we'd only be |
| 1558 | # able to read from it once. |
| 1559 | return list(self.dependency_labels.all()) |
| 1560 | |
| 1561 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1562 | def is_server_job(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1563 | """Returns whether this job is of type server.""" |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 1564 | return self.control_type == control_data.CONTROL_TYPE.SERVER |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1565 | |
| 1566 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1567 | @classmethod |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1568 | def create(cls, owner, options, hosts): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1569 | """Creates a job. |
| 1570 | |
| 1571 | The job is created by taking some information (the listed args) and |
| 1572 | filling in the rest of the necessary information. |
| 1573 | |
| 1574 | @param cls: Implicit class object. |
| 1575 | @param owner: The owner for the job. |
| 1576 | @param options: An options object. |
| 1577 | @param hosts: The hosts to use. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1578 | """ |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 1579 | AclGroup.check_for_acl_violation_hosts(hosts) |
showard | 4d23375 | 2010-01-20 19:06:40 +0000 | [diff] [blame] | 1580 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1581 | control_file = options.get('control_file') |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1582 | |
showard | 4d23375 | 2010-01-20 19:06:40 +0000 | [diff] [blame] | 1583 | user = User.current_user() |
| 1584 | if options.get('reboot_before') is None: |
| 1585 | options['reboot_before'] = user.get_reboot_before_display() |
| 1586 | if options.get('reboot_after') is None: |
| 1587 | options['reboot_after'] = user.get_reboot_after_display() |
| 1588 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1589 | drone_set = DroneSet.resolve_name(options.get('drone_set')) |
| 1590 | |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1591 | if options.get('timeout_mins') is None and options.get('timeout'): |
| 1592 | options['timeout_mins'] = options['timeout'] * 60 |
| 1593 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1594 | job = cls.add_object( |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1595 | owner=owner, |
| 1596 | name=options['name'], |
| 1597 | priority=options['priority'], |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1598 | control_file=control_file, |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1599 | control_type=options['control_type'], |
| 1600 | synch_count=options.get('synch_count'), |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1601 | # timeout needs to be deleted in the future. |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1602 | timeout=options.get('timeout'), |
Simran Basi | 94dc003 | 2013-11-12 14:09:46 -0800 | [diff] [blame] | 1603 | timeout_mins=options.get('timeout_mins'), |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1604 | max_runtime_mins=options.get('max_runtime_mins'), |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1605 | run_verify=options.get('run_verify'), |
| 1606 | email_list=options.get('email_list'), |
| 1607 | reboot_before=options.get('reboot_before'), |
| 1608 | reboot_after=options.get('reboot_after'), |
| 1609 | parse_failed_repair=options.get('parse_failed_repair'), |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1610 | created_on=datetime.now(), |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1611 | drone_set=drone_set, |
Aviv Keshet | cd1ff9b | 2013-03-01 14:55:19 -0800 | [diff] [blame] | 1612 | parent_job=options.get('parent_job_id'), |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1613 | test_retry=options.get('test_retry'), |
Dan Shi | c9e1714 | 2015-02-19 11:50:55 -0800 | [diff] [blame] | 1614 | run_reset=options.get('run_reset'), |
| 1615 | require_ssp=options.get('require_ssp')) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1616 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1617 | job.dependency_labels = options['dependencies'] |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1618 | |
jamesren | d8b6e17 | 2010-04-16 23:45:00 +0000 | [diff] [blame] | 1619 | if options.get('keyvals'): |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1620 | for key, value in options['keyvals'].iteritems(): |
| 1621 | JobKeyval.objects.create(job=job, key=key, value=value) |
| 1622 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1623 | return job |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1624 | |
| 1625 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1626 | @classmethod |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 1627 | def assign_to_shard(cls, shard, known_ids): |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1628 | """Assigns unassigned jobs to a shard. |
| 1629 | |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 1630 | For all labels that have been assigned to this shard, all jobs that |
| 1631 | have this label, are assigned to this shard. |
| 1632 | |
| 1633 | Jobs that are assigned to the shard but aren't already present on the |
| 1634 | shard are returned. |
| 1635 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1636 | @param shard: The shard to assign jobs to. |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 1637 | @param known_ids: List of all ids of incomplete jobs, the shard already |
| 1638 | knows about. |
| 1639 | This is used to figure out which jobs should be sent |
| 1640 | to the shard. If shard_ids were used instead, jobs |
| 1641 | would only be transferred once, even if the client |
| 1642 | failed persisting them. |
| 1643 | The number of unfinished jobs usually lies in O(1000). |
| 1644 | Assuming one id takes 8 chars in the json, this means |
| 1645 | overhead that lies in the lower kilobyte range. |
| 1646 | A not in query with 5000 id's takes about 30ms. |
| 1647 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1648 | @returns The job objects that should be sent to the shard. |
| 1649 | """ |
| 1650 | # Disclaimer: Concurrent heartbeats should not occur in today's setup. |
| 1651 | # If this changes or they are triggered manually, this applies: |
| 1652 | # Jobs may be returned more than once by concurrent calls of this |
| 1653 | # function, as there is a race condition between SELECT and UPDATE. |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1654 | job_ids = set([]) |
| 1655 | check_known_jobs_exclude = '' |
| 1656 | check_known_jobs_include = '' |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 1657 | |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1658 | if known_ids: |
| 1659 | check_known_jobs = ( |
| 1660 | cls.NON_ABORTED_KNOWN_JOBS % |
| 1661 | {'known_ids': ','.join([str(i) for i in known_ids])}) |
| 1662 | check_known_jobs_exclude = 'AND NOT ' + check_known_jobs |
| 1663 | check_known_jobs_include = 'OR ' + check_known_jobs |
| 1664 | |
Jacob Kopczynski | d6a5e91 | 2018-03-01 09:52:34 -0800 | [diff] [blame^] | 1665 | raw_sql = cls.SQL_SHARD_JOBS % { |
| 1666 | 'check_known_jobs': check_known_jobs_exclude, |
| 1667 | 'shard_id': shard.id |
| 1668 | } |
| 1669 | if cls.FETCH_READONLY_JOBS: |
| 1670 | #TODO(jkop): Get rid of this kludge when we update Django to >=1.7 |
| 1671 | #correct usage would be .raw(..., using='readonly') |
| 1672 | old_db = Job.objects._db |
| 1673 | try: |
| 1674 | Job.objects._db = 'readonly' |
| 1675 | job_ids |= set([j.id for j in Job.objects.raw(raw_sql)]) |
| 1676 | finally: |
| 1677 | Job.objects._db = old_db |
| 1678 | if not job_ids: |
| 1679 | #If the replica is down or we're in a test, fetch from master. |
| 1680 | job_ids |= set([j.id for j in Job.objects.raw(raw_sql)]) |
Xixuan Wu | 4379e93 | 2018-01-17 10:29:50 -0800 | [diff] [blame] | 1681 | |
| 1682 | static_labels, non_static_labels = Host.classify_label_objects( |
| 1683 | shard.labels.all()) |
| 1684 | if static_labels: |
| 1685 | label_ids = [str(l.id) for l in static_labels] |
| 1686 | query = Job.objects.raw(cls.SQL_SHARD_JOBS_WITH_HOSTS % { |
| 1687 | 'check_known_jobs': check_known_jobs_exclude, |
| 1688 | 'host_label_table': 'afe_static_hosts_labels', |
| 1689 | 'host_label_column': 'staticlabel_id', |
| 1690 | 'label_ids': '(%s)' % ','.join(label_ids)}) |
| 1691 | job_ids |= set([j.id for j in query]) |
| 1692 | |
| 1693 | if non_static_labels: |
| 1694 | label_ids = [str(l.id) for l in non_static_labels] |
| 1695 | query = Job.objects.raw(cls.SQL_SHARD_JOBS_WITH_HOSTS % { |
| 1696 | 'check_known_jobs': check_known_jobs_exclude, |
| 1697 | 'host_label_table': 'afe_hosts_labels', |
| 1698 | 'host_label_column': 'label_id', |
| 1699 | 'label_ids': '(%s)' % ','.join(label_ids)}) |
Fang Deng | 2b79fd7 | 2015-05-20 19:11:36 -0700 | [diff] [blame] | 1700 | job_ids |= set([j.id for j in query]) |
| 1701 | |
| 1702 | if job_ids: |
| 1703 | query = Job.objects.raw( |
| 1704 | cls.SQL_JOBS_TO_EXCLUDE % |
| 1705 | {'check_known_jobs': check_known_jobs_include, |
| 1706 | 'candidates': ','.join([str(i) for i in job_ids])}) |
| 1707 | job_ids -= set([j.id for j in query]) |
| 1708 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1709 | if job_ids: |
| 1710 | Job.objects.filter(pk__in=job_ids).update(shard=shard) |
| 1711 | return list(Job.objects.filter(pk__in=job_ids).all()) |
| 1712 | return [] |
| 1713 | |
| 1714 | |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1715 | def queue(self, hosts, is_template=False): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1716 | """Enqueue a job on the given hosts. |
| 1717 | |
| 1718 | @param hosts: The hosts to use. |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1719 | @param is_template: Whether the status should be "Template". |
| 1720 | """ |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1721 | if not hosts: |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1722 | # hostless job |
| 1723 | entry = HostQueueEntry.create(job=self, is_template=is_template) |
| 1724 | entry.save() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1725 | return |
| 1726 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1727 | for host in hosts: |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1728 | host.enqueue_job(self, is_template=is_template) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1729 | |
| 1730 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1731 | def user(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1732 | """Gets the user of this job, or None if it doesn't exist.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1733 | try: |
| 1734 | return User.objects.get(login=self.owner) |
| 1735 | except self.DoesNotExist: |
| 1736 | return None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1737 | |
| 1738 | |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1739 | def abort(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1740 | """Aborts this job.""" |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 1741 | for queue_entry in self.hostqueueentry_set.all(): |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1742 | queue_entry.abort() |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 1743 | |
| 1744 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1745 | def tag(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1746 | """Returns a string tag for this job.""" |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 1747 | return server_utils.get_job_tag(self.id, self.owner) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1748 | |
| 1749 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1750 | def keyval_dict(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1751 | """Returns all keyvals for this job as a dictionary.""" |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1752 | return dict((keyval.key, keyval.value) |
| 1753 | for keyval in self.jobkeyval_set.all()) |
| 1754 | |
| 1755 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 1756 | @classmethod |
| 1757 | def get_attribute_model(cls): |
| 1758 | """Return the attribute model. |
| 1759 | |
| 1760 | Override method in parent class. This class is called when |
| 1761 | deserializing the one-to-many relationship betwen Job and JobKeyval. |
| 1762 | On deserialization, we will try to clear any existing job keyvals |
| 1763 | associated with a job to avoid any inconsistency. |
| 1764 | Though Job doesn't implement ModelWithAttribute, we still treat |
| 1765 | it as an attribute model for this purpose. |
| 1766 | |
| 1767 | @returns: The attribute model of Job. |
| 1768 | """ |
| 1769 | return JobKeyval |
| 1770 | |
| 1771 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1772 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1773 | """Metadata for class Job.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1774 | db_table = 'afe_jobs' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1775 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1776 | def __unicode__(self): |
| 1777 | return u'%s (%s-%s)' % (self.name, self.id, self.owner) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1778 | |
| 1779 | |
Allen Li | c1f0a91 | 2017-10-19 17:19:02 -0700 | [diff] [blame] | 1780 | class JobHandoff(dbmodels.Model, model_logic.ModelExtensions): |
| 1781 | """Jobs that have been handed off to lucifer.""" |
| 1782 | |
Allen Li | fef16ae | 2017-11-20 15:23:02 -0800 | [diff] [blame] | 1783 | job = dbmodels.OneToOneField(Job, on_delete=dbmodels.CASCADE, |
| 1784 | primary_key=True) |
| 1785 | created = dbmodels.DateTimeField(auto_now_add=True) |
| 1786 | completed = dbmodels.BooleanField(default=False) |
Allen Li | 1fc27d3 | 2018-01-26 17:51:16 -0800 | [diff] [blame] | 1787 | drone = dbmodels.CharField( |
| 1788 | max_length=128, null=True, |
| 1789 | help_text=''' |
| 1790 | The hostname of the drone the job is running on and whose job_aborter |
| 1791 | should be responsible for aborting the job if the job process dies. |
| 1792 | NULL means any drone's job_aborter has free reign to abort the job. |
| 1793 | ''') |
Allen Li | c1f0a91 | 2017-10-19 17:19:02 -0700 | [diff] [blame] | 1794 | |
| 1795 | class Meta: |
| 1796 | """Metadata for class Job.""" |
| 1797 | db_table = 'afe_job_handoffs' |
| 1798 | |
| 1799 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1800 | class JobKeyval(dbmodels.Model, model_logic.ModelExtensions): |
| 1801 | """Keyvals associated with jobs""" |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 1802 | |
| 1803 | SERIALIZATION_LINKS_TO_KEEP = set(['job']) |
| 1804 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['value']) |
| 1805 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1806 | job = dbmodels.ForeignKey(Job) |
| 1807 | key = dbmodels.CharField(max_length=90) |
| 1808 | value = dbmodels.CharField(max_length=300) |
| 1809 | |
| 1810 | objects = model_logic.ExtendedManager() |
| 1811 | |
Fang Deng | ff36159 | 2015-02-02 15:27:34 -0800 | [diff] [blame] | 1812 | |
| 1813 | @classmethod |
| 1814 | def get_record(cls, data): |
| 1815 | """Check the database for an identical record. |
| 1816 | |
| 1817 | Use job_id and key to search for a existing record. |
| 1818 | |
| 1819 | @raises: DoesNotExist, if no record found |
| 1820 | @raises: MultipleObjectsReturned if multiple records found. |
| 1821 | """ |
| 1822 | # TODO(fdeng): We should use job_id and key together as |
| 1823 | # a primary key in the db. |
| 1824 | return cls.objects.get(job_id=data['job_id'], key=data['key']) |
| 1825 | |
| 1826 | |
| 1827 | @classmethod |
| 1828 | def deserialize(cls, data): |
| 1829 | """Override deserialize in parent class. |
| 1830 | |
| 1831 | Do not deserialize id as id is not kept consistent on master and shards. |
| 1832 | |
| 1833 | @param data: A dictionary of data to deserialize. |
| 1834 | |
| 1835 | @returns: A JobKeyval object. |
| 1836 | """ |
| 1837 | if data: |
| 1838 | data.pop('id') |
| 1839 | return super(JobKeyval, cls).deserialize(data) |
| 1840 | |
| 1841 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1842 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1843 | """Metadata for class JobKeyval.""" |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1844 | db_table = 'afe_job_keyvals' |
| 1845 | |
| 1846 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1847 | class IneligibleHostQueue(dbmodels.Model, model_logic.ModelExtensions): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1848 | """Represents an ineligible host queue.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1849 | job = dbmodels.ForeignKey(Job) |
| 1850 | host = dbmodels.ForeignKey(Host) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1851 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1852 | objects = model_logic.ExtendedManager() |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 1853 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1854 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1855 | """Metadata for class IneligibleHostQueue.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1856 | db_table = 'afe_ineligible_host_queues' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1857 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1858 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 1859 | class HostQueueEntry(dbmodels.Model, model_logic.ModelExtensions): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1860 | """Represents a host queue entry.""" |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 1861 | |
| 1862 | SERIALIZATION_LINKS_TO_FOLLOW = set(['meta_host']) |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 1863 | SERIALIZATION_LINKS_TO_KEEP = set(['host']) |
Jakob Juelich | f865d33 | 2014-09-29 10:47:49 -0700 | [diff] [blame] | 1864 | SERIALIZATION_LOCAL_LINKS_TO_UPDATE = set(['aborted']) |
Jakob Juelich | 3bb7c80 | 2014-09-02 16:31:11 -0700 | [diff] [blame] | 1865 | |
Jakob Juelich | f88fa93 | 2014-09-03 17:58:04 -0700 | [diff] [blame] | 1866 | |
| 1867 | def custom_deserialize_relation(self, link, data): |
| 1868 | assert link == 'meta_host' |
| 1869 | self.meta_host = Label.deserialize(data) |
| 1870 | |
| 1871 | |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1872 | def sanity_check_update_from_shard(self, shard, updated_serialized, |
| 1873 | job_ids_sent): |
| 1874 | if self.job_id not in job_ids_sent: |
Aviv Keshet | 6dec0e1 | 2017-04-17 15:23:33 -0700 | [diff] [blame] | 1875 | raise error.IgnorableUnallowedRecordsSentToMaster( |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1876 | 'Sent HostQueueEntry without corresponding ' |
| 1877 | 'job entry: %s' % updated_serialized) |
| 1878 | |
| 1879 | |
showard | eaa408e | 2009-09-11 18:45:31 +0000 | [diff] [blame] | 1880 | Status = host_queue_entry_states.Status |
| 1881 | ACTIVE_STATUSES = host_queue_entry_states.ACTIVE_STATUSES |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1882 | COMPLETE_STATUSES = host_queue_entry_states.COMPLETE_STATUSES |
Laurence Goodby | 303d266 | 2016-07-01 15:57:04 -0700 | [diff] [blame] | 1883 | PRE_JOB_STATUSES = host_queue_entry_states.PRE_JOB_STATUSES |
| 1884 | IDLE_PRE_JOB_STATUSES = host_queue_entry_states.IDLE_PRE_JOB_STATUSES |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 1885 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1886 | job = dbmodels.ForeignKey(Job) |
| 1887 | host = dbmodels.ForeignKey(Host, blank=True, null=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1888 | status = dbmodels.CharField(max_length=255) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1889 | meta_host = dbmodels.ForeignKey(Label, blank=True, null=True, |
| 1890 | db_column='meta_host') |
| 1891 | active = dbmodels.BooleanField(default=False) |
| 1892 | complete = dbmodels.BooleanField(default=False) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 1893 | deleted = dbmodels.BooleanField(default=False) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1894 | execution_subdir = dbmodels.CharField(max_length=255, blank=True, |
| 1895 | default='') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1896 | # If atomic_group is set, this is a virtual HostQueueEntry that will |
| 1897 | # be expanded into many actual hosts within the group at schedule time. |
| 1898 | atomic_group = dbmodels.ForeignKey(AtomicGroup, blank=True, null=True) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1899 | aborted = dbmodels.BooleanField(default=False) |
showard | d3771cc | 2009-10-07 20:48:22 +0000 | [diff] [blame] | 1900 | started_on = dbmodels.DateTimeField(null=True, blank=True) |
Fang Deng | 5159903 | 2014-06-23 17:24:27 -0700 | [diff] [blame] | 1901 | finished_on = dbmodels.DateTimeField(null=True, blank=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1902 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1903 | objects = model_logic.ExtendedManager() |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 1904 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1905 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1906 | def __init__(self, *args, **kwargs): |
| 1907 | super(HostQueueEntry, self).__init__(*args, **kwargs) |
| 1908 | self._record_attributes(['status']) |
| 1909 | |
| 1910 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1911 | @classmethod |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1912 | def create(cls, job, host=None, meta_host=None, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1913 | is_template=False): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1914 | """Creates a new host queue entry. |
| 1915 | |
| 1916 | @param cls: Implicit class object. |
| 1917 | @param job: The associated job. |
| 1918 | @param host: The associated host. |
| 1919 | @param meta_host: The associated meta host. |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1920 | @param is_template: Whether the status should be "Template". |
| 1921 | """ |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1922 | if is_template: |
| 1923 | status = cls.Status.TEMPLATE |
| 1924 | else: |
| 1925 | status = cls.Status.QUEUED |
| 1926 | |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1927 | return cls(job=job, host=host, meta_host=meta_host, status=status) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1928 | |
| 1929 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1930 | def save(self, *args, **kwargs): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1931 | self._set_active_and_complete() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1932 | super(HostQueueEntry, self).save(*args, **kwargs) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1933 | self._check_for_updated_attributes() |
| 1934 | |
| 1935 | |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1936 | def execution_path(self): |
| 1937 | """ |
| 1938 | Path to this entry's results (relative to the base results directory). |
| 1939 | """ |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 1940 | return server_utils.get_hqe_exec_path(self.job.tag(), |
| 1941 | self.execution_subdir) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1942 | |
| 1943 | |
showard | 3f15eed | 2008-11-14 22:40:48 +0000 | [diff] [blame] | 1944 | def host_or_metahost_name(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1945 | """Returns the first non-None name found in priority order. |
| 1946 | |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1947 | The priority order checked is: (1) host name; (2) meta host name |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1948 | """ |
showard | 3f15eed | 2008-11-14 22:40:48 +0000 | [diff] [blame] | 1949 | if self.host: |
| 1950 | return self.host.hostname |
showard | 7890e79 | 2009-07-28 20:10:20 +0000 | [diff] [blame] | 1951 | else: |
Allen Li | 204eac1 | 2017-01-30 18:31:26 -0800 | [diff] [blame] | 1952 | assert self.meta_host |
| 1953 | return self.meta_host.name |
showard | 3f15eed | 2008-11-14 22:40:48 +0000 | [diff] [blame] | 1954 | |
| 1955 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1956 | def _set_active_and_complete(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1957 | if self.status in self.ACTIVE_STATUSES: |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1958 | self.active, self.complete = True, False |
| 1959 | elif self.status in self.COMPLETE_STATUSES: |
| 1960 | self.active, self.complete = False, True |
| 1961 | else: |
| 1962 | self.active, self.complete = False, False |
| 1963 | |
| 1964 | |
| 1965 | def on_attribute_changed(self, attribute, old_value): |
| 1966 | assert attribute == 'status' |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 1967 | logging.info('%s/%d (%d) -> %s', self.host, self.job.id, self.id, |
| 1968 | self.status) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1969 | |
| 1970 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1971 | def is_meta_host_entry(self): |
| 1972 | 'True if this is a entry has a meta_host instead of a host.' |
| 1973 | return self.host is None and self.meta_host is not None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1974 | |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 1975 | |
Simran Basi | c1b2676 | 2013-06-26 14:23:21 -0700 | [diff] [blame] | 1976 | # This code is shared between rpc_interface and models.HostQueueEntry. |
| 1977 | # Sadly due to circular imports between the 2 (crbug.com/230100) making it |
| 1978 | # a class method was the best way to refactor it. Attempting to put it in |
| 1979 | # rpc_utils or a new utils module failed as that would require us to import |
| 1980 | # models.py but to call it from here we would have to import the utils.py |
| 1981 | # thus creating a cycle. |
| 1982 | @classmethod |
| 1983 | def abort_host_queue_entries(cls, host_queue_entries): |
| 1984 | """Aborts a collection of host_queue_entries. |
| 1985 | |
| 1986 | Abort these host queue entry and all host queue entries of jobs created |
| 1987 | by them. |
| 1988 | |
| 1989 | @param host_queue_entries: List of host queue entries we want to abort. |
| 1990 | """ |
| 1991 | # This isn't completely immune to race conditions since it's not atomic, |
| 1992 | # but it should be safe given the scheduler's behavior. |
| 1993 | |
| 1994 | # TODO(milleral): crbug.com/230100 |
| 1995 | # The |abort_host_queue_entries| rpc does nearly exactly this, |
| 1996 | # however, trying to re-use the code generates some horrible |
| 1997 | # circular import error. I'd be nice to refactor things around |
| 1998 | # sometime so the code could be reused. |
| 1999 | |
| 2000 | # Fixpoint algorithm to find the whole tree of HQEs to abort to |
| 2001 | # minimize the total number of database queries: |
| 2002 | children = set() |
| 2003 | new_children = set(host_queue_entries) |
| 2004 | while new_children: |
| 2005 | children.update(new_children) |
| 2006 | new_child_ids = [hqe.job_id for hqe in new_children] |
| 2007 | new_children = HostQueueEntry.objects.filter( |
| 2008 | job__parent_job__in=new_child_ids, |
| 2009 | complete=False, aborted=False).all() |
| 2010 | # To handle circular parental relationships |
| 2011 | new_children = set(new_children) - children |
| 2012 | |
| 2013 | # Associate a user with the host queue entries that we're about |
| 2014 | # to abort so that we can look up who to blame for the aborts. |
| 2015 | now = datetime.now() |
| 2016 | user = User.current_user() |
| 2017 | aborted_hqes = [AbortedHostQueueEntry(queue_entry=hqe, |
| 2018 | aborted_by=user, aborted_on=now) for hqe in children] |
| 2019 | AbortedHostQueueEntry.objects.bulk_create(aborted_hqes) |
| 2020 | # Bulk update all of the HQEs to set the abort bit. |
| 2021 | child_ids = [hqe.id for hqe in children] |
| 2022 | HostQueueEntry.objects.filter(id__in=child_ids).update(aborted=True) |
| 2023 | |
| 2024 | |
Scott Zawalski | 2304143 | 2013-04-17 07:39:09 -0700 | [diff] [blame] | 2025 | def abort(self): |
Alex Miller | dea6704 | 2013-04-22 17:23:34 -0700 | [diff] [blame] | 2026 | """ Aborts this host queue entry. |
Simran Basi | c1b2676 | 2013-06-26 14:23:21 -0700 | [diff] [blame] | 2027 | |
Alex Miller | dea6704 | 2013-04-22 17:23:34 -0700 | [diff] [blame] | 2028 | Abort this host queue entry and all host queue entries of jobs created by |
| 2029 | this one. |
| 2030 | |
| 2031 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2032 | if not self.complete and not self.aborted: |
Simran Basi | 97582a2 | 2013-06-27 12:03:21 -0700 | [diff] [blame] | 2033 | HostQueueEntry.abort_host_queue_entries([self]) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 2034 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2035 | |
| 2036 | @classmethod |
| 2037 | def compute_full_status(cls, status, aborted, complete): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2038 | """Returns a modified status msg if the host queue entry was aborted. |
| 2039 | |
| 2040 | @param cls: Implicit class object. |
| 2041 | @param status: The original status message. |
| 2042 | @param aborted: Whether the host queue entry was aborted. |
| 2043 | @param complete: Whether the host queue entry was completed. |
| 2044 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2045 | if aborted and not complete: |
| 2046 | return 'Aborted (%s)' % status |
| 2047 | return status |
| 2048 | |
| 2049 | |
| 2050 | def full_status(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2051 | """Returns the full status of this host queue entry, as a string.""" |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2052 | return self.compute_full_status(self.status, self.aborted, |
| 2053 | self.complete) |
| 2054 | |
| 2055 | |
| 2056 | def _postprocess_object_dict(self, object_dict): |
| 2057 | object_dict['full_status'] = self.full_status() |
| 2058 | |
| 2059 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2060 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2061 | """Metadata for class HostQueueEntry.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 2062 | db_table = 'afe_host_queue_entries' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 2063 | |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2064 | |
showard | 4c11904 | 2008-09-29 19:16:18 +0000 | [diff] [blame] | 2065 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2066 | def __unicode__(self): |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2067 | hostname = None |
| 2068 | if self.host: |
| 2069 | hostname = self.host.hostname |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2070 | return u"%s/%d (%d)" % (hostname, self.job.id, self.id) |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2071 | |
| 2072 | |
Paul Hobbs | 4621bf1 | 2017-05-11 17:42:02 -0700 | [diff] [blame] | 2073 | class HostQueueEntryStartTimes(dbmodels.Model): |
| 2074 | """An auxilary table to HostQueueEntry to index by start time.""" |
Paul Hobbs | 69de2fa | 2017-06-07 14:59:33 -0700 | [diff] [blame] | 2075 | insert_time = dbmodels.DateTimeField() |
Paul Hobbs | 4621bf1 | 2017-05-11 17:42:02 -0700 | [diff] [blame] | 2076 | highest_hqe_id = dbmodels.IntegerField() |
| 2077 | |
| 2078 | class Meta: |
| 2079 | """Metadata for class HostQueueEntryStartTimes.""" |
Paul Hobbs | 69de2fa | 2017-06-07 14:59:33 -0700 | [diff] [blame] | 2080 | db_table = 'afe_host_queue_entry_start_times' |
Paul Hobbs | 4621bf1 | 2017-05-11 17:42:02 -0700 | [diff] [blame] | 2081 | |
| 2082 | |
showard | 4c11904 | 2008-09-29 19:16:18 +0000 | [diff] [blame] | 2083 | class AbortedHostQueueEntry(dbmodels.Model, model_logic.ModelExtensions): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2084 | """Represents an aborted host queue entry.""" |
showard | 4c11904 | 2008-09-29 19:16:18 +0000 | [diff] [blame] | 2085 | queue_entry = dbmodels.OneToOneField(HostQueueEntry, primary_key=True) |
| 2086 | aborted_by = dbmodels.ForeignKey(User) |
showard | 68c7aa0 | 2008-10-09 16:49:11 +0000 | [diff] [blame] | 2087 | aborted_on = dbmodels.DateTimeField() |
showard | 4c11904 | 2008-09-29 19:16:18 +0000 | [diff] [blame] | 2088 | |
| 2089 | objects = model_logic.ExtendedManager() |
| 2090 | |
showard | 68c7aa0 | 2008-10-09 16:49:11 +0000 | [diff] [blame] | 2091 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2092 | def save(self, *args, **kwargs): |
showard | 68c7aa0 | 2008-10-09 16:49:11 +0000 | [diff] [blame] | 2093 | self.aborted_on = datetime.now() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2094 | super(AbortedHostQueueEntry, self).save(*args, **kwargs) |
showard | 68c7aa0 | 2008-10-09 16:49:11 +0000 | [diff] [blame] | 2095 | |
showard | 4c11904 | 2008-09-29 19:16:18 +0000 | [diff] [blame] | 2096 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2097 | """Metadata for class AbortedHostQueueEntry.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 2098 | db_table = 'afe_aborted_host_queue_entries' |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 2099 | |
| 2100 | |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2101 | class SpecialTask(dbmodels.Model, model_logic.ModelExtensions): |
| 2102 | """\ |
| 2103 | Tasks to run on hosts at the next time they are in the Ready state. Use this |
| 2104 | for high-priority tasks, such as forced repair or forced reinstall. |
| 2105 | |
| 2106 | host: host to run this task on |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2107 | task: special task to run |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2108 | time_requested: date and time the request for this task was made |
| 2109 | is_active: task is currently running |
| 2110 | is_complete: task has finished running |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 2111 | is_aborted: task was aborted |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2112 | time_started: date and time the task started |
Dan Shi | d072554 | 2014-06-23 15:34:27 -0700 | [diff] [blame] | 2113 | time_finished: date and time the task finished |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2114 | queue_entry: Host queue entry waiting on this task (or None, if task was not |
| 2115 | started in preparation of a job) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2116 | """ |
Alex Miller | dfff2fd | 2013-05-28 13:05:06 -0700 | [diff] [blame] | 2117 | Task = enum.Enum('Verify', 'Cleanup', 'Repair', 'Reset', 'Provision', |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 2118 | string_values=True) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2119 | |
| 2120 | host = dbmodels.ForeignKey(Host, blank=False, null=False) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2121 | task = dbmodels.CharField(max_length=64, choices=Task.choices(), |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2122 | blank=False, null=False) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 2123 | requested_by = dbmodels.ForeignKey(User) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2124 | time_requested = dbmodels.DateTimeField(auto_now_add=True, blank=False, |
| 2125 | null=False) |
| 2126 | is_active = dbmodels.BooleanField(default=False, blank=False, null=False) |
| 2127 | is_complete = dbmodels.BooleanField(default=False, blank=False, null=False) |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 2128 | is_aborted = dbmodels.BooleanField(default=False, blank=False, null=False) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 2129 | time_started = dbmodels.DateTimeField(null=True, blank=True) |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2130 | queue_entry = dbmodels.ForeignKey(HostQueueEntry, blank=True, null=True) |
showard | e60e44e | 2009-11-13 20:45:38 +0000 | [diff] [blame] | 2131 | success = dbmodels.BooleanField(default=False, blank=False, null=False) |
Dan Shi | d072554 | 2014-06-23 15:34:27 -0700 | [diff] [blame] | 2132 | time_finished = dbmodels.DateTimeField(null=True, blank=True) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2133 | |
| 2134 | objects = model_logic.ExtendedManager() |
| 2135 | |
| 2136 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2137 | def save(self, **kwargs): |
| 2138 | if self.queue_entry: |
| 2139 | self.requested_by = User.objects.get( |
| 2140 | login=self.queue_entry.job.owner) |
| 2141 | super(SpecialTask, self).save(**kwargs) |
| 2142 | |
| 2143 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2144 | def execution_path(self): |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 2145 | """Returns the execution path for a special task.""" |
| 2146 | return server_utils.get_special_task_exec_path( |
| 2147 | self.host.hostname, self.id, self.task, self.time_requested) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2148 | |
| 2149 | |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 2150 | # property to emulate HostQueueEntry.status |
| 2151 | @property |
| 2152 | def status(self): |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 2153 | """Returns a host queue entry status appropriate for a speical task.""" |
| 2154 | return server_utils.get_special_task_status( |
| 2155 | self.is_complete, self.success, self.is_active) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 2156 | |
| 2157 | |
| 2158 | # property to emulate HostQueueEntry.started_on |
| 2159 | @property |
| 2160 | def started_on(self): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2161 | """Returns the time at which this special task started.""" |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 2162 | return self.time_started |
| 2163 | |
| 2164 | |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2165 | @classmethod |
showard | c510344 | 2010-01-15 00:20:26 +0000 | [diff] [blame] | 2166 | def schedule_special_task(cls, host, task): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2167 | """Schedules a special task on a host if not already scheduled. |
| 2168 | |
| 2169 | @param cls: Implicit class object. |
| 2170 | @param host: The host to use. |
| 2171 | @param task: The task to schedule. |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2172 | """ |
showard | c510344 | 2010-01-15 00:20:26 +0000 | [diff] [blame] | 2173 | existing_tasks = SpecialTask.objects.filter(host__id=host.id, task=task, |
| 2174 | is_active=False, |
| 2175 | is_complete=False) |
| 2176 | if existing_tasks: |
| 2177 | return existing_tasks[0] |
| 2178 | |
| 2179 | special_task = SpecialTask(host=host, task=task, |
| 2180 | requested_by=User.current_user()) |
| 2181 | special_task.save() |
| 2182 | return special_task |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2183 | |
| 2184 | |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 2185 | def abort(self): |
| 2186 | """ Abort this special task.""" |
| 2187 | self.is_aborted = True |
| 2188 | self.save() |
| 2189 | |
| 2190 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2191 | def activate(self): |
showard | 474d136 | 2009-08-20 23:32:01 +0000 | [diff] [blame] | 2192 | """ |
| 2193 | Sets a task as active and sets the time started to the current time. |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2194 | """ |
showard | 9744688 | 2009-07-20 22:37:28 +0000 | [diff] [blame] | 2195 | logging.info('Starting: %s', self) |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2196 | self.is_active = True |
| 2197 | self.time_started = datetime.now() |
| 2198 | self.save() |
| 2199 | |
| 2200 | |
showard | e60e44e | 2009-11-13 20:45:38 +0000 | [diff] [blame] | 2201 | def finish(self, success): |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2202 | """Sets a task as completed. |
| 2203 | |
| 2204 | @param success: Whether or not the task was successful. |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2205 | """ |
showard | 9744688 | 2009-07-20 22:37:28 +0000 | [diff] [blame] | 2206 | logging.info('Finished: %s', self) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2207 | self.is_active = False |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2208 | self.is_complete = True |
showard | e60e44e | 2009-11-13 20:45:38 +0000 | [diff] [blame] | 2209 | self.success = success |
Dan Shi | d85d611 | 2014-07-14 10:32:55 -0700 | [diff] [blame] | 2210 | if self.time_started: |
| 2211 | self.time_finished = datetime.now() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2212 | self.save() |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2213 | |
| 2214 | |
| 2215 | class Meta: |
Dennis Jeffrey | 7db38ba | 2013-02-13 10:03:17 -0800 | [diff] [blame] | 2216 | """Metadata for class SpecialTask.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 2217 | db_table = 'afe_special_tasks' |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2218 | |
showard | 474d136 | 2009-08-20 23:32:01 +0000 | [diff] [blame] | 2219 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2220 | def __unicode__(self): |
| 2221 | result = u'Special Task %s (host %s, task %s, time %s)' % ( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2222 | self.id, self.host, self.task, self.time_requested) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2223 | if self.is_complete: |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2224 | result += u' (completed)' |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2225 | elif self.is_active: |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2226 | result += u' (active)' |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2227 | |
| 2228 | return result |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 2229 | |
| 2230 | |
| 2231 | class StableVersion(dbmodels.Model, model_logic.ModelExtensions): |
| 2232 | |
| 2233 | board = dbmodels.CharField(max_length=255, unique=True) |
| 2234 | version = dbmodels.CharField(max_length=255) |
| 2235 | |
| 2236 | class Meta: |
| 2237 | """Metadata for class StableVersion.""" |
Fang Deng | 8624850 | 2014-12-18 16:38:00 -0800 | [diff] [blame] | 2238 | db_table = 'afe_stable_versions' |