Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | #!/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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | # along with this program. If not, see http://www.gnu.org/licenses/. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | # Usage: source toaster [start|stop] |
| 21 | # [webport=<port>] [noui] [noweb] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | # Helper function to kill a background toaster development server |
| 24 | |
| 25 | webserverKillAll() |
| 26 | { |
| 27 | local pidfile |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 28 | for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 34 | # Kill processes if they are still running - may happen |
| 35 | # in interactive shells |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | ps fux | grep "python.*manage.py runserver" | awk '{print $2}' | xargs kill |
| 37 | done |
| 38 | rm ${pidfile} |
| 39 | fi |
| 40 | done |
| 41 | } |
| 42 | |
| 43 | webserverStartAll() |
| 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 52 | # you can always add a superuser later via |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME> |
| 54 | $MANAGE migrate --noinput || retval=1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 55 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | if [ $retval -eq 1 ]; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 57 | echo "Failed migrations, aborting system start" 1>&2 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 58 | return $retval |
| 59 | fi |
| 60 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 61 | $MANAGE checksettings --traceback || retval=1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 62 | |
| 63 | if [ $retval -eq 1 ]; then |
| 64 | printf "\nError while checking settings; aborting\n" |
| 65 | return $retval |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | fi |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 67 | |
| 68 | echo "Starting webserver..." |
| 69 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | $MANAGE runserver "0.0.0.0:$WEB_PORT" \ |
| 71 | </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \ |
| 72 | & echo $! >${BUILDDIR}/.toastermain.pid |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 73 | |
| 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | return $retval |
| 84 | } |
| 85 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | INSTOPSYSTEM=0 |
| 87 | |
| 88 | # define the stop command |
| 89 | stop_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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | webserverKillAll |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 99 | # unset exported variables |
| 100 | unset DATABASE_URL |
| 101 | unset TOASTER_CONF |
| 102 | unset TOASTER_DIR |
| 103 | unset BITBAKE_UI |
| 104 | unset BBBASEDIR |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | trap - SIGHUP |
| 106 | #trap - SIGCHLD |
| 107 | INSTOPSYSTEM=0 |
| 108 | } |
| 109 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | verify_prereq() { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 111 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | return 2 |
| 121 | fi |
| 122 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | return 0 |
| 124 | } |
| 125 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | # read command line parameters |
| 127 | if [ -n "$BASH_SOURCE" ] ; then |
| 128 | TOASTER=${BASH_SOURCE} |
| 129 | elif [ -n "$ZSH_NAME" ] ; then |
| 130 | TOASTER=${(%):-%x} |
| 131 | else |
| 132 | TOASTER=$0 |
| 133 | fi |
| 134 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 135 | export BBBASEDIR=`dirname $TOASTER`/.. |
| 136 | MANAGE=$BBBASEDIR/lib/toaster/manage.py |
| 137 | OEROOT=`dirname $TOASTER`/../.. |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 138 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 139 | # this is the configuraton file we are using for toaster |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 140 | # 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 |
| 148 | if [ -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 |
| 159 | fi |
| 160 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 161 | if [ "$TOASTER_CONF" = "" ]; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 162 | TOASTER_CONF="$TEMPLATECONF/toasterconf.json" |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 163 | export TOASTER_CONF=$(python -c "import os; print os.path.realpath('$TOASTER_CONF')") |
| 164 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 166 | if [ ! -f $TOASTER_CONF ]; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 167 | echo "$TOASTER_CONF configuration file not found. Set TOASTER_CONF to specify file or fix .templateconf" |
| 168 | return 1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 169 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 170 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 171 | # 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. |
| 178 | export TOASTER_DIR=`pwd` |
| 179 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 180 | WEBSERVER=1 |
| 181 | WEB_PORT="8000" |
| 182 | unset CMD |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 183 | for param in $*; do |
| 184 | case $param in |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 185 | noweb ) |
| 186 | WEBSERVER=0 |
| 187 | ;; |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 188 | start ) |
| 189 | CMD=$param |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | ;; |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 191 | stop ) |
| 192 | CMD=$param |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | ;; |
| 194 | webport=*) |
| 195 | WEB_PORT="${param#*=}" |
| 196 | esac |
| 197 | done |
| 198 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 199 | if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then |
| 200 | echo "Error: This script needs to be sourced. Please run as . $TOASTER" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 201 | return 1 |
| 202 | fi |
| 203 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 204 | verify_prereq || return 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 205 | |
| 206 | # We make sure we're running in the current shell and in a good environment |
| 207 | if [ -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 |
| 210 | fi |
| 211 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 212 | # 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. |
| 220 | export TOASTER_DIR=`dirname $BUILDDIR` |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 221 | |
| 222 | # Determine the action. If specified by arguments, fine, if not, toggle it |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 223 | if [ "$CMD" = "start" ] ; then |
| 224 | if [ -n "$BBSERVER" ]; then |
| 225 | echo " Toaster is already running. Exiting..." |
| 226 | return 1 |
| 227 | fi |
| 228 | elif [ "$CMD" = "" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | if [ -z "$BBSERVER" ]; then |
| 230 | CMD="start" |
| 231 | else |
| 232 | CMD="stop" |
| 233 | fi |
| 234 | fi |
| 235 | |
| 236 | echo "The system will $CMD." |
| 237 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 238 | # Execute the commands |
| 239 | |
| 240 | case $CMD in |
| 241 | start ) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 242 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then |
| 261 | echo "Failed ${CMD}." |
| 262 | return 4 |
| 263 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 264 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 268 | trap stop_system SIGHUP |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 269 | echo "Successful ${CMD}." |
| 270 | return 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 271 | ;; |
| 272 | stop ) |
| 273 | stop_system |
| 274 | echo "Successful ${CMD}." |
| 275 | ;; |
| 276 | esac |