Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2014 Intel Corporation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | import os |
| 10 | import sys |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | from django.db.models import Q |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 12 | from bldcontrol.models import BuildEnvironment, BRLayer, BRBitbake |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | |
| 14 | # load Bitbake components |
| 15 | path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
| 16 | sys.path.insert(0, path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | |
| 18 | class BitbakeController(object): |
| 19 | """ This is the basic class that controlls a bitbake server. |
| 20 | It is outside the scope of this class on how the server is started and aquired |
| 21 | """ |
| 22 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 23 | def __init__(self, be): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 24 | import bb.server.xmlrpcclient |
| 25 | self.connection = bb.server.xmlrpcclient._create_server(be.bbaddress, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 26 | int(be.bbport))[0] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | def _runCommand(self, command): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 29 | result, error = self.connection.runCommand(command) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | if error: |
| 31 | raise Exception(error) |
| 32 | return result |
| 33 | |
| 34 | def disconnect(self): |
| 35 | return self.connection.removeClient() |
| 36 | |
| 37 | def setVariable(self, name, value): |
| 38 | return self._runCommand(["setVariable", name, value]) |
| 39 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 40 | def getVariable(self, name): |
| 41 | return self._runCommand(["getVariable", name]) |
| 42 | |
| 43 | def triggerEvent(self, event): |
| 44 | return self._runCommand(["triggerEvent", event]) |
| 45 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | def build(self, targets, task = None): |
| 47 | if task is None: |
| 48 | task = "build" |
| 49 | return self._runCommand(["buildTargets", targets, task]) |
| 50 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 51 | def forceShutDown(self): |
| 52 | return self._runCommand(["stateForceShutdown"]) |
| 53 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def getBuildEnvironmentController(**kwargs): |
| 57 | """ Gets you a BuildEnvironmentController that encapsulates a build environment, |
| 58 | based on the query dictionary sent in. |
| 59 | |
| 60 | This is used to retrieve, for example, the currently running BE from inside |
| 61 | the toaster UI, or find a new BE to start a new build in it. |
| 62 | |
| 63 | The return object MUST always be a BuildEnvironmentController. |
| 64 | """ |
| 65 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | from bldcontrol.localhostbecontroller import LocalhostBEController |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | |
| 68 | be = BuildEnvironment.objects.filter(Q(**kwargs))[0] |
| 69 | if be.betype == BuildEnvironment.TYPE_LOCAL: |
| 70 | return LocalhostBEController(be) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | else: |
| 72 | raise Exception("FIXME: Implement BEC for type %s" % str(be.betype)) |
| 73 | |
| 74 | |
| 75 | class BuildEnvironmentController(object): |
| 76 | """ BuildEnvironmentController (BEC) is the abstract class that defines the operations that MUST |
| 77 | or SHOULD be supported by a Build Environment. It is used to establish the framework, and must |
| 78 | not be instantiated directly by the user. |
| 79 | |
| 80 | Use the "getBuildEnvironmentController()" function to get a working BEC for your remote. |
| 81 | |
| 82 | How the BuildEnvironments are discovered is outside the scope of this class. |
| 83 | |
| 84 | You must derive this class to teach Toaster how to operate in your own infrastructure. |
| 85 | We provide some specific BuildEnvironmentController classes that can be used either to |
| 86 | directly set-up Toaster infrastructure, or as a model for your own infrastructure set: |
| 87 | |
| 88 | * Localhost controller will run the Toaster BE on the same account as the web server |
| 89 | (current user if you are using the the Django development web server) |
| 90 | on the local machine, with the "build/" directory under the "poky/" source checkout directory. |
| 91 | Bash is expected to be available. |
| 92 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | """ |
| 94 | def __init__(self, be): |
| 95 | """ Takes a BuildEnvironment object as parameter that points to the settings of the BE. |
| 96 | """ |
| 97 | self.be = be |
| 98 | self.connection = None |
| 99 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | def setLayers(self, bitbake, ls): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | """ Checks-out bitbake executor and layers from git repositories. |
| 102 | Sets the layer variables in the config file, after validating local layer paths. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 103 | bitbake must be a single BRBitbake instance |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | The layer paths must be in a list of BRLayer object |
| 105 | |
| 106 | a word of attention: by convention, the first layer for any build will be poky! |
| 107 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 108 | raise NotImplementedError("FIXME: Must override setLayers") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | |
| 110 | def getArtifact(self, path): |
| 111 | """ This call returns an artifact identified by the 'path'. How 'path' is interpreted as |
| 112 | up to the implementing BEC. The return MUST be a REST URL where a GET will actually return |
| 113 | the content of the artifact, e.g. for use as a "download link" in a web UI. |
| 114 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 115 | raise NotImplementedError("Must return the REST URL of the artifact") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |
| 117 | def triggerBuild(self, bitbake, layers, variables, targets): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 118 | raise NotImplementedError("Must override BE release") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | |
| 120 | class ShellCmdException(Exception): |
| 121 | pass |
| 122 | |
| 123 | |
| 124 | class BuildSetupException(Exception): |
| 125 | pass |
| 126 | |