blob: c3472dfee89e0457b894d8b47632a1fc97c4357f [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#
Brad Bishopc342db32019-05-15 21:57:59 -04007# SPDX-License-Identifier: GPL-2.0-or-later
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
Patrick Williamsc0f7c042017-02-23 20:41:17 -060010HELP="
Brad Bishop5dd7cbb2018-09-05 22:26:40 -070011Usage: source toaster start|stop [webport=<address:port>] [noweb] [nobuild] [toasterdir]
Patrick Williamsc0f7c042017-02-23 20:41:17 -060012 Optional arguments:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050013 [nobuild] Setup the environment for capturing builds with toaster but disable managed builds
14 [noweb] Setup the environment for capturing builds with toaster but don't start the web server
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015 [webport] Set the development server (default: localhost:8000)
Brad Bishop5dd7cbb2018-09-05 22:26:40 -070016 [toasterdir] Set absolute path to be used as TOASTER_DIR (default: BUILDDIR/../)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Brad Bishopd7bf8c12018-02-25 22:55:05 -050019custom_extention()
20{
21 custom_extension=$BBBASEDIR/lib/toaster/orm/fixtures/custom_toaster_append.sh
22 if [ -f $custom_extension ] ; then
23 $custom_extension $*
24 fi
25}
26
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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053 if [ -f ${BUILDDIR}/.toastermain.pid ] ; then
54 custom_extention web_stop_postpend
55 else
56 custom_extention noweb_stop_postpend
57 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050058 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 if [ -f ${pidfile} ]; then
60 pid=`cat ${pidfile}`
61 while kill -0 $pid 2>/dev/null; do
Brad Bishop316dfdd2018-06-25 12:45:53 -040062 kill -SIGTERM $pid 2>/dev/null
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 sleep 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 done
65 rm ${pidfile}
66 fi
67 done
68}
69
70webserverStartAll()
71{
72 # do not start if toastermain points to a valid process
73 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
74 retval=1
75 rm "${BUILDDIR}/.toastermain.pid"
76 fi
77
78 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050079
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 # check the database
81 databaseCheck || return 1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050082
83 echo "Starting webserver..."
84
Brad Bishop316dfdd2018-06-25 12:45:53 -040085 $MANAGE runserver --noreload "$ADDR_PORT" \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050086 </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
87 & echo $! >${BUILDDIR}/.toastermain.pid
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088
89 sleep 1
90
91 if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
92 retval=1
93 rm "${BUILDDIR}/.toastermain.pid"
94 else
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 echo "Toaster development webserver started at http://$ADDR_PORT"
96 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"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 custom_extention web_start_postpend $ADDR_PORT
Patrick Williamsf1e5d692016-03-30 15:21:19 -050098 fi
99
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 return $retval
101}
102
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103INSTOPSYSTEM=0
104
105# define the stop command
106stop_system()
107{
108 # prevent reentry
109 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
110 INSTOPSYSTEM=1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500112 # unset exported variables
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500113 unset TOASTER_DIR
114 unset BITBAKE_UI
115 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 trap - SIGHUP
117 #trap - SIGCHLD
118 INSTOPSYSTEM=0
119}
120
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500122 # Verify Django version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600123 reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500124 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125 # expand version parts to 2 digits to support 1.10.x > 1.8
126 # (note:helper functions hard to insert in-line)
127 exp=$exp'import sys,django;'
128 exp=$exp'version=["%02d" % int(n) for n in django.get_version().split(".")];'
129 exp=$exp'vmin=["%02d" % int(n) for n in "\2".split(".")];'
130 exp=$exp'vmax=["%02d" % int(n) for n in "\4".split(".")];'
131 exp=$exp'sys.exit(not (version \1 vmin and version \3 vmax))'
132 exp=$exp'/p'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600133 if ! sed -n "$exp" $reqfile | python3 - ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134 req=`grep ^Django $reqfile`
135 echo "This program needs $req"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136 echo "Please install with pip3 install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 return 2
138 fi
139
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 return 0
141}
142
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143# read command line parameters
144if [ -n "$BASH_SOURCE" ] ; then
145 TOASTER=${BASH_SOURCE}
146elif [ -n "$ZSH_NAME" ] ; then
147 TOASTER=${(%):-%x}
148else
149 TOASTER=$0
150fi
151
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500152export BBBASEDIR=`dirname $TOASTER`/..
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600153MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
Brad Bishopc4ea0752018-11-15 14:30:15 -0800154if [ -z "$OE_ROOT" ]; then
155 OE_ROOT=`dirname $TOASTER`/../..
156fi
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500157
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500158# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500159# we are using the same logic that oe-setup-builddir uses
160# (based on TEMPLATECONF and .templateconf) to determine
161# which toasterconf.json to use.
162# note: There are a number of relative path assumptions
163# in the local layers that currently make using an arbitrary
164# toasterconf.json difficult.
165
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166. $OE_ROOT/.templateconf
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167if [ -n "$TEMPLATECONF" ]; then
168 if [ ! -d "$TEMPLATECONF" ]; then
169 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170 if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
171 TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500172 fi
173 fi
174fi
175
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600176unset OE_ROOT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500177
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500179WEBSERVER=1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500180export TOASTER_BUILDSERVER=1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181ADDR_PORT="localhost:8000"
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700182TOASTERDIR=`dirname $BUILDDIR`
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500183unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184for param in $*; do
185 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 noweb )
187 WEBSERVER=0
188 ;;
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189 nobuild )
190 TOASTER_BUILDSERVER=0
191 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500192 start )
193 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500195 stop )
196 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 ;;
198 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600199 ADDR_PORT="${param#*=}"
200 # Split the addr:port string
201 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
202 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
203 # If only a port has been speified then set address to localhost.
204 if [ $ADDR = $PORT ] ; then
205 ADDR_PORT="localhost:$PORT"
206 fi
207 ;;
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700208 toasterdir=*)
209 TOASTERDIR="${param#*=}"
210 ;;
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211 --help)
212 echo "$HELP"
213 return 0
214 ;;
215 *)
216 echo "$HELP"
217 return 1
218 ;;
219
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220 esac
221done
222
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500223if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
224 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 return 1
226fi
227
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500228verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229
230# We make sure we're running in the current shell and in a good environment
231if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
232 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
233 return 2
234fi
235
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500236# this defines the dir toaster will use for
237# 1) clones of layers (in _toaster_clones )
238# 2) the build dir (in build)
239# 3) the sqlite db if that is being used.
240# 4) pid's we need to clean up on exit/shutdown
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700241export TOASTER_DIR=$TOASTERDIR
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500242export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE TOASTER_DIR"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243
244# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245if [ "$CMD" = "start" ] ; then
246 if [ -n "$BBSERVER" ]; then
247 echo " Toaster is already running. Exiting..."
248 return 1
249fi
250elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251 echo "No command specified"
252 echo "$HELP"
253 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254fi
255
256echo "The system will $CMD."
257
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258# Execute the commands
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259custom_extention toaster_prepend $CMD $ADDR_PORT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260
261case $CMD in
262 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500263 # check if addr:port is not in use
264 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600265 if [ $WEBSERVER -gt 0 ]; then
266 $MANAGE checksocket "$ADDR_PORT" || return 1
267 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500268 fi
269
270 # Create configuration file
271 conf=${BUILDDIR}/conf/local.conf
272 line='INHERIT+="toaster buildhistory"'
273 grep -q "$line" $conf || echo $line >> $conf
274
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500275 if [ $WEBSERVER -eq 0 ] ; then
276 # Do not update the database for "noweb" unless
277 # it does not yet exist
278 if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then
279 if ! databaseCheck; then
280 echo "Failed ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 return 4
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500282 fi
283 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500284 custom_extention noweb_start_postpend $ADDR_PORT
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500285 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
287 echo "Failed ${CMD}."
288 return 4
289 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500290 export BITBAKE_UI='toasterui'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500291 if [ $TOASTER_BUILDSERVER -eq 1 ] ; then
292 $MANAGE runbuilds \
293 </dev/null >>${BUILDDIR}/toaster_runbuilds.log 2>&1 \
294 & echo $! >${BUILDDIR}/.runbuilds.pid
295 else
296 echo "Toaster build server not started."
297 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500298
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500299 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500301 echo "Successful ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500302 custom_extention toaster_postpend $CMD $ADDR_PORT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500303 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500304 ;;
305 stop )
306 stop_system
307 echo "Successful ${CMD}."
308 ;;
309esac
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500310custom_extention toaster_postpend $CMD $ADDR_PORT
311