blob: 9fffbc6ae25b3702fd06d8b054d2ab7236a9b1ac [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="
Brad Bishop5dd7cbb2018-09-05 22:26:40 -070021Usage: source toaster start|stop [webport=<address:port>] [noweb] [nobuild] [toasterdir]
Patrick Williamsc0f7c042017-02-23 20:41:17 -060022 Optional arguments:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023 [nobuild] Setup the environment for capturing builds with toaster but disable managed builds
24 [noweb] Setup the environment for capturing builds with toaster but don't start the web server
Patrick Williamsc0f7c042017-02-23 20:41:17 -060025 [webport] Set the development server (default: localhost:8000)
Brad Bishop5dd7cbb2018-09-05 22:26:40 -070026 [toasterdir] Set absolute path to be used as TOASTER_DIR (default: BUILDDIR/../)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029custom_extention()
30{
31 custom_extension=$BBBASEDIR/lib/toaster/orm/fixtures/custom_toaster_append.sh
32 if [ -f $custom_extension ] ; then
33 $custom_extension $*
34 fi
35}
36
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037databaseCheck()
38{
39 retval=0
40 # you can always add a superuser later via
41 # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
42 $MANAGE migrate --noinput || retval=1
43
44 if [ $retval -eq 1 ]; then
45 echo "Failed migrations, aborting system start" 1>&2
46 return $retval
47 fi
48 # Make sure that checksettings can pick up any value for TEMPLATECONF
49 export TEMPLATECONF
50 $MANAGE checksettings --traceback || retval=1
51
52 if [ $retval -eq 1 ]; then
53 printf "\nError while checking settings; aborting\n"
54 return $retval
55 fi
56
57 return $retval
58}
59
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060webserverKillAll()
61{
62 local pidfile
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063 if [ -f ${BUILDDIR}/.toastermain.pid ] ; then
64 custom_extention web_stop_postpend
65 else
66 custom_extention noweb_stop_postpend
67 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050068 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 if [ -f ${pidfile} ]; then
70 pid=`cat ${pidfile}`
71 while kill -0 $pid 2>/dev/null; do
Brad Bishop316dfdd2018-06-25 12:45:53 -040072 kill -SIGTERM $pid 2>/dev/null
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 sleep 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 done
75 rm ${pidfile}
76 fi
77 done
78}
79
80webserverStartAll()
81{
82 # do not start if toastermain points to a valid process
83 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
84 retval=1
85 rm "${BUILDDIR}/.toastermain.pid"
86 fi
87
88 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050089
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 # check the database
91 databaseCheck || return 1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050092
93 echo "Starting webserver..."
94
Brad Bishop316dfdd2018-06-25 12:45:53 -040095 $MANAGE runserver --noreload "$ADDR_PORT" \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096 </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
97 & echo $! >${BUILDDIR}/.toastermain.pid
Patrick Williamsf1e5d692016-03-30 15:21:19 -050098
99 sleep 1
100
101 if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
102 retval=1
103 rm "${BUILDDIR}/.toastermain.pid"
104 else
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 echo "Toaster development webserver started at http://$ADDR_PORT"
106 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 -0500107 custom_extention web_start_postpend $ADDR_PORT
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500108 fi
109
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 return $retval
111}
112
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113INSTOPSYSTEM=0
114
115# define the stop command
116stop_system()
117{
118 # prevent reentry
119 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
120 INSTOPSYSTEM=1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500122 # unset exported variables
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123 unset TOASTER_DIR
124 unset BITBAKE_UI
125 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 trap - SIGHUP
127 #trap - SIGCHLD
128 INSTOPSYSTEM=0
129}
130
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500132 # Verify Django version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600133 reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135 # expand version parts to 2 digits to support 1.10.x > 1.8
136 # (note:helper functions hard to insert in-line)
137 exp=$exp'import sys,django;'
138 exp=$exp'version=["%02d" % int(n) for n in django.get_version().split(".")];'
139 exp=$exp'vmin=["%02d" % int(n) for n in "\2".split(".")];'
140 exp=$exp'vmax=["%02d" % int(n) for n in "\4".split(".")];'
141 exp=$exp'sys.exit(not (version \1 vmin and version \3 vmax))'
142 exp=$exp'/p'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600143 if ! sed -n "$exp" $reqfile | python3 - ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144 req=`grep ^Django $reqfile`
145 echo "This program needs $req"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 echo "Please install with pip3 install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 return 2
148 fi
149
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 return 0
151}
152
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153# read command line parameters
154if [ -n "$BASH_SOURCE" ] ; then
155 TOASTER=${BASH_SOURCE}
156elif [ -n "$ZSH_NAME" ] ; then
157 TOASTER=${(%):-%x}
158else
159 TOASTER=$0
160fi
161
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500162export BBBASEDIR=`dirname $TOASTER`/..
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600163MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
164OE_ROOT=`dirname $TOASTER`/../..
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500165
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500166# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167# we are using the same logic that oe-setup-builddir uses
168# (based on TEMPLATECONF and .templateconf) to determine
169# which toasterconf.json to use.
170# note: There are a number of relative path assumptions
171# in the local layers that currently make using an arbitrary
172# toasterconf.json difficult.
173
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600174. $OE_ROOT/.templateconf
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500175if [ -n "$TEMPLATECONF" ]; then
176 if [ ! -d "$TEMPLATECONF" ]; then
177 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600178 if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
179 TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500180 fi
181 fi
182fi
183
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600184unset OE_ROOT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500186
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500187WEBSERVER=1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500188export TOASTER_BUILDSERVER=1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600189ADDR_PORT="localhost:8000"
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700190TOASTERDIR=`dirname $BUILDDIR`
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500191unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192for param in $*; do
193 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194 noweb )
195 WEBSERVER=0
196 ;;
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500197 nobuild )
198 TOASTER_BUILDSERVER=0
199 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500200 start )
201 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500203 stop )
204 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 ;;
206 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207 ADDR_PORT="${param#*=}"
208 # Split the addr:port string
209 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
210 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
211 # If only a port has been speified then set address to localhost.
212 if [ $ADDR = $PORT ] ; then
213 ADDR_PORT="localhost:$PORT"
214 fi
215 ;;
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700216 toasterdir=*)
217 TOASTERDIR="${param#*=}"
218 ;;
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600219 --help)
220 echo "$HELP"
221 return 0
222 ;;
223 *)
224 echo "$HELP"
225 return 1
226 ;;
227
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 esac
229done
230
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500231if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
232 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 return 1
234fi
235
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500236verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237
238# We make sure we're running in the current shell and in a good environment
239if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
240 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
241 return 2
242fi
243
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500244# this defines the dir toaster will use for
245# 1) clones of layers (in _toaster_clones )
246# 2) the build dir (in build)
247# 3) the sqlite db if that is being used.
248# 4) pid's we need to clean up on exit/shutdown
Brad Bishop5dd7cbb2018-09-05 22:26:40 -0700249export TOASTER_DIR=$TOASTERDIR
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500250export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE TOASTER_DIR"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251
252# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500253if [ "$CMD" = "start" ] ; then
254 if [ -n "$BBSERVER" ]; then
255 echo " Toaster is already running. Exiting..."
256 return 1
257fi
258elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600259 echo "No command specified"
260 echo "$HELP"
261 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262fi
263
264echo "The system will $CMD."
265
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266# Execute the commands
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500267custom_extention toaster_prepend $CMD $ADDR_PORT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268
269case $CMD in
270 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500271 # check if addr:port is not in use
272 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600273 if [ $WEBSERVER -gt 0 ]; then
274 $MANAGE checksocket "$ADDR_PORT" || return 1
275 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500276 fi
277
278 # Create configuration file
279 conf=${BUILDDIR}/conf/local.conf
280 line='INHERIT+="toaster buildhistory"'
281 grep -q "$line" $conf || echo $line >> $conf
282
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500283 if [ $WEBSERVER -eq 0 ] ; then
284 # Do not update the database for "noweb" unless
285 # it does not yet exist
286 if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then
287 if ! databaseCheck; then
288 echo "Failed ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500289 return 4
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500290 fi
291 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500292 custom_extention noweb_start_postpend $ADDR_PORT
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500293 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500294 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
295 echo "Failed ${CMD}."
296 return 4
297 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500298 export BITBAKE_UI='toasterui'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500299 if [ $TOASTER_BUILDSERVER -eq 1 ] ; then
300 $MANAGE runbuilds \
301 </dev/null >>${BUILDDIR}/toaster_runbuilds.log 2>&1 \
302 & echo $! >${BUILDDIR}/.runbuilds.pid
303 else
304 echo "Toaster build server not started."
305 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500306
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500307 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500308 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500309 echo "Successful ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500310 custom_extention toaster_postpend $CMD $ADDR_PORT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500311 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500312 ;;
313 stop )
314 stop_system
315 echo "Successful ${CMD}."
316 ;;
317esac
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500318custom_extention toaster_postpend $CMD $ADDR_PORT
319