blob: 5195600d901346e143757b5d9c5c75efdd60c5b4 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039 def __init__(self, be):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 import bb.server.xmlrpcclient
41 self.connection = bb.server.xmlrpcclient._create_server(be.bbaddress,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050042 int(be.bbport))[0]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043
44 def _runCommand(self, command):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045 result, error = self.connection.runCommand(command)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 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 Williamsd8c66bc2016-06-20 12:57:21 -050056 def getVariable(self, name):
57 return self._runCommand(["getVariable", name])
58
59 def triggerEvent(self, event):
60 return self._runCommand(["triggerEvent", event])
61
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 def build(self, targets, task = None):
63 if task is None:
64 task = "build"
65 return self._runCommand(["buildTargets", targets, task])
66
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067 def forceShutDown(self):
68 return self._runCommand(["stateForceShutdown"])
69
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070
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
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082 from bldcontrol.localhostbecontroller import LocalhostBEController
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083
84 be = BuildEnvironment.objects.filter(Q(**kwargs))[0]
85 if be.betype == BuildEnvironment.TYPE_LOCAL:
86 return LocalhostBEController(be)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 """
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 Williamsd8c66bc2016-06-20 12:57:21 -0500116 def setLayers(self, bitbake, ls):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 """ Checks-out bitbake executor and layers from git repositories.
118 Sets the layer variables in the config file, after validating local layer paths.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 bitbake must be a single BRBitbake instance
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 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 Williamsd8c66bc2016-06-20 12:57:21 -0500124 raise NotImplementedError("FIXME: Must override setLayers")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125
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 Williamsd8c66bc2016-06-20 12:57:21 -0500131 raise NotImplementedError("Must return the REST URL of the artifact")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132
133 def triggerBuild(self, bitbake, layers, variables, targets):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134 raise NotImplementedError("Must override BE release")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135
136class ShellCmdException(Exception):
137 pass
138
139
140class BuildSetupException(Exception):
141 pass
142