localhostbecontroller.py 24 KB

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