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