blob: 71c288df34cc86c1e85b440c8eb147f5abf99fe5 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002# BitBake Toaster Implementation
3#
4# Copyright (C) 2014 Intel Corporation
5#
Brad Bishopc342db32019-05-15 21:57:59 -04006# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
9import os
10import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011from django.db.models import Q
Andrew Geissler82c905d2020-04-13 13:39:40 -050012from bldcontrol.models import BuildEnvironment, BRLayer, BRBitbake
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
14# load Bitbake components
15path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
16sys.path.insert(0, path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017
18class 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 Williamsd8c66bc2016-06-20 12:57:21 -050023 def __init__(self, be):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024 import bb.server.xmlrpcclient
25 self.connection = bb.server.xmlrpcclient._create_server(be.bbaddress,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050026 int(be.bbport))[0]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
28 def _runCommand(self, command):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050029 result, error = self.connection.runCommand(command)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 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 Williamsd8c66bc2016-06-20 12:57:21 -050040 def getVariable(self, name):
41 return self._runCommand(["getVariable", name])
42
43 def triggerEvent(self, event):
44 return self._runCommand(["triggerEvent", event])
45
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 def build(self, targets, task = None):
47 if task is None:
48 task = "build"
49 return self._runCommand(["buildTargets", targets, task])
50
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050051 def forceShutDown(self):
52 return self._runCommand(["stateForceShutdown"])
53
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
55
56def 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 Williamsc0f7c042017-02-23 20:41:17 -060066 from bldcontrol.localhostbecontroller import LocalhostBEController
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
68 be = BuildEnvironment.objects.filter(Q(**kwargs))[0]
69 if be.betype == BuildEnvironment.TYPE_LOCAL:
70 return LocalhostBEController(be)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 else:
72 raise Exception("FIXME: Implement BEC for type %s" % str(be.betype))
73
74
75class 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 Williamsc124f4f2015-09-15 14:41:29 -050093 """
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 Williamsd8c66bc2016-06-20 12:57:21 -0500100 def setLayers(self, bitbake, ls):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 """ Checks-out bitbake executor and layers from git repositories.
102 Sets the layer variables in the config file, after validating local layer paths.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 bitbake must be a single BRBitbake instance
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 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 Williamsd8c66bc2016-06-20 12:57:21 -0500108 raise NotImplementedError("FIXME: Must override setLayers")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
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 Williamsd8c66bc2016-06-20 12:57:21 -0500115 raise NotImplementedError("Must return the REST URL of the artifact")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117 def triggerBuild(self, bitbake, layers, variables, targets):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 raise NotImplementedError("Must override BE release")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119
120class ShellCmdException(Exception):
121 pass
122
123
124class BuildSetupException(Exception):
125 pass
126