blob: f92d38ecabd951722f84292ff4c84de4a6b3eacf [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="
21Usage: source toaster start|stop [webport=<address:port>] [noweb]
22 Optional arguments:
23 [noweb] Setup the environment for building with toaster but don't start the development server
24 [webport] Set the development server (default: localhost:8000)
25"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
27webserverKillAll()
28{
29 local pidfile
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031 if [ -f ${pidfile} ]; then
32 pid=`cat ${pidfile}`
33 while kill -0 $pid 2>/dev/null; do
34 kill -SIGTERM -$pid 2>/dev/null
35 sleep 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036 done
37 rm ${pidfile}
38 fi
39 done
40}
41
42webserverStartAll()
43{
44 # do not start if toastermain points to a valid process
45 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
46 retval=1
47 rm "${BUILDDIR}/.toastermain.pid"
48 fi
49
50 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050051 # you can always add a superuser later via
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050052 # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
53 $MANAGE migrate --noinput || retval=1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050054
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 if [ $retval -eq 1 ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050056 echo "Failed migrations, aborting system start" 1>&2
Patrick Williamsf1e5d692016-03-30 15:21:19 -050057 return $retval
58 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060059 # Make sure that checksettings can pick up any value for TEMPLATECONF
60 export TEMPLATECONF
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061 $MANAGE checksettings --traceback || retval=1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050062
63 if [ $retval -eq 1 ]; then
64 printf "\nError while checking settings; aborting\n"
65 return $retval
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 fi
Patrick Williamsf1e5d692016-03-30 15:21:19 -050067
68 echo "Starting webserver..."
69
Patrick Williamsc0f7c042017-02-23 20:41:17 -060070 $MANAGE runserver "$ADDR_PORT" \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050071 </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
72 & echo $! >${BUILDDIR}/.toastermain.pid
Patrick Williamsf1e5d692016-03-30 15:21:19 -050073
74 sleep 1
75
76 if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
77 retval=1
78 rm "${BUILDDIR}/.toastermain.pid"
79 else
Patrick Williamsc0f7c042017-02-23 20:41:17 -060080 echo "Toaster development webserver started at http://$ADDR_PORT"
81 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"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050082 fi
83
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 return $retval
85}
86
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087INSTOPSYSTEM=0
88
89# define the stop command
90stop_system()
91{
92 # prevent reentry
93 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
94 INSTOPSYSTEM=1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096 # unset exported variables
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 unset TOASTER_DIR
98 unset BITBAKE_UI
99 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 trap - SIGHUP
101 #trap - SIGCHLD
102 INSTOPSYSTEM=0
103}
104
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500106 # Verify Django version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600107 reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500108 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
109 exp=$exp'import sys,django;version=django.get_version().split(".");'
110 exp=$exp'sys.exit(not (version \1 "\2".split(".") and version \3 "\4".split(".")))/p'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600111 if ! sed -n "$exp" $reqfile | python3 - ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500112 req=`grep ^Django $reqfile`
113 echo "This program needs $req"
114 echo "Please install with pip install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115 return 2
116 fi
117
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 return 0
119}
120
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121# read command line parameters
122if [ -n "$BASH_SOURCE" ] ; then
123 TOASTER=${BASH_SOURCE}
124elif [ -n "$ZSH_NAME" ] ; then
125 TOASTER=${(%):-%x}
126else
127 TOASTER=$0
128fi
129
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500130export BBBASEDIR=`dirname $TOASTER`/..
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
132OE_ROOT=`dirname $TOASTER`/../..
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500133
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500134# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500135# we are using the same logic that oe-setup-builddir uses
136# (based on TEMPLATECONF and .templateconf) to determine
137# which toasterconf.json to use.
138# note: There are a number of relative path assumptions
139# in the local layers that currently make using an arbitrary
140# toasterconf.json difficult.
141
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600142. $OE_ROOT/.templateconf
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143if [ -n "$TEMPLATECONF" ]; then
144 if [ ! -d "$TEMPLATECONF" ]; then
145 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600146 if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
147 TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 fi
149 fi
150fi
151
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600152unset OE_ROOT
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500154# this defines the dir toaster will use for
155# 1) clones of layers (in _toaster_clones )
156# 2) the build dir (in build)
157# 3) the sqlite db if that is being used.
158# 4) pid's we need to clean up on exit/shutdown
159# note: for future. in order to make this an arbitrary directory, we need to
160# make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does.
161export TOASTER_DIR=`pwd`
162
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500163WEBSERVER=1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600164ADDR_PORT="localhost:8000"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166for param in $*; do
167 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 noweb )
169 WEBSERVER=0
170 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 start )
172 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500174 stop )
175 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 ;;
177 webport=*)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600178 ADDR_PORT="${param#*=}"
179 # Split the addr:port string
180 ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
181 PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
182 # If only a port has been speified then set address to localhost.
183 if [ $ADDR = $PORT ] ; then
184 ADDR_PORT="localhost:$PORT"
185 fi
186 ;;
187 --help)
188 echo "$HELP"
189 return 0
190 ;;
191 *)
192 echo "$HELP"
193 return 1
194 ;;
195
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196 esac
197done
198
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500199if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
200 echo "Error: This script needs to be sourced. Please run as . $TOASTER"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201 return 1
202fi
203
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500204verify_prereq || return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205
206# We make sure we're running in the current shell and in a good environment
207if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
208 echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
209 return 2
210fi
211
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500212# this defines the dir toaster will use for
213# 1) clones of layers (in _toaster_clones )
214# 2) the build dir (in build)
215# 3) the sqlite db if that is being used.
216# 4) pid's we need to clean up on exit/shutdown
217# note: for future. in order to make this an arbitrary directory, we need to
218# make sure that the toaster.sqlite file doesn't default to `pwd`
219# like it currently does.
220export TOASTER_DIR=`dirname $BUILDDIR`
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221
222# Determine the action. If specified by arguments, fine, if not, toggle it
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500223if [ "$CMD" = "start" ] ; then
224 if [ -n "$BBSERVER" ]; then
225 echo " Toaster is already running. Exiting..."
226 return 1
227fi
228elif [ "$CMD" = "" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600229 echo "No command specified"
230 echo "$HELP"
231 return 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232fi
233
234echo "The system will $CMD."
235
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236# Execute the commands
237
238case $CMD in
239 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240 # check if addr:port is not in use
241 if [ "$CMD" == 'start' ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600242 if [ $WEBSERVER -gt 0 ]; then
243 $MANAGE checksocket "$ADDR_PORT" || return 1
244 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245 fi
246
247 # Create configuration file
248 conf=${BUILDDIR}/conf/local.conf
249 line='INHERIT+="toaster buildhistory"'
250 grep -q "$line" $conf || echo $line >> $conf
251
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
253 echo "Failed ${CMD}."
254 return 4
255 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500256 export BITBAKE_UI='toasterui'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500257 $MANAGE runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid
258 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500260 echo "Successful ${CMD}."
261 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 ;;
263 stop )
264 stop_system
265 echo "Successful ${CMD}."
266 ;;
267esac