toaster 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/bash
  2. # (c) 2013 Intel Corp.
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. # This script can be run in two modes.
  17. # When used with "source", from a build directory,
  18. # it enables toaster event logging and starts the bitbake resident server.
  19. # use as: source toaster [start|stop] [noweb] [noui]
  20. # When it is called as a stand-alone script, it starts just the
  21. # web server, and the building shall be done through the web interface.
  22. # As script, it will not return to the command prompt. Stop with Ctrl-C.
  23. # Helper function to kill a background toaster development server
  24. function webserverKillAll()
  25. {
  26. local pidfile
  27. for pidfile in ${BUILDDIR}/.toastermain.pid; do
  28. if [ -f ${pidfile} ]; then
  29. while kill -0 $(< ${pidfile}) 2>/dev/null; do
  30. kill -SIGTERM -$(< ${pidfile}) 2>/dev/null
  31. sleep 1;
  32. # Kill processes if they are still running - may happen in interactive shells
  33. ps fux | grep "python.*manage.py" | awk '{print $2}' | xargs kill
  34. done;
  35. rm ${pidfile}
  36. fi
  37. done
  38. }
  39. function webserverStartAll()
  40. {
  41. # do not start if toastermain points to a valid process
  42. if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
  43. retval=1
  44. rm "${BUILDDIR}/.toastermain.pid"
  45. fi
  46. retval=0
  47. python $BBBASEDIR/lib/toaster/manage.py syncdb || retval=1
  48. python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=2
  49. if [ $retval -eq 1 ]; then
  50. echo "Failed db sync, stopping system start" 1>&2
  51. elif [ $retval -eq 2 ]; then
  52. echo -e "\nError on migration, trying to recover... \n"
  53. python $BBBASEDIR/lib/toaster/manage.py migrate orm 0001_initial --fake
  54. retval=0
  55. python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=1
  56. fi
  57. if [ "x$TOASTER_MANAGED" == "x1" ]; then
  58. python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol || retval=1
  59. python $BBBASEDIR/lib/toaster/manage.py checksettings || retval=1
  60. fi
  61. echo "Starting webserver"
  62. if [ $retval -eq 0 ]; then
  63. python $BBBASEDIR/lib/toaster/manage.py runserver 0.0.0.0:8000 </dev/null >${BUILDDIR}/toaster_web.log 2>&1 & echo $! >${BUILDDIR}/.toastermain.pid
  64. sleep 1
  65. if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
  66. retval=1
  67. rm "${BUILDDIR}/.toastermain.pid"
  68. fi
  69. fi
  70. return $retval
  71. }
  72. # Helper functions to add a special configuration file
  73. function addtoConfiguration()
  74. {
  75. echo "#Created by toaster start script" > ${BUILDDIR}/conf/$2
  76. echo $1 >> ${BUILDDIR}/conf/$2
  77. }
  78. INSTOPSYSTEM=0
  79. # define the stop command
  80. function stop_system()
  81. {
  82. # prevent reentry
  83. if [ $INSTOPSYSTEM == 1 ]; then return; fi
  84. INSTOPSYSTEM=1
  85. if [ -f ${BUILDDIR}/.toasterui.pid ]; then
  86. kill $(< ${BUILDDIR}/.toasterui.pid ) 2>/dev/null
  87. rm ${BUILDDIR}/.toasterui.pid
  88. fi
  89. BBSERVER=localhost:8200 bitbake -m
  90. unset BBSERVER
  91. webserverKillAll
  92. # force stop any misbehaving bitbake server
  93. lsof bitbake.lock | awk '{print $2}' | grep "[0-9]\+" | xargs -n1 -r kill
  94. trap - SIGHUP
  95. #trap - SIGCHLD
  96. INSTOPSYSTEM=0
  97. }
  98. function check_pidbyfile() {
  99. [ -e $1 ] && kill -0 $(< $1) 2>/dev/null
  100. }
  101. function notify_chldexit() {
  102. if [ $NOTOASTERUI == 0 ]; then
  103. check_pidbyfile ${BUILDDIR}/.toasterui.pid && return
  104. stop_system
  105. fi
  106. }
  107. BBBASEDIR=`dirname ${BASH_SOURCE}`/..
  108. RUNNING=0
  109. if [ -z "$ZSH_NAME" ] && [ `basename \"$0\"` = `basename \"$BASH_SOURCE\"` ]; then
  110. # We are called as standalone. We refuse to run in a build environment - we need the interactive mode for that.
  111. # Start just the web server, point the web browser to the interface, and start any Django services.
  112. if [ -n "$BUILDDIR" ]; then
  113. echo -e "Error: build/ directory detected. Toaster will not start in managed mode if a build environment is detected.\nUse a clean terminal to start Toaster." 1>&2;
  114. exit 1;
  115. fi
  116. # Define a fake builddir where only the pid files are actually created. No real builds will take place here.
  117. BUILDDIR=/tmp
  118. RUNNING=1
  119. function trap_ctrlc() {
  120. echo "** Stopping system"
  121. webserverKillAll
  122. RUNNING=0
  123. }
  124. TOASTER_MANAGED=1
  125. export TOASTER_MANAGED=1
  126. webserverStartAll || (echo "Fail to start the web server, stopping" 1>&2 && exit 1)
  127. xdg-open http://0.0.0.0:8000/ >/dev/null 2>&1 &
  128. trap trap_ctrlc SIGINT
  129. echo "Running. Stop with Ctrl-C"
  130. while [ $RUNNING -gt 0 ]; do
  131. python $BBBASEDIR/lib/toaster/manage.py runbuilds
  132. sleep 1
  133. done
  134. echo "**** Exit"
  135. exit 0
  136. fi
  137. # We make sure we're running in the current shell and in a good environment
  138. if [ -z "$BUILDDIR" ] || [ -z `which bitbake` ]; then
  139. echo "Error: Build environment is not setup or bitbake is not in path." 1>&2;
  140. return 2
  141. fi
  142. # Verify prerequisites
  143. if ! echo "import django; print (1,) == django.VERSION[0:1] and django.VERSION[1:2][0] in (5,6)" | python 2>/dev/null | grep True >/dev/null; then
  144. echo -e "This program needs Django 1.5 or 1.6. Please install with\n\npip install django==1.6"
  145. return 2
  146. fi
  147. if ! echo "import south; print [0,8,4] == map(int,south.__version__.split(\".\"))" | python 2>/dev/null | grep True >/dev/null; then
  148. echo -e "This program needs South 0.8.4. Please install with\n\npip install south==0.8.4"
  149. return 2
  150. fi
  151. # Determine the action. If specified by arguments, fine, if not, toggle it
  152. if [ "x$1" == "xstart" ] || [ "x$1" == "xstop" ]; then
  153. CMD="$1"
  154. else
  155. if [ -z "$BBSERVER" ]; then
  156. CMD="start"
  157. else
  158. CMD="stop"
  159. fi;
  160. fi
  161. NOTOASTERUI=0
  162. WEBSERVER=1
  163. for param in $*; do
  164. case $param in
  165. noui )
  166. NOTOASTERUI=1
  167. ;;
  168. noweb )
  169. WEBSERVER=0
  170. ;;
  171. esac
  172. done
  173. echo "The system will $CMD."
  174. # Make sure it's safe to run by checking bitbake lock
  175. lock=1
  176. if [ -e $BUILDDIR/bitbake.lock ]; then
  177. (flock -n 200 ) 200<$BUILDDIR/bitbake.lock || lock=0
  178. fi
  179. if [ ${CMD} == "start" ] && ( [ $lock -eq 0 ] || [ -e $BUILDDIR/.toastermain.pid ] ); then
  180. echo "Error: bitbake lock state error. File locks show that the system is on." 2>&1
  181. echo "If you see problems, stop and then start the system again." 2>&1
  182. return 3
  183. fi
  184. # Execute the commands
  185. case $CMD in
  186. start )
  187. start_success=1
  188. addtoConfiguration "INHERIT+=\"toaster buildhistory\"" toaster.conf
  189. if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
  190. echo "Failed ${CMD}."
  191. return 4
  192. fi
  193. unset BBSERVER
  194. bitbake --postread conf/toaster.conf --server-only -t xmlrpc -B localhost:8200
  195. if [ $? -ne 0 ]; then
  196. start_success=0
  197. echo "Bitbake server start failed"
  198. else
  199. export BBSERVER=localhost:8200
  200. if [ $NOTOASTERUI == 0 ]; then # we start the TOASTERUI only if not inhibited
  201. bitbake --observe-only -u toasterui >${BUILDDIR}/toaster_ui.log 2>&1 & echo $! >${BUILDDIR}/.toasterui.pid
  202. fi
  203. fi
  204. if [ $start_success -eq 1 ]; then
  205. # set fail safe stop system on terminal exit
  206. trap stop_system SIGHUP
  207. echo "Successful ${CMD}."
  208. else
  209. # failed start, do stop
  210. stop_system
  211. echo "Failed ${CMD}."
  212. fi
  213. # stop system on terminal exit
  214. set -o monitor
  215. trap stop_system SIGHUP
  216. #trap notify_chldexit SIGCHLD
  217. ;;
  218. stop )
  219. stop_system
  220. echo "Successful ${CMD}."
  221. ;;
  222. esac