Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 26 | import shutil |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | from django.db import transaction |
| 28 | from django.db.models import Q |
| 29 | from bldcontrol.models import BuildEnvironment, BRLayer, BRVariable, BRTarget, BRBitbake |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 30 | from orm.models import CustomImageRecipe, Layer, Layer_Version, ProjectLayer |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | import subprocess |
| 32 | |
| 33 | from toastermain import settings |
| 34 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | from bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, BitbakeController |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | |
| 37 | import logging |
| 38 | logger = logging.getLogger("toaster") |
| 39 | |
| 40 | from pprint import pprint, pformat |
| 41 | |
| 42 | class 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | self.pokydirname = None |
| 52 | self.islayerset = False |
| 53 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 54 | def _shellcmd(self, command, cwd=None, nowait=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 60 | if nowait: |
| 61 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | (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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | def getGitCloneDirectory(self, url, branch): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 76 | """Construct unique clone directory name out of url and branch.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | if branch != "HEAD": |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 78 | return "_toaster_clones/_%s_%s" % (re.sub('[:/@%]', '_', url), branch) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 | def setLayers(self, bitbake, layers, targets): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | """ 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | # 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 97 | gitrepos[(bitbake.giturl, bitbake.commit)] = [] |
| 98 | gitrepos[(bitbake.giturl, bitbake.commit)].append( ("bitbake", bitbake.dirpath) ) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | |
| 100 | for layer in layers: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 101 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 113 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |
| 117 | cached_layers = {} |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 118 | |
| 119 | try: |
| 120 | for remotes in self._shellcmd("git remote -v", self.be.sourcedir).split("\n"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | try: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 122 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | pass |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 127 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | |
| 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 154 | logger.debug("localhostbecontroller: cloning %s in %s" % (giturl, localdirname)) |
| 155 | self._shellcmd('git clone "%s" "%s"' % (giturl, localdirname)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | |
| 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 160 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 171 | self._shellcmd("git clone -b \"%s\" \"%s\" \"%s\" " % (bitbake.commit, bitbake.giturl, os.path.join(self.pokydirname, 'bitbake'))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 185 | # 5. create custom layer and add custom recipes to it |
| 186 | layerpath = os.path.join(self.be.builddir, |
| 187 | CustomImageRecipe.LAYER_NAME) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 188 | for target in targets: |
| 189 | try: |
| 190 | customrecipe = CustomImageRecipe.objects.get(name=target.target, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 191 | project=bitbake.req.project) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 192 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 207 | # 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 226 | # create recipe |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 227 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 238 | |
| 239 | # create *Layer* objects needed for build machinery to work |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 240 | BRLayer.objects.get_or_create(req=target.req, |
| 241 | name=layer.name, |
| 242 | dirpath=layerpath, |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 243 | giturl="file://%s" % layerpath) |
| 244 | if os.path.isdir(layerpath): |
| 245 | layerlist.append(layerpath) |
| 246 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | self.islayerset = True |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 248 | return layerlist |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | |
| 250 | def readServerLogFile(self): |
| 251 | return open(os.path.join(self.be.builddir, "toaster_server.log"), "r").read() |
| 252 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 253 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 254 | def triggerBuild(self, bitbake, layers, variables, targets, brbe): |
| 255 | layers = self.setLayers(bitbake, layers, targets) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 257 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 263 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 264 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 277 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 278 | bblayers.write('# line added by toaster build control\n' |
| 279 | 'BBLAYERS = "%s"' % ' '.join(layers)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 280 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 281 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 287 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 288 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 293 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 294 | # 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) |