toaster 9.2 KB

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