showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 1 | from datetime import datetime |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 2 | from django.db import models as dbmodels, connection |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 3 | from frontend.afe import model_logic |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 4 | from frontend import settings, thread_local |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import enum, host_protections |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 6 | |
| 7 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 8 | class AclAccessViolation(Exception): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 9 | """\ |
| 10 | Raised when an operation is attempted with proper permissions as |
| 11 | dictated by ACLs. |
| 12 | """ |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 13 | |
| 14 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 15 | class Label(model_logic.ModelWithInvalid, dbmodels.Model): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 16 | """\ |
| 17 | Required: |
| 18 | name: label name |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 19 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 20 | Optional: |
| 21 | kernel_config: url/path to kernel config to use for jobs run on this |
| 22 | label |
| 23 | platform: if True, this is a platform label (defaults to False) |
| 24 | """ |
| 25 | name = dbmodels.CharField(maxlength=255, unique=True) |
| 26 | kernel_config = dbmodels.CharField(maxlength=255, blank=True) |
| 27 | platform = dbmodels.BooleanField(default=False) |
| 28 | invalid = dbmodels.BooleanField(default=False, |
| 29 | editable=settings.FULL_ADMIN) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 30 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 31 | name_field = 'name' |
| 32 | objects = model_logic.ExtendedManager() |
| 33 | valid_objects = model_logic.ValidObjectsManager() |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 34 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 35 | def clean_object(self): |
| 36 | self.host_set.clear() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 37 | |
| 38 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | def enqueue_job(self, job): |
| 40 | 'Enqueue a job on any host of this label.' |
| 41 | queue_entry = HostQueueEntry(meta_host=self, job=job, |
| 42 | status=Job.Status.QUEUED, |
| 43 | priority=job.priority) |
| 44 | queue_entry.save() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 45 | |
| 46 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 47 | class Meta: |
| 48 | db_table = 'labels' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 49 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 50 | class Admin: |
| 51 | list_display = ('name', 'kernel_config') |
| 52 | # see Host.Admin |
| 53 | manager = model_logic.ValidObjectsManager() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 54 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 55 | def __str__(self): |
| 56 | return self.name |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 57 | |
| 58 | |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 59 | class User(dbmodels.Model, model_logic.ModelExtensions): |
| 60 | """\ |
| 61 | Required: |
| 62 | login :user login name |
| 63 | |
| 64 | Optional: |
| 65 | access_level: 0=User (default), 1=Admin, 100=Root |
| 66 | """ |
| 67 | ACCESS_ROOT = 100 |
| 68 | ACCESS_ADMIN = 1 |
| 69 | ACCESS_USER = 0 |
| 70 | |
| 71 | login = dbmodels.CharField(maxlength=255, unique=True) |
| 72 | access_level = dbmodels.IntegerField(default=ACCESS_USER, blank=True) |
| 73 | |
| 74 | name_field = 'login' |
| 75 | objects = model_logic.ExtendedManager() |
| 76 | |
| 77 | |
| 78 | def save(self): |
| 79 | # is this a new object being saved for the first time? |
| 80 | first_time = (self.id is None) |
| 81 | user = thread_local.get_user() |
| 82 | if user and not user.is_superuser(): |
| 83 | raise AclAccessViolation("You cannot modify users!") |
| 84 | super(User, self).save() |
| 85 | if first_time: |
| 86 | everyone = AclGroup.objects.get(name='Everyone') |
| 87 | everyone.users.add(self) |
| 88 | |
| 89 | |
| 90 | def is_superuser(self): |
| 91 | return self.access_level >= self.ACCESS_ROOT |
| 92 | |
| 93 | |
| 94 | class Meta: |
| 95 | db_table = 'users' |
| 96 | |
| 97 | class Admin: |
| 98 | list_display = ('login', 'access_level') |
| 99 | search_fields = ('login',) |
| 100 | |
| 101 | def __str__(self): |
| 102 | return self.login |
| 103 | |
| 104 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 105 | class Host(model_logic.ModelWithInvalid, dbmodels.Model): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 106 | """\ |
| 107 | Required: |
| 108 | hostname |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 109 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 110 | optional: |
| 111 | locked: host is locked and will not be queued |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 112 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 113 | Internal: |
| 114 | synch_id: currently unused |
| 115 | status: string describing status of host |
| 116 | """ |
| 117 | Status = enum.Enum('Verifying', 'Running', 'Ready', 'Repairing', |
| 118 | 'Repair Failed', 'Dead', 'Rebooting', |
| 119 | string_values=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 120 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 121 | hostname = dbmodels.CharField(maxlength=255, unique=True) |
| 122 | labels = dbmodels.ManyToManyField(Label, blank=True, |
| 123 | filter_interface=dbmodels.HORIZONTAL) |
| 124 | locked = dbmodels.BooleanField(default=False) |
| 125 | synch_id = dbmodels.IntegerField(blank=True, null=True, |
| 126 | editable=settings.FULL_ADMIN) |
| 127 | status = dbmodels.CharField(maxlength=255, default=Status.READY, |
| 128 | choices=Status.choices(), |
| 129 | editable=settings.FULL_ADMIN) |
| 130 | invalid = dbmodels.BooleanField(default=False, |
| 131 | editable=settings.FULL_ADMIN) |
showard | d5afc2f | 2008-08-12 17:19:44 +0000 | [diff] [blame] | 132 | protection = dbmodels.SmallIntegerField(null=False, blank=True, |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 133 | choices=host_protections.choices, |
| 134 | default=host_protections.default) |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 135 | locked_by = dbmodels.ForeignKey(User, null=True, blank=True, editable=False) |
| 136 | lock_time = dbmodels.DateTimeField(null=True, blank=True, editable=False) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 137 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 138 | name_field = 'hostname' |
| 139 | objects = model_logic.ExtendedManager() |
| 140 | valid_objects = model_logic.ValidObjectsManager() |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 141 | |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 142 | @staticmethod |
| 143 | def create_one_time_host(hostname): |
| 144 | query = Host.objects.filter(hostname=hostname) |
| 145 | if query.count() == 0: |
| 146 | host = Host(hostname=hostname, invalid=True) |
showard | a8411af | 2008-08-07 22:35:58 +0000 | [diff] [blame] | 147 | host.do_validate() |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 148 | else: |
| 149 | host = query[0] |
| 150 | if not host.invalid: |
| 151 | raise model_logic.ValidationError({ |
| 152 | 'hostname' : '%s already exists!' % hostname |
| 153 | }) |
| 154 | host.clean_object() |
showard | e65d2af | 2008-07-30 23:34:21 +0000 | [diff] [blame] | 155 | AclGroup.objects.get(name='Everyone').hosts.add(host) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 156 | host.status = Host.Status.READY |
showard | 1ab512b | 2008-07-30 23:39:04 +0000 | [diff] [blame] | 157 | host.protection = host_protections.Protection.DO_NOT_REPAIR |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 158 | host.save() |
| 159 | return host |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 160 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 161 | def clean_object(self): |
| 162 | self.aclgroup_set.clear() |
| 163 | self.labels.clear() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 164 | |
| 165 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 166 | def save(self): |
| 167 | # extra spaces in the hostname can be a sneaky source of errors |
| 168 | self.hostname = self.hostname.strip() |
| 169 | # is this a new object being saved for the first time? |
| 170 | first_time = (self.id is None) |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 171 | if not first_time: |
| 172 | AclGroup.check_for_acl_violation_hosts([self]) |
showard | fb2a7fa | 2008-07-17 17:04:12 +0000 | [diff] [blame] | 173 | if self.locked and not self.locked_by: |
| 174 | self.locked_by = thread_local.get_user() |
| 175 | self.lock_time = datetime.now() |
| 176 | elif not self.locked and self.locked_by: |
| 177 | self.locked_by = None |
| 178 | self.lock_time = None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 179 | super(Host, self).save() |
| 180 | if first_time: |
| 181 | everyone = AclGroup.objects.get(name='Everyone') |
| 182 | everyone.hosts.add(self) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 183 | |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 184 | def delete(self): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 185 | AclGroup.check_for_acl_violation_hosts([self]) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 186 | for queue_entry in self.hostqueueentry_set.all(): |
| 187 | queue_entry.deleted = True |
| 188 | queue_entry.abort() |
| 189 | super(Host, self).delete() |
| 190 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 191 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 192 | def enqueue_job(self, job): |
| 193 | ' Enqueue a job on this host.' |
| 194 | queue_entry = HostQueueEntry(host=self, job=job, |
| 195 | status=Job.Status.QUEUED, |
| 196 | priority=job.priority) |
| 197 | # allow recovery of dead hosts from the frontend |
| 198 | if not self.active_queue_entry() and self.is_dead(): |
| 199 | self.status = Host.Status.READY |
| 200 | self.save() |
| 201 | queue_entry.save() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 202 | |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 203 | block = IneligibleHostQueue(job=job, host=self) |
| 204 | block.save() |
| 205 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 206 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 207 | def platform(self): |
| 208 | # TODO(showard): slighly hacky? |
| 209 | platforms = self.labels.filter(platform=True) |
| 210 | if len(platforms) == 0: |
| 211 | return None |
| 212 | return platforms[0] |
| 213 | platform.short_description = 'Platform' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 214 | |
| 215 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 216 | def is_dead(self): |
| 217 | return self.status == Host.Status.REPAIR_FAILED |
mbligh | 3cab4a7 | 2008-03-05 23:19:09 +0000 | [diff] [blame] | 218 | |
| 219 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 220 | def active_queue_entry(self): |
| 221 | active = list(self.hostqueueentry_set.filter(active=True)) |
| 222 | if not active: |
| 223 | return None |
| 224 | assert len(active) == 1, ('More than one active entry for ' |
| 225 | 'host ' + self.hostname) |
| 226 | return active[0] |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 227 | |
| 228 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 229 | class Meta: |
| 230 | db_table = 'hosts' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 231 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 232 | class Admin: |
| 233 | # TODO(showard) - showing platform requires a SQL query for |
| 234 | # each row (since labels are many-to-many) - should we remove |
| 235 | # it? |
| 236 | list_display = ('hostname', 'platform', 'locked', 'status') |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 237 | list_filter = ('labels', 'locked', 'protection') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 238 | search_fields = ('hostname', 'status') |
| 239 | # undocumented Django feature - if you set manager here, the |
| 240 | # admin code will use it, otherwise it'll use a default Manager |
| 241 | manager = model_logic.ValidObjectsManager() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 242 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 243 | def __str__(self): |
| 244 | return self.hostname |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 245 | |
| 246 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 247 | class Test(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 248 | """\ |
| 249 | Required: |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 250 | author: author name |
| 251 | description: description of the test |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 252 | name: test name |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 253 | time: short, medium, long |
| 254 | test_class: This describes the class for your the test belongs in. |
| 255 | test_category: This describes the category for your tests |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 256 | test_type: Client or Server |
| 257 | path: path to pass to run_test() |
| 258 | synch_type: whether the test should run synchronously or asynchronously |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 259 | sync_count: is a number >=1 (1 being the default). If it's 1, then it's an |
| 260 | async job. If it's >1 it's sync job for that number of machines |
| 261 | i.e. if sync_count = 2 it is a sync job that requires two |
| 262 | machines. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 263 | Optional: |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 264 | dependencies: What the test requires to run. Comma deliminated list |
| 265 | experimental: If this is set to True production servers will ignore the test |
| 266 | run_verify: Whether or not the scheduler should run the verify stage |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 267 | """ |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 268 | TestTime = enum.Enum('SHORT', 'MEDIUM', 'LONG', start_value=1) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 269 | SynchType = enum.Enum('Asynchronous', 'Synchronous', start_value=1) |
| 270 | # TODO(showard) - this should be merged with Job.ControlType (but right |
| 271 | # now they use opposite values) |
| 272 | Types = enum.Enum('Client', 'Server', start_value=1) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 273 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 274 | name = dbmodels.CharField(maxlength=255, unique=True) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 275 | author = dbmodels.CharField(maxlength=255) |
| 276 | test_class = dbmodels.CharField(maxlength=255) |
| 277 | test_category = dbmodels.CharField(maxlength=255) |
| 278 | dependencies = dbmodels.CharField(maxlength=255, blank=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 279 | description = dbmodels.TextField(blank=True) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 280 | experimental = dbmodels.BooleanField(default=True) |
| 281 | run_verify = dbmodels.BooleanField(default=True) |
| 282 | test_time = dbmodels.SmallIntegerField(choices=TestTime.choices(), |
| 283 | default=TestTime.MEDIUM) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 284 | test_type = dbmodels.SmallIntegerField(choices=Types.choices()) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 285 | sync_count = dbmodels.IntegerField(default=1) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 286 | synch_type = dbmodels.SmallIntegerField(choices=SynchType.choices(), |
| 287 | default=SynchType.ASYNCHRONOUS) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 288 | path = dbmodels.CharField(maxlength=255, unique=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 289 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 290 | name_field = 'name' |
| 291 | objects = model_logic.ExtendedManager() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 292 | |
| 293 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 294 | class Meta: |
| 295 | db_table = 'autotests' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 296 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 297 | class Admin: |
| 298 | fields = ( |
| 299 | (None, {'fields' : |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 300 | ('name', 'author', 'test_category', 'test_class', |
| 301 | 'test_time', 'synch_type', 'test_type', 'sync_count', |
| 302 | 'path', 'dependencies', 'experimental', 'run_verify', |
| 303 | 'description')}), |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 304 | ) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 305 | list_display = ('name', 'test_type', 'description', 'synch_type') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 306 | search_fields = ('name',) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 307 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 308 | def __str__(self): |
| 309 | return self.name |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 310 | |
| 311 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 312 | class Profiler(dbmodels.Model, model_logic.ModelExtensions): |
| 313 | """\ |
| 314 | Required: |
| 315 | name: profiler name |
| 316 | test_type: Client or Server |
| 317 | |
| 318 | Optional: |
| 319 | description: arbirary text description |
| 320 | """ |
| 321 | name = dbmodels.CharField(maxlength=255, unique=True) |
| 322 | description = dbmodels.TextField(blank=True) |
| 323 | |
| 324 | name_field = 'name' |
| 325 | objects = model_logic.ExtendedManager() |
| 326 | |
| 327 | |
| 328 | class Meta: |
| 329 | db_table = 'profilers' |
| 330 | |
| 331 | class Admin: |
| 332 | list_display = ('name', 'description') |
| 333 | search_fields = ('name',) |
| 334 | |
| 335 | def __str__(self): |
| 336 | return self.name |
| 337 | |
| 338 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 339 | class AclGroup(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 340 | """\ |
| 341 | Required: |
| 342 | name: name of ACL group |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 343 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 344 | Optional: |
| 345 | description: arbitrary description of group |
| 346 | """ |
| 347 | name = dbmodels.CharField(maxlength=255, unique=True) |
| 348 | description = dbmodels.CharField(maxlength=255, blank=True) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 349 | users = dbmodels.ManyToManyField(User, blank=True, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 350 | filter_interface=dbmodels.HORIZONTAL) |
| 351 | hosts = dbmodels.ManyToManyField(Host, |
| 352 | filter_interface=dbmodels.HORIZONTAL) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 353 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 354 | name_field = 'name' |
| 355 | objects = model_logic.ExtendedManager() |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 356 | |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 357 | @staticmethod |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 358 | def check_for_acl_violation_hosts(hosts): |
| 359 | user = thread_local.get_user() |
| 360 | if user.is_superuser(): |
| 361 | return None |
| 362 | accessible_host_ids = set( |
| 363 | host.id for host in Host.objects.filter(acl_group__users=user)) |
| 364 | for host in hosts: |
| 365 | # Check if the user has access to this host, |
| 366 | # but only if it is not a metahost |
| 367 | if (isinstance(host, Host) |
| 368 | and int(host.id) not in accessible_host_ids): |
| 369 | raise AclAccessViolation("You do not have access to %s" |
| 370 | % str(host)) |
| 371 | |
| 372 | def check_for_acl_violation_acl_group(self): |
| 373 | user = thread_local.get_user() |
| 374 | if user.is_superuser(): |
| 375 | return None |
| 376 | if not user in self.users.all(): |
| 377 | raise AclAccessViolation("You do not have access to %s" |
| 378 | % self.name) |
| 379 | |
| 380 | @staticmethod |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 381 | def on_host_membership_change(): |
| 382 | everyone = AclGroup.objects.get(name='Everyone') |
| 383 | |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 384 | # 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] | 385 | # TODO(showard): this is a bit of a hack, since the fact that this query |
| 386 | # works is kind of a coincidence of Django internals. This trick |
| 387 | # doesn't work in general (on all foreign key relationships). I'll |
| 388 | # replace it with a better technique when the need arises. |
| 389 | orphaned_hosts = Host.valid_objects.filter(acl_group__id__isnull=True) |
| 390 | everyone.hosts.add(*orphaned_hosts.distinct()) |
| 391 | |
| 392 | # find hosts in both Everyone and another ACL group, and remove them |
| 393 | # from Everyone |
| 394 | hosts_in_everyone = Host.valid_objects.filter_custom_join( |
| 395 | '_everyone', acl_group__name='Everyone') |
| 396 | acled_hosts = hosts_in_everyone.exclude(acl_group__name='Everyone') |
| 397 | everyone.hosts.remove(*acled_hosts.distinct()) |
| 398 | |
| 399 | |
| 400 | def delete(self): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 401 | if (self.name == 'Everyone'): |
| 402 | raise AclAccessViolation("You cannot delete 'Everyone'!") |
| 403 | self.check_for_acl_violation_acl_group() |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 404 | super(AclGroup, self).delete() |
| 405 | self.on_host_membership_change() |
| 406 | |
| 407 | |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 408 | def add_current_user_if_empty(self): |
| 409 | if not self.users.count(): |
| 410 | self.users.add(thread_local.get_user()) |
| 411 | |
| 412 | |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 413 | # if you have a model attribute called "Manipulator", Django will |
| 414 | # automatically insert it into the beginning of the superclass list |
| 415 | # for the model's manipulators |
| 416 | class Manipulator(object): |
| 417 | """ |
| 418 | Custom manipulator to get notification when ACLs are changed through |
| 419 | the admin interface. |
| 420 | """ |
| 421 | def save(self, new_data): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 422 | user = thread_local.get_user() |
showard | 31c570e | 2008-07-23 19:29:17 +0000 | [diff] [blame] | 423 | if hasattr(self, 'original_object'): |
| 424 | if (not user.is_superuser() |
| 425 | and self.original_object.name == 'Everyone'): |
| 426 | raise AclAccessViolation("You cannot modify 'Everyone'!") |
| 427 | self.original_object.check_for_acl_violation_acl_group() |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 428 | obj = super(AclGroup.Manipulator, self).save(new_data) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 429 | if not hasattr(self, 'original_object'): |
| 430 | obj.users.add(thread_local.get_user()) |
| 431 | obj.add_current_user_if_empty() |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 432 | obj.on_host_membership_change() |
| 433 | return obj |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 434 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 435 | class Meta: |
| 436 | db_table = 'acl_groups' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 437 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 438 | class Admin: |
| 439 | list_display = ('name', 'description') |
| 440 | search_fields = ('name',) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 441 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 442 | def __str__(self): |
| 443 | return self.name |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 444 | |
| 445 | # hack to make the column name in the many-to-many DB tables match the one |
| 446 | # generated by ruby |
| 447 | AclGroup._meta.object_name = 'acl_group' |
| 448 | |
| 449 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 450 | class JobManager(model_logic.ExtendedManager): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 451 | 'Custom manager to provide efficient status counts querying.' |
| 452 | def get_status_counts(self, job_ids): |
| 453 | """\ |
| 454 | Returns a dictionary mapping the given job IDs to their status |
| 455 | count dictionaries. |
| 456 | """ |
| 457 | if not job_ids: |
| 458 | return {} |
| 459 | id_list = '(%s)' % ','.join(str(job_id) for job_id in job_ids) |
| 460 | cursor = connection.cursor() |
| 461 | cursor.execute(""" |
| 462 | SELECT job_id, status, COUNT(*) |
| 463 | FROM host_queue_entries |
| 464 | WHERE job_id IN %s |
| 465 | GROUP BY job_id, status |
| 466 | """ % id_list) |
| 467 | all_job_counts = {} |
| 468 | for job_id in job_ids: |
| 469 | all_job_counts[job_id] = {} |
| 470 | for job_id, status, count in cursor.fetchall(): |
| 471 | all_job_counts[job_id][status] = count |
| 472 | return all_job_counts |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 473 | |
| 474 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 475 | class Job(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 476 | """\ |
| 477 | owner: username of job owner |
| 478 | name: job name (does not have to be unique) |
| 479 | priority: Low, Medium, High, Urgent (or 0-3) |
| 480 | control_file: contents of control file |
| 481 | control_type: Client or Server |
| 482 | created_on: date of job creation |
| 483 | submitted_on: date of job submission |
| 484 | synch_type: Asynchronous or Synchronous (i.e. job must run on all hosts |
| 485 | simultaneously; used for server-side control files) |
| 486 | synch_count: ??? |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 487 | run_verify: Whether or not to run the verify phase |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 488 | synchronizing: for scheduler use |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 489 | timeout: hours until job times out |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame^] | 490 | email_list: list of people to email on completion delimited by any of: |
| 491 | white space, ',', ':', ';' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 492 | """ |
| 493 | Priority = enum.Enum('Low', 'Medium', 'High', 'Urgent') |
| 494 | ControlType = enum.Enum('Server', 'Client', start_value=1) |
| 495 | Status = enum.Enum('Created', 'Queued', 'Pending', 'Running', |
| 496 | 'Completed', 'Abort', 'Aborting', 'Aborted', |
showard | d823b36 | 2008-07-24 16:35:46 +0000 | [diff] [blame] | 497 | 'Failed', 'Starting', string_values=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 498 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 499 | owner = dbmodels.CharField(maxlength=255) |
| 500 | name = dbmodels.CharField(maxlength=255) |
| 501 | priority = dbmodels.SmallIntegerField(choices=Priority.choices(), |
| 502 | blank=True, # to allow 0 |
| 503 | default=Priority.MEDIUM) |
| 504 | control_file = dbmodels.TextField() |
| 505 | control_type = dbmodels.SmallIntegerField(choices=ControlType.choices(), |
| 506 | blank=True) # to allow 0 |
| 507 | created_on = dbmodels.DateTimeField(auto_now_add=True) |
| 508 | synch_type = dbmodels.SmallIntegerField( |
| 509 | blank=True, null=True, choices=Test.SynchType.choices()) |
| 510 | synch_count = dbmodels.IntegerField(blank=True, null=True) |
| 511 | synchronizing = dbmodels.BooleanField(default=False) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 512 | run_verify = dbmodels.BooleanField(default=True) |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 513 | timeout = dbmodels.IntegerField() |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame^] | 514 | email_list = dbmodels.CharField(maxlength=250, blank=True) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 515 | |
| 516 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 517 | # custom manager |
| 518 | objects = JobManager() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 519 | |
| 520 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 521 | def is_server_job(self): |
| 522 | return self.control_type == self.ControlType.SERVER |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 523 | |
| 524 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 525 | @classmethod |
| 526 | def create(cls, owner, name, priority, control_file, control_type, |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame^] | 527 | hosts, synch_type, timeout, run_verify, email_list): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 528 | """\ |
| 529 | Creates a job by taking some information (the listed args) |
| 530 | and filling in the rest of the necessary information. |
| 531 | """ |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 532 | AclGroup.check_for_acl_violation_hosts(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 533 | job = cls.add_object( |
| 534 | owner=owner, name=name, priority=priority, |
| 535 | control_file=control_file, control_type=control_type, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 536 | synch_type=synch_type, timeout=timeout, |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame^] | 537 | run_verify=run_verify, email_list=email_list) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 538 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 539 | if job.synch_type == Test.SynchType.SYNCHRONOUS: |
| 540 | job.synch_count = len(hosts) |
| 541 | else: |
| 542 | if len(hosts) == 0: |
| 543 | errors = {'hosts': |
| 544 | 'asynchronous jobs require at least' |
| 545 | + ' one host to run on'} |
| 546 | raise model_logic.ValidationError(errors) |
| 547 | job.save() |
| 548 | return job |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 549 | |
| 550 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 551 | def queue(self, hosts): |
| 552 | 'Enqueue a job on the given hosts.' |
| 553 | for host in hosts: |
| 554 | host.enqueue_job(self) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 555 | |
| 556 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 557 | def abort(self): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 558 | user = thread_local.get_user() |
| 559 | if not user.is_superuser() and user.login != self.owner: |
| 560 | raise AclAccessViolation("You cannot abort other people's jobs!") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 561 | for queue_entry in self.hostqueueentry_set.all(): |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 562 | queue_entry.abort() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 563 | |
| 564 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 565 | def user(self): |
| 566 | try: |
| 567 | return User.objects.get(login=self.owner) |
| 568 | except self.DoesNotExist: |
| 569 | return None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 570 | |
| 571 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 572 | class Meta: |
| 573 | db_table = 'jobs' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 574 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 575 | if settings.FULL_ADMIN: |
| 576 | class Admin: |
| 577 | list_display = ('id', 'owner', 'name', 'control_type') |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 578 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 579 | def __str__(self): |
| 580 | return '%s (%s-%s)' % (self.name, self.id, self.owner) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 581 | |
| 582 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 583 | class IneligibleHostQueue(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 584 | job = dbmodels.ForeignKey(Job) |
| 585 | host = dbmodels.ForeignKey(Host) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 586 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 587 | objects = model_logic.ExtendedManager() |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 588 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 589 | class Meta: |
| 590 | db_table = 'ineligible_host_queues' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 591 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 592 | if settings.FULL_ADMIN: |
| 593 | class Admin: |
| 594 | list_display = ('id', 'job', 'host') |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 595 | |
| 596 | |
showard | 7c78528 | 2008-05-29 19:45:12 +0000 | [diff] [blame] | 597 | class HostQueueEntry(dbmodels.Model, model_logic.ModelExtensions): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 598 | job = dbmodels.ForeignKey(Job) |
| 599 | host = dbmodels.ForeignKey(Host, blank=True, null=True) |
| 600 | priority = dbmodels.SmallIntegerField() |
| 601 | status = dbmodels.CharField(maxlength=255) |
| 602 | meta_host = dbmodels.ForeignKey(Label, blank=True, null=True, |
| 603 | db_column='meta_host') |
| 604 | active = dbmodels.BooleanField(default=False) |
| 605 | complete = dbmodels.BooleanField(default=False) |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 606 | deleted = dbmodels.BooleanField(default=False) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 607 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 608 | objects = model_logic.ExtendedManager() |
showard | eb3be4d | 2008-04-21 20:59:26 +0000 | [diff] [blame] | 609 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 610 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 611 | def is_meta_host_entry(self): |
| 612 | 'True if this is a entry has a meta_host instead of a host.' |
| 613 | return self.host is None and self.meta_host is not None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 614 | |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 615 | def abort(self): |
| 616 | if self.active: |
| 617 | self.status = Job.Status.ABORT |
| 618 | elif not self.complete: |
| 619 | self.status = Job.Status.ABORTED |
| 620 | self.active = False |
| 621 | self.complete = True |
| 622 | self.save() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 623 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 624 | class Meta: |
| 625 | db_table = 'host_queue_entries' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 626 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 627 | if settings.FULL_ADMIN: |
| 628 | class Admin: |
| 629 | list_display = ('id', 'job', 'host', 'status', |
| 630 | 'meta_host') |