2014-06-12 11:57:22 +00:00
|
|
|
from django.core.management.base import NoArgsCommand, CommandError
|
|
|
|
from django.db import transaction
|
|
|
|
from orm.models import Build
|
2014-07-15 18:31:10 +00:00
|
|
|
from bldcontrol.bbcontroller import getBuildEnvironmentController, ShellCmdException, BuildSetupException
|
|
|
|
from bldcontrol.models import BuildRequest, BuildEnvironment, BRError
|
2014-06-12 11:57:22 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
class Command(NoArgsCommand):
|
|
|
|
args = ""
|
|
|
|
help = "Schedules and executes build requests as possible. Does not return (interrupt with Ctrl-C)"
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.commit_on_success
|
|
|
|
def _selectBuildEnvironment(self):
|
|
|
|
bec = getBuildEnvironmentController(lock = BuildEnvironment.LOCK_FREE)
|
|
|
|
bec.be.lock = BuildEnvironment.LOCK_LOCK
|
|
|
|
bec.be.save()
|
|
|
|
return bec
|
|
|
|
|
|
|
|
@transaction.commit_on_success
|
|
|
|
def _selectBuildRequest(self):
|
|
|
|
br = BuildRequest.objects.filter(state = BuildRequest.REQ_QUEUED).order_by('pk')[0]
|
|
|
|
br.state = BuildRequest.REQ_INPROGRESS
|
|
|
|
br.save()
|
|
|
|
return br
|
|
|
|
|
|
|
|
def schedule(self):
|
2014-07-15 18:31:10 +00:00
|
|
|
import traceback
|
2014-06-12 11:57:22 +00:00
|
|
|
try:
|
|
|
|
br = None
|
|
|
|
try:
|
|
|
|
# select the build environment and the request to build
|
|
|
|
br = self._selectBuildRequest()
|
|
|
|
except IndexError as e:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
bec = self._selectBuildEnvironment()
|
|
|
|
except IndexError as e:
|
|
|
|
# we could not find a BEC; postpone the BR
|
|
|
|
br.state = BuildRequest.REQ_QUEUED
|
|
|
|
br.save()
|
2014-10-13 16:10:39 +00:00
|
|
|
print "No build env"
|
2014-06-12 11:57:22 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
print "Build %s, Environment %s" % (br, bec.be)
|
2014-10-13 16:10:39 +00:00
|
|
|
# let the build request know where it is being executed
|
|
|
|
br.environment = bec.be
|
|
|
|
br.save()
|
2014-06-12 11:57:22 +00:00
|
|
|
|
2014-10-13 16:10:39 +00:00
|
|
|
# set up the buid environment with the needed layers
|
|
|
|
bec.setLayers(br.brbitbake_set.all(), br.brlayer_set.all())
|
2014-06-12 11:57:22 +00:00
|
|
|
|
2014-10-13 16:10:39 +00:00
|
|
|
# get the bb server running with the build req id and build env id
|
|
|
|
bbctrl = bec.getBBController("%d:%d" % (br.pk, bec.be.pk))
|
2014-06-12 11:57:22 +00:00
|
|
|
|
|
|
|
# set the build configuration
|
|
|
|
for variable in br.brvariable_set.all():
|
|
|
|
bbctrl.setVariable(variable.name, variable.value)
|
|
|
|
|
|
|
|
# trigger the build command
|
2014-10-13 16:10:39 +00:00
|
|
|
task = reduce(lambda x, y: x if len(y)== 0 else y, map(lambda y: y.task, br.brtarget_set.all()))
|
|
|
|
if len(task) == 0:
|
|
|
|
task = None
|
|
|
|
bbctrl.build(list(map(lambda x:x.target, br.brtarget_set.all())), task)
|
2014-06-12 11:57:22 +00:00
|
|
|
|
|
|
|
print "Build launched, exiting"
|
|
|
|
# disconnect from the server
|
|
|
|
bbctrl.disconnect()
|
|
|
|
|
|
|
|
# cleanup to be performed by toaster when the deed is done
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2014-07-15 18:31:10 +00:00
|
|
|
print " EE Error executing shell command\n", e
|
|
|
|
traceback.print_exc(e)
|
|
|
|
BRError.objects.create(req = br,
|
|
|
|
errtype = str(type(e)),
|
|
|
|
errmsg = str(e),
|
|
|
|
traceback = traceback.format_exc(e))
|
|
|
|
br.state = BuildRequest.REQ_FAILED
|
|
|
|
br.save()
|
|
|
|
bec.be.lock = BuildEnvironment.LOCK_FREE
|
|
|
|
bec.be.save()
|
|
|
|
|
2014-06-12 11:57:22 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
from django.utils import timezone
|
|
|
|
from datetime import timedelta
|
|
|
|
# environments locked for more than 30 seconds - they should be unlocked
|
|
|
|
BuildEnvironment.objects.filter(lock=BuildEnvironment.LOCK_LOCK).filter(updated__lt = timezone.now() - timedelta(seconds = 30)).update(lock = BuildEnvironment.LOCK_FREE)
|
|
|
|
|
|
|
|
|
|
|
|
def handle_noargs(self, **options):
|
|
|
|
self.cleanup()
|
|
|
|
self.schedule()
|