diff options
Diffstat (limited to 'bitbake/bin/toaster')
-rwxr-xr-x | bitbake/bin/toaster | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/bitbake/bin/toaster b/bitbake/bin/toaster new file mode 100755 index 0000000000..75c7a076b1 --- /dev/null +++ b/bitbake/bin/toaster | |||
@@ -0,0 +1,267 @@ | |||
1 | #!/bin/bash | ||
2 | # (c) 2013 Intel Corp. | ||
3 | |||
4 | # This program is free software; you can redistribute it and/or modify | ||
5 | # it under the terms of the GNU General Public License as published by | ||
6 | # the Free Software Foundation; either version 2 of the License, or | ||
7 | # (at your option) any later version. | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | # GNU General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with this program; if not, write to the Free Software | ||
16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
17 | |||
18 | |||
19 | # This script can be run in two modes. | ||
20 | |||
21 | # When used with "source", from a build directory, | ||
22 | # it enables toaster event logging and starts the bitbake resident server. | ||
23 | # use as: source toaster [start|stop] [noweb] [noui] | ||
24 | |||
25 | # When it is called as a stand-alone script, it starts just the | ||
26 | # web server, and the building shall be done through the web interface. | ||
27 | # As script, it will not return to the command prompt. Stop with Ctrl-C. | ||
28 | |||
29 | # Helper function to kill a background toaster development server | ||
30 | |||
31 | function webserverKillAll() | ||
32 | { | ||
33 | local pidfile | ||
34 | for pidfile in ${BUILDDIR}/.toastermain.pid; do | ||
35 | if [ -f ${pidfile} ]; then | ||
36 | while kill -0 $(< ${pidfile}) 2>/dev/null; do | ||
37 | kill -SIGTERM -$(< ${pidfile}) 2>/dev/null | ||
38 | sleep 1; | ||
39 | # Kill processes if they are still running - may happen in interactive shells | ||
40 | ps fux | grep "python.*manage.py" | awk '{print $2}' | xargs kill | ||
41 | done; | ||
42 | rm ${pidfile} | ||
43 | fi | ||
44 | done | ||
45 | } | ||
46 | |||
47 | function webserverStartAll() | ||
48 | { | ||
49 | # do not start if toastermain points to a valid process | ||
50 | if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then | ||
51 | retval=1 | ||
52 | rm "${BUILDDIR}/.toastermain.pid" | ||
53 | fi | ||
54 | |||
55 | retval=0 | ||
56 | python $BBBASEDIR/lib/toaster/manage.py syncdb || retval=1 | ||
57 | python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=2 | ||
58 | if [ $retval -eq 1 ]; then | ||
59 | echo "Failed db sync, stopping system start" 1>&2 | ||
60 | elif [ $retval -eq 2 ]; then | ||
61 | echo -e "\nError on migration, trying to recover... \n" | ||
62 | python $BBBASEDIR/lib/toaster/manage.py migrate orm 0001_initial --fake | ||
63 | retval=0 | ||
64 | python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=1 | ||
65 | fi | ||
66 | if [ "x$TOASTER_MANAGED" == "x1" ]; then | ||
67 | python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol || retval=1 | ||
68 | python $BBBASEDIR/lib/toaster/manage.py checksettings --traceback || retval=1 | ||
69 | fi | ||
70 | if [ $retval -eq 0 ]; then | ||
71 | echo "Starting webserver" | ||
72 | python $BBBASEDIR/lib/toaster/manage.py runserver 0.0.0.0:8000 </dev/null >${BUILDDIR}/toaster_web.log 2>&1 & echo $! >${BUILDDIR}/.toastermain.pid | ||
73 | sleep 1 | ||
74 | if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then | ||
75 | retval=1 | ||
76 | rm "${BUILDDIR}/.toastermain.pid" | ||
77 | fi | ||
78 | fi | ||
79 | return $retval | ||
80 | } | ||
81 | |||
82 | # Helper functions to add a special configuration file | ||
83 | |||
84 | function addtoConfiguration() | ||
85 | { | ||
86 | echo "#Created by toaster start script" > ${BUILDDIR}/conf/$2 | ||
87 | echo $1 >> ${BUILDDIR}/conf/$2 | ||
88 | } | ||
89 | |||
90 | INSTOPSYSTEM=0 | ||
91 | |||
92 | # define the stop command | ||
93 | function stop_system() | ||
94 | { | ||
95 | # prevent reentry | ||
96 | if [ $INSTOPSYSTEM == 1 ]; then return; fi | ||
97 | INSTOPSYSTEM=1 | ||
98 | if [ -f ${BUILDDIR}/.toasterui.pid ]; then | ||
99 | kill $(< ${BUILDDIR}/.toasterui.pid ) 2>/dev/null | ||
100 | rm ${BUILDDIR}/.toasterui.pid | ||
101 | fi | ||
102 | BBSERVER=0.0.0.0:8200 bitbake -m | ||
103 | unset BBSERVER | ||
104 | webserverKillAll | ||
105 | # force stop any misbehaving bitbake server | ||
106 | lsof bitbake.lock | awk '{print $2}' | grep "[0-9]\+" | xargs -n1 -r kill | ||
107 | trap - SIGHUP | ||
108 | #trap - SIGCHLD | ||
109 | INSTOPSYSTEM=0 | ||
110 | } | ||
111 | |||
112 | function check_pidbyfile() { | ||
113 | [ -e $1 ] && kill -0 $(< $1) 2>/dev/null | ||
114 | } | ||
115 | |||
116 | |||
117 | function notify_chldexit() { | ||
118 | if [ $NOTOASTERUI == 0 ]; then | ||
119 | check_pidbyfile ${BUILDDIR}/.toasterui.pid && return | ||
120 | stop_system | ||
121 | fi | ||
122 | } | ||
123 | |||
124 | |||
125 | BBBASEDIR=`dirname ${BASH_SOURCE}`/.. | ||
126 | RUNNING=0 | ||
127 | |||
128 | if [ -z "$ZSH_NAME" ] && [ `basename \"$0\"` = `basename \"$BASH_SOURCE\"` ]; then | ||
129 | # We are called as standalone. We refuse to run in a build environment - we need the interactive mode for that. | ||
130 | # Start just the web server, point the web browser to the interface, and start any Django services. | ||
131 | |||
132 | if [ -n "$BUILDDIR" ]; then | ||
133 | echo -e "Error: build/ directory detected. Toaster will not start in managed mode if a build environment is detected.\nUse a clean terminal to start Toaster." 1>&2; | ||
134 | exit 1; | ||
135 | fi | ||
136 | |||
137 | # Define a fake builddir where only the pid files are actually created. No real builds will take place here. | ||
138 | BUILDDIR=/tmp | ||
139 | RUNNING=1 | ||
140 | function trap_ctrlc() { | ||
141 | echo "** Stopping system" | ||
142 | webserverKillAll | ||
143 | RUNNING=0 | ||
144 | } | ||
145 | TOASTER_MANAGED=1 | ||
146 | export TOASTER_MANAGED=1 | ||
147 | if ! webserverStartAll; then | ||
148 | echo "Failed to start the web server, stopping" 1>&2; | ||
149 | exit 1; | ||
150 | fi | ||
151 | xdg-open http://0.0.0.0:8000/ >/dev/null 2>&1 & | ||
152 | trap trap_ctrlc SIGINT | ||
153 | echo "Running. Stop with Ctrl-C" | ||
154 | while [ $RUNNING -gt 0 ]; do | ||
155 | python $BBBASEDIR/lib/toaster/manage.py runbuilds | ||
156 | sleep 1 | ||
157 | done | ||
158 | echo "**** Exit" | ||
159 | exit 0 | ||
160 | fi | ||
161 | |||
162 | # We make sure we're running in the current shell and in a good environment | ||
163 | if [ -z "$BUILDDIR" ] || [ -z `which bitbake` ]; then | ||
164 | echo "Error: Build environment is not setup or bitbake is not in path." 1>&2; | ||
165 | return 2 | ||
166 | fi | ||
167 | |||
168 | |||
169 | |||
170 | # Verify prerequisites | ||
171 | |||
172 | if ! echo "import django; print (1,) == django.VERSION[0:1] and django.VERSION[1:2][0] in (5,6)" | python 2>/dev/null | grep True >/dev/null; then | ||
173 | echo -e "This program needs Django 1.5 or 1.6. Please install with\n\npip install django==1.6" | ||
174 | return 2 | ||
175 | fi | ||
176 | |||
177 | if ! echo "import south; print [0,8,4] == map(int,south.__version__.split(\".\"))" | python 2>/dev/null | grep True >/dev/null; then | ||
178 | echo -e "This program needs South 0.8.4. Please install with\n\npip install south==0.8.4" | ||
179 | return 2 | ||
180 | fi | ||
181 | |||
182 | |||
183 | |||
184 | |||
185 | |||
186 | # Determine the action. If specified by arguments, fine, if not, toggle it | ||
187 | if [ "x$1" == "xstart" ] || [ "x$1" == "xstop" ]; then | ||
188 | CMD="$1" | ||
189 | else | ||
190 | if [ -z "$BBSERVER" ]; then | ||
191 | CMD="start" | ||
192 | else | ||
193 | CMD="stop" | ||
194 | fi; | ||
195 | fi | ||
196 | |||
197 | NOTOASTERUI=0 | ||
198 | WEBSERVER=1 | ||
199 | for param in $*; do | ||
200 | case $param in | ||
201 | noui ) | ||
202 | NOTOASTERUI=1 | ||
203 | ;; | ||
204 | noweb ) | ||
205 | WEBSERVER=0 | ||
206 | ;; | ||
207 | esac | ||
208 | done | ||
209 | |||
210 | echo "The system will $CMD." | ||
211 | |||
212 | # Make sure it's safe to run by checking bitbake lock | ||
213 | |||
214 | lock=1 | ||
215 | if [ -e $BUILDDIR/bitbake.lock ]; then | ||
216 | (flock -n 200 ) 200<$BUILDDIR/bitbake.lock || lock=0 | ||
217 | fi | ||
218 | |||
219 | if [ ${CMD} == "start" ] && ( [ $lock -eq 0 ] || [ -e $BUILDDIR/.toastermain.pid ] ); then | ||
220 | echo "Error: bitbake lock state error. File locks show that the system is on." 2>&1 | ||
221 | echo "If you see problems, stop and then start the system again." 2>&1 | ||
222 | return 3 | ||
223 | fi | ||
224 | |||
225 | |||
226 | # Execute the commands | ||
227 | |||
228 | case $CMD in | ||
229 | start ) | ||
230 | start_success=1 | ||
231 | addtoConfiguration "INHERIT+=\"toaster buildhistory\"" toaster.conf | ||
232 | if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then | ||
233 | echo "Failed ${CMD}." | ||
234 | return 4 | ||
235 | fi | ||
236 | unset BBSERVER | ||
237 | bitbake --postread conf/toaster.conf --server-only -t xmlrpc -B 0.0.0.0:8200 | ||
238 | if [ $? -ne 0 ]; then | ||
239 | start_success=0 | ||
240 | echo "Bitbake server start failed" | ||
241 | else | ||
242 | export BBSERVER=0.0.0.0:8200 | ||
243 | if [ $NOTOASTERUI == 0 ]; then # we start the TOASTERUI only if not inhibited | ||
244 | bitbake --observe-only -u toasterui >${BUILDDIR}/toaster_ui.log 2>&1 & echo $! >${BUILDDIR}/.toasterui.pid | ||
245 | fi | ||
246 | fi | ||
247 | if [ $start_success -eq 1 ]; then | ||
248 | # set fail safe stop system on terminal exit | ||
249 | trap stop_system SIGHUP | ||
250 | echo "Successful ${CMD}." | ||
251 | else | ||
252 | # failed start, do stop | ||
253 | stop_system | ||
254 | echo "Failed ${CMD}." | ||
255 | fi | ||
256 | # stop system on terminal exit | ||
257 | set -o monitor | ||
258 | trap stop_system SIGHUP | ||
259 | #trap notify_chldexit SIGCHLD | ||
260 | ;; | ||
261 | stop ) | ||
262 | stop_system | ||
263 | echo "Successful ${CMD}." | ||
264 | ;; | ||
265 | esac | ||
266 | |||
267 | |||