toaster 8.6 KB

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