diff options
Diffstat (limited to 'bitbake/lib/toaster/bldcontrol/localhostbecontroller.py')
-rw-r--r-- | bitbake/lib/toaster/bldcontrol/localhostbecontroller.py | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/bldcontrol/localhostbecontroller.py b/bitbake/lib/toaster/bldcontrol/localhostbecontroller.py new file mode 100644 index 0000000000..fe7fd81fb9 --- /dev/null +++ b/bitbake/lib/toaster/bldcontrol/localhostbecontroller.py | |||
@@ -0,0 +1,191 @@ | |||
1 | # | ||
2 | # ex:ts=4:sw=4:sts=4:et | ||
3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
4 | # | ||
5 | # BitBake Toaster Implementation | ||
6 | # | ||
7 | # Copyright (C) 2014 Intel Corporation | ||
8 | # | ||
9 | # This program is free software; you can redistribute it and/or modify | ||
10 | # it under the terms of the GNU General Public License version 2 as | ||
11 | # published by the Free Software Foundation. | ||
12 | # | ||
13 | # This program is distributed in the hope that it will be useful, | ||
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | # GNU General Public License for more details. | ||
17 | # | ||
18 | # You should have received a copy of the GNU General Public License along | ||
19 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
21 | |||
22 | |||
23 | import os | ||
24 | import sys | ||
25 | import re | ||
26 | from django.db import transaction | ||
27 | from django.db.models import Q | ||
28 | from bldcontrol.models import BuildEnvironment, BRLayer, BRVariable, BRTarget, BRBitbake | ||
29 | import subprocess | ||
30 | |||
31 | from toastermain import settings | ||
32 | |||
33 | from bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, _getgitcheckoutdirectoryname | ||
34 | |||
35 | class LocalhostBEController(BuildEnvironmentController): | ||
36 | """ Implementation of the BuildEnvironmentController for the localhost; | ||
37 | this controller manages the default build directory, | ||
38 | the server setup and system start and stop for the localhost-type build environment | ||
39 | |||
40 | """ | ||
41 | |||
42 | def __init__(self, be): | ||
43 | super(LocalhostBEController, self).__init__(be) | ||
44 | self.dburl = settings.getDATABASE_URL() | ||
45 | self.pokydirname = None | ||
46 | self.islayerset = False | ||
47 | |||
48 | def _shellcmd(self, command, cwd = None): | ||
49 | if cwd is None: | ||
50 | cwd = self.be.sourcedir | ||
51 | |||
52 | p = subprocess.Popen(command, cwd = cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
53 | (out,err) = p.communicate() | ||
54 | if p.returncode: | ||
55 | if len(err) == 0: | ||
56 | err = "command: %s \n%s" % (command, out) | ||
57 | else: | ||
58 | err = "command: %s \n%s" % (command, err) | ||
59 | raise ShellCmdException(err) | ||
60 | else: | ||
61 | return out | ||
62 | |||
63 | def _createdirpath(self, path): | ||
64 | from os.path import dirname as DN | ||
65 | if path == "": | ||
66 | raise Exception("Invalid path creation specified.") | ||
67 | if not os.path.exists(DN(path)): | ||
68 | self._createdirpath(DN(path)) | ||
69 | if not os.path.exists(path): | ||
70 | os.mkdir(path, 0755) | ||
71 | |||
72 | def _setupBE(self): | ||
73 | assert self.pokydirname and os.path.exists(self.pokydirname) | ||
74 | self._createdirpath(self.be.builddir) | ||
75 | self._shellcmd("bash -c \"source %s/oe-init-build-env %s\"" % (self.pokydirname, self.be.builddir)) | ||
76 | |||
77 | def startBBServer(self): | ||
78 | assert self.pokydirname and os.path.exists(self.pokydirname) | ||
79 | assert self.islayerset | ||
80 | print("DEBUG: executing ", "bash -c \"source %s/oe-init-build-env %s && DATABASE_URL=%s source toaster start noweb && sleep 1\"" % (self.pokydirname, self.be.builddir, self.dburl)) | ||
81 | print self._shellcmd("bash -c \"source %s/oe-init-build-env %s && DATABASE_URL=%s source toaster start noweb && sleep 1\"" % (self.pokydirname, self.be.builddir, self.dburl)) | ||
82 | # FIXME unfortunate sleep 1 - we need to make sure that bbserver is started and the toaster ui is connected | ||
83 | # but since they start async without any return, we just wait a bit | ||
84 | print "Started server" | ||
85 | assert self.be.sourcedir and os.path.exists(self.be.builddir) | ||
86 | self.be.bbaddress = "localhost" | ||
87 | self.be.bbport = "8200" | ||
88 | self.be.bbstate = BuildEnvironment.SERVER_STARTED | ||
89 | self.be.save() | ||
90 | |||
91 | def stopBBServer(self): | ||
92 | assert self.pokydirname and os.path.exists(self.pokydirname) | ||
93 | assert self.islayerset | ||
94 | print self._shellcmd("bash -c \"source %s/oe-init-build-env %s && %s source toaster stop\"" % | ||
95 | (self.pokydirname, self.be.builddir, (lambda: "" if self.be.bbtoken is None else "BBTOKEN=%s" % self.be.bbtoken)())) | ||
96 | self.be.bbstate = BuildEnvironment.SERVER_STOPPED | ||
97 | self.be.save() | ||
98 | print "Stopped server" | ||
99 | |||
100 | def setLayers(self, bitbakes, layers): | ||
101 | """ a word of attention: by convention, the first layer for any build will be poky! """ | ||
102 | |||
103 | assert self.be.sourcedir is not None | ||
104 | assert len(bitbakes) == 1 | ||
105 | # set layers in the layersource | ||
106 | |||
107 | # 1. get a list of repos, and map dirpaths for each layer | ||
108 | gitrepos = {} | ||
109 | gitrepos[bitbakes[0].giturl] = [] | ||
110 | gitrepos[bitbakes[0].giturl].append( ("bitbake", bitbakes[0].dirpath, bitbakes[0].commit) ) | ||
111 | |||
112 | for layer in layers: | ||
113 | # we don't process local URLs | ||
114 | if layer.giturl.startswith("file://"): | ||
115 | continue | ||
116 | if not layer.giturl in gitrepos: | ||
117 | gitrepos[layer.giturl] = [] | ||
118 | gitrepos[layer.giturl].append( (layer.name, layer.dirpath, layer.commit)) | ||
119 | for giturl in gitrepos.keys(): | ||
120 | commitid = gitrepos[giturl][0][2] | ||
121 | for e in gitrepos[giturl]: | ||
122 | if commitid != e[2]: | ||
123 | raise BuildSetupException("More than one commit per git url, unsupported configuration") | ||
124 | |||
125 | |||
126 | layerlist = [] | ||
127 | |||
128 | # 2. checkout the repositories | ||
129 | for giturl in gitrepos.keys(): | ||
130 | localdirname = os.path.join(self.be.sourcedir, _getgitcheckoutdirectoryname(giturl)) | ||
131 | print "DEBUG: giturl ", giturl ,"checking out in current directory", localdirname | ||
132 | |||
133 | # make sure our directory is a git repository | ||
134 | if os.path.exists(localdirname): | ||
135 | if not giturl in self._shellcmd("git remote -v", localdirname): | ||
136 | raise BuildSetupException("Existing git repository at %s, but with different remotes (not '%s'). Aborting." % (localdirname, giturl)) | ||
137 | else: | ||
138 | self._shellcmd("git clone \"%s\" \"%s\"" % (giturl, localdirname)) | ||
139 | # checkout the needed commit | ||
140 | commit = gitrepos[giturl][0][2] | ||
141 | |||
142 | # branch magic name "HEAD" will inhibit checkout | ||
143 | if commit != "HEAD": | ||
144 | print "DEBUG: checking out commit ", commit, "to", localdirname | ||
145 | self._shellcmd("git fetch --all && git checkout \"%s\"" % commit , localdirname) | ||
146 | |||
147 | # take the localdirname as poky dir if we can find the oe-init-build-env | ||
148 | if self.pokydirname is None and os.path.exists(os.path.join(localdirname, "oe-init-build-env")): | ||
149 | print "DEBUG: selected poky dir name", localdirname | ||
150 | self.pokydirname = localdirname | ||
151 | |||
152 | # verify our repositories | ||
153 | for name, dirpath, commit in gitrepos[giturl]: | ||
154 | localdirpath = os.path.join(localdirname, dirpath) | ||
155 | if not os.path.exists(localdirpath): | ||
156 | raise BuildSetupException("Cannot find layer git path '%s' in checked out repository '%s:%s'. Aborting." % (localdirpath, giturl, commit)) | ||
157 | |||
158 | if name != "bitbake": | ||
159 | layerlist.append(localdirpath) | ||
160 | |||
161 | print "DEBUG: current layer list ", layerlist | ||
162 | |||
163 | # 3. configure the build environment, so we have a conf/bblayers.conf | ||
164 | assert self.pokydirname is not None | ||
165 | self._setupBE() | ||
166 | |||
167 | # 4. update the bblayers.conf | ||
168 | bblayerconf = os.path.join(self.be.builddir, "conf/bblayers.conf") | ||
169 | if not os.path.exists(bblayerconf): | ||
170 | raise BuildSetupException("BE is not consistent: bblayers.conf file missing at %s" % bblayerconf) | ||
171 | |||
172 | conflines = open(bblayerconf, "r").readlines() | ||
173 | |||
174 | bblayerconffile = open(bblayerconf, "w") | ||
175 | for i in xrange(len(conflines)): | ||
176 | if conflines[i].startswith("# line added by toaster"): | ||
177 | i += 2 | ||
178 | else: | ||
179 | bblayerconffile.write(conflines[i]) | ||
180 | |||
181 | bblayerconffile.write("\n# line added by toaster build control\nBBLAYERS = \"" + " ".join(layerlist) + "\"") | ||
182 | bblayerconffile.close() | ||
183 | |||
184 | self.islayerset = True | ||
185 | return True | ||
186 | |||
187 | def release(self): | ||
188 | assert self.be.sourcedir and os.path.exists(self.be.builddir) | ||
189 | import shutil | ||
190 | shutil.rmtree(os.path.join(self.be.sourcedir, "build")) | ||
191 | assert not os.path.exists(self.be.builddir) | ||