blob: 94906359347de5338cc340347fce5320dd50bc8a [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
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
23import os
24import sys
25import re
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026import shutil
Brad Bishopd7bf8c12018-02-25 22:55:05 -050027import time
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028from django.db import transaction
29from django.db.models import Q
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080030from bldcontrol.models import BuildEnvironment, BuildRequest, BRLayer, BRVariable, BRTarget, BRBitbake, Build
31from orm.models import CustomImageRecipe, Layer, Layer_Version, Project, ProjectLayer, ToasterSetting
32from orm.models import signal_runbuilds
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033import subprocess
34
35from toastermain import settings
36
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037from bldcontrol.bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, BitbakeController
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
39import logging
40logger = logging.getLogger("toaster")
41
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080042install_dir = os.environ.get('TOASTER_DIR')
43
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044from pprint import pprint, pformat
45
46class LocalhostBEController(BuildEnvironmentController):
47 """ Implementation of the BuildEnvironmentController for the localhost;
48 this controller manages the default build directory,
49 the server setup and system start and stop for the localhost-type build environment
50
51 """
52
53 def __init__(self, be):
54 super(LocalhostBEController, self).__init__(be)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 self.pokydirname = None
56 self.islayerset = False
57
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058 def _shellcmd(self, command, cwd=None, nowait=False,env=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 if cwd is None:
60 cwd = self.be.sourcedir
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061 if env is None:
62 env=os.environ.copy()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063
Brad Bishopd7bf8c12018-02-25 22:55:05 -050064 logger.debug("lbc_shellcmd: (%s) %s" % (cwd, command))
65 p = subprocess.Popen(command, cwd = cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050066 if nowait:
67 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 (out,err) = p.communicate()
69 p.wait()
70 if p.returncode:
71 if len(err) == 0:
72 err = "command: %s \n%s" % (command, out)
73 else:
74 err = "command: %s \n%s" % (command, err)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075 logger.warning("localhostbecontroller: shellcmd error %s" % err)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 raise ShellCmdException(err)
77 else:
78 logger.debug("localhostbecontroller: shellcmd success")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079 return out.decode('utf-8')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 def getGitCloneDirectory(self, url, branch):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050082 """Construct unique clone directory name out of url and branch."""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 if branch != "HEAD":
Patrick Williamsc0f7c042017-02-23 20:41:17 -060084 return "_toaster_clones/_%s_%s" % (re.sub('[:/@+%]', '_', url), branch)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
86 # word of attention; this is a localhost-specific issue; only on the localhost we expect to have "HEAD" releases
87 # which _ALWAYS_ means the current poky checkout
88 from os.path import dirname as DN
89 local_checkout_path = DN(DN(DN(DN(DN(os.path.abspath(__file__))))))
90 #logger.debug("localhostbecontroller: using HEAD checkout in %s" % local_checkout_path)
91 return local_checkout_path
92
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080093 def setCloneStatus(self,bitbake,status,total,current,repo_name):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 bitbake.req.build.repos_cloned=current
95 bitbake.req.build.repos_to_clone=total
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080096 bitbake.req.build.progress_item=repo_name
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 bitbake.req.build.save()
98
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 def setLayers(self, bitbake, layers, targets):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 """ a word of attention: by convention, the first layer for any build will be poky! """
101
102 assert self.be.sourcedir is not None
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600103
104 layerlist = []
105 nongitlayerlist = []
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800106 layer_index = 0
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 git_env = os.environ.copy()
108 # (note: add custom environment settings here)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600109
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 # set layers in the layersource
111
112 # 1. get a list of repos with branches, and map dirpaths for each layer
113 gitrepos = {}
114
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600115 # if we're using a remotely fetched version of bitbake add its git
116 # details to the list of repos to clone
117 if bitbake.giturl and bitbake.commit:
118 gitrepos[(bitbake.giturl, bitbake.commit)] = []
119 gitrepos[(bitbake.giturl, bitbake.commit)].append(
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800120 ("bitbake", bitbake.dirpath, 0))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
122 for layer in layers:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123 # We don't need to git clone the layer for the CustomImageRecipe
124 # as it's generated by us layer on if needed
125 if CustomImageRecipe.LAYER_NAME in layer.name:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 continue
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600127
128 # If we have local layers then we don't need clone them
129 # For local layers giturl will be empty
130 if not layer.giturl:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800131 nongitlayerlist.append( "%03d:%s" % (layer_index,layer.local_source_dir) )
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600132 continue
133
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 if not (layer.giturl, layer.commit) in gitrepos:
135 gitrepos[(layer.giturl, layer.commit)] = []
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800136 gitrepos[(layer.giturl, layer.commit)].append( (layer.name,layer.dirpath,layer_index) )
137 layer_index += 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138
139
140 logger.debug("localhostbecontroller, our git repos are %s" % pformat(gitrepos))
141
142
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500143 # 2. Note for future use if the current source directory is a
144 # checked-out git repos that could match a layer's vcs_url and therefore
145 # be used to speed up cloning (rather than fetching it again).
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146
147 cached_layers = {}
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500148
149 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500150 for remotes in self._shellcmd("git remote -v", self.be.sourcedir,env=git_env).split("\n"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 try:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500152 remote = remotes.split("\t")[1].split(" ")[0]
153 if remote not in cached_layers:
154 cached_layers[remote] = self.be.sourcedir
155 except IndexError:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 pass
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500157 except ShellCmdException:
158 # ignore any errors in collecting git remotes this is an optional
159 # step
160 pass
161
162 logger.info("Using pre-checked out source for layer %s", cached_layers)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164 # 3. checkout the repositories
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500165 clone_count=0
166 clone_total=len(gitrepos.keys())
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800167 self.setCloneStatus(bitbake,'Started',clone_total,clone_count,'')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 for giturl, commit in gitrepos.keys():
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800169 self.setCloneStatus(bitbake,'progress',clone_total,clone_count,gitrepos[(giturl, commit)][0][0])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500170 clone_count += 1
171
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 localdirname = os.path.join(self.be.sourcedir, self.getGitCloneDirectory(giturl, commit))
173 logger.debug("localhostbecontroller: giturl %s:%s checking out in current directory %s" % (giturl, commit, localdirname))
174
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600175 # see if our directory is a git repository
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 if os.path.exists(localdirname):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600177 try:
178 localremotes = self._shellcmd("git remote -v",
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 localdirname,env=git_env)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800180 # NOTE: this nice-to-have check breaks when using git remaping to get past firewall
181 # Re-enable later with .gitconfig remapping checks
182 #if not giturl in localremotes and commit != 'HEAD':
183 # raise BuildSetupException("Existing git repository at %s, but with different remotes ('%s', expected '%s'). Toaster will not continue out of fear of damaging something." % (localdirname, ", ".join(localremotes.split("\n")), giturl))
184 pass
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600185 except ShellCmdException:
186 # our localdirname might not be a git repository
187 #- that's fine
188 pass
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 else:
190 if giturl in cached_layers:
191 logger.debug("localhostbecontroller git-copying %s to %s" % (cached_layers[giturl], localdirname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500192 self._shellcmd("git clone \"%s\" \"%s\"" % (cached_layers[giturl], localdirname),env=git_env)
193 self._shellcmd("git remote remove origin", localdirname,env=git_env)
194 self._shellcmd("git remote add origin \"%s\"" % giturl, localdirname,env=git_env)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500196 logger.debug("localhostbecontroller: cloning %s in %s" % (giturl, localdirname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500197 self._shellcmd('git clone "%s" "%s"' % (giturl, localdirname),env=git_env)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
199 # branch magic name "HEAD" will inhibit checkout
200 if commit != "HEAD":
201 logger.debug("localhostbecontroller: checking out commit %s to %s " % (commit, localdirname))
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500202 ref = commit if re.match('^[a-fA-F0-9]+$', commit) else 'origin/%s' % commit
Brad Bishop316dfdd2018-06-25 12:45:53 -0400203 self._shellcmd('git fetch && git reset --hard "%s"' % ref, localdirname,env=git_env)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204
205 # take the localdirname as poky dir if we can find the oe-init-build-env
206 if self.pokydirname is None and os.path.exists(os.path.join(localdirname, "oe-init-build-env")):
207 logger.debug("localhostbecontroller: selected poky dir name %s" % localdirname)
208 self.pokydirname = localdirname
209
210 # make sure we have a working bitbake
211 if not os.path.exists(os.path.join(self.pokydirname, 'bitbake')):
212 logger.debug("localhostbecontroller: checking bitbake into the poky dirname %s " % self.pokydirname)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500213 self._shellcmd("git clone -b \"%s\" \"%s\" \"%s\" " % (bitbake.commit, bitbake.giturl, os.path.join(self.pokydirname, 'bitbake')),env=git_env)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214
215 # verify our repositories
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800216 for name, dirpath, index in gitrepos[(giturl, commit)]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 localdirpath = os.path.join(localdirname, dirpath)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800218 logger.debug("localhostbecontroller: localdirpath expects '%s'" % localdirpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500219 if not os.path.exists(localdirpath):
220 raise BuildSetupException("Cannot find layer git path '%s' in checked out repository '%s:%s'. Aborting." % (localdirpath, giturl, commit))
221
222 if name != "bitbake":
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800223 layerlist.append("%03d:%s" % (index,localdirpath.rstrip("/")))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500224
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800225 self.setCloneStatus(bitbake,'complete',clone_total,clone_count,'')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226 logger.debug("localhostbecontroller: current layer list %s " % pformat(layerlist))
227
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700228 # Resolve self.pokydirname if not resolved yet, consider the scenario
229 # where all layers are local, that's the else clause
230 if self.pokydirname is None:
231 if os.path.exists(os.path.join(self.be.sourcedir, "oe-init-build-env")):
232 logger.debug("localhostbecontroller: selected poky dir name %s" % self.be.sourcedir)
233 self.pokydirname = self.be.sourcedir
234 else:
235 # Alternatively, scan local layers for relative "oe-init-build-env" location
236 for layer in layers:
237 if os.path.exists(os.path.join(layer.layer_version.layer.local_source_dir,"..","oe-init-build-env")):
238 logger.debug("localhostbecontroller, setting pokydirname to %s" % (layer.layer_version.layer.local_source_dir))
239 self.pokydirname = os.path.join(layer.layer_version.layer.local_source_dir,"..")
240 break
241 else:
242 logger.error("pokydirname is not set, you will run into trouble!")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500243
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500244 # 5. create custom layer and add custom recipes to it
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500245 for target in targets:
246 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500247 customrecipe = CustomImageRecipe.objects.get(
248 name=target.target,
249 project=bitbake.req.project)
250
251 custom_layer_path = self.setup_custom_image_recipe(
252 customrecipe, layers)
253
254 if os.path.isdir(custom_layer_path):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800255 layerlist.append("%03d:%s" % (layer_index,custom_layer_path))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500256
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500257 except CustomImageRecipe.DoesNotExist:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500258 continue # not a custom recipe, skip
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500259
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260 layerlist.extend(nongitlayerlist)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 logger.debug("\n\nset layers gives this list %s" % pformat(layerlist))
262 self.islayerset = True
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800263
264 # restore the order of layer list for bblayers.conf
265 layerlist.sort()
266 sorted_layerlist = [l[4:] for l in layerlist]
267 return sorted_layerlist
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500269 def setup_custom_image_recipe(self, customrecipe, layers):
270 """ Set up toaster-custom-images layer and recipe files """
271 layerpath = os.path.join(self.be.builddir,
272 CustomImageRecipe.LAYER_NAME)
273
274 # create directory structure
275 for name in ("conf", "recipes"):
276 path = os.path.join(layerpath, name)
277 if not os.path.isdir(path):
278 os.makedirs(path)
279
280 # create layer.conf
281 config = os.path.join(layerpath, "conf", "layer.conf")
282 if not os.path.isfile(config):
283 with open(config, "w") as conf:
284 conf.write('BBPATH .= ":${LAYERDIR}"\nBBFILES += "${LAYERDIR}/recipes/*.bb"\n')
285
286 # Update the Layer_Version dirpath that has our base_recipe in
287 # to be able to read the base recipe to then generate the
288 # custom recipe.
289 br_layer_base_recipe = layers.get(
290 layer_version=customrecipe.base_recipe.layer_version)
291
292 # If the layer is one that we've cloned we know where it lives
293 if br_layer_base_recipe.giturl and br_layer_base_recipe.commit:
294 layer_path = self.getGitCloneDirectory(
295 br_layer_base_recipe.giturl,
296 br_layer_base_recipe.commit)
297 # Otherwise it's a local layer
298 elif br_layer_base_recipe.local_source_dir:
299 layer_path = br_layer_base_recipe.local_source_dir
300 else:
301 logger.error("Unable to workout the dir path for the custom"
302 " image recipe")
303
304 br_layer_base_dirpath = os.path.join(
305 self.be.sourcedir,
306 layer_path,
307 customrecipe.base_recipe.layer_version.dirpath)
308
309 customrecipe.base_recipe.layer_version.dirpath = br_layer_base_dirpath
310
311 customrecipe.base_recipe.layer_version.save()
312
313 # create recipe
314 recipe_path = os.path.join(layerpath, "recipes", "%s.bb" %
315 customrecipe.name)
316 with open(recipe_path, "w") as recipef:
317 recipef.write(customrecipe.generate_recipe_file_contents())
318
319 # Update the layer and recipe objects
320 customrecipe.layer_version.dirpath = layerpath
321 customrecipe.layer_version.layer.local_source_dir = layerpath
322 customrecipe.layer_version.layer.save()
323 customrecipe.layer_version.save()
324
325 customrecipe.file_path = recipe_path
326 customrecipe.save()
327
328 return layerpath
329
330
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331 def readServerLogFile(self):
332 return open(os.path.join(self.be.builddir, "toaster_server.log"), "r").read()
333
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500335 def triggerBuild(self, bitbake, layers, variables, targets, brbe):
336 layers = self.setLayers(bitbake, layers, targets)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800337 is_merged_attr = bitbake.req.project.merged_attr
338
339 git_env = os.environ.copy()
340 # (note: add custom environment settings here)
341 try:
342 # insure that the project init/build uses the selected bitbake, and not Toaster's
343 del git_env['TEMPLATECONF']
344 del git_env['BBBASEDIR']
345 del git_env['BUILDDIR']
346 except KeyError:
347 pass
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500348
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500349 # init build environment from the clone
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800350 if bitbake.req.project.builddir:
351 builddir = bitbake.req.project.builddir
352 else:
353 builddir = '%s-toaster-%d' % (self.be.builddir, bitbake.req.project.id)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500354 oe_init = os.path.join(self.pokydirname, 'oe-init-build-env')
355 # init build environment
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500356 try:
357 custom_script = ToasterSetting.objects.get(name="CUSTOM_BUILD_INIT_SCRIPT").value
358 custom_script = custom_script.replace("%BUILDDIR%" ,builddir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800359 self._shellcmd("bash -c 'source %s'" % (custom_script),env=git_env)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500360 except ToasterSetting.DoesNotExist:
361 self._shellcmd("bash -c 'source %s %s'" % (oe_init, builddir),
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800362 self.be.sourcedir,env=git_env)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500364 # update bblayers.conf
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800365 if not is_merged_attr:
366 bblconfpath = os.path.join(builddir, "conf/toaster-bblayers.conf")
367 with open(bblconfpath, 'w') as bblayers:
368 bblayers.write('# line added by toaster build control\n'
369 'BBLAYERS = "%s"' % ' '.join(layers))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500370
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800371 # write configuration file
372 confpath = os.path.join(builddir, 'conf/toaster.conf')
373 with open(confpath, 'w') as conf:
374 for var in variables:
375 conf.write('%s="%s"\n' % (var.name, var.value))
376 conf.write('INHERIT+="toaster buildhistory"')
377 else:
378 # Append the Toaster-specific values directly to the bblayers.conf
379 bblconfpath = os.path.join(builddir, "conf/bblayers.conf")
380 bblconfpath_save = os.path.join(builddir, "conf/bblayers.conf.save")
381 shutil.copyfile(bblconfpath, bblconfpath_save)
382 with open(bblconfpath) as bblayers:
383 content = bblayers.readlines()
384 do_write = True
385 was_toaster = False
386 with open(bblconfpath,'w') as bblayers:
387 for line in content:
388 #line = line.strip('\n')
389 if 'TOASTER_CONFIG_PROLOG' in line:
390 do_write = False
391 was_toaster = True
392 elif 'TOASTER_CONFIG_EPILOG' in line:
393 do_write = True
394 elif do_write:
395 bblayers.write(line)
396 if not was_toaster:
397 bblayers.write('\n')
398 bblayers.write('#=== TOASTER_CONFIG_PROLOG ===\n')
399 bblayers.write('BBLAYERS = "\\\n')
400 for layer in layers:
401 bblayers.write(' %s \\\n' % layer)
402 bblayers.write(' "\n')
403 bblayers.write('#=== TOASTER_CONFIG_EPILOG ===\n')
404 # Append the Toaster-specific values directly to the local.conf
405 bbconfpath = os.path.join(builddir, "conf/local.conf")
406 bbconfpath_save = os.path.join(builddir, "conf/local.conf.save")
407 shutil.copyfile(bbconfpath, bbconfpath_save)
408 with open(bbconfpath) as f:
409 content = f.readlines()
410 do_write = True
411 was_toaster = False
412 with open(bbconfpath,'w') as conf:
413 for line in content:
414 #line = line.strip('\n')
415 if 'TOASTER_CONFIG_PROLOG' in line:
416 do_write = False
417 was_toaster = True
418 elif 'TOASTER_CONFIG_EPILOG' in line:
419 do_write = True
420 elif do_write:
421 conf.write(line)
422 if not was_toaster:
423 conf.write('\n')
424 conf.write('#=== TOASTER_CONFIG_PROLOG ===\n')
425 for var in variables:
426 if (not var.name.startswith("INTERNAL_")) and (not var.name == "BBLAYERS"):
427 conf.write('%s="%s"\n' % (var.name, var.value))
428 conf.write('#=== TOASTER_CONFIG_EPILOG ===\n')
429
430 # If 'target' is just the project preparation target, then we are done
431 for target in targets:
432 if "_PROJECT_PREPARE_" == target.target:
433 logger.debug('localhostbecontroller: Project has been prepared. Done.')
434 # Update the Build Request and release the build environment
435 bitbake.req.state = BuildRequest.REQ_COMPLETED
436 bitbake.req.save()
437 self.be.lock = BuildEnvironment.LOCK_FREE
438 self.be.save()
439 # Close the project build and progress bar
440 bitbake.req.build.outcome = Build.SUCCEEDED
441 bitbake.req.build.save()
442 # Update the project status
443 bitbake.req.project.set_variable(Project.PROJECT_SPECIFIC_STATUS,Project.PROJECT_SPECIFIC_CLONING_SUCCESS)
444 signal_runbuilds()
445 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500446
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500447 # clean the Toaster to build environment
448 env_clean = 'unset BBPATH;' # clean BBPATH for <= YP-2.4.0
449
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700450 # run bitbake server from the clone if available
451 # otherwise pick it from the PATH
452 bitbake = os.path.join(self.pokydirname, 'bitbake', 'bin', 'bitbake')
453 if not os.path.exists(bitbake):
454 logger.info("Bitbake not available under %s, will try to use it from PATH" %
455 self.pokydirname)
456 for path in os.environ["PATH"].split(os.pathsep):
457 if os.path.exists(os.path.join(path, 'bitbake')):
458 bitbake = os.path.join(path, 'bitbake')
459 break
460 else:
461 logger.error("Looks like Bitbake is not available, please fix your environment")
462
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500463 toasterlayers = os.path.join(builddir,"conf/toaster-bblayers.conf")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800464 if not is_merged_attr:
465 self._shellcmd('%s bash -c \"source %s %s; BITBAKE_UI="knotty" %s --read %s --read %s '
466 '--server-only -B 0.0.0.0:0\"' % (env_clean, oe_init,
467 builddir, bitbake, confpath, toasterlayers), self.be.sourcedir)
468 else:
469 self._shellcmd('%s bash -c \"source %s %s; BITBAKE_UI="knotty" %s '
470 '--server-only -B 0.0.0.0:0\"' % (env_clean, oe_init,
471 builddir, bitbake), self.be.sourcedir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500472
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500473 # read port number from bitbake.lock
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500474 self.be.bbport = -1
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500475 bblock = os.path.join(builddir, 'bitbake.lock')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500476 # allow 10 seconds for bb lock file to appear but also be populated
477 for lock_check in range(10):
478 if not os.path.exists(bblock):
479 logger.debug("localhostbecontroller: waiting for bblock file to appear")
480 time.sleep(1)
481 continue
482 if 10 < os.stat(bblock).st_size:
483 break
484 logger.debug("localhostbecontroller: waiting for bblock content to appear")
485 time.sleep(1)
486 else:
487 raise BuildSetupException("Cannot find bitbake server lock file '%s'. Aborting." % bblock)
488
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500489 with open(bblock) as fplock:
490 for line in fplock:
491 if ":" in line:
492 self.be.bbport = line.split(":")[-1].strip()
493 logger.debug("localhostbecontroller: bitbake port %s", self.be.bbport)
494 break
495
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500496 if -1 == self.be.bbport:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500497 raise BuildSetupException("localhostbecontroller: can't read bitbake port from %s" % bblock)
498
499 self.be.bbaddress = "localhost"
500 self.be.bbstate = BuildEnvironment.SERVER_STARTED
501 self.be.lock = BuildEnvironment.LOCK_RUNNING
502 self.be.save()
503
504 bbtargets = ''
505 for target in targets:
506 task = target.task
507 if task:
508 if not task.startswith('do_'):
509 task = 'do_' + task
510 task = ':%s' % task
511 bbtargets += '%s%s ' % (target.target, task)
512
513 # run build with local bitbake. stop the server after the build.
514 log = os.path.join(builddir, 'toaster_ui.log')
515 local_bitbake = os.path.join(os.path.dirname(os.getenv('BBBASEDIR')),
516 'bitbake')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800517 if not is_merged_attr:
518 self._shellcmd(['%s bash -c \"(TOASTER_BRBE="%s" BBSERVER="0.0.0.0:%s" '
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500519 '%s %s -u toasterui --read %s --read %s --token="" >>%s 2>&1;'
520 'BITBAKE_UI="knotty" BBSERVER=0.0.0.0:%s %s -m)&\"' \
521 % (env_clean, brbe, self.be.bbport, local_bitbake, bbtargets, confpath, toasterlayers, log,
522 self.be.bbport, bitbake,)],
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500523 builddir, nowait=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800524 else:
525 self._shellcmd(['%s bash -c \"(TOASTER_BRBE="%s" BBSERVER="0.0.0.0:%s" '
526 '%s %s -u toasterui --token="" >>%s 2>&1;'
527 'BITBAKE_UI="knotty" BBSERVER=0.0.0.0:%s %s -m)&\"' \
528 % (env_clean, brbe, self.be.bbport, local_bitbake, bbtargets, log,
529 self.be.bbport, bitbake,)],
530 builddir, nowait=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500531
532 logger.debug('localhostbecontroller: Build launched, exiting. '
533 'Follow build logs at %s' % log)