blob: cb49a58c451ef2aba9d91fd8b020304c109ac61e [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 Williamsd8c66bc2016-06-20 12:57:21 -05004from django.utils.encoding import force_bytes
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
66 REQ_COMPLETED = 3
67 REQ_FAILED = 4
68 REQ_DELETED = 5
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069 REQ_CANCELLING = 6
70 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"),
76 (REQ_COMPLETED, "completed"),
77 (REQ_FAILED, "failed"),
78 (REQ_DELETED, "deleted"),
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050079 (REQ_CANCELLING, "cancelling"),
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)
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 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 Williamsd8c66bc2016-06-20 12:57:21 -0500124 return force_bytes('%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
129class 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 Williamsf1e5d692016-03-30 15:21:19 -0500135 layer_version = models.ForeignKey(Layer_Version, null=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136
137class BRBitbake(models.Model):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500138 req = models.OneToOneField(BuildRequest) # only one bitbake for a request
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139 giturl = models.CharField(max_length =254)
140 commit = models.CharField(max_length = 254)
141 dirpath = models.CharField(max_length = 254)
142
143class BRVariable(models.Model):
144 req = models.ForeignKey(BuildRequest)
145 name = models.CharField(max_length=100)
146 value = models.TextField(blank = True)
147
148class 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
153class 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)