Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | from django.db import models |
| 3 | from django.core.validators import MaxValueValidator, MinValueValidator |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 4 | from django.utils.encoding import force_bytes |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 5 | from orm.models import Project, ProjectLayer, ProjectVariable, ProjectTarget, Build, Layer_Version |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 7 | import logging |
| 8 | logger = logging.getLogger("toaster") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | # a BuildEnvironment is the equivalent of the "build/" directory on the localhost |
| 10 | class BuildEnvironment(models.Model): |
| 11 | SERVER_STOPPED = 0 |
| 12 | SERVER_STARTED = 1 |
| 13 | SERVER_STATE = ( |
| 14 | (SERVER_STOPPED, "stopped"), |
| 15 | (SERVER_STARTED, "started"), |
| 16 | ) |
| 17 | |
| 18 | TYPE_LOCAL = 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | TYPE = ( |
| 20 | (TYPE_LOCAL, "local"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | LOCK_FREE = 0 |
| 24 | LOCK_LOCK = 1 |
| 25 | LOCK_RUNNING = 2 |
| 26 | LOCK_STATE = ( |
| 27 | (LOCK_FREE, "free"), |
| 28 | (LOCK_LOCK, "lock"), |
| 29 | (LOCK_RUNNING, "running"), |
| 30 | ) |
| 31 | |
| 32 | address = models.CharField(max_length = 254) |
| 33 | betype = models.IntegerField(choices = TYPE) |
| 34 | bbaddress = models.CharField(max_length = 254, blank = True) |
| 35 | bbport = models.IntegerField(default = -1) |
| 36 | bbtoken = models.CharField(max_length = 126, blank = True) |
| 37 | bbstate = models.IntegerField(choices = SERVER_STATE, default = SERVER_STOPPED) |
| 38 | sourcedir = models.CharField(max_length = 512, blank = True) |
| 39 | builddir = models.CharField(max_length = 512, blank = True) |
| 40 | lock = models.IntegerField(choices = LOCK_STATE, default = LOCK_FREE) |
| 41 | created = models.DateTimeField(auto_now_add = True) |
| 42 | updated = models.DateTimeField(auto_now = True) |
| 43 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | def get_artifact(self, path): |
| 45 | if self.betype == BuildEnvironment.TYPE_LOCAL: |
| 46 | return open(path, "r") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 47 | raise NotImplementedError("FIXME: artifact download not implemented "\ |
| 48 | "for build environment type %s" % \ |
| 49 | self.get_betype_display()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | |
| 51 | def has_artifact(self, path): |
| 52 | import os |
| 53 | if self.betype == BuildEnvironment.TYPE_LOCAL: |
| 54 | return os.path.exists(path) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 55 | raise NotImplementedError("FIXME: has artifact not implemented for "\ |
| 56 | "build environment type %s" % \ |
| 57 | self.get_betype_display()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
| 59 | # a BuildRequest is a request that the scheduler will build using a BuildEnvironment |
| 60 | # the build request queue is the table itself, ordered by state |
| 61 | |
| 62 | class BuildRequest(models.Model): |
| 63 | REQ_CREATED = 0 |
| 64 | REQ_QUEUED = 1 |
| 65 | REQ_INPROGRESS = 2 |
| 66 | REQ_COMPLETED = 3 |
| 67 | REQ_FAILED = 4 |
| 68 | REQ_DELETED = 5 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | REQ_CANCELLING = 6 |
| 70 | REQ_ARCHIVE = 7 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | |
| 72 | REQUEST_STATE = ( |
| 73 | (REQ_CREATED, "created"), |
| 74 | (REQ_QUEUED, "queued"), |
| 75 | (REQ_INPROGRESS, "in progress"), |
| 76 | (REQ_COMPLETED, "completed"), |
| 77 | (REQ_FAILED, "failed"), |
| 78 | (REQ_DELETED, "deleted"), |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 79 | (REQ_CANCELLING, "cancelling"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | (REQ_ARCHIVE, "archive"), |
| 81 | ) |
| 82 | |
| 83 | search_allowed_fields = ("brtarget__target", "build__project__name") |
| 84 | |
| 85 | project = models.ForeignKey(Project) |
| 86 | build = models.OneToOneField(Build, null = True) # TODO: toasterui should set this when Build is created |
| 87 | environment = models.ForeignKey(BuildEnvironment, null = True) |
| 88 | state = models.IntegerField(choices = REQUEST_STATE, default = REQ_CREATED) |
| 89 | created = models.DateTimeField(auto_now_add = True) |
| 90 | updated = models.DateTimeField(auto_now = True) |
| 91 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 | def __init__(self, *args, **kwargs): |
| 93 | super(BuildRequest, self).__init__(*args, **kwargs) |
| 94 | # Save the old state incase it's about to be modified |
| 95 | self.old_state = self.state |
| 96 | |
| 97 | def save(self, *args, **kwargs): |
| 98 | # Check that the state we're trying to set is not going backwards |
| 99 | # e.g. from REQ_FAILED to REQ_INPROGRESS |
| 100 | if self.old_state != self.state and self.old_state > self.state: |
| 101 | logger.warn("Invalid state change requested: " |
| 102 | "Cannot go from %s to %s - ignoring request" % |
| 103 | (BuildRequest.REQUEST_STATE[self.old_state][1], |
| 104 | BuildRequest.REQUEST_STATE[self.state][1]) |
| 105 | ) |
| 106 | # Set property back to the old value |
| 107 | self.state = self.old_state |
| 108 | return |
| 109 | |
| 110 | super(BuildRequest, self).save(*args, **kwargs) |
| 111 | |
| 112 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | def get_duration(self): |
| 114 | return (self.updated - self.created).total_seconds() |
| 115 | |
| 116 | def get_sorted_target_list(self): |
| 117 | tgts = self.brtarget_set.order_by( 'target' ); |
| 118 | return( tgts ); |
| 119 | |
| 120 | def get_machine(self): |
| 121 | return self.brvariable_set.get(name="MACHINE").value |
| 122 | |
| 123 | def __str__(self): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 124 | return force_bytes('%s %s' % (self.project, self.get_state_display())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
| 126 | # These tables specify the settings for running an actual build. |
| 127 | # They MUST be kept in sync with the tables in orm.models.Project* |
| 128 | |
| 129 | class BRLayer(models.Model): |
| 130 | req = models.ForeignKey(BuildRequest) |
| 131 | name = models.CharField(max_length = 100) |
| 132 | giturl = models.CharField(max_length = 254) |
| 133 | commit = models.CharField(max_length = 254) |
| 134 | dirpath = models.CharField(max_length = 254) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 135 | layer_version = models.ForeignKey(Layer_Version, null=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | |
| 137 | class BRBitbake(models.Model): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 138 | req = models.OneToOneField(BuildRequest) # only one bitbake for a request |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 139 | giturl = models.CharField(max_length =254) |
| 140 | commit = models.CharField(max_length = 254) |
| 141 | dirpath = models.CharField(max_length = 254) |
| 142 | |
| 143 | class BRVariable(models.Model): |
| 144 | req = models.ForeignKey(BuildRequest) |
| 145 | name = models.CharField(max_length=100) |
| 146 | value = models.TextField(blank = True) |
| 147 | |
| 148 | class BRTarget(models.Model): |
| 149 | req = models.ForeignKey(BuildRequest) |
| 150 | target = models.CharField(max_length=100) |
| 151 | task = models.CharField(max_length=100, null=True) |
| 152 | |
| 153 | class BRError(models.Model): |
| 154 | req = models.ForeignKey(BuildRequest) |
| 155 | errtype = models.CharField(max_length=100) |
| 156 | errmsg = models.TextField() |
| 157 | traceback = models.TextField() |
| 158 | |
| 159 | def __str__(self): |
| 160 | return "%s (%s)" % (self.errmsg, self.req) |