aboutsummaryrefslogtreecommitdiffstats
path: root/bitbake/bin/toaster
blob: 792fb7210c18051fc65f8909549a14f2774bdae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
# (c) 2013 Intel Corp.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


# This script can be run in two modes.

# When used with "source", from a build directory,
# it enables toaster event logging and starts the bitbake resident server.
# use as:  source toaster [start|stop] [noweb] [noui]

# When it is called as a stand-alone script, it starts just the
# web server, and the building shall be done through the web interface.
# As script, it will not return to the command prompt. Stop with Ctrl-C.

# Helper function to kill a background toaster development server

function webserverKillAll()
{
	local pidfile
	for pidfile in ${BUILDDIR}/.toastermain.pid; do
		if [ -f ${pidfile} ]; then
		while kill -0 $(< ${pidfile}) 2>/dev/null; do
			kill -SIGTERM -$(< ${pidfile}) 2>/dev/null
			sleep 1;
			# Kill processes if they are still running - may happen in interactive shells
			pkill -U $UID -f "python.*manage.py runserver"
		done;
		rm  ${pidfile}
		fi
	done
}

function webserverStartAll()
{
	# do not start if toastermain points to a valid process
	if ! cat "${BUILDDIR}/.toastermain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
            retval=1
            rm "${BUILDDIR}/.toastermain.pid"
        fi

        retval=0
        python $BBBASEDIR/lib/toaster/manage.py syncdb || retval=1
        python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=2
        if [ $retval -eq 1 ]; then
                echo "Failed db sync, stopping system start" 1>&2
        elif [ $retval -eq 2 ]; then
            echo -e "\nError on migration, trying to recover... \n"
            python $BBBASEDIR/lib/toaster/manage.py migrate orm 0001_initial --fake
            retval=0
            python $BBBASEDIR/lib/toaster/manage.py migrate orm || retval=1
        fi
	if [ "x$TOASTER_MANAGED" == "x1" ]; then
	        python $BBBASEDIR/lib/toaster/manage.py migrate bldcontrol || retval=1
		python $BBBASEDIR/lib/toaster/manage.py checksettings  --traceback || retval=1
	fi
        if [ $retval -eq 0 ]; then
	    echo "Starting webserver..."
            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
            sleep 1
            if ! cat "${BUILDDIR}/.toastermain.pid" | xargs -I{} kill -0 {} ; then
                retval=1
                rm "${BUILDDIR}/.toastermain.pid"
	    else
		echo "Webserver address:  http://0.0.0.0:$WEB_PORT/"
            fi
        fi
        return $retval
}

# Helper functions to add a special configuration file

function addtoConfiguration()
{
        file=$1
        shift
        echo "#Created by toaster start script" > ${BUILDDIR}/conf/$file
        for var in "$@"; do echo $var >> ${BUILDDIR}/conf/$file; done
}

INSTOPSYSTEM=0

# define the stop command
function stop_system()
{
    # prevent reentry
    if [ $INSTOPSYSTEM == 1 ]; then return; fi
    INSTOPSYSTEM=1
    if [ -f ${BUILDDIR}/.toasterui.pid ]; then
        kill $(< ${BUILDDIR}/.toasterui.pid ) 2>/dev/null
        rm ${BUILDDIR}/.toasterui.pid
    fi
    BBSERVER=0.0.0.0:-1 bitbake -m
    unset BBSERVER
    webserverKillAll
    # force stop any misbehaving bitbake server
    lsof bitbake.lock | awk '{print $2}' | grep "[0-9]\+" | xargs -n1 -r kill
    trap - SIGHUP
    #trap - SIGCHLD
    INSTOPSYSTEM=0
}

function check_pidbyfile() {
    [ -e $1 ] && kill -0 $(< $1) 2>/dev/null
}


function notify_chldexit() {
    if [ $NOTOASTERUI == 0 ]; then
        check_pidbyfile ${BUILDDIR}/.toasterui.pid && return
        stop_system
    fi
}


function verify_prereq() {
	# Verify prerequisites

    if ! echo "import django; print (1,) == django.VERSION[0:1] and django.VERSION[1:2][0] in (6,)" | python 2>/dev/null | grep True >/dev/null; then
        echo -e "This program needs Django 1.6. Please install with\n\npip install django==1.6\n"
        return 2
    fi

    if ! echo "import south; print reduce(lambda x, y: 2 if x==2 else 0 if x == 0 else y, map(lambda x: 1+cmp(x[1]-x[0],0), zip([0,8,4], map(int,south.__version__.split(\".\"))))) > 0" | python 2>/dev/null | grep True >/dev/null; then
        echo -e "This program needs South 0.8.4. Please install with\n\npip install south==0.8.4\n"
        return 2
    fi
    return 0
}


# read command line parameters

BBBASEDIR=`dirname ${BASH_SOURCE}`/..
RUNNING=0

NOTOASTERUI=0
WEBSERVER=1
TOASTER_BRBE=""
WEB_PORT="8000"

for param in $*; do
    case $param in
    noui )
            NOTOASTERUI=1
    ;;
    noweb )
            WEBSERVER=0
    ;;
    brbe=* )
            TOASTER_BRBE=$'\n'"TOASTER_BRBE=\""${param#*=}"\""
    ;;
    webport=*)
            WEB_PORT="${param#*=}"
    esac
done


if [ -z "$ZSH_NAME" ] && [ `basename \"$0\"` = `basename \"$BASH_SOURCE\"` ]; then
    # We are called as standalone. We refuse to run in a build environment - we need the interactive mode for that.
    # Start just the web server, point the web browser to the interface, and start any Django services.

    if ! verify_prereq; then
        echo -e "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;
        exit 1;
    fi

    if [ -n "$BUILDDIR" ]; then
	echo -e "Error: It looks like you sourced oe-init-build-env. Toaster cannot start in build mode from an oe-core build environment.\n You should be starting Toaster from a new terminal window." 1>&2;
        exit 1;
    fi

    if [ "x`which daemon`" == "x" ]; then
        echo -e "Failed dependency; toaster needs the 'daemon' program in order to be able to start builds'. Please install the 'daemon' program from your distribution repositories or http://www.libslack.org/daemon/" 1>&2;
	exit 1;
    fi

    # Define a fake builddir where only the pid files are actually created. No real builds will take place here.
    BUILDDIR=/tmp/toaster_$$
    if [ -d "$BUILDDIR" ]; then
        echo -e "Previous toaster run directory $BUILDDIR found, cowardly refusing to start. Please remove the directory when that toaster instance is over" 2>&1
        exit 1;
    fi

    mkdir -p "$BUILDDIR"

    RUNNING=1
    function trap_ctrlc() {
        echo "** Stopping system"
        webserverKillAll
        RUNNING=0
    }

    function do_cleanup() {
        find "$BUILDDIR" -type f | xargs rm
        rmdir "$BUILDDIR"
    }
    function cleanup() {
        if grep -ir error "$BUILDDIR" >/dev/null; then
            if grep -irn "That port is already in use" "$BUILDDIR"; then
                echo "You can use the \"webport=PORTNUMBER\" parameter to start Toaster on a different port (port $WEB_PORT is already in use)"
                do_cleanup
            else
                echo -e "\nErrors found in the Toaster log files present in '$BUILDDIR'. Directory will not be cleaned.\n Please review the errors and notify toaster@yoctoproject.org or submit a bug https://bugzilla.yoctoproject.org/enter_bug.cgi?product=Toaster"
            fi
        else
            echo "No errors found, removing the run directory '$BUILDDIR'"
            do_cleanup
        fi;
    }
    TOASTER_MANAGED=1
    export TOASTER_MANAGED=1
    if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
        echo "Failed to start the web server, stopping" 1>&2;
        cleanup
        exit 1;
    fi
    if [ $WEBSERVER -gt 0 ]; then
        echo "Starting browser..."
        xdg-open http://127.0.0.1:$WEB_PORT/ >/dev/null 2>&1 &
    fi
    trap trap_ctrlc SIGINT
    echo "Toaster is now running. You can stop it with Ctrl-C"
    while [ $RUNNING -gt 0 ]; do
        python $BBBASEDIR/lib/toaster/manage.py runbuilds 2>&1 | tee -a "$BUILDDIR/toaster.log"
        sleep 1
    done
    cleanup
    echo "**** Exit"
    exit 0
fi


if ! verify_prereq; then
	echo -e "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;
	return 1;
fi


# We make sure we're running in the current shell and in a good environment
if [ -z "$BUILDDIR" ] || [ -z `which bitbake` ]; then
    echo "Error: Build environment is not setup or bitbake is not in path." 1>&2;
    return 2
fi


# Determine the action. If specified by arguments, fine, if not, toggle it
if [ "x$1" == "xstart" ] || [ "x$1" == "xstop" ]; then
    CMD="$1"
else
    if [ -z "$BBSERVER" ]; then
        CMD="start"
    else
        CMD="stop"
    fi;
fi

echo "The system will $CMD."

# Make sure it's safe to run by checking bitbake lock

lock=1
if [ -e $BUILDDIR/bitbake.lock ]; then
    (flock -n 200 ) 200<$BUILDDIR/bitbake.lock || lock=0
fi

if [ ${CMD} == "start" ] && [ $lock -eq 0 ]; then
    echo "Error: bitbake lock state error. File locks show that the system is on." 1>&2
    echo "Please wait for the current build to finish, stop and then start the system again." 1>&2
    return 3
fi

if [ ${CMD} == "start" ] && [ -e $BUILDDIR/.toastermain.pid ] && kill -0 `cat $BUILDDIR/.toastermain.pid`; then
    echo "Warning: bitbake appears to be dead, but the Toaster web server is running. Something fishy is going on." 1>&2
    echo "Cleaning up the web server to start from a clean slate."
    webserverKillAll
fi


# Execute the commands

case $CMD in
    start )
        start_success=1
        addtoConfiguration toaster.conf "INHERIT+=\"toaster buildhistory\"" $TOASTER_BRBE
        if [ $WEBSERVER -gt 0 ] && ! webserverStartAll; then
            echo "Failed ${CMD}."
            return 4
        fi
        unset BBSERVER
        PREREAD=""
        if [ -e ${BUILDDIR}/conf/toaster-pre.conf ]; then
		rm ${BUILDDIR}/conf/toaster-pre.conf
        fi
        bitbake $PREREAD --postread conf/toaster.conf --server-only -t xmlrpc -B 0.0.0.0:0
        if [ $? -ne 0 ]; then
            start_success=0
            echo "Bitbake server start failed"
        else
            export BBSERVER=0.0.0.0:-1
            if [ $NOTOASTERUI == 0 ]; then        # we start the TOASTERUI only if not inhibited
                bitbake --observe-only -u toasterui >>${BUILDDIR}/toaster_ui.log 2>&1 & echo $! >${BUILDDIR}/.toasterui.pid
            fi
        fi
        if [ $start_success -eq 1 ]; then
            # set fail safe stop system on terminal exit
            trap stop_system SIGHUP
            echo "Successful ${CMD}."
            return 0
        else
            # failed start, do stop
            stop_system
            echo "Failed ${CMD}."
            return 1
        fi
        # stop system on terminal exit
        set -o monitor
        trap stop_system SIGHUP
        #trap notify_chldexit SIGCHLD
    ;;
    stop )
        stop_system
        echo "Successful ${CMD}."
    ;;
esac