toaster 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 1: 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. Usage 2: source toaster manage [createsuperuser|lsupdates|migrate|makemigrations|checksettings|collectstatic|...]
  15. "
  16. custom_extention()
  17. {
  18. custom_extension=$BBBASEDIR/lib/toaster/orm/fixtures/custom_toaster_append.sh
  19. if [ -f $custom_extension ] ; then
  20. $custom_extension $*
  21. fi
  22. }
  23. databaseCheck()
  24. {
  25. retval=0
  26. # you can always add a superuser later via
  27. # ../bitbake/lib/toaster/manage.py createsuperuser --username=<ME>
  28. $MANAGE migrate --noinput || retval=1
  29. if [ $retval -eq 1 ]; then
  30. echo "Failed migrations, aborting system start" 1>&2
  31. return $retval
  32. fi
  33. # Make sure that checksettings can pick up any value for TEMPLATECONF
  34. export TEMPLATECONF
  35. $MANAGE checksettings --traceback || retval=1
  36. if [ $retval -eq 1 ]; then
  37. printf "\nError while checking settings; aborting\n"
  38. return $retval
  39. fi
  40. return $retval
  41. }
  42. webserverKillAll()
  43. {
  44. local pidfile
  45. if [ -f ${BUILDDIR}/.toastermain.pid ] ; then
  46. custom_extention web_stop_postpend
  47. else
  48. custom_extention noweb_stop_postpend
  49. fi
  50. for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
  51. if [ -f ${pidfile} ]; then
  52. pid=`cat ${pidfile}`
  53. while kill -0 $pid 2>/dev/null; do
  54. kill -SIGTERM $pid 2>/dev/null
  55. sleep 1
  56. done
  57. rm ${pidfile}
  58. fi
  59. done
  60. }
  61. webserverStartAll()
  62. {
  63. # do not start if toastermain points to a valid process
  64. if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
  65. retval=1
  66. rm "${BUILDDIR}/.toastermain.pid"
  67. fi
  68. retval=0
  69. # check the database
  70. databaseCheck || return 1
  71. echo "Starting webserver..."
  72. $MANAGE runserver --noreload "$ADDR_PORT" \
  73. </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 \
  74. & echo $! >${BUILDDIR}/.toastermain.pid
  75. sleep 1
  76. if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
  77. retval=1
  78. rm "${BUILDDIR}/.toastermain.pid"
  79. else
  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"
  82. custom_extention web_start_postpend $ADDR_PORT
  83. fi
  84. return $retval
  85. }
  86. INSTOPSYSTEM=0
  87. # define the stop command
  88. stop_system()
  89. {
  90. # prevent reentry
  91. if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
  92. INSTOPSYSTEM=1
  93. webserverKillAll
  94. # unset exported variables
  95. unset TOASTER_DIR
  96. unset BITBAKE_UI
  97. unset BBBASEDIR
  98. trap - SIGHUP
  99. #trap - SIGCHLD
  100. INSTOPSYSTEM=0
  101. }
  102. verify_prereq() {
  103. # Verify Django version
  104. reqfile=$(python3 -c "import os; print(os.path.realpath('$BBBASEDIR/toaster-requirements.txt'))")
  105. exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
  106. # expand version parts to 2 digits to support 1.10.x > 1.8
  107. # (note:helper functions hard to insert in-line)
  108. exp=$exp'import sys,django;'
  109. exp=$exp'version=["%02d" % int(n) for n in django.get_version().split(".")];'
  110. exp=$exp'vmin=["%02d" % int(n) for n in "\2".split(".")];'
  111. exp=$exp'vmax=["%02d" % int(n) for n in "\4".split(".")];'
  112. exp=$exp'sys.exit(not (version \1 vmin and version \3 vmax))'
  113. exp=$exp'/p'
  114. if ! sed -n "$exp" $reqfile | python3 - ; then
  115. req=`grep ^Django $reqfile`
  116. echo "This program needs $req"
  117. echo "Please install with pip3 install -r $reqfile"
  118. return 2
  119. fi
  120. return 0
  121. }
  122. # read command line parameters
  123. if [ -n "$BASH_SOURCE" ] ; then
  124. TOASTER=${BASH_SOURCE}
  125. elif [ -n "$ZSH_NAME" ] ; then
  126. TOASTER=${(%):-%x}
  127. else
  128. TOASTER=$0
  129. fi
  130. export BBBASEDIR=`dirname $TOASTER`/..
  131. MANAGE="python3 $BBBASEDIR/lib/toaster/manage.py"
  132. if [ -z "$OE_ROOT" ]; then
  133. OE_ROOT=`dirname $TOASTER`/../..
  134. fi
  135. # this is the configuraton file we are using for toaster
  136. # we are using the same logic that oe-setup-builddir uses
  137. # (based on TEMPLATECONF and .templateconf) to determine
  138. # which toasterconf.json to use.
  139. # note: There are a number of relative path assumptions
  140. # in the local layers that currently make using an arbitrary
  141. # toasterconf.json difficult.
  142. . $OE_ROOT/.templateconf
  143. if [ -n "$TEMPLATECONF" ]; then
  144. if [ ! -d "$TEMPLATECONF" ]; then
  145. # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
  146. if [ -d "$OE_ROOT/$TEMPLATECONF" ]; then
  147. TEMPLATECONF="$OE_ROOT/$TEMPLATECONF"
  148. fi
  149. fi
  150. fi
  151. unset OE_ROOT
  152. WEBSERVER=1
  153. export TOASTER_BUILDSERVER=1
  154. ADDR_PORT="localhost:8000"
  155. TOASTERDIR=`dirname $BUILDDIR`
  156. unset CMD
  157. for param in $*; do
  158. case $param in
  159. noweb )
  160. WEBSERVER=0
  161. ;;
  162. nobuild )
  163. TOASTER_BUILDSERVER=0
  164. ;;
  165. start )
  166. CMD=$param
  167. ;;
  168. stop )
  169. CMD=$param
  170. ;;
  171. webport=*)
  172. ADDR_PORT="${param#*=}"
  173. # Split the addr:port string
  174. ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
  175. PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
  176. # If only a port has been speified then set address to localhost.
  177. if [ $ADDR = $PORT ] ; then
  178. ADDR_PORT="localhost:$PORT"
  179. fi
  180. ;;
  181. toasterdir=*)
  182. TOASTERDIR="${param#*=}"
  183. ;;
  184. manage )
  185. CMD=$param
  186. manage_cmd=""
  187. ;;
  188. --help)
  189. echo "$HELP"
  190. return 0
  191. ;;
  192. *)
  193. if [ "manage" == "$CMD" ] ; then
  194. manage_cmd="$manage_cmd $param"
  195. else
  196. echo "$HELP"
  197. exit 1
  198. fi
  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. manage )
  279. cd $BBBASEDIR/lib/toaster
  280. $MANAGE $manage_cmd
  281. ;;
  282. esac
  283. custom_extention toaster_postpend $CMD $ADDR_PORT