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