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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 27 | databaseCheck() |
| 28 | { |
| 29 | retval=0 |
| 30 | # you can always add a superuser later via |
| 31 | # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME> |
| 32 | $MANAGE migrate --noinput || retval=1 |
| 33 | |
| 34 | if [ $retval -eq 1 ]; then |
| 35 | echo "Failed migrations, aborting system start" 1>&2 |
| 36 | return $retval |
| 37 | fi |
| 38 | # Make sure that checksettings can pick up any value for TEMPLATECONF |
| 39 | export TEMPLATECONF |
| 40 | $MANAGE checksettings --traceback || retval=1 |
| 41 | |
| 42 | if [ $retval -eq 1 ]; then |
| 43 | printf "\nError while checking settings; aborting\n" |
| 44 | return $retval |
| 45 | fi |
| 46 | |
| 47 | return $retval |
| 48 | } |
| 49 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | webserverKillAll() |
| 51 | { |
| 52 | local pidfile |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | if [ -f ${pidfile} ]; then |
| 55 | pid=`cat ${pidfile}` |
| 56 | while kill -0 $pid 2>/dev/null; do |
| 57 | kill -SIGTERM -$pid 2>/dev/null |
| 58 | sleep 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | done |
| 60 | rm ${pidfile} |
| 61 | fi |
| 62 | done |
| 63 | } |
| 64 | |
| 65 | webserverStartAll() |
| 66 | { |
| 67 | # do not start if toastermain points to a valid process |
| 68 | if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then |
| 69 | retval=1 |
| 70 | rm "${BUILDDIR}/.toastermain.pid" |
| 71 | fi |
| 72 | |
| 73 | retval=0 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 74 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 75 | # check the database |
| 76 | databaseCheck || return 1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 77 | |
| 78 | echo "Starting webserver..." |
| 79 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 80 | $MANAGE runserver "$ADDR_PORT" \ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 81 | </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \ |
| 82 | & echo $! >${BUILDDIR}/.toastermain.pid |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 83 | |
| 84 | sleep 1 |
| 85 | |
| 86 | if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then |
| 87 | retval=1 |
| 88 | rm "${BUILDDIR}/.toastermain.pid" |
| 89 | else |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | echo "Toaster development webserver started at http://$ADDR_PORT" |
| 91 | 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] | 92 | fi |
| 93 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | return $retval |
| 95 | } |
| 96 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | INSTOPSYSTEM=0 |
| 98 | |
| 99 | # define the stop command |
| 100 | stop_system() |
| 101 | { |
| 102 | # prevent reentry |
| 103 | if [ $INSTOPSYSTEM -eq 1 ]; then return; fi |
| 104 | INSTOPSYSTEM=1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | webserverKillAll |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 106 | # unset exported variables |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 107 | unset TOASTER_DIR |
| 108 | unset BITBAKE_UI |
| 109 | unset BBBASEDIR |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | trap - SIGHUP |
| 111 | #trap - SIGCHLD |
| 112 | INSTOPSYSTEM=0 |
| 113 | } |
| 114 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | verify_prereq() { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 116 | # Verify Django version |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 117 | 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] | 118 | exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/' |
| 119 | exp=$exp'import sys,django;version=django.get_version().split(".");' |
| 120 | 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] | 121 | if ! sed -n "$exp" $reqfile | python3 - ; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 122 | req=`grep ^Django $reqfile` |
| 123 | echo "This program needs $req" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 124 | echo "Please install with pip3 install -r $reqfile" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | return 2 |
| 126 | fi |
| 127 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | return 0 |
| 129 | } |
| 130 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | # read command line parameters |
| 132 | if [ -n "$BASH_SOURCE" ] ; then |
| 133 | TOASTER=${BASH_SOURCE} |
| 134 | elif [ -n "$ZSH_NAME" ] ; then |
| 135 | TOASTER=${(%):-%x} |
| 136 | else |
| 137 | TOASTER=$0 |
| 138 | fi |
| 139 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 140 | export BBBASEDIR=`dirname $TOASTER`/.. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 141 | MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py" |
| 142 | OE_ROOT=`dirname $TOASTER`/../.. |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 143 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 144 | # this is the configuraton file we are using for toaster |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 145 | # we are using the same logic that oe-setup-builddir uses |
| 146 | # (based on TEMPLATECONF and .templateconf) to determine |
| 147 | # which toasterconf.json to use. |
| 148 | # note: There are a number of relative path assumptions |
| 149 | # in the local layers that currently make using an arbitrary |
| 150 | # toasterconf.json difficult. |
| 151 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 152 | . $OE_ROOT/.templateconf |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 153 | if [ -n "$TEMPLATECONF" ]; then |
| 154 | if [ ! -d "$TEMPLATECONF" ]; then |
| 155 | # Allow TEMPLATECONF=meta-xyz/conf as a shortcut |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 156 | if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then |
| 157 | TEMPLATECONF="$OE_ROOT/$TEMPLATECONF" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 158 | fi |
| 159 | fi |
| 160 | fi |
| 161 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 162 | unset OE_ROOT |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 163 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 164 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 165 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 166 | WEBSERVER=1 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | ADDR_PORT="localhost:8000" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 168 | unset CMD |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 169 | for param in $*; do |
| 170 | case $param in |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | noweb ) |
| 172 | WEBSERVER=0 |
| 173 | ;; |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 174 | start ) |
| 175 | CMD=$param |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | ;; |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 177 | stop ) |
| 178 | CMD=$param |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | ;; |
| 180 | webport=*) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 181 | ADDR_PORT="${param#*=}" |
| 182 | # Split the addr:port string |
| 183 | ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'` |
| 184 | PORT=`echo $ADDR_PORT | cut -f 2 -d ':'` |
| 185 | # If only a port has been speified then set address to localhost. |
| 186 | if [ $ADDR = $PORT ] ; then |
| 187 | ADDR_PORT="localhost:$PORT" |
| 188 | fi |
| 189 | ;; |
| 190 | --help) |
| 191 | echo "$HELP" |
| 192 | return 0 |
| 193 | ;; |
| 194 | *) |
| 195 | echo "$HELP" |
| 196 | return 1 |
| 197 | ;; |
| 198 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 199 | esac |
| 200 | done |
| 201 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 202 | if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then |
| 203 | echo "Error: This script needs to be sourced. Please run as . $TOASTER" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 204 | return 1 |
| 205 | fi |
| 206 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 207 | verify_prereq || return 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 208 | |
| 209 | # We make sure we're running in the current shell and in a good environment |
| 210 | if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then |
| 211 | echo "Error: Build environment is not setup or bitbake is not in path." 1>&2 |
| 212 | return 2 |
| 213 | fi |
| 214 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 215 | # this defines the dir toaster will use for |
| 216 | # 1) clones of layers (in _toaster_clones ) |
| 217 | # 2) the build dir (in build) |
| 218 | # 3) the sqlite db if that is being used. |
| 219 | # 4) pid's we need to clean up on exit/shutdown |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 220 | export TOASTER_DIR=`dirname $BUILDDIR` |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 221 | export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE TOASTER_DIR" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 222 | |
| 223 | # Determine the action. If specified by arguments, fine, if not, toggle it |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 224 | if [ "$CMD" = "start" ] ; then |
| 225 | if [ -n "$BBSERVER" ]; then |
| 226 | echo " Toaster is already running. Exiting..." |
| 227 | return 1 |
| 228 | fi |
| 229 | elif [ "$CMD" = "" ]; then |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 230 | echo "No command specified" |
| 231 | echo "$HELP" |
| 232 | return 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | fi |
| 234 | |
| 235 | echo "The system will $CMD." |
| 236 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | # Execute the commands |
| 238 | |
| 239 | case $CMD in |
| 240 | start ) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 241 | # check if addr:port is not in use |
| 242 | if [ "$CMD" == 'start' ]; then |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 243 | if [ $WEBSERVER -gt 0 ]; then |
| 244 | $MANAGE checksocket "$ADDR_PORT" || return 1 |
| 245 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 246 | fi |
| 247 | |
| 248 | # Create configuration file |
| 249 | conf=${BUILDDIR}/conf/local.conf |
| 250 | line='INHERIT+="toaster buildhistory"' |
| 251 | grep -q "$line" $conf || echo $line >> $conf |
| 252 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 253 | if [ $WEBSERVER -eq 0 ] ; then |
| 254 | # Do not update the database for "noweb" unless |
| 255 | # it does not yet exist |
| 256 | if [ ! -f "$TOASTER_DIR/toaster.sqlite" ] ; then |
| 257 | if ! databaseCheck; then |
| 258 | echo "Failed ${CMD}." |
| 259 | return 4 |
| 260 | fi |
| 261 | fi |
| 262 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 263 | if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then |
| 264 | echo "Failed ${CMD}." |
| 265 | return 4 |
| 266 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 267 | export BITBAKE_UI='toasterui' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 268 | $MANAGE runbuilds \ |
| 269 | </dev/null >>${BUILDDIR}/toaster_runbuilds.log 2>&1 \ |
| 270 | & echo $! >${BUILDDIR}/.runbuilds.pid |
| 271 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 272 | # set fail safe stop system on terminal exit |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 273 | trap stop_system SIGHUP |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 274 | echo "Successful ${CMD}." |
| 275 | return 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | ;; |
| 277 | stop ) |
| 278 | stop_system |
| 279 | echo "Successful ${CMD}." |
| 280 | ;; |
| 281 | esac |