toaster 9.1 KB

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