diff options
Diffstat (limited to 'bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py')
-rw-r--r-- | bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py b/bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py new file mode 100644 index 0000000000..8efe8e62bc --- /dev/null +++ b/bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py | |||
@@ -0,0 +1,90 @@ | |||
1 | from django.core.management.base import NoArgsCommand, CommandError | ||
2 | from django.db import transaction | ||
3 | from orm.models import Build | ||
4 | from bldcontrol.bbcontroller import getBuildEnvironmentController, ShellCmdException, BuildSetupException | ||
5 | from bldcontrol.models import BuildRequest, BuildEnvironment, BRError | ||
6 | import os | ||
7 | |||
8 | class Command(NoArgsCommand): | ||
9 | args = "" | ||
10 | help = "Schedules and executes build requests as possible. Does not return (interrupt with Ctrl-C)" | ||
11 | |||
12 | |||
13 | @transaction.commit_on_success | ||
14 | def _selectBuildEnvironment(self): | ||
15 | bec = getBuildEnvironmentController(lock = BuildEnvironment.LOCK_FREE) | ||
16 | bec.be.lock = BuildEnvironment.LOCK_LOCK | ||
17 | bec.be.save() | ||
18 | return bec | ||
19 | |||
20 | @transaction.commit_on_success | ||
21 | def _selectBuildRequest(self): | ||
22 | br = BuildRequest.objects.filter(state = BuildRequest.REQ_QUEUED).order_by('pk')[0] | ||
23 | br.state = BuildRequest.REQ_INPROGRESS | ||
24 | br.save() | ||
25 | return br | ||
26 | |||
27 | def schedule(self): | ||
28 | import traceback | ||
29 | try: | ||
30 | br = None | ||
31 | try: | ||
32 | # select the build environment and the request to build | ||
33 | br = self._selectBuildRequest() | ||
34 | except IndexError as e: | ||
35 | return | ||
36 | try: | ||
37 | bec = self._selectBuildEnvironment() | ||
38 | except IndexError as e: | ||
39 | # we could not find a BEC; postpone the BR | ||
40 | br.state = BuildRequest.REQ_QUEUED | ||
41 | br.save() | ||
42 | return | ||
43 | |||
44 | # set up the buid environment with the needed layers | ||
45 | print "Build %s, Environment %s" % (br, bec.be) | ||
46 | bec.setLayers(br.brbitbake_set.all(), br.brlayer_set.all()) | ||
47 | |||
48 | # get the bb server running | ||
49 | bbctrl = bec.getBBController() | ||
50 | |||
51 | # let toasterui that this is a managed build | ||
52 | bbctrl.setVariable("TOASTER_BRBE", "%d:%d" % (br.pk, bec.be.pk)) | ||
53 | |||
54 | # set the build configuration | ||
55 | for variable in br.brvariable_set.all(): | ||
56 | bbctrl.setVariable(variable.name, variable.value) | ||
57 | |||
58 | # trigger the build command | ||
59 | bbctrl.build(list(map(lambda x:x.target, br.brtarget_set.all()))) | ||
60 | |||
61 | print "Build launched, exiting" | ||
62 | # disconnect from the server | ||
63 | bbctrl.disconnect() | ||
64 | |||
65 | # cleanup to be performed by toaster when the deed is done | ||
66 | |||
67 | |||
68 | except Exception as e: | ||
69 | print " EE Error executing shell command\n", e | ||
70 | traceback.print_exc(e) | ||
71 | BRError.objects.create(req = br, | ||
72 | errtype = str(type(e)), | ||
73 | errmsg = str(e), | ||
74 | traceback = traceback.format_exc(e)) | ||
75 | br.state = BuildRequest.REQ_FAILED | ||
76 | br.save() | ||
77 | bec.be.lock = BuildEnvironment.LOCK_FREE | ||
78 | bec.be.save() | ||
79 | |||
80 | |||
81 | def cleanup(self): | ||
82 | from django.utils import timezone | ||
83 | from datetime import timedelta | ||
84 | # environments locked for more than 30 seconds - they should be unlocked | ||
85 | BuildEnvironment.objects.filter(lock=BuildEnvironment.LOCK_LOCK).filter(updated__lt = timezone.now() - timedelta(seconds = 30)).update(lock = BuildEnvironment.LOCK_FREE) | ||
86 | |||
87 | |||
88 | def handle_noargs(self, **options): | ||
89 | self.cleanup() | ||
90 | self.schedule() | ||