diff options
Diffstat (limited to 'bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py')
-rw-r--r-- | bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py | 85 |
1 files changed, 85 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..dd8f84b07a --- /dev/null +++ b/bitbake/lib/toaster/bldcontrol/management/commands/runbuilds.py | |||
@@ -0,0 +1,85 @@ | |||
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 | ||
5 | from bldcontrol.models import BuildRequest, BuildEnvironment | ||
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 | try: | ||
29 | br = None | ||
30 | try: | ||
31 | # select the build environment and the request to build | ||
32 | br = self._selectBuildRequest() | ||
33 | except IndexError as e: | ||
34 | return | ||
35 | try: | ||
36 | bec = self._selectBuildEnvironment() | ||
37 | except IndexError as e: | ||
38 | # we could not find a BEC; postpone the BR | ||
39 | br.state = BuildRequest.REQ_QUEUED | ||
40 | br.save() | ||
41 | return | ||
42 | |||
43 | # set up the buid environment with the needed layers | ||
44 | print "Build %s, Environment %s" % (br, bec.be) | ||
45 | bec.setLayers(br.brlayer_set.all()) | ||
46 | |||
47 | # get the bb server running | ||
48 | bbctrl = bec.getBBController() | ||
49 | |||
50 | # let toasterui that this is a managed build | ||
51 | bbctrl.setVariable("TOASTER_BRBE", "%d:%d" % (br.pk, bec.be.pk)) | ||
52 | |||
53 | # set the build configuration | ||
54 | for variable in br.brvariable_set.all(): | ||
55 | bbctrl.setVariable(variable.name, variable.value) | ||
56 | |||
57 | # trigger the build command | ||
58 | bbctrl.build(list(map(lambda x:x.target, br.brtarget_set.all()))) | ||
59 | |||
60 | print "Build launched, exiting" | ||
61 | # disconnect from the server | ||
62 | bbctrl.disconnect() | ||
63 | |||
64 | # cleanup to be performed by toaster when the deed is done | ||
65 | |||
66 | except ShellCmdException as e: | ||
67 | import traceback | ||
68 | print " EE Error executing shell command\n", e | ||
69 | traceback.format_exc(e) | ||
70 | |||
71 | except Exception as e: | ||
72 | import traceback | ||
73 | traceback.print_exc() | ||
74 | raise e | ||
75 | |||
76 | def cleanup(self): | ||
77 | from django.utils import timezone | ||
78 | from datetime import timedelta | ||
79 | # environments locked for more than 30 seconds - they should be unlocked | ||
80 | BuildEnvironment.objects.filter(lock=BuildEnvironment.LOCK_LOCK).filter(updated__lt = timezone.now() - timedelta(seconds = 30)).update(lock = BuildEnvironment.LOCK_FREE) | ||
81 | |||
82 | |||
83 | def handle_noargs(self, **options): | ||
84 | self.cleanup() | ||
85 | self.schedule() | ||