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
23import os
24import sys
25import re
26from django.db import transaction
27from django.db.models import Q
28from bldcontrol.models import BuildEnvironment, BRLayer, BRVariable, BRTarget, BRBitbake
29
30# load Bitbake components
31path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
32sys.path.insert(0, path)
33
34class 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
39    def __init__(self, be):
40        import bb.server.xmlrpcclient
41        self.connection = bb.server.xmlrpcclient._create_server(be.bbaddress,
42                                                          int(be.bbport))[0]
43
44    def _runCommand(self, command):
45        result, error = self.connection.runCommand(command)
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
56    def getVariable(self, name):
57        return self._runCommand(["getVariable", name])
58
59    def triggerEvent(self, event):
60        return self._runCommand(["triggerEvent", event])
61
62    def build(self, targets, task = None):
63        if task is None:
64            task = "build"
65        return self._runCommand(["buildTargets", targets, task])
66
67    def forceShutDown(self):
68        return self._runCommand(["stateForceShutdown"])
69
70
71
72def 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
82    from bldcontrol.localhostbecontroller import LocalhostBEController
83
84    be = BuildEnvironment.objects.filter(Q(**kwargs))[0]
85    if be.betype == BuildEnvironment.TYPE_LOCAL:
86        return LocalhostBEController(be)
87    else:
88        raise Exception("FIXME: Implement BEC for type %s" % str(be.betype))
89
90
91class 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
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
116    def setLayers(self, bitbake, ls):
117        """ Checks-out bitbake executor and layers from git repositories.
118            Sets the layer variables in the config file, after validating local layer paths.
119            bitbake must be a single BRBitbake instance
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        """
124        raise NotImplementedError("FIXME: Must override setLayers")
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        """
131        raise NotImplementedError("Must return the REST URL of the artifact")
132
133    def triggerBuild(self, bitbake, layers, variables, targets):
134        raise NotImplementedError("Must override BE release")
135
136class ShellCmdException(Exception):
137    pass
138
139
140class BuildSetupException(Exception):
141    pass
142
143