blob: 409614b9e74028c80e6dd58c4eb65ef3b33843ea [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001from __future__ import unicode_literals
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002from django.db import models
3from django.core.validators import MaxValueValidator, MinValueValidator
Patrick Williamsc0f7c042017-02-23 20:41:17 -06004from django.utils.encoding import force_text
Patrick Williamsf1e5d692016-03-30 15:21:19 -05005from orm.models import Project, ProjectLayer, ProjectVariable, ProjectTarget, Build, Layer_Version
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007import logging
8logger = logging.getLogger("toaster")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009# a BuildEnvironment is the equivalent of the "build/" directory on the localhost
10class 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 Williamsc124f4f2015-09-15 14:41:29 -050019 TYPE = (
20 (TYPE_LOCAL, "local"),
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 )
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 Williamsc124f4f2015-09-15 14:41:29 -050044 def get_artifact(self, path):
45 if self.betype == BuildEnvironment.TYPE_LOCAL:
46 return open(path, "r")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 raise NotImplementedError("FIXME: artifact download not implemented "\
48 "for build environment type %s" % \
49 self.get_betype_display())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
51 def has_artifact(self, path):
52 import os
53 if self.betype == BuildEnvironment.TYPE_LOCAL:
54 return os.path.exists(path)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050055 raise NotImplementedError("FIXME: has artifact not implemented for "\
56 "build environment type %s" % \
57 self.get_betype_display())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058
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
62class BuildRequest(models.Model):
63 REQ_CREATED = 0
64 REQ_QUEUED = 1
65 REQ_INPROGRESS = 2
Patrick Williamsc0f7c042017-02-23 20:41:17 -060066 REQ_FAILED = 3
67 REQ_DELETED = 4
68 REQ_CANCELLING = 5
69 REQ_COMPLETED = 6
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 REQ_ARCHIVE = 7
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071
72 REQUEST_STATE = (
73 (REQ_CREATED, "created"),
74 (REQ_QUEUED, "queued"),
75 (REQ_INPROGRESS, "in progress"),
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 (REQ_FAILED, "failed"),
77 (REQ_DELETED, "deleted"),
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050078 (REQ_CANCELLING, "cancelling"),
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079 (REQ_COMPLETED, "completed"),
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 (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 Williamsd8c66bc2016-06-20 12:57:21 -050092 def __init__(self, *args, **kwargs):
93 super(BuildRequest, self).__init__(*args, **kwargs)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060094 # Save the old state in case it's about to be modified
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050095 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 Williamsc0f7c042017-02-23 20:41:17 -0600101 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 Williamsd8c66bc2016-06-20 12:57:21 -0500106 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500113 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 Williamsc0f7c042017-02-23 20:41:17 -0600124 return force_text('%s %s' % (self.project, self.get_state_display()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125
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 Williamsc0f7c042017-02-23 20:41:17 -0600129
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130class BRLayer(models.Model):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131 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 Williamsf1e5d692016-03-30 15:21:19 -0500137 layer_version = models.ForeignKey(Layer_Version, null=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138
139class BRBitbake(models.Model):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500140 req = models.OneToOneField(BuildRequest) # only one bitbake for a request
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 giturl = models.CharField(max_length =254)
142 commit = models.CharField(max_length = 254)
143 dirpath = models.CharField(max_length = 254)
144
145class BRVariable(models.Model):
146 req = models.ForeignKey(BuildRequest)
147 name = models.CharField(max_length=100)
148 value = models.TextField(blank = True)
149
150class 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
155class 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)