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 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 27 | import time |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | from django.db import transaction |
| 29 | from django.db.models import Q |
| 30 | from bldcontrol.models import BuildEnvironment, BRLayer, BRVariable, BRTarget, BRBitbake |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 31 | from orm.models import CustomImageRecipe, Layer, Layer_Version, ProjectLayer, ToasterSetting |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | import subprocess |
| 33 | |
| 34 | from toastermain import settings |
| 35 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 36 | from bldcontrol.bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, BitbakeController |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | import logging |
| 39 | logger = logging.getLogger("toaster") |
| 40 | |
| 41 | from pprint import pprint, pformat |
| 42 | |
| 43 | class LocalhostBEController(BuildEnvironmentController): |
| 44 | """ Implementation of the BuildEnvironmentController for the localhost; |
| 45 | this controller manages the default build directory, |
| 46 | the server setup and system start and stop for the localhost-type build environment |
| 47 | |
| 48 | """ |
| 49 | |
| 50 | def __init__(self, be): |
| 51 | super(LocalhostBEController, self).__init__(be) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | self.pokydirname = None |
| 53 | self.islayerset = False |
| 54 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 55 | def _shellcmd(self, command, cwd=None, nowait=False,env=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | if cwd is None: |
| 57 | cwd = self.be.sourcedir |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | if env is None: |
| 59 | env=os.environ.copy() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 61 | logger.debug("lbc_shellcmd: (%s) %s" % (cwd, command)) |
| 62 | p = subprocess.Popen(command, cwd = cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 63 | if nowait: |
| 64 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | (out,err) = p.communicate() |
| 66 | p.wait() |
| 67 | if p.returncode: |
| 68 | if len(err) == 0: |
| 69 | err = "command: %s \n%s" % (command, out) |
| 70 | else: |
| 71 | err = "command: %s \n%s" % (command, err) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 72 | logger.warning("localhostbecontroller: shellcmd error %s" % err) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | raise ShellCmdException(err) |
| 74 | else: |
| 75 | logger.debug("localhostbecontroller: shellcmd success") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | return out.decode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | def getGitCloneDirectory(self, url, branch): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 79 | """Construct unique clone directory name out of url and branch.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | if branch != "HEAD": |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 81 | return "_toaster_clones/_%s_%s" % (re.sub('[:/@+%]', '_', url), branch) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
| 83 | # word of attention; this is a localhost-specific issue; only on the localhost we expect to have "HEAD" releases |
| 84 | # which _ALWAYS_ means the current poky checkout |
| 85 | from os.path import dirname as DN |
| 86 | local_checkout_path = DN(DN(DN(DN(DN(os.path.abspath(__file__)))))) |
| 87 | #logger.debug("localhostbecontroller: using HEAD checkout in %s" % local_checkout_path) |
| 88 | return local_checkout_path |
| 89 | |
| 90 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 91 | def setCloneStatus(self,bitbake,status,total,current): |
| 92 | bitbake.req.build.repos_cloned=current |
| 93 | bitbake.req.build.repos_to_clone=total |
| 94 | bitbake.req.build.save() |
| 95 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 96 | def setLayers(self, bitbake, layers, targets): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | """ a word of attention: by convention, the first layer for any build will be poky! """ |
| 98 | |
| 99 | assert self.be.sourcedir is not None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | |
| 101 | layerlist = [] |
| 102 | nongitlayerlist = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 103 | git_env = os.environ.copy() |
| 104 | # (note: add custom environment settings here) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 105 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | # set layers in the layersource |
| 107 | |
| 108 | # 1. get a list of repos with branches, and map dirpaths for each layer |
| 109 | gitrepos = {} |
| 110 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | # if we're using a remotely fetched version of bitbake add its git |
| 112 | # details to the list of repos to clone |
| 113 | if bitbake.giturl and bitbake.commit: |
| 114 | gitrepos[(bitbake.giturl, bitbake.commit)] = [] |
| 115 | gitrepos[(bitbake.giturl, bitbake.commit)].append( |
| 116 | ("bitbake", bitbake.dirpath)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | |
| 118 | for layer in layers: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 | # We don't need to git clone the layer for the CustomImageRecipe |
| 120 | # as it's generated by us layer on if needed |
| 121 | if CustomImageRecipe.LAYER_NAME in layer.name: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | continue |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 123 | |
| 124 | # If we have local layers then we don't need clone them |
| 125 | # For local layers giturl will be empty |
| 126 | if not layer.giturl: |
| 127 | nongitlayerlist.append(layer.layer_version.layer.local_source_dir) |
| 128 | continue |
| 129 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | if not (layer.giturl, layer.commit) in gitrepos: |
| 131 | gitrepos[(layer.giturl, layer.commit)] = [] |
| 132 | gitrepos[(layer.giturl, layer.commit)].append( (layer.name, layer.dirpath) ) |
| 133 | |
| 134 | |
| 135 | logger.debug("localhostbecontroller, our git repos are %s" % pformat(gitrepos)) |
| 136 | |
| 137 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 138 | # 2. Note for future use if the current source directory is a |
| 139 | # checked-out git repos that could match a layer's vcs_url and therefore |
| 140 | # be used to speed up cloning (rather than fetching it again). |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
| 142 | cached_layers = {} |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 143 | |
| 144 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 145 | for remotes in self._shellcmd("git remote -v", self.be.sourcedir,env=git_env).split("\n"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 146 | try: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 147 | remote = remotes.split("\t")[1].split(" ")[0] |
| 148 | if remote not in cached_layers: |
| 149 | cached_layers[remote] = self.be.sourcedir |
| 150 | except IndexError: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | pass |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 152 | except ShellCmdException: |
| 153 | # ignore any errors in collecting git remotes this is an optional |
| 154 | # step |
| 155 | pass |
| 156 | |
| 157 | logger.info("Using pre-checked out source for layer %s", cached_layers) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | # 3. checkout the repositories |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 160 | clone_count=0 |
| 161 | clone_total=len(gitrepos.keys()) |
| 162 | self.setCloneStatus(bitbake,'Started',clone_total,clone_count) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | for giturl, commit in gitrepos.keys(): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 164 | self.setCloneStatus(bitbake,'progress',clone_total,clone_count) |
| 165 | clone_count += 1 |
| 166 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 167 | localdirname = os.path.join(self.be.sourcedir, self.getGitCloneDirectory(giturl, commit)) |
| 168 | logger.debug("localhostbecontroller: giturl %s:%s checking out in current directory %s" % (giturl, commit, localdirname)) |
| 169 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 170 | # see if our directory is a git repository |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | if os.path.exists(localdirname): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 172 | try: |
| 173 | localremotes = self._shellcmd("git remote -v", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 174 | localdirname,env=git_env) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 175 | if not giturl in localremotes and commit != 'HEAD': |
| 176 | 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)) |
| 177 | except ShellCmdException: |
| 178 | # our localdirname might not be a git repository |
| 179 | #- that's fine |
| 180 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 181 | else: |
| 182 | if giturl in cached_layers: |
| 183 | logger.debug("localhostbecontroller git-copying %s to %s" % (cached_layers[giturl], localdirname)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 184 | self._shellcmd("git clone \"%s\" \"%s\"" % (cached_layers[giturl], localdirname),env=git_env) |
| 185 | self._shellcmd("git remote remove origin", localdirname,env=git_env) |
| 186 | self._shellcmd("git remote add origin \"%s\"" % giturl, localdirname,env=git_env) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 187 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 188 | logger.debug("localhostbecontroller: cloning %s in %s" % (giturl, localdirname)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 189 | self._shellcmd('git clone "%s" "%s"' % (giturl, localdirname),env=git_env) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | |
| 191 | # branch magic name "HEAD" will inhibit checkout |
| 192 | if commit != "HEAD": |
| 193 | logger.debug("localhostbecontroller: checking out commit %s to %s " % (commit, localdirname)) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 194 | ref = commit if re.match('^[a-fA-F0-9]+$', commit) else 'origin/%s' % commit |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 195 | self._shellcmd('git fetch && git reset --hard "%s"' % ref, localdirname,env=git_env) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 196 | |
| 197 | # take the localdirname as poky dir if we can find the oe-init-build-env |
| 198 | if self.pokydirname is None and os.path.exists(os.path.join(localdirname, "oe-init-build-env")): |
| 199 | logger.debug("localhostbecontroller: selected poky dir name %s" % localdirname) |
| 200 | self.pokydirname = localdirname |
| 201 | |
| 202 | # make sure we have a working bitbake |
| 203 | if not os.path.exists(os.path.join(self.pokydirname, 'bitbake')): |
| 204 | logger.debug("localhostbecontroller: checking bitbake into the poky dirname %s " % self.pokydirname) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 205 | self._shellcmd("git clone -b \"%s\" \"%s\" \"%s\" " % (bitbake.commit, bitbake.giturl, os.path.join(self.pokydirname, 'bitbake')),env=git_env) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 206 | |
| 207 | # verify our repositories |
| 208 | for name, dirpath in gitrepos[(giturl, commit)]: |
| 209 | localdirpath = os.path.join(localdirname, dirpath) |
| 210 | logger.debug("localhostbecontroller: localdirpath expected '%s'" % localdirpath) |
| 211 | if not os.path.exists(localdirpath): |
| 212 | raise BuildSetupException("Cannot find layer git path '%s' in checked out repository '%s:%s'. Aborting." % (localdirpath, giturl, commit)) |
| 213 | |
| 214 | if name != "bitbake": |
| 215 | layerlist.append(localdirpath.rstrip("/")) |
| 216 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 217 | self.setCloneStatus(bitbake,'complete',clone_total,clone_count) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 218 | logger.debug("localhostbecontroller: current layer list %s " % pformat(layerlist)) |
| 219 | |
Brad Bishop | 5dd7cbb | 2018-09-05 22:26:40 -0700 | [diff] [blame] | 220 | # Resolve self.pokydirname if not resolved yet, consider the scenario |
| 221 | # where all layers are local, that's the else clause |
| 222 | if self.pokydirname is None: |
| 223 | if os.path.exists(os.path.join(self.be.sourcedir, "oe-init-build-env")): |
| 224 | logger.debug("localhostbecontroller: selected poky dir name %s" % self.be.sourcedir) |
| 225 | self.pokydirname = self.be.sourcedir |
| 226 | else: |
| 227 | # Alternatively, scan local layers for relative "oe-init-build-env" location |
| 228 | for layer in layers: |
| 229 | if os.path.exists(os.path.join(layer.layer_version.layer.local_source_dir,"..","oe-init-build-env")): |
| 230 | logger.debug("localhostbecontroller, setting pokydirname to %s" % (layer.layer_version.layer.local_source_dir)) |
| 231 | self.pokydirname = os.path.join(layer.layer_version.layer.local_source_dir,"..") |
| 232 | break |
| 233 | else: |
| 234 | logger.error("pokydirname is not set, you will run into trouble!") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 235 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 236 | # 5. create custom layer and add custom recipes to it |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 237 | for target in targets: |
| 238 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 239 | customrecipe = CustomImageRecipe.objects.get( |
| 240 | name=target.target, |
| 241 | project=bitbake.req.project) |
| 242 | |
| 243 | custom_layer_path = self.setup_custom_image_recipe( |
| 244 | customrecipe, layers) |
| 245 | |
| 246 | if os.path.isdir(custom_layer_path): |
| 247 | layerlist.append(custom_layer_path) |
| 248 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 249 | except CustomImageRecipe.DoesNotExist: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 250 | continue # not a custom recipe, skip |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 251 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 252 | layerlist.extend(nongitlayerlist) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 253 | logger.debug("\n\nset layers gives this list %s" % pformat(layerlist)) |
| 254 | self.islayerset = True |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 255 | return layerlist |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 257 | def setup_custom_image_recipe(self, customrecipe, layers): |
| 258 | """ Set up toaster-custom-images layer and recipe files """ |
| 259 | layerpath = os.path.join(self.be.builddir, |
| 260 | CustomImageRecipe.LAYER_NAME) |
| 261 | |
| 262 | # create directory structure |
| 263 | for name in ("conf", "recipes"): |
| 264 | path = os.path.join(layerpath, name) |
| 265 | if not os.path.isdir(path): |
| 266 | os.makedirs(path) |
| 267 | |
| 268 | # create layer.conf |
| 269 | config = os.path.join(layerpath, "conf", "layer.conf") |
| 270 | if not os.path.isfile(config): |
| 271 | with open(config, "w") as conf: |
| 272 | conf.write('BBPATH .= ":${LAYERDIR}"\nBBFILES += "${LAYERDIR}/recipes/*.bb"\n') |
| 273 | |
| 274 | # Update the Layer_Version dirpath that has our base_recipe in |
| 275 | # to be able to read the base recipe to then generate the |
| 276 | # custom recipe. |
| 277 | br_layer_base_recipe = layers.get( |
| 278 | layer_version=customrecipe.base_recipe.layer_version) |
| 279 | |
| 280 | # If the layer is one that we've cloned we know where it lives |
| 281 | if br_layer_base_recipe.giturl and br_layer_base_recipe.commit: |
| 282 | layer_path = self.getGitCloneDirectory( |
| 283 | br_layer_base_recipe.giturl, |
| 284 | br_layer_base_recipe.commit) |
| 285 | # Otherwise it's a local layer |
| 286 | elif br_layer_base_recipe.local_source_dir: |
| 287 | layer_path = br_layer_base_recipe.local_source_dir |
| 288 | else: |
| 289 | logger.error("Unable to workout the dir path for the custom" |
| 290 | " image recipe") |
| 291 | |
| 292 | br_layer_base_dirpath = os.path.join( |
| 293 | self.be.sourcedir, |
| 294 | layer_path, |
| 295 | customrecipe.base_recipe.layer_version.dirpath) |
| 296 | |
| 297 | customrecipe.base_recipe.layer_version.dirpath = br_layer_base_dirpath |
| 298 | |
| 299 | customrecipe.base_recipe.layer_version.save() |
| 300 | |
| 301 | # create recipe |
| 302 | recipe_path = os.path.join(layerpath, "recipes", "%s.bb" % |
| 303 | customrecipe.name) |
| 304 | with open(recipe_path, "w") as recipef: |
| 305 | recipef.write(customrecipe.generate_recipe_file_contents()) |
| 306 | |
| 307 | # Update the layer and recipe objects |
| 308 | customrecipe.layer_version.dirpath = layerpath |
| 309 | customrecipe.layer_version.layer.local_source_dir = layerpath |
| 310 | customrecipe.layer_version.layer.save() |
| 311 | customrecipe.layer_version.save() |
| 312 | |
| 313 | customrecipe.file_path = recipe_path |
| 314 | customrecipe.save() |
| 315 | |
| 316 | return layerpath |
| 317 | |
| 318 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 319 | def readServerLogFile(self): |
| 320 | return open(os.path.join(self.be.builddir, "toaster_server.log"), "r").read() |
| 321 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 322 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 323 | def triggerBuild(self, bitbake, layers, variables, targets, brbe): |
| 324 | layers = self.setLayers(bitbake, layers, targets) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 325 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 326 | # init build environment from the clone |
| 327 | builddir = '%s-toaster-%d' % (self.be.builddir, bitbake.req.project.id) |
| 328 | oe_init = os.path.join(self.pokydirname, 'oe-init-build-env') |
| 329 | # init build environment |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 330 | try: |
| 331 | custom_script = ToasterSetting.objects.get(name="CUSTOM_BUILD_INIT_SCRIPT").value |
| 332 | custom_script = custom_script.replace("%BUILDDIR%" ,builddir) |
| 333 | self._shellcmd("bash -c 'source %s'" % (custom_script)) |
| 334 | except ToasterSetting.DoesNotExist: |
| 335 | self._shellcmd("bash -c 'source %s %s'" % (oe_init, builddir), |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 336 | self.be.sourcedir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 337 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 338 | # update bblayers.conf |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 339 | bblconfpath = os.path.join(builddir, "conf/toaster-bblayers.conf") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 340 | with open(bblconfpath, 'w') as bblayers: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 341 | bblayers.write('# line added by toaster build control\n' |
| 342 | 'BBLAYERS = "%s"' % ' '.join(layers)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 343 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 344 | # write configuration file |
| 345 | confpath = os.path.join(builddir, 'conf/toaster.conf') |
| 346 | with open(confpath, 'w') as conf: |
| 347 | for var in variables: |
| 348 | conf.write('%s="%s"\n' % (var.name, var.value)) |
| 349 | conf.write('INHERIT+="toaster buildhistory"') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 350 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 351 | # clean the Toaster to build environment |
| 352 | env_clean = 'unset BBPATH;' # clean BBPATH for <= YP-2.4.0 |
| 353 | |
Brad Bishop | 5dd7cbb | 2018-09-05 22:26:40 -0700 | [diff] [blame] | 354 | # run bitbake server from the clone if available |
| 355 | # otherwise pick it from the PATH |
| 356 | bitbake = os.path.join(self.pokydirname, 'bitbake', 'bin', 'bitbake') |
| 357 | if not os.path.exists(bitbake): |
| 358 | logger.info("Bitbake not available under %s, will try to use it from PATH" % |
| 359 | self.pokydirname) |
| 360 | for path in os.environ["PATH"].split(os.pathsep): |
| 361 | if os.path.exists(os.path.join(path, 'bitbake')): |
| 362 | bitbake = os.path.join(path, 'bitbake') |
Awais Belal | 34261d6 | 2018-09-30 00:31:09 -0700 | [diff] [blame] | 363 | logger.info("Found Bitbake at: %s" % path) |
Brad Bishop | 5dd7cbb | 2018-09-05 22:26:40 -0700 | [diff] [blame] | 364 | break |
| 365 | else: |
| 366 | logger.error("Looks like Bitbake is not available, please fix your environment") |
| 367 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 368 | # run bitbake server from the clone |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 369 | toasterlayers = os.path.join(builddir,"conf/toaster-bblayers.conf") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 370 | self._shellcmd('%s bash -c \"source %s %s; BITBAKE_UI="knotty" %s --read %s --read %s ' |
| 371 | '--server-only -B 0.0.0.0:0\"' % (env_clean, oe_init, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 372 | builddir, bitbake, confpath, toasterlayers), self.be.sourcedir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 373 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 374 | # read port number from bitbake.lock |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 375 | self.be.bbport = -1 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 376 | bblock = os.path.join(builddir, 'bitbake.lock') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 377 | # allow 10 seconds for bb lock file to appear but also be populated |
| 378 | for lock_check in range(10): |
| 379 | if not os.path.exists(bblock): |
| 380 | logger.debug("localhostbecontroller: waiting for bblock file to appear") |
| 381 | time.sleep(1) |
| 382 | continue |
| 383 | if 10 < os.stat(bblock).st_size: |
| 384 | break |
| 385 | logger.debug("localhostbecontroller: waiting for bblock content to appear") |
| 386 | time.sleep(1) |
| 387 | else: |
| 388 | raise BuildSetupException("Cannot find bitbake server lock file '%s'. Aborting." % bblock) |
| 389 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 390 | with open(bblock) as fplock: |
| 391 | for line in fplock: |
| 392 | if ":" in line: |
| 393 | self.be.bbport = line.split(":")[-1].strip() |
| 394 | logger.debug("localhostbecontroller: bitbake port %s", self.be.bbport) |
| 395 | break |
| 396 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 397 | if -1 == self.be.bbport: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 398 | raise BuildSetupException("localhostbecontroller: can't read bitbake port from %s" % bblock) |
| 399 | |
| 400 | self.be.bbaddress = "localhost" |
| 401 | self.be.bbstate = BuildEnvironment.SERVER_STARTED |
| 402 | self.be.lock = BuildEnvironment.LOCK_RUNNING |
| 403 | self.be.save() |
| 404 | |
| 405 | bbtargets = '' |
| 406 | for target in targets: |
| 407 | task = target.task |
| 408 | if task: |
| 409 | if not task.startswith('do_'): |
| 410 | task = 'do_' + task |
| 411 | task = ':%s' % task |
| 412 | bbtargets += '%s%s ' % (target.target, task) |
| 413 | |
| 414 | # run build with local bitbake. stop the server after the build. |
| 415 | log = os.path.join(builddir, 'toaster_ui.log') |
| 416 | local_bitbake = os.path.join(os.path.dirname(os.getenv('BBBASEDIR')), |
| 417 | 'bitbake') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 418 | self._shellcmd(['%s bash -c \"(TOASTER_BRBE="%s" BBSERVER="0.0.0.0:%s" ' |
| 419 | '%s %s -u toasterui --read %s --read %s --token="" >>%s 2>&1;' |
| 420 | 'BITBAKE_UI="knotty" BBSERVER=0.0.0.0:%s %s -m)&\"' \ |
| 421 | % (env_clean, brbe, self.be.bbport, local_bitbake, bbtargets, confpath, toasterlayers, log, |
| 422 | self.be.bbport, bitbake,)], |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 423 | builddir, nowait=True) |
| 424 | |
| 425 | logger.debug('localhostbecontroller: Build launched, exiting. ' |
| 426 | 'Follow build logs at %s' % log) |