blob: 7def1f3a17225ffa8e56628db81b0479fc65ed01 [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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027from django.db import transaction
28from django.db.models import Q
29from bldcontrol.models import BuildEnvironment, BRLayer, BRVariable, BRTarget, BRBitbake
Patrick Williamsf1e5d692016-03-30 15:21:19 -050030from orm.models import CustomImageRecipe, Layer, Layer_Version, ProjectLayer
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031import subprocess
32
33from toastermain import settings
34
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035from bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, BitbakeController
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036
37import logging
38logger = logging.getLogger("toaster")
39
40from pprint import pprint, pformat
41
42class LocalhostBEController(BuildEnvironmentController):
43 """ Implementation of the BuildEnvironmentController for the localhost;
44 this controller manages the default build directory,
45 the server setup and system start and stop for the localhost-type build environment
46
47 """
48
49 def __init__(self, be):
50 super(LocalhostBEController, self).__init__(be)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 self.pokydirname = None
52 self.islayerset = False
53
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050054 def _shellcmd(self, command, cwd=None, nowait=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 if cwd is None:
56 cwd = self.be.sourcedir
57
58 logger.debug("lbc_shellcmmd: (%s) %s" % (cwd, command))
59 p = subprocess.Popen(command, cwd = cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050060 if nowait:
61 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 (out,err) = p.communicate()
63 p.wait()
64 if p.returncode:
65 if len(err) == 0:
66 err = "command: %s \n%s" % (command, out)
67 else:
68 err = "command: %s \n%s" % (command, err)
69 logger.warn("localhostbecontroller: shellcmd error %s" % err)
70 raise ShellCmdException(err)
71 else:
72 logger.debug("localhostbecontroller: shellcmd success")
73 return out
74
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 def getGitCloneDirectory(self, url, branch):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050076 """Construct unique clone directory name out of url and branch."""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 if branch != "HEAD":
Patrick Williamsf1e5d692016-03-30 15:21:19 -050078 return "_toaster_clones/_%s_%s" % (re.sub('[:/@%]', '_', url), branch)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
80 # word of attention; this is a localhost-specific issue; only on the localhost we expect to have "HEAD" releases
81 # which _ALWAYS_ means the current poky checkout
82 from os.path import dirname as DN
83 local_checkout_path = DN(DN(DN(DN(DN(os.path.abspath(__file__))))))
84 #logger.debug("localhostbecontroller: using HEAD checkout in %s" % local_checkout_path)
85 return local_checkout_path
86
87
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 def setLayers(self, bitbake, layers, targets):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 """ a word of attention: by convention, the first layer for any build will be poky! """
90
91 assert self.be.sourcedir is not None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 # set layers in the layersource
93
94 # 1. get a list of repos with branches, and map dirpaths for each layer
95 gitrepos = {}
96
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 gitrepos[(bitbake.giturl, bitbake.commit)] = []
98 gitrepos[(bitbake.giturl, bitbake.commit)].append( ("bitbake", bitbake.dirpath) )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099
100 for layer in layers:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500101 # We don't need to git clone the layer for the CustomImageRecipe
102 # as it's generated by us layer on if needed
103 if CustomImageRecipe.LAYER_NAME in layer.name:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 continue
105 if not (layer.giturl, layer.commit) in gitrepos:
106 gitrepos[(layer.giturl, layer.commit)] = []
107 gitrepos[(layer.giturl, layer.commit)].append( (layer.name, layer.dirpath) )
108
109
110 logger.debug("localhostbecontroller, our git repos are %s" % pformat(gitrepos))
111
112
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500113 # 2. Note for future use if the current source directory is a
114 # checked-out git repos that could match a layer's vcs_url and therefore
115 # be used to speed up cloning (rather than fetching it again).
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117 cached_layers = {}
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500118
119 try:
120 for remotes in self._shellcmd("git remote -v", self.be.sourcedir).split("\n"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 try:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500122 remote = remotes.split("\t")[1].split(" ")[0]
123 if remote not in cached_layers:
124 cached_layers[remote] = self.be.sourcedir
125 except IndexError:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 pass
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500127 except ShellCmdException:
128 # ignore any errors in collecting git remotes this is an optional
129 # step
130 pass
131
132 logger.info("Using pre-checked out source for layer %s", cached_layers)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
134 layerlist = []
135
136
137 # 3. checkout the repositories
138 for giturl, commit in gitrepos.keys():
139 localdirname = os.path.join(self.be.sourcedir, self.getGitCloneDirectory(giturl, commit))
140 logger.debug("localhostbecontroller: giturl %s:%s checking out in current directory %s" % (giturl, commit, localdirname))
141
142 # make sure our directory is a git repository
143 if os.path.exists(localdirname):
144 localremotes = self._shellcmd("git remote -v", localdirname)
145 if not giturl in localremotes:
146 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))
147 else:
148 if giturl in cached_layers:
149 logger.debug("localhostbecontroller git-copying %s to %s" % (cached_layers[giturl], localdirname))
150 self._shellcmd("git clone \"%s\" \"%s\"" % (cached_layers[giturl], localdirname))
151 self._shellcmd("git remote remove origin", localdirname)
152 self._shellcmd("git remote add origin \"%s\"" % giturl, localdirname)
153 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500154 logger.debug("localhostbecontroller: cloning %s in %s" % (giturl, localdirname))
155 self._shellcmd('git clone "%s" "%s"' % (giturl, localdirname))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156
157 # branch magic name "HEAD" will inhibit checkout
158 if commit != "HEAD":
159 logger.debug("localhostbecontroller: checking out commit %s to %s " % (commit, localdirname))
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500160 ref = commit if re.match('^[a-fA-F0-9]+$', commit) else 'origin/%s' % commit
161 self._shellcmd('git fetch --all && git reset --hard "%s"' % ref, localdirname)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162
163 # take the localdirname as poky dir if we can find the oe-init-build-env
164 if self.pokydirname is None and os.path.exists(os.path.join(localdirname, "oe-init-build-env")):
165 logger.debug("localhostbecontroller: selected poky dir name %s" % localdirname)
166 self.pokydirname = localdirname
167
168 # make sure we have a working bitbake
169 if not os.path.exists(os.path.join(self.pokydirname, 'bitbake')):
170 logger.debug("localhostbecontroller: checking bitbake into the poky dirname %s " % self.pokydirname)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 self._shellcmd("git clone -b \"%s\" \"%s\" \"%s\" " % (bitbake.commit, bitbake.giturl, os.path.join(self.pokydirname, 'bitbake')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172
173 # verify our repositories
174 for name, dirpath in gitrepos[(giturl, commit)]:
175 localdirpath = os.path.join(localdirname, dirpath)
176 logger.debug("localhostbecontroller: localdirpath expected '%s'" % localdirpath)
177 if not os.path.exists(localdirpath):
178 raise BuildSetupException("Cannot find layer git path '%s' in checked out repository '%s:%s'. Aborting." % (localdirpath, giturl, commit))
179
180 if name != "bitbake":
181 layerlist.append(localdirpath.rstrip("/"))
182
183 logger.debug("localhostbecontroller: current layer list %s " % pformat(layerlist))
184
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185 # 5. create custom layer and add custom recipes to it
186 layerpath = os.path.join(self.be.builddir,
187 CustomImageRecipe.LAYER_NAME)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500188 for target in targets:
189 try:
190 customrecipe = CustomImageRecipe.objects.get(name=target.target,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500191 project=bitbake.req.project)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500192 except CustomImageRecipe.DoesNotExist:
193 continue # not a custom recipe, skip
194
195 # create directory structure
196 for name in ("conf", "recipes"):
197 path = os.path.join(layerpath, name)
198 if not os.path.isdir(path):
199 os.makedirs(path)
200
201 # create layer.oonf
202 config = os.path.join(layerpath, "conf", "layer.conf")
203 if not os.path.isfile(config):
204 with open(config, "w") as conf:
205 conf.write('BBPATH .= ":${LAYERDIR}"\nBBFILES += "${LAYERDIR}/recipes/*.bb"\n')
206
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500207 # Update the Layer_Version dirpath that has our base_recipe in
208 # to be able to read the base recipe to then generate the
209 # custom recipe.
210 br_layer_base_recipe = layers.get(
211 layer_version=customrecipe.base_recipe.layer_version)
212
213 br_layer_base_dirpath = \
214 os.path.join(self.be.sourcedir,
215 self.getGitCloneDirectory(
216 br_layer_base_recipe.giturl,
217 br_layer_base_recipe.commit),
218 customrecipe.base_recipe.layer_version.dirpath
219 )
220
221 customrecipe.base_recipe.layer_version.dirpath = \
222 br_layer_base_dirpath
223
224 customrecipe.base_recipe.layer_version.save()
225
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500226 # create recipe
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500227 recipe_path = \
228 os.path.join(layerpath, "recipes", "%s.bb" % target.target)
229 with open(recipe_path, "w") as recipef:
230 recipef.write(customrecipe.generate_recipe_file_contents())
231
232 # Update the layer and recipe objects
233 customrecipe.layer_version.dirpath = layerpath
234 customrecipe.layer_version.save()
235
236 customrecipe.file_path = recipe_path
237 customrecipe.save()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500238
239 # create *Layer* objects needed for build machinery to work
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240 BRLayer.objects.get_or_create(req=target.req,
241 name=layer.name,
242 dirpath=layerpath,
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500243 giturl="file://%s" % layerpath)
244 if os.path.isdir(layerpath):
245 layerlist.append(layerpath)
246
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247 self.islayerset = True
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500248 return layerlist
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249
250 def readServerLogFile(self):
251 return open(os.path.join(self.be.builddir, "toaster_server.log"), "r").read()
252
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500253
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500254 def triggerBuild(self, bitbake, layers, variables, targets, brbe):
255 layers = self.setLayers(bitbake, layers, targets)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500257 # init build environment from the clone
258 builddir = '%s-toaster-%d' % (self.be.builddir, bitbake.req.project.id)
259 oe_init = os.path.join(self.pokydirname, 'oe-init-build-env')
260 # init build environment
261 self._shellcmd("bash -c 'source %s %s'" % (oe_init, builddir),
262 self.be.sourcedir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500264 # update bblayers.conf
265 bblconfpath = os.path.join(builddir, "conf/bblayers.conf")
266 conflines = open(bblconfpath, "r").readlines()
267 skip = False
268 with open(bblconfpath, 'w') as bblayers:
269 for line in conflines:
270 if line.startswith("# line added by toaster"):
271 skip = True
272 continue
273 if skip:
274 skip = False
275 else:
276 bblayers.write(line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500278 bblayers.write('# line added by toaster build control\n'
279 'BBLAYERS = "%s"' % ' '.join(layers))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500281 # write configuration file
282 confpath = os.path.join(builddir, 'conf/toaster.conf')
283 with open(confpath, 'w') as conf:
284 for var in variables:
285 conf.write('%s="%s"\n' % (var.name, var.value))
286 conf.write('INHERIT+="toaster buildhistory"')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500288 # run bitbake server from the clone
289 bitbake = os.path.join(self.pokydirname, 'bitbake', 'bin', 'bitbake')
290 self._shellcmd('bash -c \"source %s %s; BITBAKE_UI="" %s --read %s '
291 '--server-only -t xmlrpc -B 0.0.0.0:0\"' % (oe_init,
292 builddir, bitbake, confpath), self.be.sourcedir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500294 # read port number from bitbake.lock
295 self.be.bbport = ""
296 bblock = os.path.join(builddir, 'bitbake.lock')
297 with open(bblock) as fplock:
298 for line in fplock:
299 if ":" in line:
300 self.be.bbport = line.split(":")[-1].strip()
301 logger.debug("localhostbecontroller: bitbake port %s", self.be.bbport)
302 break
303
304 if not self.be.bbport:
305 raise BuildSetupException("localhostbecontroller: can't read bitbake port from %s" % bblock)
306
307 self.be.bbaddress = "localhost"
308 self.be.bbstate = BuildEnvironment.SERVER_STARTED
309 self.be.lock = BuildEnvironment.LOCK_RUNNING
310 self.be.save()
311
312 bbtargets = ''
313 for target in targets:
314 task = target.task
315 if task:
316 if not task.startswith('do_'):
317 task = 'do_' + task
318 task = ':%s' % task
319 bbtargets += '%s%s ' % (target.target, task)
320
321 # run build with local bitbake. stop the server after the build.
322 log = os.path.join(builddir, 'toaster_ui.log')
323 local_bitbake = os.path.join(os.path.dirname(os.getenv('BBBASEDIR')),
324 'bitbake')
325 self._shellcmd(['bash -c \"(TOASTER_BRBE="%s" BBSERVER="0.0.0.0:-1" '
326 '%s %s -u toasterui --token="" >>%s 2>&1;'
327 'BITBAKE_UI="" BBSERVER=0.0.0.0:-1 %s -m)&\"' \
328 % (brbe, local_bitbake, bbtargets, log, bitbake)],
329 builddir, nowait=True)
330
331 logger.debug('localhostbecontroller: Build launched, exiting. '
332 'Follow build logs at %s' % log)