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