toaster script improvements
cover toaster script improvements
Hi,
This patchset includes bunch of readability, usability and code style fixes for toaster script. It was started as a fix for #8775 and then I just couldn't stop and decided to make the script a bit better.
The following changes since commit 8e8b585191cafe9420e31de58d786afffe93d532:
toasterconf: remove SDKMACHINE variable (2015-12-10 16:28:21 +0200)
are available in the git repository at:
git://git.yoctoproject.org/poky-contrib ed/toaster/improve-toaster-script
http://git.yoctoproject.org/cgit.cgi/poky-contrib/log/?h=ed/toaster/improve-toaster-script
Ed Bartosh (11):
toaster: implement checksocket command
toaster: check if address:port is in use
toaster: split long lines, add/remove witespaces
toaster: remove unused variable
toaster: add MANAGE variable
toaster: updated header of the toaster script
toaster: remove addtoConfiguration function
toaster: remove 2 unused functions
toaster: move checks to another place
toaster: move setting of default values
toaster: remove 2 confusing parameters
bitbake/bin/toaster | 144 +++++++++------------ .../toastermain/management/commands/checksocket.py | 69 ++++++++++ 2 files changed, 129 insertions(+), 84 deletions(-) create mode 100644 bitbake/lib/toaster/toastermain/management/commands/checksocket.py
--
Regards,
Ed
PATCH 1/11 toaster: implement checksocket command
Implemented new management command to check if it's possible to listen on specified address:port.
[YOCTO #8775]
--- .../toastermain/management/commands/checksocket.py | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 bitbake/lib/toaster/toastermain/management/commands/checksocket.py
bitbake/lib/toaster/toastermain/management/commands/checksocket.py new file +69 -0
@@ -0,0 +1,69 @@ +#!/usr/bin/env python +# ex:ts=4:sw=4:sts=4:et +# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- +# +# BitBake Toaster Implementation +# +# Copyright (C) 2015 Intel Corporation +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Custom management command checksocket.""" + +import errno +import socket + +from django.core.management.base import BaseCommand, CommandError +from django.utils.encoding import force_text + +DEFAULT_ADDRPORT = "0.0.0.0:8000" + +class Command(BaseCommand): + """Custom management command.""" + + help = 'Check if Toaster can listen on address:port' + + def add_arguments(self, parser): + parser.add_argument('addrport', nargs='?', default=DEFAULT_ADDRPORT, + help='ipaddr:port to check, %s by default' % \ + DEFAULT_ADDRPORT) + + def handle(self, *args, **options): + addrport = options['addrport'] + if ':' not in addrport: + raise CommandError('Invalid addr:port specified: %s' % addrport) + splitted = addrport.split(':') + try: + splitted[1] = int(splitted[1]) + except ValueError: + raise CommandError('Invalid port specified: %s' % splitted[1]) + self.stdout.write('Check if toaster can listen on %s' % addrport) + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(tuple(splitted)) + except (socket.error, OverflowError) as err: + errors = { + errno.EACCES: 'You don\'t have permission to access port %s' \ + % splitted[1], + errno.EADDRINUSE: 'Port %s is already in use' % splitted[1], + errno.EADDRNOTAVAIL: 'IP address can\'t be assigned to', + } + if hasattr(err, 'errno') and err.errno in errors: + errtext = errors[err.errno] + else: + errtext = force_text(err) + raise CommandError(errtext) + + self.stdout.write("OK")
Signature · 1 line
2.1.4
PATCH 2/11 toaster: check if address:port is in use
In reply to Ed BartoshUsed new management command checksocket to check if Toaster can listen on address:port.
[YOCTO #8775]
--- bitbake/bin/toaster | 5 +++++ 1 file changed, 5 insertions(+)
bitbake/bin/toaster +5 -0
@@ -309,6 +309,11 @@ fi echo "The system will $CMD." +# check if addr:port is not in use +if [ "$CMD" == 'start' ]; then + python $BBBASEDIR/lib/toaster/manage.py checksocket "0.0.0.0:$WEB_PORT" || return 1 +fi + # Make sure it's safe to run by checking bitbake lock lock=1
Signature · 1 line
2.1.4
PATCH 3/11 toaster: split long lines, add/remove witespaces
In reply to Ed BartoshMade toaster script more readable by splitting long lines and removing and adding whitespaces.
--- bitbake/bin/toaster | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
bitbake/bin/toaster +19 -12
@@ -37,7 +37,8 @@ webserverKillAll() 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 + # 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}
@@ -55,7 +56,7 @@ webserverStartAll() retval=0 # you can always add a superuser later via - # python bitbake/lib/toaster/manage.py python manage.py createsuperuser --username=<ME> + # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME> python $BBBASEDIR/lib/toaster/manage.py migrate --noinput || retval=1 if [ $retval -eq 1 ]; then
@@ -72,7 +73,9 @@ webserverStartAll() 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 + 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
@@ -128,8 +131,10 @@ start_bitbake() { 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 \ + # we start the TOASTERUI only if not inhibited + if [ $NOTOASTERUI -eq 0 ]; then + bitbake --observe-only -u toasterui --remote-server=$BBSERVER -t xmlrpc \ + >>${BUILDDIR}/toaster_ui.log 2>&1 \ & echo $! >${BUILDDIR}/.toasterui.pid fi return 0
@@ -146,7 +151,6 @@ check_pidbyfile() { [ -e $1 ] && kill -0 `cat $1` 2>/dev/null } - notify_chldexit() { if [ $NOTOASTERUI -eq 0 ]; then check_pidbyfile ${BUILDDIR}/.toasterui.pid && return
@@ -154,7 +158,6 @@ notify_chldexit() { fi } - verify_prereq() { # Verify Django version reqfile=$(python -c "import os; print os.path.realpath('$BBBASEDIR/toaster-requirements.txt')")
@@ -171,7 +174,6 @@ verify_prereq() { return 0 } - # read command line parameters if [ -n "$BASH_SOURCE" ] ; then TOASTER=${BASH_SOURCE}
@@ -207,7 +209,7 @@ if [ -n "$TEMPLATECONF" ]; then fi if [ ! -d "$TEMPLATECONF" ]; then echo >&2 "Error: '$TEMPLATECONF' must be a directory containing toasterconf.json" - [ "$TOASTER_MANAGED" = '1' ] && exit 1 || return 1 + [ "$TOASTER_MANAGED" = '1' ] && exit 1 || return 1 fi fi fi
@@ -216,10 +218,12 @@ 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)
@@ -284,7 +288,8 @@ if [ "$TOASTER_CONF" = "" ]; then 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" + echo "$TOASTER_CONF configuration file not found." + echo " set TOASTER_CONF to specify a path" return 1 fi # this defines the dir toaster will use for
@@ -293,7 +298,8 @@ fi # 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. +# 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
@@ -328,7 +334,8 @@ if [ ${CMD} = 'start' ] && [ $lock -eq 0 ]; then 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 "Warning: bitbake appears to be dead, but the Toaster web server is running." 1>&2 + echo " Something fishy is going on." 1>&2 echo "Cleaning up the web server to start from a clean slate." webserverKillAll fi
Signature · 1 line
2.1.4
PATCH 4/11 toaster: remove unused variable
In reply to Ed BartoshRemoved unused variable RUNNING from the toaster script.
--- bitbake/bin/toaster | 1 - 1 file changed, 1 deletion(-)
bitbake/bin/toaster +0 -1
@@ -185,7 +185,6 @@ fi BBBASEDIR=`dirname $TOASTER`/.. OEROOT=`dirname $TOASTER`/../.. -RUNNING=0 NOTOASTERUI=0 WEBSERVER=1 TOASTER_BRBE=""
Signature · 1 line
2.1.4
PATCH 5/11 toaster: add MANAGE variable
In reply to Ed BartoshUsed MANAGE variable to avoid repeating path tomanage.py in many places in toaster script.
--- bitbake/bin/toaster | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
bitbake/bin/toaster +7 -6
@@ -57,14 +57,14 @@ webserverStartAll() retval=0 # you can always add a superuser later via # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME> - python $BBBASEDIR/lib/toaster/manage.py migrate --noinput || retval=1 + $MANAGE migrate --noinput || retval=1 if [ $retval -eq 1 ]; then echo "Failed migrations, aborting system start" 1>&2 return $retval fi - python $BBBASEDIR/lib/toaster/manage.py checksettings --traceback || retval=1 + $MANAGE checksettings --traceback || retval=1 if [ $retval -eq 1 ]; then printf "\nError while checking settings; aborting\n"
@@ -73,7 +73,7 @@ webserverStartAll() echo "Starting webserver..." - python $BBBASEDIR/lib/toaster/manage.py runserver "0.0.0.0:$WEB_PORT" \ + $MANAGE runserver "0.0.0.0:$WEB_PORT" \ </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \ & echo $! >${BUILDDIR}/.toastermain.pid
@@ -130,7 +130,7 @@ start_bitbake() { return 1 fi export BBSERVER=0.0.0.0:-1 - export DATABASE_URL=`$BBBASEDIR/lib/toaster/manage.py get-dburl` + export DATABASE_URL=`$MANAGE get-dburl` # we start the TOASTERUI only if not inhibited if [ $NOTOASTERUI -eq 0 ]; then bitbake --observe-only -u toasterui --remote-server=$BBSERVER -t xmlrpc \
@@ -184,6 +184,7 @@ else fi BBBASEDIR=`dirname $TOASTER`/.. +MANAGE=$BBBASEDIR/lib/toaster/manage.py OEROOT=`dirname $TOASTER`/../.. NOTOASTERUI=0 WEBSERVER=1
@@ -316,7 +317,7 @@ echo "The system will $CMD." # check if addr:port is not in use if [ "$CMD" == 'start' ]; then - python $BBBASEDIR/lib/toaster/manage.py checksocket "0.0.0.0:$WEB_PORT" || return 1 + $MANAGE checksocket "0.0.0.0:$WEB_PORT" || return 1 fi # Make sure it's safe to run by checking bitbake lock
@@ -351,7 +352,7 @@ case $CMD in fi start_bitbake if [ $? -eq 0 ]; then - python $BBBASEDIR/lib/toaster/manage.py runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid + $MANAGE runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid # set fail safe stop system on terminal exit trap stop_system SIGHUP echo "Successful ${CMD}."
Signature · 1 line
2.1.4
PATCH 6/11 toaster: updated header of the toaster script
In reply to Ed BartoshUpdated GPL information, years of development and usage information. Removed outdated information about 2 ways of starting Toaster.
--- bitbake/bin/toaster | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
bitbake/bin/toaster +7 -13
@@ -1,5 +1,8 @@ #!/bin/echo ERROR: This script needs to be sourced. Please run as . -# (c) 2013 Intel Corp. + +# toaster - shell script to start Toaster + +# Copyright (C) 2013-2015 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
@@ -12,19 +15,10 @@ # 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] +# along with this program. If not, see http://www.gnu.org/licenses/. -# 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. +# Usage: source toaster [start|stop|restart-bitbake] [webport=<port>] +# [noui] [noweb] [nobrowser] [brbe=<BRBE>] # Helper function to kill a background toaster development server
Signature · 1 line
2.1.4
PATCH 7/11 toaster: remove addtoConfiguration function
In reply to Ed BartoshThis function is useless as it's called just once
and makes code less readable.
--- bitbake/bin/toaster | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
bitbake/bin/toaster +6 -11
@@ -83,16 +83,6 @@ webserverStartAll() 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
@@ -339,7 +329,12 @@ fi case $CMD in start ) - addtoConfiguration toaster.conf "INHERIT+=\"toaster buildhistory\"" $TOASTER_BRBE + # Create configuration file + conf=${BUILDDIR}/conf/toaster.conf + echo "# Created by toaster start script" > $conf + echo "INHERIT+=\"toaster buildhistory\"" >> $conf + [ -n "$TOASTER_BRBE" ] && echo $TOASTER_BRBE >> $conf + if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then echo "Failed ${CMD}." return 4
Signature · 1 line
2.1.4
PATCH 8/11 toaster: remove 2 unused functions
In reply to Ed BartoshRemoved check_pidbyfile and notify_chldexit functions from toaster script as they're not called in the script.
--- bitbake/bin/toaster | 12 ------------ 1 file changed, 12 deletions(-)
bitbake/bin/toaster +0 -12
@@ -131,17 +131,6 @@ stop_bitbake() { 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 Django version reqfile=$(python -c "import os; print os.path.realpath('$BBBASEDIR/toaster-requirements.txt')")
@@ -355,7 +344,6 @@ case $CMD in # stop system on terminal exit set -o monitor trap stop_system SIGHUP - #trap notify_chldexit SIGCHLD ;; stop ) stop_system
Signature · 1 line
2.1.4
PATCH 9/11 toaster: move checks to another place
In reply to Ed BartoshMoved addr:port, bitbake.lock and toastermain.pid checks to the place where the rest of Toaster starting happens.
--- bitbake/bin/toaster | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-)
bitbake/bin/toaster +23 -26
@@ -288,36 +288,33 @@ fi echo "The system will $CMD." -# check if addr:port is not in use -if [ "$CMD" == 'start' ]; then - $MANAGE checksocket "0.0.0.0:$WEB_PORT" || return 1 -fi - -# 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." 1>&2 - echo " 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 ) + # check if addr:port is not in use + if [ "$CMD" == 'start' ]; then + $MANAGE checksocket "0.0.0.0:$WEB_PORT" || return 1 + fi + + # Make sure it's safe to start by checking bitbake lock + 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 + if [ $? -ne 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 + fi + + # kill Toaster web server if it's alive + if [ -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." 1>&2 + echo " Something fishy is going on." 1>&2 + echo "Cleaning up the web server to start from a clean slate." + webserverKillAll + fi + # Create configuration file conf=${BUILDDIR}/conf/toaster.conf echo "# Created by toaster start script" > $conf
Signature · 1 line
2.1.4
PATCH 10/11 toaster: move setting of default values
In reply to Ed BartoshSet default values of script parameters just before they are parsed to increase readability.
--- bitbake/bin/toaster | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
bitbake/bin/toaster +5 -6
@@ -159,12 +159,7 @@ fi BBBASEDIR=`dirname $TOASTER`/.. MANAGE=$BBBASEDIR/lib/toaster/manage.py OEROOT=`dirname $TOASTER`/../.. -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
@@ -206,7 +201,11 @@ fi # make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does. export TOASTER_DIR=`pwd` +NOTOASTERUI=0 +WEBSERVER=1 NOBROWSER=0 +TOASTER_BRBE="" +WEB_PORT="8000" for param in $*; do case $param in
Signature · 1 line
2.1.4
PATCH 11/11 toaster: remove 2 confusing parameters
In reply to Ed BartoshRemoved nobrowser and brbe script parameters as both are confusing and nobrowser is not used anywhere.
brbe parameter usage can only be justified if toaster doesn't work properly and user has to manually connect toaster to running bitbake server. Even in this scenario it's very unlikely to achieve as toaster script is not designed for this kind of usage.
--- bitbake/bin/toaster | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
bitbake/bin/toaster +2 -11
@@ -17,8 +17,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see http://www.gnu.org/licenses/. -# Usage: source toaster [start|stop|restart-bitbake] [webport=<port>] -# [noui] [noweb] [nobrowser] [brbe=<BRBE>] +# Usage: source toaster [start|stop|restart-bitbake] +# [webport=<port>] [noui] [noweb] # Helper function to kill a background toaster development server
@@ -203,8 +203,6 @@ export TOASTER_DIR=`pwd` NOTOASTERUI=0 WEBSERVER=1 -NOBROWSER=0 -TOASTER_BRBE="" WEB_PORT="8000" for param in $*; do
@@ -215,12 +213,6 @@ for param in $*; do noweb ) WEBSERVER=0 ;; - nobrowser ) - NOBROWSER=1 - ;; - brbe=* ) - TOASTER_BRBE=$'\n'"TOASTER_BRBE=\""${param#*=}"\"" - ;; webport=*) WEB_PORT="${param#*=}" esac
@@ -318,7 +310,6 @@ case $CMD in conf=${BUILDDIR}/conf/toaster.conf echo "# Created by toaster start script" > $conf echo "INHERIT+=\"toaster buildhistory\"" >> $conf - [ -n "$TOASTER_BRBE" ] && echo $TOASTER_BRBE >> $conf if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then echo "Failed ${CMD}."
Signature · 1 line
2.1.4