1#
2# BitBake Toaster Implementation
3#
4# Copyright (C) 2014        Intel Corporation
5#
6# SPDX-License-Identifier: GPL-2.0-only
7#
8
9import os
10import sys
11from django.db.models import Q
12from bldcontrol.models import BuildEnvironment, BRLayer, BRBitbake
13
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)
17
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
23    def __init__(self, be):
24        import bb.server.xmlrpcclient
25        self.connection = bb.server.xmlrpcclient._create_server(be.bbaddress,
26                                                          int(be.bbport))[0]
27
28    def _runCommand(self, command):
29        result, error = self.connection.runCommand(command)
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
40    def getVariable(self, name):
41        return self._runCommand(["getVariable", name])
42
43    def triggerEvent(self, event):
44        return self._runCommand(["triggerEvent", event])
45
46    def build(self, targets, task = None):
47        if task is None:
48            task = "build"
49        return self._runCommand(["buildTargets", targets, task])
50
51    def forceShutDown(self):
52        return self._runCommand(["stateForceShutdown"])
53
54
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
66    from bldcontrol.localhostbecontroller import LocalhostBEController
67
68    be = BuildEnvironment.objects.filter(Q(**kwargs))[0]
69    if be.betype == BuildEnvironment.TYPE_LOCAL:
70        return LocalhostBEController(be)
71    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
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
100    def setLayers(self, bitbake, ls):
101        """ Checks-out bitbake executor and layers from git repositories.
102            Sets the layer variables in the config file, after validating local layer paths.
103            bitbake must be a single BRBitbake instance
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        """
108        raise NotImplementedError("FIXME: Must override setLayers")
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        """
115        raise NotImplementedError("Must return the REST URL of the artifact")
116
117    def triggerBuild(self, bitbake, layers, variables, targets):
118        raise NotImplementedError("Must override BE release")
119
120class ShellCmdException(Exception):
121    pass
122
123
124class BuildSetupException(Exception):
125    pass
126
127