123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- #!/bin/echo ERROR: This script needs to be sourced. Please run as .
- # (c) 2013 Intel Corp.
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- # This script can be run in two modes.
- # When used with "source", from a build directory,
- # it enables toaster event logging and starts the bitbake resident server.
- # use as: source toaster [start|stop] [noweb] [noui]
- # When it is called as a stand-alone script, it starts just the
- # web server, and the building shall be done through the web interface.
- # As script, it will not return to the command prompt. Stop with Ctrl-C.
- # Helper function to kill a background toaster development server
- webserverKillAll()
- {
- local pidfile
- for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
- if [ -f ${pidfile} ]; then
- pid=`cat ${pidfile}`
- while kill -0 $pid 2>/dev/null; do
- kill -SIGTERM -$pid 2>/dev/null
- sleep 1
- # Kill processes if they are still running - may happen in interactive shells
- ps fux | grep "python.*manage.py runserver" | awk '{print $2}' | xargs kill
- done
- rm ${pidfile}
- fi
- done
- }
- webserverStartAll()
- {
- # do not start if toastermain points to a valid process
- if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
- retval=1
- rm "${BUILDDIR}/.toastermain.pid"
- fi
- retval=0
- # you can always add a superuser later via
- # python bitbake/lib/toaster/manage.py python manage.py createsuperuser --username=<ME>
- python $BBBASEDIR/lib/toaster/manage.py syncdb --noinput || retval=1
- python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=2
- if [ $retval -eq 1 ]; then
- echo "Failed db sync, aborting system start" 1>&2
- return $retval
- fi
- python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=1
- if [ $retval -eq 1 ]; then
- printf "\nError on orm migration, rolling back...\n"
- python $BBBASEDIR/lib/toaster/manage.py migrate orm 0001_initial --fake
- return $retval
- fi
- python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol || retval=1
- if [ $retval -eq 1 ]; then
- printf "\nError on bldcontrol migration, rolling back...\n"
- python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol 0001_initial --fake
- return $retval
- fi
- python $BBBASEDIR/lib/toaster/manage.py checksettings --traceback || retval=1
- if [ $retval -eq 1 ]; then
- printf "\nError while checking settings; aborting\n"
- return $retval
- fi
- echo "Starting webserver..."
- python $BBBASEDIR/lib/toaster/manage.py runserver "0.0.0.0:$WEB_PORT" </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 & echo $! >${BUILDDIR}/.toastermain.pid
- sleep 1
- if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
- retval=1
- rm "${BUILDDIR}/.toastermain.pid"
- else
- echo "Webserver address: http://0.0.0.0:$WEB_PORT/"
- fi
- return $retval
- }
- # Helper functions to add a special configuration file
- addtoConfiguration()
- {
- file=$1
- shift
- echo "#Created by toaster start script" > ${BUILDDIR}/conf/$file
- for var in "$@"; do echo $var >> ${BUILDDIR}/conf/$file; done
- }
- INSTOPSYSTEM=0
- # define the stop command
- stop_system()
- {
- # prevent reentry
- if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
- INSTOPSYSTEM=1
- if [ -f ${BUILDDIR}/.toasterui.pid ]; then
- kill `cat ${BUILDDIR}/.toasterui.pid` 2>/dev/null
- rm ${BUILDDIR}/.toasterui.pid
- fi
- stop_bitbake
- webserverKillAll
- # unset exported variables
- unset DATABASE_URL
- unset TOASTER_CONF
- unset TOASTER_DIR
- trap - SIGHUP
- #trap - SIGCHLD
- INSTOPSYSTEM=0
- }
- start_bitbake() {
- unset BBSERVER
- bitbake --postread conf/toaster.conf --server-only -t xmlrpc -B 0.0.0.0:0
- if [ $? -ne 0 ]; then
- echo "Bitbake server start failed"
- return 1
- fi
- export BBSERVER=0.0.0.0:-1
- export DATABASE_URL=`$BBBASEDIR/lib/toaster/manage.py get-dburl`
- if [ $NOTOASTERUI -eq 0 ]; then # we start the TOASTERUI only if not inhibited
- bitbake --observe-only -u toasterui --remote-server=$BBSERVER -t xmlrpc >>${BUILDDIR}/toaster_ui.log 2>&1 \
- & echo $! >${BUILDDIR}/.toasterui.pid
- fi
- return 0
- }
- stop_bitbake() {
- BBSERVER=0.0.0.0:-1 bitbake -m
- unset BBSERVER
- # force stop any misbehaving bitbake server
- lsof bitbake.lock | awk '{print $2}' | grep "[0-9]\+" | xargs -n1 -r kill
- }
- check_pidbyfile() {
- [ -e $1 ] && kill -0 `cat $1` 2>/dev/null
- }
- notify_chldexit() {
- if [ $NOTOASTERUI -eq 0 ]; then
- check_pidbyfile ${BUILDDIR}/.toasterui.pid && return
- stop_system
- fi
- }
- verify_prereq() {
- # Verify prerequisites
- if ! echo "import django; print (1,) == django.VERSION[0:1] and django.VERSION[1:2][0] in (8,)" | python 2>/dev/null | grep True >/dev/null; then
- printf "This program needs Django 1.8. Please install with\n\npip install django<=1.8.7\n"
- return 2
- fi
- return 0
- }
- # read command line parameters
- if [ -n "$BASH_SOURCE" ] ; then
- TOASTER=${BASH_SOURCE}
- elif [ -n "$ZSH_NAME" ] ; then
- TOASTER=${(%):-%x}
- else
- TOASTER=$0
- fi
- BBBASEDIR=`dirname $TOASTER`/..
- OEROOT=`dirname $TOASTER`/../..
- RUNNING=0
- NOTOASTERUI=0
- WEBSERVER=1
- TOASTER_BRBE=""
- if [ "$WEB_PORT" = "" ]; then
- WEB_PORT="8000"
- fi
- # this is the configuraton file we are using for toaster
- # we are using the same logic that oe-setup-builddir uses
- # (based on TEMPLATECONF and .templateconf) to determine
- # which toasterconf.json to use.
- # note: There are a number of relative path assumptions
- # in the local layers that currently make using an arbitrary
- # toasterconf.json difficult.
- . $OEROOT/.templateconf
- if [ -n "$TEMPLATECONF" ]; then
- if [ ! -d "$TEMPLATECONF" ]; then
- # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
- if [ -d "$OEROOT/$TEMPLATECONF" ]; then
- TEMPLATECONF="$OEROOT/$TEMPLATECONF"
- fi
- if [ ! -d "$TEMPLATECONF" ]; then
- echo >&2 "Error: '$TEMPLATECONF' must be a directory containing toasterconf.json"
- [ "$TOASTER_MANAGED" = '1' ] && exit 1 || return 1
- fi
- fi
- fi
- if [ "$TOASTER_CONF" = "" ]; then
- TOASTER_CONF="$TEMPLATECONF/toasterconf.json"
- export TOASTER_CONF=$(python -c "import os; print os.path.realpath('$TOASTER_CONF')")
- fi
- if [ ! -f $TOASTER_CONF ]; then
- echo "$TOASTER_CONF configuration file not found. Set TOASTER_CONF to specify file or fix .templateconf"
- [ "$TOASTER_MANAGED" = '1' ] && exit 1 || return 1
- fi
- # this defines the dir toaster will use for
- # 1) clones of layers (in _toaster_clones )
- # 2) the build dir (in build)
- # 3) the sqlite db if that is being used.
- # 4) pid's we need to clean up on exit/shutdown
- # note: for future. in order to make this an arbitrary directory, we need to
- # make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does.
- export TOASTER_DIR=`pwd`
- NOBROWSER=0
- for param in $*; do
- case $param in
- noui )
- NOTOASTERUI=1
- ;;
- noweb )
- WEBSERVER=0
- ;;
- nobrowser )
- NOBROWSER=1
- ;;
- brbe=* )
- TOASTER_BRBE=$'\n'"TOASTER_BRBE=\""${param#*=}"\""
- ;;
- webport=*)
- WEB_PORT="${param#*=}"
- esac
- done
- if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
- echo "Error: This script needs to be sourced. Please run as . $TOASTER"
- exit 1
- fi
- if [ "$1" = 'restart-bitbake' ] ; then
- stop_bitbake
- sleep 1
- start_bitbake
- rc=$?
- sleep 1
- return $rc
- fi
- if ! verify_prereq; then
- echo "Error: Could not verify that the needed dependencies are installed. Please use virtualenv and pip to install dependencies listed in toaster-requirements.txt" 1>&2
- return 1
- fi
- # We make sure we're running in the current shell and in a good environment
- if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
- echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
- return 2
- fi
- # this is the configuraton file we are using for toaster
- # note default is assuming yocto. Override this if you are
- # running in a pure OE environment and use the toasterconf.json
- # in meta/conf/toasterconf.json
- # note: for future there are a number of relative path assumptions
- # in the local layers that currently prevent using an arbitrary
- # toasterconf.json
- if [ "$TOASTER_CONF" = "" ]; then
- TOASTER_CONF="$(dirname $TOASTER)/../../meta-yocto/conf/toasterconf.json"
- export TOASTER_CONF=$(python -c "import os; print os.path.realpath('$TOASTER_CONF')")
- fi
- if [ ! -f $TOASTER_CONF ]; then
- echo "$TOASTER_CONF configuration file not found. set TOASTER_CONF to specify a path"
- return 1
- fi
- # this defines the dir toaster will use for
- # 1) clones of layers (in _toaster_clones )
- # 2) the build dir (in build)
- # 3) the sqlite db if that is being used.
- # 4) pid's we need to clean up on exit/shutdown
- # note: for future. in order to make this an arbitrary directory, we need to
- # make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does.
- export TOASTER_DIR=`dirname $BUILDDIR`
- # Determine the action. If specified by arguments, fine, if not, toggle it
- if [ "$1" = 'start' ] || [ "$1" = 'stop' ]; then
- CMD="$1"
- else
- if [ -z "$BBSERVER" ]; then
- CMD="start"
- else
- CMD="stop"
- fi
- fi
- echo "The system will $CMD."
- # Make sure it's safe to run by checking bitbake lock
- lock=1
- if [ -e $BUILDDIR/bitbake.lock ]; then
- python -c "import fcntl; fcntl.flock(open(\"$BUILDDIR/bitbake.lock\"), fcntl.LOCK_EX|fcntl.LOCK_NB)" 2>/dev/null || lock=0
- fi
- if [ ${CMD} = 'start' ] && [ $lock -eq 0 ]; then
- echo "Error: bitbake lock state error. File locks show that the system is on." 1>&2
- echo "Please wait for the current build to finish, stop and then start the system again." 1>&2
- return 3
- fi
- if [ ${CMD} = 'start' ] && [ -e $BUILDDIR/.toastermain.pid ] && kill -0 `cat $BUILDDIR/.toastermain.pid`; then
- echo "Warning: bitbake appears to be dead, but the Toaster web server is running. Something fishy is going on." 1>&2
- echo "Cleaning up the web server to start from a clean slate."
- webserverKillAll
- fi
- # Execute the commands
- case $CMD in
- start )
- addtoConfiguration toaster.conf "INHERIT+=\"toaster buildhistory\"" $TOASTER_BRBE
- if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
- echo "Failed ${CMD}."
- return 4
- fi
- start_bitbake
- if [ $? -eq 0 ]; then
- python $BBBASEDIR/lib/toaster/manage.py runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid
- # set fail safe stop system on terminal exit
- trap stop_system SIGHUP
- echo "Successful ${CMD}."
- return 0
- else
- # failed start, do stop
- stop_system
- echo "Failed ${CMD}."
- return 1
- fi
- # stop system on terminal exit
- set -o monitor
- trap stop_system SIGHUP
- #trap notify_chldexit SIGCHLD
- ;;
- stop )
- stop_system
- echo "Successful ${CMD}."
- ;;
- esac
|