| 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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 20 | HELP=" | 
|  | 21 | Usage: 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 |  | 
|  | 27 | webserverKillAll() | 
|  | 28 | { | 
|  | 29 | local pidfile | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 30 | for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | done | 
|  | 37 | rm  ${pidfile} | 
|  | 38 | fi | 
|  | 39 | done | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | webserverStartAll() | 
|  | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 51 | # you can always add a superuser later via | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 52 | # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME> | 
|  | 53 | $MANAGE migrate --noinput || retval=1 | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 54 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | if [ $retval -eq 1 ]; then | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 | echo "Failed migrations, aborting system start" 1>&2 | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 57 | return $retval | 
|  | 58 | fi | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 59 | # Make sure that checksettings can pick up any value for TEMPLATECONF | 
|  | 60 | export TEMPLATECONF | 
| 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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 70 | $MANAGE runserver "$ADDR_PORT" \ | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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 | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 80 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 82 | fi | 
|  | 83 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | return $retval | 
|  | 85 | } | 
|  | 86 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | INSTOPSYSTEM=0 | 
|  | 88 |  | 
|  | 89 | # define the stop command | 
|  | 90 | stop_system() | 
|  | 91 | { | 
|  | 92 | # prevent reentry | 
|  | 93 | if [ $INSTOPSYSTEM -eq 1 ]; then return; fi | 
|  | 94 | INSTOPSYSTEM=1 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | webserverKillAll | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 96 | # unset exported variables | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 97 | unset TOASTER_DIR | 
|  | 98 | unset BITBAKE_UI | 
|  | 99 | unset BBBASEDIR | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | trap - SIGHUP | 
|  | 101 | #trap - SIGCHLD | 
|  | 102 | INSTOPSYSTEM=0 | 
|  | 103 | } | 
|  | 104 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | verify_prereq() { | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 106 | # Verify Django version | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 107 | reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))") | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 108 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | if ! sed -n "$exp" $reqfile | python3 - ; then | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 112 | req=`grep ^Django $reqfile` | 
|  | 113 | echo "This program needs $req" | 
|  | 114 | echo "Please install with pip install -r $reqfile" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | return 2 | 
|  | 116 | fi | 
|  | 117 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | return 0 | 
|  | 119 | } | 
|  | 120 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | # read command line parameters | 
|  | 122 | if [ -n "$BASH_SOURCE" ] ; then | 
|  | 123 | TOASTER=${BASH_SOURCE} | 
|  | 124 | elif [ -n "$ZSH_NAME" ] ; then | 
|  | 125 | TOASTER=${(%):-%x} | 
|  | 126 | else | 
|  | 127 | TOASTER=$0 | 
|  | 128 | fi | 
|  | 129 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 130 | export BBBASEDIR=`dirname $TOASTER`/.. | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 131 | MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py" | 
|  | 132 | OE_ROOT=`dirname $TOASTER`/../.. | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 133 |  | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 134 | # this is the configuraton file we are using for toaster | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 135 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 142 | . $OE_ROOT/.templateconf | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 143 | if [ -n "$TEMPLATECONF" ]; then | 
|  | 144 | if [ ! -d "$TEMPLATECONF" ]; then | 
|  | 145 | # Allow TEMPLATECONF=meta-xyz/conf as a shortcut | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 146 | if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then | 
|  | 147 | TEMPLATECONF="$OE_ROOT/$TEMPLATECONF" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 148 | fi | 
|  | 149 | fi | 
|  | 150 | fi | 
|  | 151 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 152 | unset OE_ROOT | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 153 |  | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 154 | # 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. | 
|  | 161 | export TOASTER_DIR=`pwd` | 
|  | 162 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 163 | WEBSERVER=1 | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 164 | ADDR_PORT="localhost:8000" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | unset CMD | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | for param in $*; do | 
|  | 167 | case $param in | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | noweb ) | 
|  | 169 | WEBSERVER=0 | 
|  | 170 | ;; | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 171 | start ) | 
|  | 172 | CMD=$param | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 173 | ;; | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 174 | stop ) | 
|  | 175 | CMD=$param | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | ;; | 
|  | 177 | webport=*) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 178 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 229 | echo "No command specified" | 
|  | 230 | echo "$HELP" | 
|  | 231 | return 1 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | fi | 
|  | 233 |  | 
|  | 234 | echo "The system will $CMD." | 
|  | 235 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 236 | # Execute the commands | 
|  | 237 |  | 
|  | 238 | case $CMD in | 
|  | 239 | start ) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 240 | # check if addr:port is not in use | 
|  | 241 | if [ "$CMD" == 'start' ]; then | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 242 | if [ $WEBSERVER -gt 0 ]; then | 
|  | 243 | $MANAGE checksocket "$ADDR_PORT" || return 1 | 
|  | 244 | fi | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 245 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 252 | if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then | 
|  | 253 | echo "Failed ${CMD}." | 
|  | 254 | return 4 | 
|  | 255 | fi | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 256 | export BITBAKE_UI='toasterui' | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 257 | $MANAGE runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid | 
|  | 258 | # set fail safe stop system on terminal exit | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 259 | trap stop_system SIGHUP | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 260 | echo "Successful ${CMD}." | 
|  | 261 | return 0 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 262 | ;; | 
|  | 263 | stop ) | 
|  | 264 | stop_system | 
|  | 265 | echo "Successful ${CMD}." | 
|  | 266 | ;; | 
|  | 267 | esac |