toaster 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #!/bin/echo ERROR: This script needs to be sourced. Please run as .
  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. webserverKillAll()
  25. {
  26. local pidfile
  27. for pidfile in ${BUILDDIR}/.toastermain.pid ${BUILDDIR}/.runbuilds.pid; do
  28. if [ -f ${pidfile} ]; then
  29. pid=`cat ${pidfile}`
  30. while kill -0 $pid 2>/dev/null; do
  31. kill -SIGTERM -$pid 2>/dev/null
  32. sleep 1
  33. # Kill processes if they are still running - may happen in interactive shells
  34. ps fux | grep "python.*manage.py runserver" | awk '{print $2}' | xargs kill
  35. done
  36. rm ${pidfile}
  37. fi
  38. done
  39. }
  40. webserverStartAll()
  41. {
  42. # do not start if toastermain points to a valid process
  43. if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
  44. retval=1
  45. rm "${BUILDDIR}/.toastermain.pid"
  46. fi
  47. retval=0
  48. # you can always add a superuser later via
  49. # python bitbake/lib/toaster/manage.py python manage.py createsuperuser --username=<ME>
  50. python $BBBASEDIR/lib/toaster/manage.py syncdb --noinput || retval=1
  51. python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=2
  52. if [ $retval -eq 1 ]; then
  53. echo "Failed db sync, aborting system start" 1>&2
  54. return $retval
  55. fi
  56. python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=1
  57. if [ $retval -eq 1 ]; then
  58. printf "\nError on orm migration, rolling back...\n"
  59. python $BBBASEDIR/lib/toaster/manage.py migrate orm 0001_initial --fake
  60. return $retval
  61. fi
  62. python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol || retval=1
  63. if [ $retval -eq 1 ]; then
  64. printf "\nError on bldcontrol migration, rolling back...\n"
  65. python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol 0001_initial --fake
  66. return $retval
  67. fi
  68. python $BBBASEDIR/lib/toaster/manage.py checksettings --traceback || retval=1
  69. if [ $retval -eq 1 ]; then
  70. printf "\nError while checking settings; aborting\n"
  71. return $retval
  72. fi
  73. echo "Starting webserver..."
  74. python $BBBASEDIR/lib/toaster/manage.py runserver "0.0.0.0:$WEB_PORT" </dev/null >>${BUILDDIR}/toaster_web.log 2>&1 & 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 "Webserver address: http://0.0.0.0:$WEB_PORT/"
  81. fi
  82. return $retval
  83. }
  84. # Helper functions to add a special configuration file
  85. addtoConfiguration()
  86. {
  87. file=$1
  88. shift
  89. echo "#Created by toaster start script" > ${BUILDDIR}/conf/$file
  90. for var in "$@"; do echo $var >> ${BUILDDIR}/conf/$file; done
  91. }
  92. INSTOPSYSTEM=0
  93. # define the stop command
  94. stop_system()
  95. {
  96. # prevent reentry
  97. if [ $INSTOPSYSTEM -eq 1 ]; then return; fi
  98. INSTOPSYSTEM=1
  99. if [ -f ${BUILDDIR}/.toasterui.pid ]; then
  100. kill `cat ${BUILDDIR}/.toasterui.pid` 2>/dev/null
  101. rm ${BUILDDIR}/.toasterui.pid
  102. fi
  103. stop_bitbake
  104. webserverKillAll
  105. # unset exported variables
  106. unset DATABASE_URL
  107. unset TOASTER_CONF
  108. unset TOASTER_DIR
  109. trap - SIGHUP
  110. #trap - SIGCHLD
  111. INSTOPSYSTEM=0
  112. }
  113. start_bitbake() {
  114. unset BBSERVER
  115. bitbake --postread conf/toaster.conf --server-only -t xmlrpc -B 0.0.0.0:0
  116. if [ $? -ne 0 ]; then
  117. echo "Bitbake server start failed"
  118. return 1
  119. fi
  120. export BBSERVER=0.0.0.0:-1
  121. export DATABASE_URL=`$BBBASEDIR/lib/toaster/manage.py get-dburl`
  122. if [ $NOTOASTERUI -eq 0 ]; then # we start the TOASTERUI only if not inhibited
  123. bitbake --observe-only -u toasterui --remote-server=$BBSERVER -t xmlrpc >>${BUILDDIR}/toaster_ui.log 2>&1 \
  124. & echo $! >${BUILDDIR}/.toasterui.pid
  125. fi
  126. return 0
  127. }
  128. stop_bitbake() {
  129. BBSERVER=0.0.0.0:-1 bitbake -m
  130. unset BBSERVER
  131. # force stop any misbehaving bitbake server
  132. lsof bitbake.lock | awk '{print $2}' | grep "[0-9]\+" | xargs -n1 -r kill
  133. }
  134. check_pidbyfile() {
  135. [ -e $1 ] && kill -0 `cat $1` 2>/dev/null
  136. }
  137. notify_chldexit() {
  138. if [ $NOTOASTERUI -eq 0 ]; then
  139. check_pidbyfile ${BUILDDIR}/.toasterui.pid && return
  140. stop_system
  141. fi
  142. }
  143. verify_prereq() {
  144. # Verify prerequisites
  145. if ! echo "import django; print (1,) == django.VERSION[0:1] and django.VERSION[1:2][0] in (8,)" | python 2>/dev/null | grep True >/dev/null; then
  146. printf "This program needs Django 1.8. Please install with\n\npip install django<=1.8.7\n"
  147. return 2
  148. fi
  149. return 0
  150. }
  151. # read command line parameters
  152. if [ -n "$BASH_SOURCE" ] ; then
  153. TOASTER=${BASH_SOURCE}
  154. elif [ -n "$ZSH_NAME" ] ; then
  155. TOASTER=${(%):-%x}
  156. else
  157. TOASTER=$0
  158. fi
  159. BBBASEDIR=`dirname $TOASTER`/..
  160. OEROOT=`dirname $TOASTER`/../..
  161. RUNNING=0
  162. NOTOASTERUI=0
  163. WEBSERVER=1
  164. TOASTER_BRBE=""
  165. if [ "$WEB_PORT" = "" ]; then
  166. WEB_PORT="8000"
  167. fi
  168. # this is the configuraton file we are using for toaster
  169. # we are using the same logic that oe-setup-builddir uses
  170. # (based on TEMPLATECONF and .templateconf) to determine
  171. # which toasterconf.json to use.
  172. # note: There are a number of relative path assumptions
  173. # in the local layers that currently make using an arbitrary
  174. # toasterconf.json difficult.
  175. . $OEROOT/.templateconf
  176. if [ -n "$TEMPLATECONF" ]; then
  177. if [ ! -d "$TEMPLATECONF" ]; then
  178. # Allow TEMPLATECONF=meta-xyz/conf as a shortcut
  179. if [ -d "$OEROOT/$TEMPLATECONF" ]; then
  180. TEMPLATECONF="$OEROOT/$TEMPLATECONF"
  181. fi
  182. if [ ! -d "$TEMPLATECONF" ]; then
  183. echo >&2 "Error: '$TEMPLATECONF' must be a directory containing toasterconf.json"
  184. [ "$TOASTER_MANAGED" = '1' ] && exit 1 || return 1
  185. fi
  186. fi
  187. fi
  188. if [ "$TOASTER_CONF" = "" ]; then
  189. TOASTER_CONF="$TEMPLATECONF/toasterconf.json"
  190. export TOASTER_CONF=$(python -c "import os; print os.path.realpath('$TOASTER_CONF')")
  191. fi
  192. if [ ! -f $TOASTER_CONF ]; then
  193. echo "$TOASTER_CONF configuration file not found. Set TOASTER_CONF to specify file or fix .templateconf"
  194. [ "$TOASTER_MANAGED" = '1' ] && exit 1 || return 1
  195. fi
  196. # this defines the dir toaster will use for
  197. # 1) clones of layers (in _toaster_clones )
  198. # 2) the build dir (in build)
  199. # 3) the sqlite db if that is being used.
  200. # 4) pid's we need to clean up on exit/shutdown
  201. # note: for future. in order to make this an arbitrary directory, we need to
  202. # make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does.
  203. export TOASTER_DIR=`pwd`
  204. NOBROWSER=0
  205. for param in $*; do
  206. case $param in
  207. noui )
  208. NOTOASTERUI=1
  209. ;;
  210. noweb )
  211. WEBSERVER=0
  212. ;;
  213. nobrowser )
  214. NOBROWSER=1
  215. ;;
  216. brbe=* )
  217. TOASTER_BRBE=$'\n'"TOASTER_BRBE=\""${param#*=}"\""
  218. ;;
  219. webport=*)
  220. WEB_PORT="${param#*=}"
  221. esac
  222. done
  223. if [ `basename \"$0\"` = `basename \"${TOASTER}\"` ]; then
  224. echo "Error: This script needs to be sourced. Please run as . $TOASTER"
  225. exit 1
  226. fi
  227. if [ "$1" = 'restart-bitbake' ] ; then
  228. stop_bitbake
  229. sleep 1
  230. start_bitbake
  231. rc=$?
  232. sleep 1
  233. return $rc
  234. fi
  235. if ! verify_prereq; then
  236. echo "Error: Could not verify that the needed dependencies are installed. Please use virtualenv and pip to install dependencies listed in toaster-requirements.txt" 1>&2
  237. return 1
  238. fi
  239. # We make sure we're running in the current shell and in a good environment
  240. if [ -z "$BUILDDIR" ] || ! which bitbake >/dev/null 2>&1 ; then
  241. echo "Error: Build environment is not setup or bitbake is not in path." 1>&2
  242. return 2
  243. fi
  244. # this is the configuraton file we are using for toaster
  245. # note default is assuming yocto. Override this if you are
  246. # running in a pure OE environment and use the toasterconf.json
  247. # in meta/conf/toasterconf.json
  248. # note: for future there are a number of relative path assumptions
  249. # in the local layers that currently prevent using an arbitrary
  250. # toasterconf.json
  251. if [ "$TOASTER_CONF" = "" ]; then
  252. TOASTER_CONF="$(dirname $TOASTER)/../../meta-yocto/conf/toasterconf.json"
  253. export TOASTER_CONF=$(python -c "import os; print os.path.realpath('$TOASTER_CONF')")
  254. fi
  255. if [ ! -f $TOASTER_CONF ]; then
  256. echo "$TOASTER_CONF configuration file not found. set TOASTER_CONF to specify a path"
  257. return 1
  258. fi
  259. # this defines the dir toaster will use for
  260. # 1) clones of layers (in _toaster_clones )
  261. # 2) the build dir (in build)
  262. # 3) the sqlite db if that is being used.
  263. # 4) pid's we need to clean up on exit/shutdown
  264. # note: for future. in order to make this an arbitrary directory, we need to
  265. # make sure that the toaster.sqlite file doesn't default to `pwd` like it currently does.
  266. export TOASTER_DIR=`dirname $BUILDDIR`
  267. # Determine the action. If specified by arguments, fine, if not, toggle it
  268. if [ "$1" = 'start' ] || [ "$1" = 'stop' ]; then
  269. CMD="$1"
  270. else
  271. if [ -z "$BBSERVER" ]; then
  272. CMD="start"
  273. else
  274. CMD="stop"
  275. fi
  276. fi
  277. echo "The system will $CMD."
  278. # Make sure it's safe to run by checking bitbake lock
  279. lock=1
  280. if [ -e $BUILDDIR/bitbake.lock ]; then
  281. python -c "import fcntl; fcntl.flock(open(\"$BUILDDIR/bitbake.lock\"), fcntl.LOCK_EX|fcntl.LOCK_NB)" 2>/dev/null || lock=0
  282. fi
  283. if [ ${CMD} = 'start' ] && [ $lock -eq 0 ]; then
  284. echo "Error: bitbake lock state error. File locks show that the system is on." 1>&2
  285. echo "Please wait for the current build to finish, stop and then start the system again." 1>&2
  286. return 3
  287. fi
  288. if [ ${CMD} = 'start' ] && [ -e $BUILDDIR/.toastermain.pid ] && kill -0 `cat $BUILDDIR/.toastermain.pid`; then
  289. echo "Warning: bitbake appears to be dead, but the Toaster web server is running. Something fishy is going on." 1>&2
  290. echo "Cleaning up the web server to start from a clean slate."
  291. webserverKillAll
  292. fi
  293. # Execute the commands
  294. case $CMD in
  295. start )
  296. addtoConfiguration toaster.conf "INHERIT+=\"toaster buildhistory\"" $TOASTER_BRBE
  297. if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
  298. echo "Failed ${CMD}."
  299. return 4
  300. fi
  301. start_bitbake
  302. if [ $? -eq 0 ]; then
  303. python $BBBASEDIR/lib/toaster/manage.py runbuilds & echo $! >${BUILDDIR}/.runbuilds.pid
  304. # set fail safe stop system on terminal exit
  305. trap stop_system SIGHUP
  306. echo "Successful ${CMD}."
  307. return 0
  308. else
  309. # failed start, do stop
  310. stop_system
  311. echo "Failed ${CMD}."
  312. return 1
  313. fi
  314. # stop system on terminal exit
  315. set -o monitor
  316. trap stop_system SIGHUP
  317. #trap notify_chldexit SIGCHLD
  318. ;;
  319. stop )
  320. stop_system
  321. echo "Successful ${CMD}."
  322. ;;
  323. esac