blob: 70c66d2c2983522c81e962b942ccd0b82dede162 [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 Williamsd8c66bc2016-06-20 12:57:21 -050020# Usage: source toaster [start|stop]
21# [webport=<port>] [noui] [noweb]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022
23# Helper function to kill a background toaster development server
24
25webserverKillAll()
26{
27 local pidfile
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028 for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029 if [ -f ${pidfile} ]; then
30 pid=`cat ${pidfile}`
31 while kill -0 $pid 2>/dev/null; do
32 kill -SIGTERM -$pid 2>/dev/null
33 sleep 1
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050034 # Kill processes if they are still running - may happen
35 # in interactive shells
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036 ps fux | grep "python.*manage.py runserver" | awk '{print $2}' | xargs kill
37 done
38 rm ${pidfile}
39 fi
40 done
41}
42
43webserverStartAll()
44{
45 # do not start if toastermain points to a valid process
46 if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
47 retval=1
48 rm "${BUILDDIR}/.toastermain.pid"
49 fi
50
51 retval=0
Patrick Williamsf1e5d692016-03-30 15:21:19 -050052 # you can always add a superuser later via
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
54 $MANAGE migrate --noinput || retval=1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050055
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 if [ $retval -eq 1 ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050057 echo "Failed migrations, aborting system start" 1>&2
Patrick Williamsf1e5d692016-03-30 15:21:19 -050058 return $retval
59 fi
60
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 Williamsd8c66bc2016-06-20 12:57:21 -050070 $MANAGE runserver "0.0.0.0:$WEB_PORT" \
71 </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
80 echo "Webserver address: http://0.0.0.0:$WEB_PORT/"
81 fi
82
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 return $retval
84}
85
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086INSTOPSYSTEM=0
87
88# define the stop command
89stop_system()
90{
91 # prevent reentry
92 if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
93 INSTOPSYSTEM=1
94 if [ -f ${BUILDDIR}/.toasterui.pid ]; then
95 kill `cat ${BUILDDIR}/.toasterui.pid` 2>/dev/null
96 rm ${BUILDDIR}/.toasterui.pid
97 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 webserverKillAll
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 # unset exported variables
100 unset DATABASE_URL
101 unset TOASTER_CONF
102 unset TOASTER_DIR
103 unset BITBAKE_UI
104 unset BBBASEDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 trap - SIGHUP
106 #trap - SIGCHLD
107 INSTOPSYSTEM=0
108}
109
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110verify_prereq() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500111 # Verify Django version
112 reqfile=$(python -c "import os; print os.path.realpath('$BBBASEDIR/toaster-requirements.txt')")
113 exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
114 exp=$exp'import sys,django;version=django.get_version().split(".");'
115 exp=$exp'sys.exit(not (version \1 "\2".split(".") and version \3 "\4".split(".")))/p'
116 if ! sed -n "$exp" $reqfile | python - ; then
117 req=`grep ^Django $reqfile`
118 echo "This program needs $req"
119 echo "Please install with pip install -r $reqfile"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 return 2
121 fi
122
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123 return 0
124}
125
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126# read command line parameters
127if [ -n "$BASH_SOURCE" ] ; then
128 TOASTER=${BASH_SOURCE}
129elif [ -n "$ZSH_NAME" ] ; then
130 TOASTER=${(%):-%x}
131else
132 TOASTER=$0
133fi
134
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500135export BBBASEDIR=`dirname $TOASTER`/..
136MANAGE=$BBBASEDIR/lib/toaster/manage.py
137OEROOT=`dirname $TOASTER`/../..
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500138
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500139# this is the configuraton file we are using for toaster
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500140# we are using the same logic that oe-setup-builddir uses
141# (based on TEMPLATECONF and .templateconf) to determine
142# which toasterconf.json to use.
143# note: There are a number of relative path assumptions
144# in the local layers that currently make using an arbitrary
145# toasterconf.json difficult.
146
147. $OEROOT/.templateconf
148if [ -n "$TEMPLATECONF" ]; then
149 if [ ! -d "$TEMPLATECONF" ]; then
150 # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
151 if [ -d "$OEROOT/$TEMPLATECONF" ]; then
152 TEMPLATECONF="$OEROOT/$TEMPLATECONF"
153 fi
154 if [ ! -d "$TEMPLATECONF" ]; then
155 echo >&2 "Error: '$TEMPLATECONF' must be a directory containing toasterconf.json"
156 return 1
157 fi
158 fi
159fi
160
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500161if [ "$TOASTER_CONF" = "" ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500162 TOASTER_CONF="$TEMPLATECONF/toasterconf.json"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500163 export TOASTER_CONF=$(python -c "import os; print os.path.realpath('$TOASTER_CONF')")
164fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500166if [ ! -f $TOASTER_CONF ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 echo "$TOASTER_CONF configuration file not found. Set TOASTER_CONF to specify file or fix .templateconf"
168 return 1
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500169fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500170
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500171# this defines the dir toaster will use for
172# 1) clones of layers (in _toaster_clones )
173# 2) the build dir (in build)
174# 3) the sqlite db if that is being used.
175# 4) pid's we need to clean up on exit/shutdown
176# note: for future. in order to make this an arbitrary directory, we need to
177# make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does.
178export TOASTER_DIR=`pwd`
179
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500180WEBSERVER=1
181WEB_PORT="8000"
182unset CMD
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183for param in $*; do
184 case $param in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185 noweb )
186 WEBSERVER=0
187 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500188 start )
189 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500191 stop )
192 CMD=$param
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 ;;
194 webport=*)
195 WEB_PORT="${param#*=}"
196 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 Williamsc124f4f2015-09-15 14:41:29 -0500229 if [ -z "$BBSERVER" ]; then
230 CMD="start"
231 else
232 CMD="stop"
233 fi
234fi
235
236echo "The system will $CMD."
237
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238# Execute the commands
239
240case $CMD in
241 start )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500242 # check if addr:port is not in use
243 if [ "$CMD" == 'start' ]; then
244 $MANAGE checksocket "0.0.0.0:$WEB_PORT" || return 1
245 fi
246
247 # kill Toaster web server if it's alive
248 if [ -e $BUILDDIR/.toastermain.pid ] && kill -0 `cat $BUILDDIR/.toastermain.pid`; then
249 echo "Warning: bitbake appears to be dead, but the Toaster web server is running." 1>&2
250 echo " Something fishy is going on." 1>&2
251 echo "Cleaning up the web server to start from a clean slate."
252 webserverKillAll
253 fi
254
255 # Create configuration file
256 conf=${BUILDDIR}/conf/local.conf
257 line='INHERIT+="toaster buildhistory"'
258 grep -q "$line" $conf || echo $line >> $conf
259
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
261 echo "Failed ${CMD}."
262 return 4
263 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500264 export BITBAKE_UI='toasterui'
265 export DATABASE_URL=`$MANAGE get-dburl`
266 $MANAGE runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid
267 # set fail safe stop system on terminal exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268 trap stop_system SIGHUP
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500269 echo "Successful ${CMD}."
270 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271 ;;
272 stop )
273 stop_system
274 echo "Successful ${CMD}."
275 ;;
276esac