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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 4 | from django.utils.encoding import force_text |
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 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | REQ_FAILED = 3 |
| 67 | REQ_DELETED = 4 |
| 68 | REQ_CANCELLING = 5 |
| 69 | REQ_COMPLETED = 6 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | (REQ_FAILED, "failed"), |
| 77 | (REQ_DELETED, "deleted"), |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 78 | (REQ_CANCELLING, "cancelling"), |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 79 | (REQ_COMPLETED, "completed"), |
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) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 94 | # Save the old state in case it's about to be modified |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 101 | logger.warning("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 | ) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 124 | return force_text('%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 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 129 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | class BRLayer(models.Model): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 131 | req = models.ForeignKey(BuildRequest) |
| 132 | name = models.CharField(max_length=100) |
| 133 | giturl = models.CharField(max_length=254, null=True) |
| 134 | local_source_dir = models.CharField(max_length=254, null=True) |
| 135 | commit = models.CharField(max_length=254, null=True) |
| 136 | dirpath = models.CharField(max_length=254, null=True) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 137 | layer_version = models.ForeignKey(Layer_Version, null=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | |
| 139 | class BRBitbake(models.Model): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 140 | req = models.OneToOneField(BuildRequest) # only one bitbake for a request |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | giturl = models.CharField(max_length =254) |
| 142 | commit = models.CharField(max_length = 254) |
| 143 | dirpath = models.CharField(max_length = 254) |
| 144 | |
| 145 | class BRVariable(models.Model): |
| 146 | req = models.ForeignKey(BuildRequest) |
| 147 | name = models.CharField(max_length=100) |
| 148 | value = models.TextField(blank = True) |
| 149 | |
| 150 | class BRTarget(models.Model): |
| 151 | req = models.ForeignKey(BuildRequest) |
| 152 | target = models.CharField(max_length=100) |
| 153 | task = models.CharField(max_length=100, null=True) |
| 154 | |
| 155 | class BRError(models.Model): |
| 156 | req = models.ForeignKey(BuildRequest) |
| 157 | errtype = models.CharField(max_length=100) |
| 158 | errmsg = models.TextField() |
| 159 | traceback = models.TextField() |
| 160 | |
| 161 | def __str__(self): |
| 162 | return "%s (%s)" % (self.errmsg, self.req) |