blob: f002c8c159073c83dd1946744dbb0721ed0a0309 [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 Williamsac13d5f2023-11-24 18:59:46 -060087 </dev/null >>${TOASTER_LOGS_DIR}/web.log 2>&1 \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 & 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 Williamsac13d5f2023-11-24 18:59:46 -0600184# ${BUILDDIR}/toaster_logs/ became the default location for toaster logs
185# This is needed for implemented django-log-viewer: https://pypi.org/project/django-log-viewer/
186# If the directory does not exist, create it.
187TOASTER_LOGS_DIR="${BUILDDIR}/toaster_logs/"
188if [ ! -d $TOASTER_LOGS_DIR ]
189then
190 mkdir $TOASTER_LOGS_DIR
191fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500192unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193for param in $*; do
194 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 noweb )
196 WEBSERVER=0
197 ;;
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500198 nobuild )
199 TOASTER_BUILDSERVER=0
200 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500201 start )
202 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500204 stop )
205 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206 ;;
207 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600208 ADDR_PORT="${param#*=}"
209 # Split the addr:port string
210 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
211 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
212 # If only a port has been speified then set address to localhost.
213 if [ $ADDR = $PORT ] ; then
214 ADDR_PORT="localhost:$PORT"
215 fi
216 ;;
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700217 toasterdir=*)
218 TOASTERDIR="${param#*=}"
219 ;;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500220 manage )
221 CMD=$param
222 manage_cmd=""
223 ;;
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600224 --help)
225 echo "$HELP"
226 return 0
227 ;;
228 *)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500229 if [ "manage" == "$CMD" ] ; then
230 manage_cmd="$manage_cmd $param"
231 else
232 echo "$HELP"
233 exit 1
234 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600235 ;;
236
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 esac
238done
239
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
241 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242 return 1
243fi
244
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246
247# We make sure we're running in the current shell and in a good environment
248if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
249 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
250 return 2
251fi
252
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500253# this defines the dir toaster will use for
254# 1) clones of layers (in _toaster_clones )
255# 2) the build dir (in build)
256# 3) the sqlite db if that is being used.
257# 4) pid's we need to clean up on exit/shutdown
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700258export TOASTER_DIR=$TOASTERDIR
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000259export BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS TOASTER_DIR"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260
261# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500262if [ "$CMD" = "start" ] ; then
263 if [ -n "$BBSERVER" ]; then
264 echo " Toaster is already running. Exiting..."
265 return 1
266fi
267elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600268 echo "No command specified"
269 echo "$HELP"
270 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271fi
272
273echo "The system will $CMD."
274
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275# Execute the commands
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500276custom_extention toaster_prepend $CMD $ADDR_PORT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277
278case $CMD in
279 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500280 # check if addr:port is not in use
281 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600282 if [ $WEBSERVER -gt 0 ]; then
283 $MANAGE checksocket "$ADDR_PORT" || return 1
284 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500285 fi
286
287 # Create configuration file
288 conf=${BUILDDIR}/conf/local.conf
289 line='INHERIT+="toaster buildhistory"'
290 grep -q "$line" $conf || echo $line >> $conf
291
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500292 if [ $WEBSERVER -eq 0 ] ; then
293 # Do not update the database for "noweb" unless
294 # it does not yet exist
295 if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then
296 if ! databaseCheck; then
297 echo "Failed ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500298 return 4
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500299 fi
300 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500301 custom_extention noweb_start_postpend $ADDR_PORT
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500302 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500303 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
304 echo "Failed ${CMD}."
305 return 4
306 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500307 export BITBAKE_UI='toasterui'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500308 if [ $TOASTER_BUILDSERVER -eq 1 ] ; then
309 $MANAGE runbuilds \
Patrick Williamsac13d5f2023-11-24 18:59:46 -0600310 </dev/null >>${TOASTER_LOGS_DIR}/toaster_runbuilds.log 2>&1 \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500311 & echo $! >${BUILDDIR}/.runbuilds.pid
312 else
313 echo "Toaster build server not started."
314 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500315
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500316 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500317 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500318 echo "Successful ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500319 custom_extention toaster_postpend $CMD $ADDR_PORT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500320 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500321 ;;
322 stop )
323 stop_system
324 echo "Successful ${CMD}."
325 ;;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500326 manage )
327 cd $BBBASEDIR/lib/toaster
328 $MANAGE $manage_cmd
329 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500330esac
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331custom_extention toaster_postpend $CMD $ADDR_PORT
332