toaster 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 enables toaster event logging and
  17. # starts bitbake resident server
  18. # use as: source toaster [start|stop]
  19. # Helper function to kill a background toaster development server
  20. function webserverKillAll()
  21. {
  22. local pidfile
  23. for pidfile in ${BUILDDIR}/.toastermain.pid; do
  24. if [ -f ${pidfile} ]; then
  25. while kill -0 $(< ${pidfile}) 2>/dev/null; do
  26. kill -SIGTERM -$(< ${pidfile}) 2>/dev/null
  27. sleep 1;
  28. done;
  29. rm ${pidfile}
  30. fi
  31. done
  32. }
  33. function webserverStartAll()
  34. {
  35. retval=0
  36. python $BBBASEDIR/lib/toaster/manage.py syncdb || retval=1
  37. if [ $retval -eq 1 ]; then
  38. echo "Failed db sync, stopping system start" 1>&2
  39. else
  40. python $BBBASEDIR/lib/toaster/manage.py runserver 0.0.0.0:8000 </dev/null >${BUILDDIR}/toaster_web.log 2>&1 & echo $! >${BUILDDIR}/.toastermain.pid
  41. fi
  42. return $retval
  43. }
  44. # We make sure we're running in the current shell and in a good environment
  45. if [ -z "$ZSH_NAME" ] && [ `basename \"$0\"` = `basename \"$BASH_SOURCE\"` ]; then
  46. echo "Error: This script needs to be sourced. Please run as 'source toaster [start|stop]'" 1>&2;
  47. exit 1
  48. fi
  49. if [ -z "$BUILDDIR" ] || [ -z `which bitbake` ]; then
  50. echo "Error: Build environment is not setup or bitbake is not in path." 1>&2;
  51. return 2
  52. fi
  53. BBBASEDIR=`dirname ${BASH_SOURCE}`/..
  54. # Verify prerequisites
  55. if ! echo "import django; print (1,4,5) == django.VERSION[0:3]" | python 2>/dev/null | grep True >/dev/null; then
  56. echo -e "This program needs Django 1.4.5. Please install with\n\nsudo pip install django==1.4.5"
  57. return 2
  58. fi
  59. # Determine the action. If specified by arguments, fine, if not, toggle it
  60. if [ "x$1" == "xstart" ] || [ "x$1" == "xstop" ]; then
  61. CMD="$1"
  62. else
  63. if [ -z "$BBSERVER" ]; then
  64. CMD="start"
  65. else
  66. CMD="stop"
  67. fi;
  68. fi
  69. NOTOASTERUI=0
  70. if [ "x$2" == "xnoui" ]; then
  71. NOTOASTERUI=1
  72. fi
  73. echo "The system will $CMD."
  74. # Make sure it's safe to run by checking bitbake lock
  75. lock=1
  76. if [ -e $BUILDDIR/bitbake.lock ]; then
  77. (flock -n 200 ) 200<$BUILDDIR/bitbake.lock || lock=0
  78. fi
  79. if [ ${CMD} == "start" ] && ( [ $lock -eq 0 ] || [ -e $BUILDDIR/.toastermain.pid ] ); then
  80. echo "Error: bitbake lock state error. System is already on." 2>&1
  81. return 3
  82. elif [ ${CMD} == "stop" ] && ( [ $lock -eq 1 ] || ! [ -e $BUILDDIR/.toastermain.pid ] ) ; then
  83. echo "Error: bitbake lock state error. Trying to stop a stopped system ?
  84. If you think the system is hanged up, you can try to manually stop system with the commands
  85. # BBSERVER=localhost:8200 bitbake -m
  86. and
  87. # webserverKillAll
  88. " 2>&1
  89. return 3
  90. fi
  91. # Execute the commands
  92. case $CMD in
  93. start )
  94. webserverStartAll || return 4
  95. unset BBSERVER
  96. bitbake --server-only -t xmlrpc -B localhost:8200
  97. export BBSERVER=localhost:8200
  98. if [ $NOTOASTERUI == 0 ]; then # we start the TOASTERUI only if not inhibited
  99. bitbake --observe-only -u toasterui >${BUILDDIR}/toaster_ui.log 2>&1 & echo $! >${BUILDDIR}/.toasterui.pid
  100. fi
  101. ;;
  102. stop )
  103. if [ -f ${BUILDDIR}/.toasterui.pid ]; then
  104. kill $(< ${BUILDDIR}/.toasterui.pid )
  105. rm ${BUILDDIR}/.toasterui.pid
  106. fi
  107. bitbake -m
  108. unset BBSERVER
  109. webserverKillAll
  110. # force stop any misbehaving bitbake server
  111. lsof bitbake.lock | awk '{print $2}' | grep "[0-9]\+" | xargs -n1 -r kill
  112. ;;
  113. esac
  114. echo "Successful ${CMD}."