blob: ed365ee82efdc29fdac55d67db499cf38ee12196 [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 Bishopd7bf8c12018-02-25 22:55:05 -050021Usage: source toaster start|stop [webport=<address:port>] [noweb] [nobuild]
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)
26"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028custom_extention()
29{
30 custom_extension=$BBBASEDIR/lib/toaster/orm/fixtures/custom_toaster_append.sh
31 if [ -f $custom_extension ] ; then
32 $custom_extension $*
33 fi
34}
35
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036databaseCheck()
37{
38 retval=0
39 # you can always add a superuser later via
40 # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
41 $MANAGE migrate --noinput || retval=1
42
43 if [ $retval -eq 1 ]; then
44 echo "Failed migrations, aborting system start" 1>&2
45 return $retval
46 fi
47 # Make sure that checksettings can pick up any value for TEMPLATECONF
48 export TEMPLATECONF
49 $MANAGE checksettings --traceback || retval=1
50
51 if [ $retval -eq 1 ]; then
52 printf "\nError while checking settings; aborting\n"
53 return $retval
54 fi
55
56 return $retval
57}
58
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059webserverKillAll()
60{
61 local pidfile
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 if [ -f ${BUILDDIR}/.toastermain.pid ] ; then
63 custom_extention web_stop_postpend
64 else
65 custom_extention noweb_stop_postpend
66 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 if [ -f ${pidfile} ]; then
69 pid=`cat ${pidfile}`
70 while kill -0 $pid 2>/dev/null; do
Brad Bishop316dfdd2018-06-25 12:45:53 -040071 kill -SIGTERM $pid 2>/dev/null
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 sleep 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 done
74 rm ${pidfile}
75 fi
76 done
77}
78
79webserverStartAll()
80{
81 # do not start if toastermain points to a valid process
82 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
83 retval=1
84 rm "${BUILDDIR}/.toastermain.pid"
85 fi
86
87 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 # check the database
90 databaseCheck || return 1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050091
92 echo "Starting webserver..."
93
Brad Bishop316dfdd2018-06-25 12:45:53 -040094 $MANAGE runserver --noreload "$ADDR_PORT" \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050095 </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
96 & echo $! >${BUILDDIR}/.toastermain.pid
Patrick Williamsf1e5d692016-03-30 15:21:19 -050097
98 sleep 1
99
100 if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
101 retval=1
102 rm "${BUILDDIR}/.toastermain.pid"
103 else
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 echo "Toaster development webserver started at http://$ADDR_PORT"
105 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 -0500106 custom_extention web_start_postpend $ADDR_PORT
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500107 fi
108
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 return $retval
110}
111
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112INSTOPSYSTEM=0
113
114# define the stop command
115stop_system()
116{
117 # prevent reentry
118 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
119 INSTOPSYSTEM=1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500121 # unset exported variables
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500122 unset TOASTER_DIR
123 unset BITBAKE_UI
124 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 trap - SIGHUP
126 #trap - SIGCHLD
127 INSTOPSYSTEM=0
128}
129
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500131 # Verify Django version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600132 reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134 # expand version parts to 2 digits to support 1.10.x > 1.8
135 # (note:helper functions hard to insert in-line)
136 exp=$exp'import sys,django;'
137 exp=$exp'version=["%02d" % int(n) for n in django.get_version().split(".")];'
138 exp=$exp'vmin=["%02d" % int(n) for n in "\2".split(".")];'
139 exp=$exp'vmax=["%02d" % int(n) for n in "\4".split(".")];'
140 exp=$exp'sys.exit(not (version \1 vmin and version \3 vmax))'
141 exp=$exp'/p'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600142 if ! sed -n "$exp" $reqfile | python3 - ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 req=`grep ^Django $reqfile`
144 echo "This program needs $req"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 echo "Please install with pip3 install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 return 2
147 fi
148
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 return 0
150}
151
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152# read command line parameters
153if [ -n "$BASH_SOURCE" ] ; then
154 TOASTER=${BASH_SOURCE}
155elif [ -n "$ZSH_NAME" ] ; then
156 TOASTER=${(%):-%x}
157else
158 TOASTER=$0
159fi
160
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500161export BBBASEDIR=`dirname $TOASTER`/..
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600162MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
163OE_ROOT=`dirname $TOASTER`/../..
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500164
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500165# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500166# we are using the same logic that oe-setup-builddir uses
167# (based on TEMPLATECONF and .templateconf) to determine
168# which toasterconf.json to use.
169# note: There are a number of relative path assumptions
170# in the local layers that currently make using an arbitrary
171# toasterconf.json difficult.
172
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600173. $OE_ROOT/.templateconf
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500174if [ -n "$TEMPLATECONF" ]; then
175 if [ ! -d "$TEMPLATECONF" ]; then
176 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600177 if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
178 TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500179 fi
180 fi
181fi
182
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600183unset OE_ROOT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500184
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500186WEBSERVER=1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500187export TOASTER_BUILDSERVER=1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600188ADDR_PORT="localhost:8000"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500189unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190for param in $*; do
191 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 noweb )
193 WEBSERVER=0
194 ;;
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195 nobuild )
196 TOASTER_BUILDSERVER=0
197 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500198 start )
199 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500201 stop )
202 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 ;;
204 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600205 ADDR_PORT="${param#*=}"
206 # Split the addr:port string
207 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
208 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
209 # If only a port has been speified then set address to localhost.
210 if [ $ADDR = $PORT ] ; then
211 ADDR_PORT="localhost:$PORT"
212 fi
213 ;;
214 --help)
215 echo "$HELP"
216 return 0
217 ;;
218 *)
219 echo "$HELP"
220 return 1
221 ;;
222
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500223 esac
224done
225
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500226if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
227 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 return 1
229fi
230
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500231verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232
233# We make sure we're running in the current shell and in a good environment
234if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
235 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
236 return 2
237fi
238
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500239# this defines the dir toaster will use for
240# 1) clones of layers (in _toaster_clones )
241# 2) the build dir (in build)
242# 3) the sqlite db if that is being used.
243# 4) pid's we need to clean up on exit/shutdown
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500244export TOASTER_DIR=`dirname $BUILDDIR`
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500245export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE TOASTER_DIR"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246
247# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500248if [ "$CMD" = "start" ] ; then
249 if [ -n "$BBSERVER" ]; then
250 echo " Toaster is already running. Exiting..."
251 return 1
252fi
253elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600254 echo "No command specified"
255 echo "$HELP"
256 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257fi
258
259echo "The system will $CMD."
260
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500261# Execute the commands
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500262custom_extention toaster_prepend $CMD $ADDR_PORT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263
264case $CMD in
265 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500266 # check if addr:port is not in use
267 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600268 if [ $WEBSERVER -gt 0 ]; then
269 $MANAGE checksocket "$ADDR_PORT" || return 1
270 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500271 fi
272
273 # Create configuration file
274 conf=${BUILDDIR}/conf/local.conf
275 line='INHERIT+="toaster buildhistory"'
276 grep -q "$line" $conf || echo $line >> $conf
277
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500278 if [ $WEBSERVER -eq 0 ] ; then
279 # Do not update the database for "noweb" unless
280 # it does not yet exist
281 if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then
282 if ! databaseCheck; then
283 echo "Failed ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500284 return 4
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500285 fi
286 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500287 custom_extention noweb_start_postpend $ADDR_PORT
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500288 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
290 echo "Failed ${CMD}."
291 return 4
292 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500293 export BITBAKE_UI='toasterui'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500294 if [ $TOASTER_BUILDSERVER -eq 1 ] ; then
295 $MANAGE runbuilds \
296 </dev/null >>${BUILDDIR}/toaster_runbuilds.log 2>&1 \
297 & echo $! >${BUILDDIR}/.runbuilds.pid
298 else
299 echo "Toaster build server not started."
300 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500301
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500302 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500303 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500304 echo "Successful ${CMD}."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500305 custom_extention toaster_postpend $CMD $ADDR_PORT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500306 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307 ;;
308 stop )
309 stop_system
310 echo "Successful ${CMD}."
311 ;;
312esac
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500313custom_extention toaster_postpend $CMD $ADDR_PORT
314