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 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 30 | from bldcontrol.models import BuildEnvironment, BuildRequest, BRLayer, BRVariable, BRTarget, BRBitbake, Build |
| 31 | from orm.models import CustomImageRecipe, Layer, Layer_Version, Project, ProjectLayer, ToasterSetting |
| 32 | from orm.models import signal_runbuilds |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | import subprocess |
| 34 | |
| 35 | from toastermain import settings |
| 36 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | from bldcontrol.bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, BitbakeController |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | |
| 39 | import logging |
| 40 | logger = logging.getLogger("toaster") |
| 41 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 42 | install_dir = os.environ.get('TOASTER_DIR') |
| 43 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | from pprint import pprint, pformat |
| 45 | |
| 46 | class 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | self.pokydirname = None |
| 56 | self.islayerset = False |
| 57 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | def _shellcmd(self, command, cwd=None, nowait=False,env=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | if cwd is None: |
| 60 | cwd = self.be.sourcedir |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 61 | if env is None: |
| 62 | env=os.environ.copy() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 64 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 66 | if nowait: |
| 67 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | (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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | logger.warning("localhostbecontroller: shellcmd error %s" % err) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | raise ShellCmdException(err) |
| 77 | else: |
| 78 | logger.debug("localhostbecontroller: shellcmd success") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 79 | return out.decode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | def getGitCloneDirectory(self, url, branch): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 82 | """Construct unique clone directory name out of url and branch.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | if branch != "HEAD": |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | return "_toaster_clones/_%s_%s" % (re.sub('[:/@+%]', '_', url), branch) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
| 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 93 | def setCloneStatus(self,bitbake,status,total,current,repo_name): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 94 | bitbake.req.build.repos_cloned=current |
| 95 | bitbake.req.build.repos_to_clone=total |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 96 | bitbake.req.build.progress_item=repo_name |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 97 | bitbake.req.build.save() |
| 98 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 99 | def setLayers(self, bitbake, layers, targets): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | """ 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 103 | |
| 104 | layerlist = [] |
| 105 | nongitlayerlist = [] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 106 | layer_index = 0 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 107 | git_env = os.environ.copy() |
| 108 | # (note: add custom environment settings here) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | # 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 120 | ("bitbake", bitbake.dirpath, 0)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | |
| 122 | for layer in layers: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 123 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | continue |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | |
| 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 131 | nongitlayerlist.append( "%03d:%s" % (layer_index,layer.local_source_dir) ) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 132 | continue |
| 133 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | if not (layer.giturl, layer.commit) in gitrepos: |
| 135 | gitrepos[(layer.giturl, layer.commit)] = [] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 136 | gitrepos[(layer.giturl, layer.commit)].append( (layer.name,layer.dirpath,layer_index) ) |
| 137 | layer_index += 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | |
| 139 | |
| 140 | logger.debug("localhostbecontroller, our git repos are %s" % pformat(gitrepos)) |
| 141 | |
| 142 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 143 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 146 | |
| 147 | cached_layers = {} |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 148 | |
| 149 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 150 | 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] | 151 | try: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 152 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | pass |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 157 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | # 3. checkout the repositories |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 165 | clone_count=0 |
| 166 | clone_total=len(gitrepos.keys()) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 167 | self.setCloneStatus(bitbake,'Started',clone_total,clone_count,'') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | for giturl, commit in gitrepos.keys(): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 169 | self.setCloneStatus(bitbake,'progress',clone_total,clone_count,gitrepos[(giturl, commit)][0][0]) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 170 | clone_count += 1 |
| 171 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 175 | # see if our directory is a git repository |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | if os.path.exists(localdirname): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 177 | try: |
| 178 | localremotes = self._shellcmd("git remote -v", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 179 | localdirname,env=git_env) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 180 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 185 | except ShellCmdException: |
| 186 | # our localdirname might not be a git repository |
| 187 | #- that's fine |
| 188 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 189 | else: |
| 190 | if giturl in cached_layers: |
| 191 | logger.debug("localhostbecontroller git-copying %s to %s" % (cached_layers[giturl], localdirname)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 192 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 195 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 196 | logger.debug("localhostbecontroller: cloning %s in %s" % (giturl, localdirname)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 197 | self._shellcmd('git clone "%s" "%s"' % (giturl, localdirname),env=git_env) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | |
| 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 202 | 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] | 203 | 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] | 204 | |
| 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 213 | 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] | 214 | |
| 215 | # verify our repositories |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 216 | for name, dirpath, index in gitrepos[(giturl, commit)]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 217 | localdirpath = os.path.join(localdirname, dirpath) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 218 | logger.debug("localhostbecontroller: localdirpath expects '%s'" % localdirpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 223 | layerlist.append("%03d:%s" % (index,localdirpath.rstrip("/"))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 225 | self.setCloneStatus(bitbake,'complete',clone_total,clone_count,'') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | logger.debug("localhostbecontroller: current layer list %s " % pformat(layerlist)) |
| 227 | |
Brad Bishop | 5dd7cbb | 2018-09-05 22:26:40 -0700 | [diff] [blame] | 228 | # 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 243 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 244 | # 5. create custom layer and add custom recipes to it |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 245 | for target in targets: |
| 246 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 247 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 255 | layerlist.append("%03d:%s" % (layer_index,custom_layer_path)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 256 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 257 | except CustomImageRecipe.DoesNotExist: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 258 | continue # not a custom recipe, skip |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 259 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 260 | layerlist.extend(nongitlayerlist) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 261 | logger.debug("\n\nset layers gives this list %s" % pformat(layerlist)) |
| 262 | self.islayerset = True |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 263 | |
| 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 268 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 269 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 331 | def readServerLogFile(self): |
| 332 | return open(os.path.join(self.be.builddir, "toaster_server.log"), "r").read() |
| 333 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 334 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 335 | def triggerBuild(self, bitbake, layers, variables, targets, brbe): |
| 336 | layers = self.setLayers(bitbake, layers, targets) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 337 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 348 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 349 | # init build environment from the clone |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 350 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 354 | oe_init = os.path.join(self.pokydirname, 'oe-init-build-env') |
| 355 | # init build environment |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 356 | try: |
| 357 | custom_script = ToasterSetting.objects.get(name="CUSTOM_BUILD_INIT_SCRIPT").value |
| 358 | custom_script = custom_script.replace("%BUILDDIR%" ,builddir) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 359 | self._shellcmd("bash -c 'source %s'" % (custom_script),env=git_env) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 360 | except ToasterSetting.DoesNotExist: |
| 361 | self._shellcmd("bash -c 'source %s %s'" % (oe_init, builddir), |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 362 | self.be.sourcedir,env=git_env) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 363 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 364 | # update bblayers.conf |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 365 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 370 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 371 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 446 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 447 | # clean the Toaster to build environment |
| 448 | env_clean = 'unset BBPATH;' # clean BBPATH for <= YP-2.4.0 |
| 449 | |
Brad Bishop | 5dd7cbb | 2018-09-05 22:26:40 -0700 | [diff] [blame] | 450 | # 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 463 | toasterlayers = os.path.join(builddir,"conf/toaster-bblayers.conf") |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 464 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 472 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 473 | # read port number from bitbake.lock |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 474 | self.be.bbport = -1 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 475 | bblock = os.path.join(builddir, 'bitbake.lock') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 476 | # 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 489 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 496 | if -1 == self.be.bbport: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 497 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 517 | if not is_merged_attr: |
| 518 | self._shellcmd(['%s bash -c \"(TOASTER_BRBE="%s" BBSERVER="0.0.0.0:%s" ' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 519 | '%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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 523 | builddir, nowait=True) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 524 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 531 | |
| 532 | logger.debug('localhostbecontroller: Build launched, exiting. ' |
| 533 | 'Follow build logs at %s' % log) |