blob: 558a819570cf751696646b6bbff484a86170ab32 [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="
Andrew Geissler82c905d2020-04-13 13:39:40 -050011Usage 1: 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/../)
Andrew Geissler82c905d2020-04-13 13:39:40 -050017Usage 2: source toaster manage [createsuperuser|lsupdates|migrate|makemigrations|checksettings|collectstatic|...]
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020custom_extention()
21{
22 custom_extension=$BBBASEDIR/lib/toaster/orm/fixtures/custom_toaster_append.sh
23 if [ -f $custom_extension ] ; then
24 $custom_extension $*
25 fi
26}
27
Brad Bishop6e60e8b2018-02-01 10:27:11 -050028databaseCheck()
29{
30 retval=0
31 # you can always add a superuser later via
32 # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
33 $MANAGE migrate --noinput || retval=1
34
35 if [ $retval -eq 1 ]; then
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000036 echo "Failed migrations, halting system start" 1>&2
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037 return $retval
38 fi
39 # Make sure that checksettings can pick up any value for TEMPLATECONF
40 export TEMPLATECONF
41 $MANAGE checksettings --traceback || retval=1
42
43 if [ $retval -eq 1 ]; then
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000044 printf "\nError while checking settings; exiting\n"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045 return $retval
46 fi
47
48 return $retval
49}
50
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051webserverKillAll()
52{
53 local pidfile
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054 if [ -f ${BUILDDIR}/.toastermain.pid ] ; then
55 custom_extention web_stop_postpend
56 else
57 custom_extention noweb_stop_postpend
58 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050059 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 if [ -f ${pidfile} ]; then
61 pid=`cat ${pidfile}`
62 while kill -0 $pid 2>/dev/null; do
Brad Bishop316dfdd2018-06-25 12:45:53 -040063 kill -SIGTERM $pid 2>/dev/null
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 sleep 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 done
66 rm ${pidfile}
67 fi
68 done
69}
70
71webserverStartAll()
72{
73 # do not start if toastermain points to a valid process
74 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
75 retval=1
76 rm "${BUILDDIR}/.toastermain.pid"
77 fi
78
79 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050080
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 # check the database
82 databaseCheck || return 1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050083
84 echo "Starting webserver..."
85
Brad Bishop316dfdd2018-06-25 12:45:53 -040086 $MANAGE runserver --noreload "$ADDR_PORT" \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087 </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
88 & echo $! >${BUILDDIR}/.toastermain.pid
Patrick Williamsf1e5d692016-03-30 15:21:19 -050089
90 sleep 1
91
92 if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
93 retval=1
94 rm "${BUILDDIR}/.toastermain.pid"
95 else
Patrick Williamsc0f7c042017-02-23 20:41:17 -060096 echo "Toaster development webserver started at http://$ADDR_PORT"
97 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 -050098 custom_extention web_start_postpend $ADDR_PORT
Patrick Williamsf1e5d692016-03-30 15:21:19 -050099 fi
100
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 return $retval
102}
103
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104INSTOPSYSTEM=0
105
106# define the stop command
107stop_system()
108{
109 # prevent reentry
110 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
111 INSTOPSYSTEM=1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500113 # unset exported variables
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500114 unset TOASTER_DIR
115 unset BITBAKE_UI
116 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 trap - SIGHUP
118 #trap - SIGCHLD
119 INSTOPSYSTEM=0
120}
121
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123 # Verify Django version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600124 reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500125 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 # expand version parts to 2 digits to support 1.10.x > 1.8
127 # (note:helper functions hard to insert in-line)
128 exp=$exp'import sys,django;'
129 exp=$exp'version=["%02d" % int(n) for n in django.get_version().split(".")];'
130 exp=$exp'vmin=["%02d" % int(n) for n in "\2".split(".")];'
131 exp=$exp'vmax=["%02d" % int(n) for n in "\4".split(".")];'
132 exp=$exp'sys.exit(not (version \1 vmin and version \3 vmax))'
133 exp=$exp'/p'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600134 if ! sed -n "$exp" $reqfile | python3 - ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500135 req=`grep ^Django $reqfile`
136 echo "This program needs $req"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 echo "Please install with pip3 install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 return 2
139 fi
140
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 return 0
142}
143
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144# read command line parameters
145if [ -n "$BASH_SOURCE" ] ; then
146 TOASTER=${BASH_SOURCE}
147elif [ -n "$ZSH_NAME" ] ; then
148 TOASTER=${(%):-%x}
149else
150 TOASTER=$0
151fi
152
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153export BBBASEDIR=`dirname $TOASTER`/..
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600154MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
Brad Bishopc4ea0752018-11-15 14:30:15 -0800155if [ -z "$OE_ROOT" ]; then
156 OE_ROOT=`dirname $TOASTER`/../..
157fi
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500158
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500159# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160# we are using the same logic that oe-setup-builddir uses
161# (based on TEMPLATECONF and .templateconf) to determine
162# which toasterconf.json to use.
163# note: There are a number of relative path assumptions
164# in the local layers that currently make using an arbitrary
165# toasterconf.json difficult.
166
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167. $OE_ROOT/.templateconf
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168if [ -n "$TEMPLATECONF" ]; then
169 if [ ! -d "$TEMPLATECONF" ]; then
170 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600171 if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
172 TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500173 fi
174 fi
175fi
176
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600177unset OE_ROOT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500178
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500180WEBSERVER=1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500181export TOASTER_BUILDSERVER=1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182ADDR_PORT="localhost:8000"
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700183TOASTERDIR=`dirname $BUILDDIR`
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500184unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185for param in $*; do
186 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 noweb )
188 WEBSERVER=0
189 ;;
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500190 nobuild )
191 TOASTER_BUILDSERVER=0
192 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500193 start )
194 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500196 stop )
197 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198 ;;
199 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600200 ADDR_PORT="${param#*=}"
201 # Split the addr:port string
202 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
203 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
204 # If only a port has been speified then set address to localhost.
205 if [ $ADDR = $PORT ] ; then
206 ADDR_PORT="localhost:$PORT"
207 fi
208 ;;
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700209 toasterdir=*)
210 TOASTERDIR="${param#*=}"
211 ;;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500212 manage )
213 CMD=$param
214 manage_cmd=""
215 ;;
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600216 --help)
217 echo "$HELP"
218 return 0
219 ;;
220 *)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500221 if [ "manage" == "$CMD" ] ; then
222 manage_cmd="$manage_cmd $param"
223 else
224 echo "$HELP"
225 exit 1
226 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600227 ;;
228
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 esac
230done
231
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500232if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
233 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 return 1
235fi
236
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238
239# We make sure we're running in the current shell and in a good environment
240if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
241 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
242 return 2
243fi
244
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245# this defines the dir toaster will use for
246# 1) clones of layers (in _toaster_clones )
247# 2) the build dir (in build)
248# 3) the sqlite db if that is being used.
249# 4) pid's we need to clean up on exit/shutdown
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700250export TOASTER_DIR=$TOASTERDIR
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000251export BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS TOASTER_DIR"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252
253# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500254if [ "$CMD" = "start" ] ; then
255 if [ -n "$BBSERVER" ]; then
256 echo " Toaster is already running. Exiting..."
257 return 1
258fi
259elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260 echo "No command specified"
261 echo "$HELP"
262 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263fi
264
265echo "The system will $CMD."
266
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500267# Execute the commands
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500268custom_extention toaster_prepend $CMD $ADDR_PORT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500269
270case $CMD in
271 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500272 # check if addr:port is not in use
273 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600274 if [ $WEBSERVER -gt 0 ]; then
275 $MANAGE checksocket "$ADDR_PORT" || return 1
276 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500277 fi
278
279 # Create configuration file
280 conf=${BUILDDIR}/conf/local.conf
281 line='INHERIT+="toaster buildhistory"'
282 grep -q "$line" $conf || echo $line >> $conf
283
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500284 if [ $WEBSERVER -eq 0 ] ; then
285 # Do not update the database for "noweb" unless
286 # it does not yet exist
287 if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then
288 if ! databaseCheck; then
289 echo "Failed ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290 return 4
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500291 fi
292 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500293 custom_extention noweb_start_postpend $ADDR_PORT
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500294 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
296 echo "Failed ${CMD}."
297 return 4
298 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500299 export BITBAKE_UI='toasterui'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500300 if [ $TOASTER_BUILDSERVER -eq 1 ] ; then
301 $MANAGE runbuilds \
302 </dev/null >>${BUILDDIR}/toaster_runbuilds.log 2>&1 \
303 & echo $! >${BUILDDIR}/.runbuilds.pid
304 else
305 echo "Toaster build server not started."
306 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500307
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500308 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500310 echo "Successful ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500311 custom_extention toaster_postpend $CMD $ADDR_PORT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500312 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500313 ;;
314 stop )
315 stop_system
316 echo "Successful ${CMD}."
317 ;;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500318 manage )
319 cd $BBBASEDIR/lib/toaster
320 $MANAGE $manage_cmd
321 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500322esac
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323custom_extention toaster_postpend $CMD $ADDR_PORT
324