blob: 61a4a0f85d93aba07aa54c5fbe9bb11740c0d27c [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001#!/bin/echo ERROR: This script needs to be sourced. Please run as .
2
3# toaster - shell script to start Toaster
4
5# Copyright (C) 2013-2015 Intel Corp.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050018# along with this program. If not, see http://www.gnu.org/licenses/.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
Patrick Williamsc0f7c042017-02-23 20:41:17 -060020HELP="
21Usage: source toaster start|stop [webport=<address:port>] [noweb]
22 Optional arguments:
23 [noweb] Setup the environment for building with toaster but don't start the development server
24 [webport] Set the development server (default: localhost:8000)
25"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027databaseCheck()
28{
29 retval=0
30 # you can always add a superuser later via
31 # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
32 $MANAGE migrate --noinput || retval=1
33
34 if [ $retval -eq 1 ]; then
35 echo "Failed migrations, aborting system start" 1>&2
36 return $retval
37 fi
38 # Make sure that checksettings can pick up any value for TEMPLATECONF
39 export TEMPLATECONF
40 $MANAGE checksettings --traceback || retval=1
41
42 if [ $retval -eq 1 ]; then
43 printf "\nError while checking settings; aborting\n"
44 return $retval
45 fi
46
47 return $retval
48}
49
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050webserverKillAll()
51{
52 local pidfile
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 if [ -f ${pidfile} ]; then
55 pid=`cat ${pidfile}`
56 while kill -0 $pid 2>/dev/null; do
57 kill -SIGTERM -$pid 2>/dev/null
58 sleep 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 done
60 rm ${pidfile}
61 fi
62 done
63}
64
65webserverStartAll()
66{
67 # do not start if toastermain points to a valid process
68 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
69 retval=1
70 rm "${BUILDDIR}/.toastermain.pid"
71 fi
72
73 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050074
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 # check the database
76 databaseCheck || return 1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050077
78 echo "Starting webserver..."
79
Patrick Williamsc0f7c042017-02-23 20:41:17 -060080 $MANAGE runserver "$ADDR_PORT" \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081 </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
82 & echo $! >${BUILDDIR}/.toastermain.pid
Patrick Williamsf1e5d692016-03-30 15:21:19 -050083
84 sleep 1
85
86 if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
87 retval=1
88 rm "${BUILDDIR}/.toastermain.pid"
89 else
Patrick Williamsc0f7c042017-02-23 20:41:17 -060090 echo "Toaster development webserver started at http://$ADDR_PORT"
91 echo -e "\nYou can now run 'bitbake <target>' on the command line and monitor your build in Toaster.\nYou can also use a Toaster project to configure and run a build.\n"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050092 fi
93
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 return $retval
95}
96
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097INSTOPSYSTEM=0
98
99# define the stop command
100stop_system()
101{
102 # prevent reentry
103 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
104 INSTOPSYSTEM=1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500106 # unset exported variables
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 unset TOASTER_DIR
108 unset BITBAKE_UI
109 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 trap - SIGHUP
111 #trap - SIGCHLD
112 INSTOPSYSTEM=0
113}
114
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500116 # Verify Django version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600117 reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
119 exp=$exp'import sys,django;version=django.get_version().split(".");'
120 exp=$exp'sys.exit(not (version \1 "\2".split(".") and version \3 "\4".split(".")))/p'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600121 if ! sed -n "$exp" $reqfile | python3 - ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500122 req=`grep ^Django $reqfile`
123 echo "This program needs $req"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 echo "Please install with pip3 install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 return 2
126 fi
127
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128 return 0
129}
130
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131# read command line parameters
132if [ -n "$BASH_SOURCE" ] ; then
133 TOASTER=${BASH_SOURCE}
134elif [ -n "$ZSH_NAME" ] ; then
135 TOASTER=${(%):-%x}
136else
137 TOASTER=$0
138fi
139
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500140export BBBASEDIR=`dirname $TOASTER`/..
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600141MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
142OE_ROOT=`dirname $TOASTER`/../..
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500143
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500144# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500145# we are using the same logic that oe-setup-builddir uses
146# (based on TEMPLATECONF and .templateconf) to determine
147# which toasterconf.json to use.
148# note: There are a number of relative path assumptions
149# in the local layers that currently make using an arbitrary
150# toasterconf.json difficult.
151
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600152. $OE_ROOT/.templateconf
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153if [ -n "$TEMPLATECONF" ]; then
154 if [ ! -d "$TEMPLATECONF" ]; then
155 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
157 TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500158 fi
159 fi
160fi
161
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600162unset OE_ROOT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500163
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500165
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500166WEBSERVER=1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167ADDR_PORT="localhost:8000"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169for param in $*; do
170 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 noweb )
172 WEBSERVER=0
173 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500174 start )
175 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500177 stop )
178 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 ;;
180 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181 ADDR_PORT="${param#*=}"
182 # Split the addr:port string
183 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
184 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
185 # If only a port has been speified then set address to localhost.
186 if [ $ADDR = $PORT ] ; then
187 ADDR_PORT="localhost:$PORT"
188 fi
189 ;;
190 --help)
191 echo "$HELP"
192 return 0
193 ;;
194 *)
195 echo "$HELP"
196 return 1
197 ;;
198
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 esac
200done
201
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
203 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204 return 1
205fi
206
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500207verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208
209# We make sure we're running in the current shell and in a good environment
210if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
211 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
212 return 2
213fi
214
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500215# this defines the dir toaster will use for
216# 1) clones of layers (in _toaster_clones )
217# 2) the build dir (in build)
218# 3) the sqlite db if that is being used.
219# 4) pid's we need to clean up on exit/shutdown
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500220export TOASTER_DIR=`dirname $BUILDDIR`
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500221export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE TOASTER_DIR"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222
223# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500224if [ "$CMD" = "start" ] ; then
225 if [ -n "$BBSERVER" ]; then
226 echo " Toaster is already running. Exiting..."
227 return 1
228fi
229elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230 echo "No command specified"
231 echo "$HELP"
232 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233fi
234
235echo "The system will $CMD."
236
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237# Execute the commands
238
239case $CMD in
240 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500241 # check if addr:port is not in use
242 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600243 if [ $WEBSERVER -gt 0 ]; then
244 $MANAGE checksocket "$ADDR_PORT" || return 1
245 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500246 fi
247
248 # Create configuration file
249 conf=${BUILDDIR}/conf/local.conf
250 line='INHERIT+="toaster buildhistory"'
251 grep -q "$line" $conf || echo $line >> $conf
252
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500253 if [ $WEBSERVER -eq 0 ] ; then
254 # Do not update the database for "noweb" unless
255 # it does not yet exist
256 if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then
257 if ! databaseCheck; then
258 echo "Failed ${CMD}."
259 return 4
260 fi
261 fi
262 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
264 echo "Failed ${CMD}."
265 return 4
266 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500267 export BITBAKE_UI='toasterui'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500268 $MANAGE runbuilds \
269 </dev/null >>${BUILDDIR}/toaster_runbuilds.log 2>&1 \
270 & echo $! >${BUILDDIR}/.runbuilds.pid
271
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500272 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500273 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500274 echo "Successful ${CMD}."
275 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500276 ;;
277 stop )
278 stop_system
279 echo "Successful ${CMD}."
280 ;;
281esac