diff options
Diffstat (limited to 'bitbake/lib/toaster/bldcontrol/sshbecontroller.py')
-rw-r--r-- | bitbake/lib/toaster/bldcontrol/sshbecontroller.py | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/bldcontrol/sshbecontroller.py b/bitbake/lib/toaster/bldcontrol/sshbecontroller.py new file mode 100644 index 0000000000..64674953dc --- /dev/null +++ b/bitbake/lib/toaster/bldcontrol/sshbecontroller.py | |||
@@ -0,0 +1,193 @@ | |||
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 sys | ||
24 | import re | ||
25 | from django.db import transaction | ||
26 | from django.db.models import Q | ||
27 | from bldcontrol.models import BuildEnvironment, BRLayer, BRVariable, BRTarget, BRBitbake | ||
28 | import subprocess | ||
29 | |||
30 | from toastermain import settings | ||
31 | |||
32 | from bbcontroller import BuildEnvironmentController, ShellCmdException, BuildSetupException, _getgitcheckoutdirectoryname | ||
33 | |||
34 | def DN(path): | ||
35 | return "/".join(path.split("/")[0:-1]) | ||
36 | |||
37 | class SSHBEController(BuildEnvironmentController): | ||
38 | """ Implementation of the BuildEnvironmentController for the localhost; | ||
39 | this controller manages the default build directory, | ||
40 | the server setup and system start and stop for the localhost-type build environment | ||
41 | |||
42 | """ | ||
43 | |||
44 | def __init__(self, be): | ||
45 | super(SSHBEController, self).__init__(be) | ||
46 | self.dburl = settings.getDATABASE_URL() | ||
47 | self.pokydirname = None | ||
48 | self.islayerset = False | ||
49 | |||
50 | def _shellcmd(self, command, cwd = None): | ||
51 | if cwd is None: | ||
52 | cwd = self.be.sourcedir | ||
53 | |||
54 | p = subprocess.Popen("ssh %s 'cd %s && %s'" % (self.be.address, cwd, command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | ||
55 | (out,err) = p.communicate() | ||
56 | if p.returncode: | ||
57 | if len(err) == 0: | ||
58 | err = "command: %s \n%s" % (command, out) | ||
59 | else: | ||
60 | err = "command: %s \n%s" % (command, err) | ||
61 | raise ShellCmdException(err) | ||
62 | else: | ||
63 | return out.strip() | ||
64 | |||
65 | def _pathexists(self, path): | ||
66 | try: | ||
67 | self._shellcmd("test -e \"%s\"" % path) | ||
68 | return True | ||
69 | except ShellCmdException as e: | ||
70 | return False | ||
71 | |||
72 | def _pathcreate(self, path): | ||
73 | self._shellcmd("mkdir -p \"%s\"" % path) | ||
74 | |||
75 | def _setupBE(self): | ||
76 | assert self.pokydirname and self._pathexists(self.pokydirname) | ||
77 | self._pathcreate(self.be.builddir) | ||
78 | self._shellcmd("bash -c \"source %s/oe-init-build-env %s\"" % (self.pokydirname, self.be.builddir)) | ||
79 | |||
80 | def startBBServer(self): | ||
81 | assert self.pokydirname and self._pathexists(self.pokydirname) | ||
82 | assert self.islayerset | ||
83 | print self._shellcmd("bash -c \"source %s/oe-init-build-env %s && DATABASE_URL=%s source toaster start noweb && sleep 1\"" % (self.pokydirname, self.be.builddir, self.dburl)) | ||
84 | # FIXME unfortunate sleep 1 - we need to make sure that bbserver is started and the toaster ui is connected | ||
85 | # but since they start async without any return, we just wait a bit | ||
86 | print "Started server" | ||
87 | assert self.be.sourcedir and self._pathexists(self.be.builddir) | ||
88 | self.be.bbaddress = self.be.address.split("@")[-1] | ||
89 | self.be.bbport = "8200" | ||
90 | self.be.bbstate = BuildEnvironment.SERVER_STARTED | ||
91 | self.be.save() | ||
92 | |||
93 | def stopBBServer(self): | ||
94 | assert self.pokydirname and self._pathexists(self.pokydirname) | ||
95 | assert self.islayerset | ||
96 | print self._shellcmd("bash -c \"source %s/oe-init-build-env %s && %s source toaster stop\"" % | ||
97 | (self.pokydirname, self.be.builddir, (lambda: "" if self.be.bbtoken is None else "BBTOKEN=%s" % self.be.bbtoken)())) | ||
98 | self.be.bbstate = BuildEnvironment.SERVER_STOPPED | ||
99 | self.be.save() | ||
100 | print "Stopped server" | ||
101 | |||
102 | def setLayers(self, bitbakes, layers): | ||
103 | """ a word of attention: by convention, the first layer for any build will be poky! """ | ||
104 | |||
105 | assert self.be.sourcedir is not None | ||
106 | assert len(bitbakes) == 1 | ||
107 | # set layers in the layersource | ||
108 | |||
109 | # 1. get a list of repos, and map dirpaths for each layer | ||
110 | gitrepos = {} | ||
111 | gitrepos[bitbakes[0].giturl] = [] | ||
112 | gitrepos[bitbakes[0].giturl].append( ("bitbake", bitbakes[0].dirpath, bitbakes[0].commit) ) | ||
113 | |||
114 | for layer in layers: | ||
115 | # we don't process local URLs | ||
116 | if layer.giturl.startswith("file://"): | ||
117 | continue | ||
118 | if not layer.giturl in gitrepos: | ||
119 | gitrepos[layer.giturl] = [] | ||
120 | gitrepos[layer.giturl].append( (layer.name, layer.dirpath, layer.commit)) | ||
121 | for giturl in gitrepos.keys(): | ||
122 | commitid = gitrepos[giturl][0][2] | ||
123 | for e in gitrepos[giturl]: | ||
124 | if commitid != e[2]: | ||
125 | raise BuildSetupException("More than one commit per git url, unsupported configuration") | ||
126 | |||
127 | layerlist = [] | ||
128 | |||
129 | # 2. checkout the repositories | ||
130 | for giturl in gitrepos.keys(): | ||
131 | import os | ||
132 | localdirname = os.path.join(self.be.sourcedir, _getgitcheckoutdirectoryname(giturl)) | ||
133 | print "DEBUG: giturl ", giturl ,"checking out in current directory", localdirname | ||
134 | |||
135 | # make sure our directory is a git repository | ||
136 | if self._pathexists(localdirname): | ||
137 | if not giturl in self._shellcmd("git remote -v", localdirname): | ||
138 | raise BuildSetupException("Existing git repository at %s, but with different remotes (not '%s'). Aborting." % (localdirname, giturl)) | ||
139 | else: | ||
140 | self._shellcmd("git clone \"%s\" \"%s\"" % (giturl, localdirname)) | ||
141 | # checkout the needed commit | ||
142 | commit = gitrepos[giturl][0][2] | ||
143 | |||
144 | # branch magic name "HEAD" will inhibit checkout | ||
145 | if commit != "HEAD": | ||
146 | print "DEBUG: checking out commit ", commit, "to", localdirname | ||
147 | self._shellcmd("git fetch --all && git checkout \"%s\"" % commit , localdirname) | ||
148 | |||
149 | # take the localdirname as poky dir if we can find the oe-init-build-env | ||
150 | if self.pokydirname is None and self._pathexists(os.path.join(localdirname, "oe-init-build-env")): | ||
151 | print "DEBUG: selected poky dir name", localdirname | ||
152 | self.pokydirname = localdirname | ||
153 | |||
154 | # verify our repositories | ||
155 | for name, dirpath, commit in gitrepos[giturl]: | ||
156 | localdirpath = os.path.join(localdirname, dirpath) | ||
157 | if not self._pathexists(localdirpath): | ||
158 | raise BuildSetupException("Cannot find layer git path '%s' in checked out repository '%s:%s'. Aborting." % (localdirpath, giturl, commit)) | ||
159 | |||
160 | if name != "bitbake": | ||
161 | layerlist.append(localdirpath) | ||
162 | |||
163 | print "DEBUG: current layer list ", layerlist | ||
164 | |||
165 | # 3. configure the build environment, so we have a conf/bblayers.conf | ||
166 | assert self.pokydirname is not None | ||
167 | self._setupBE() | ||
168 | |||
169 | # 4. update the bblayers.conf | ||
170 | bblayerconf = os.path.join(self.be.builddir, "conf/bblayers.conf") | ||
171 | if not self._pathexists(bblayerconf): | ||
172 | raise BuildSetupException("BE is not consistent: bblayers.conf file missing at %s" % bblayerconf) | ||
173 | |||
174 | conflines = open(bblayerconf, "r").readlines() | ||
175 | |||
176 | bblayerconffile = open(bblayerconf, "w") | ||
177 | for i in xrange(len(conflines)): | ||
178 | if conflines[i].startswith("# line added by toaster"): | ||
179 | i += 2 | ||
180 | else: | ||
181 | bblayerconffile.write(conflines[i]) | ||
182 | |||
183 | bblayerconffile.write("\n# line added by toaster build control\nBBLAYERS = \"" + " ".join(layerlist) + "\"") | ||
184 | bblayerconffile.close() | ||
185 | |||
186 | self.islayerset = True | ||
187 | return True | ||
188 | |||
189 | def release(self): | ||
190 | assert self.be.sourcedir and self._pathexists(self.be.builddir) | ||
191 | import shutil | ||
192 | shutil.rmtree(os.path.join(self.be.sourcedir, "build")) | ||
193 | assert not self._pathexists(self.be.builddir) | ||