aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/testing/testbuilder/testbuilder
blob: 348d81195ff6b6379104b328f6cc587b6a0673aa (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/sh

# TestBuilder for OpenEmbedded
# Copyright (c) 2010-2011 Yuri Bushmelev <jay4mail@gmail.com>
#
# 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, 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.
VERSION="0.6.1"
PACKAGE="TestBuilder"

TB_DIR=`dirname $0`
TB_NAME=`basename $0`
cd "${TB_DIR}"

CFG_FILE="${TB_DIR}/testbuilder.conf"
CFG_DIR="${TB_DIR}/testbuilder.d"
. "${CFG_FILE}"

tb_usage() {
	echo "${PACKAGE} v${VERSION}"
	# Run only specified build
	echo "Usage: ${TB_NAME} -Vh"
	echo "   or  ${TB_NAME} [-B ${CFG_DIR}/build.conf [-D distro] [-M machine] [-S]]"
	# Behave like DISTRO=distro MACHINE=machine bitbake images
	# with -S testbuilder will only setup env and run interactive sub-shell
	echo "   or  ${TB_NAME} -D distro -M machine [-T /full/path/to/tmpdir] [-S | images]"
}

tb_rotate_log() {
	local _log=$1
	[ -f "${_log}" ] && mv "${_log}" "${_log}.0"
}

# Sanity cleanup
ORIG_PATH=${PATH}
ORIG_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
ORIG_LANG=${LANG}
export ORIG_PATH ORIG_LD_LIBRARY_PATH ORIG_LANG

# clean LD_LIBRARY_PATH
unset LD_LIBRARY_PATH

# clean locale
LANG=C

# include bitbake into PATH
PATH=${BB_DIR}/bin:${PATH}

BBPATH="${BLD_DIR}:${OE_DIR}"

TB_LOG="${LOG_DIR}/tb.log"
TB_MWR_FILE="${LOG_DIR}/MW.release.txt"
TB_MWT_FILE="${LOG_DIR}/MW.testing.txt"

MW_USER=${MW_USER:-${TB_BUILDER}}

#export LD_LIBRARY_PATH LANG PATH
export LD_LIBRARY_PATH LANG

BB_ENV_EXTRAWHITE="http_proxy HTTP_PROXY MACHINE DISTRO TB_BUILDER OE_DIR TMP_DIR DL_DIR MAKE_NUMBER_THREADS BB_NUMBER_THREADS IMAGE_FSTYPES_local DEPLOY_DIR_IMAGE_local ANGSTROMLIBC RM_WORK"
export   BB_ENV_EXTRAWHITE BBPATH TB_BUILDER OE_DIR TMP_DIR DL_DIR MAKE_NUMBER_THREADS BB_NUMBER_THREADS RM_WORK

BB_CMD=bitbake

# Try to guess user's distro and machine
HOST_DISTRO=${HOST_DISTRO:-"$([ -x "`which lsb_release`" ] && lsb_release -ds) /$(readelf -h `which readelf` | awk -F': *' '/^  Machine:/ {print $2;}')"}
HOST_MACHINE=${HOST_MACHINE:-"$(cat /proc/cpuinfo | awk -F': ' '/^model name/ {model=$2;} /^cpu MHz/ {freq=$2;} /^cpu cores/ {cores=$2;} END {OFS=""; OFMT="%.2g"; print model, ", ", cores, " cores @", freq/1000, "GHz"}') /$(cat /proc/meminfo | awk '/^MemTotal:/{OFMT="%.1gGb"; print $2/1024/1024;}')"}

mkdir -p ${LOG_DIR}

#tb_rotate_log "${TB_MWR_FILE}"
#tb_rotate_log "${TB_MWT_FILE}"
tb_rotate_log "${TB_LOG}"

### Functions

# log message
tb_log() {
	local _dt=`date "+%F %X (%s)"`
	echo "[${_dt}] $@" >&2
	echo "[${_dt}] $@" >> $TB_LOG
}

tb_interactive_shell() {
	echo "Distro:  $DISTRO"
	echo "Machine: $MACHINE"
	echo "TMPDIR:  $TMP_DIR"
	echo "Starting shell $TB_SHELL. Use 'exit' or ^D to quit"
	[ -n "${TB_PS1}" ] && export PS1=${TB_PS1}
	eval $TB_SHELL
}

# Do vmstat accounting
tb_accounting_vmstat() {
	exec vmstat -n "${ACCOUNTING_INTERVAL}" >> "${LOG_DIR}/A.vmstat.log"
}

# Do accounting by commands (df/ps)
tb_accounting_cmd() {
	local _dir=$1; shift
	while true; do
		[ -n "${DO_ACCOUNTING_DF}" ] && ( df -Pk "${_dir}" | awk 'END {OFS="\t"; print systime(), $4, $6;}' ) >> "${LOG_DIR}/A.df.log"
		[ -n "${DO_ACCOUNTING_PS}" ] && ( ps ax | awk 'END {OFS="\t"; print systime(), NR-1;}' ) >> "${LOG_DIR}/A.ps.log"
		sleep "${ACCOUNTING_INTERVAL}"
	done
}

# Stop all remaining tasks
tb_kill_all() {
	for proc in `jobs | cut -c 2- | cut -d] -f1`; do
		kill "%${proc}"
	done
}


# update git tree
tb_git_update() {
	local _dir=$1
	local _cwd=`pwd`

	cd "${_dir}"
	tb_log "Updating git tree ${_dir}"
	git pull
	cd "${_cwd}"
}

# checkout branch from git tree
tb_git_branch() {
	local _dir=$1
	local _branch=$2
	local _cwd=`pwd`

	cd "${_dir}"
	tb_log "Checking out branch ${_branch} from tree ${_dir}"
	git checkout -f ${_branch}
	cd "${_cwd}"
}

tb_git_rev() {
	local _dir=$1
	local _cwd=`pwd`

	cd "${_dir}"
	#git log -1 --format=%h
	git branch -v | awk '/^* / {print $2, $3;}'
	cd "${_cwd}"
}

# detect build status by log
tb_get_build_status() {
	local _log=$1
	local _line

	if [ ! -f ${_log} ]; then
		echo "UNKNOWN"
		return
	fi

	_line=$(tail -n1 ${_log})
	tb_log " ++ ${_line}"
	if [ "${_line}" != "${_line#ERROR}" ]; then
		echo "FAIL"
	else
		echo "OK"
	fi
}

# add row to mediawiki markup table file
tb_add_mw_row() {
	local _images=$1
	local _btype=$2
	local _line=$3
	local _time=$4
	local _bs
	local _bl

	case "${_btype}" in
	'c')	_bs='clean';;
	'i')	_bs='inc';;
	'*')	_bs='';;
	esac

	[ "${_line}" != "${_line#ERROR}" ] && _bl=$_line

	echo "|-" >>"${TB_MWT_FILE}"
	echo "|${MACHINE} ||${DISTRO} ||${_images} ||${HOST_DISTRO} ||${BB_REV} ||[[User:${MW_USER}|${MW_USER}]] ||${OE_REV} ||${_bs} ||${_bl}" >>"${TB_MWT_FILE}"

	echo "|-" >>"${TB_MWR_FILE}"
	echo "|${MACHINE} ||${DISTRO} ||${_images} ||${HOST_DISTRO} ||${BB_REV} ||[[User:${MW_USER}|${MW_USER}]] ||${_bs} ||${_bl} ||${OE_REV} ||${HOST_MACHINE} ||${_time}" >>"${TB_MWR_FILE}"
}

tb_build_images() {
	local _btype=$1
	local _images=$2
	local _log=$3
	local _rc
	local _bstatus
	local _line
	local _stime
	local _btime

	tb_rotate_log "${_log}"
	touch "${_log}"

	_stime=$(date +%s)
	eval ${BB_CMD} ${BB_OPTS} ${_images} 2>&1 | tee "${_log}"
	_rc=$?
	_btime=$( TZ=UTC0 date -d "now - $_stime seconds" '+%T' )

	_line=$(tail -n1 "${_log}")
	tb_log " ++ ${_line}"
	[ "${_line}" != "${_line#ERROR}" ] && _bstatus="FAILED" || _bstatus="OK"

	tb_log " ++ Build finished in ${_btime} with code ${_rc}. Build status: ${_bstatus}."
	[ -n "${DO_CREATE_MW_TABLE}" ] && tb_add_mw_row "${_images}" "${_btype}" "${_line}" "${_btime}"
}


# Do build of image-set $bbimages for machine-set $MLIST
# tb_build_machines "$btype" "$MLIST" $bbimages
tb_build_machines() {
	local _btype=$1; shift
	local _MLIST=$1; shift
	local _bbimages=$@
	local _machine
	local _log
	
	# Start accounting
	trap "tb_kill_all" RETURN
	[ -n "${DO_ACCOUNTING_VMSTAT}" ] && tb_accounting_vmstat &
	[ -n "${DO_ACCOUNTING_DF}" -o -n "${DO_ACCOUNTING_PS}" ] && tb_accounting_cmd "${TMP_DIR}" &

	for _machine in $_MLIST; do
		[ "${_machine}" != "${_machine#!}" ] && continue
		[ -n "${OMACHINE}" -a "${_machine#!}" != "${OMACHINE}" ] && continue

		export MACHINE=$_machine

		if [ -n "${DO_RUN_SHELL}" ]; then
			tb_interactive_shell
		else
			tb_log " + ${_machine} build started"

			case "${SEPARATE_BUILD}" in
			[Yy][Ee][Ss])
				# Build images separately
				local _image
				for _image in $_bbimages; do
					tb_log " ++ ${_image} build started"
					_log="${LOG_DIR}/B.${build}.${DISTRO}.${_machine}.${_image}.${_btype}.log"
					tb_build_images "${_btype}" "${_image}" "${_log}"
				done
				;;
			*)
				# Build all images with one bitbake invocation
				_log="${LOG_DIR}/B.${build}.${DISTRO}.${_machine}.${_btype}.log"
				tb_build_images "${_btype}" "${_bbimages}" "${_log}"
				;;
			esac

			tb_log " + ${_machine} build finished."
		fi
	done
}


### Main code 

# Parse stages
unset DO_RUN_BUILDS DO_UPDATE_OE DO_UPDATE_BB DO_UPLOAD_LOGS
for stage in $STAGES; do
	[ "${stage}" != "${stage#!}" ] && continue
	case ${stage} in
	'run_builds')
		DO_RUN_BUILDS=y;;
	'update_oe')
		DO_UPDATE_OE=y;;
	'update_bb')
		DO_UPDATE_BB=y;;
	'upload_logs')
		DO_UPLOAD_LOGS=y;;
	*)
		tb_log "Unknown stage ${stage}.";;
	esac
done

# Parse accounting
unset DO_ACCOUNTING_DF DO_ACCOUNTING_PS DO_ACCOUNTING_VMSTAT
for count in $ACCOUNTING; do
[ "${count}" != "${count#!}" ] && continue
	case ${count} in
	'df')
		DO_ACCOUNTING_DF=y;;
	'ps')
		DO_ACCOUNTING_PS=y;;
	'vmstat')
		DO_ACCOUNTING_VMSTAT=y;;
	*)
		tb_log "Unknown accounting type ${count}.";;
	esac
done

# Parse cmdline
unset ODISTRO OBUILD OMACHINE OIMAGES OTMPDIR DO_RUN_SHELL
while getopts VhSB:D:M:T: opt; do
	case $opt in
	B) OBUILD="${OPTARG}";;
	D) ODISTRO="${OPTARG}";;
	M) OMACHINE="${OPTARG}";;
	T) OTMPDIR="${OPTARG}";;
	S) DO_RUN_SHELL=y;;
	*)
		tb_usage
		exit 2
		;;
	esac
done

shift $(($OPTIND - 1))
OIMAGES=$@

# Kill all tasks on exit
trap "tb_kill_all" EXIT

# Check for 'direct' mode
if [ -z "${OBUILD}" -a -n "${ODISTRO}" ]; then
	DISTRO="${ODISTRO}"
	MACHINE="${OMACHINE}"
	TMP_DIR=${OTMPDIR:-${CLN_DIR}}
	export DISTRO MACHINE TMP_DIR

	if [ -n "${DO_RUN_SHELL}" ]; then
		# Run shell if -s option was given
		tb_interactive_shell
	else
		tb_build_machines "${MACHINE}" ${OIMAGES}
	fi

	exit $rc
fi

# Do stages
[ -n "${DO_UPDATE_BB}" ] && tb_git_update ${BB_DIR}
[ -n "${DO_UPDATE_OE}" ] && tb_git_update ${OE_DIR}
[ -z "${DO_RUN_BUILDS}" ] && exit 0

# Use build from cmdline if any
if [ -n "$OBUILD" ]; then
	BUILDS=$OBUILD
else
	BUILDS="$CFG_DIR"/*.conf
fi

# Auto-building mode
tb_log "${HOST_DISTRO}"
tb_log "${HOST_MACHINE}"
tb_log "${PACKAGE} v${VERSION}"

for buildconf in ${BUILDS}; do (

	. ${buildconf}

	[ "$DISABLED" == "yes" ] && continue

	# Export local overrides
	[ -n "$IMAGE_FSTYPES" ] && export IMAGE_FSTYPES_local="$IMAGE_FSTYPES"
	[ -n "$DEPLOY_DIR_IMAGE" ] && export DEPLOY_DIR_IMAGE_local="$DEPLOY_DIR_IMAGE"

	# Export angstrom libc (only way to build angstrom with different libc flavors)
	[ -n "$ANGSTROMLIBC" ] && export ANGSTROMLIBC

	b=$(basename ${buildconf})
	build=${b%.conf}

	tb_log ""
	tb_log "=== Started build '$build'"
	[ -n "$BUILD" ] && tb_log "=== $BUILD"

	# change BB branch
	if [ -n "${BB_BRANCH}" ]; then
		tb_git_branch "$BB_DIR" "$BB_BRANCH"
	fi
	
	# change OE branch
	if [ -n "${OE_BRANCH}" ]; then
		tb_git_branch "$OE_DIR" "$OE_BRANCH"
	fi

	OE_REV=$(tb_git_rev "$OE_DIR")
	BB_REV=$(tb_git_rev "$BB_DIR")

	tb_log "=== OE rev: ${OE_REV}"
	tb_log "=== BB rev: ${BB_REV}"

	# parse tasks
	unset DO_BUILD_CLEAN DO_BUILD_INCREMENTAL DO_CREATE_MW_TABLE
	for task in $TASKS; do
		[ "${task}" != "${task#!}" ] && continue
		case ${task} in
		'build_clean')
			DO_BUILD_CLEAN=y;;
		'build_incremental')
			DO_BUILD_INCREMENTAL=y;;
		'create_mw_table')
			DO_CREATE_MW_TABLE=y;;
		*)
			tb_log "Unknown task ${task}.";;
		esac
	done

	# distros
	for distro in $DISTROS; do
		[ "${distro}" != "${distro#!}" ] && continue
		[ -n "${ODISTRO}" -a "${distro#!}" != "${ODISTRO}" ] && continue

		export DISTRO=$distro

		# Empty $SETS is exception - we should use $MACHINES 
		# to build every machine in separate TMPDIRs
		if [ -z "${SETS}" ]; then
			# Use every machine as separate build set
			for machine in $MACHINES; do
				[ "${machine}" != "${machine#!}" ] && continue
				eval "MACHINE_${machine}=${machine}"
				SETS="${SETS} ${machine}"
			done
		fi

		tb_log "|"
		tb_log "++= DISTRO: ${DISTRO}"

		for bset in $SETS; do
			[ "${bset}" != "${bset#!}" ] && continue
	
			eval MLIST="\$MACHINES_${bset}"
			[ -z "$MLIST" ] && MLIST=$MACHINES

			tb_log " += Processing set ${bset}"
	
			INC_TMP_DIR="${INC_DIR}/${build}/${distro}/${bset}"
	
		
			# collect images to build in $bbimages
			bbimages=''
			for image in $IMAGES; do
				[ "${image}" != "${image#!}" ] && continue
				bbimages="${bbimages} ${image}"
			done
	
			# If there is no previous incremental builddir and we are requested
			# to do both incremental and clean builds
			# then skip clean build and do only incremental because it will be clean build then :)
			if [	"${DO_BUILD_INCREMENTAL}" = 'y' \
				-a "${DO_BUILD_CLEAN}" = 'y' \
				-a ! -d "${INC_TMP_DIR}/work" \
			]; then
				tb_log " + Will do incremental build instead of clean to populate TMPDIR"
				DO_BUILD_CLEAN='skip-once'
			fi
	
			## Do clean builds
			case "${DO_BUILD_CLEAN}" in
			'y')
				export TMP_DIR="${CLN_DIR}"
				mkdir -p "${TMP_DIR}"
		
				tb_log " + Cleaning ${TMP_DIR}"
				#rm -rf ${TMP_DIR}/*
				find ${TMP_DIR}/* -delete 2>/dev/null
				
				tb_log " + Starting clean builds for machines {$MLIST}"
				tb_build_machines c "$MLIST" $bbimages
	
				tb_log " + Cleaning ${TMP_DIR} again"
				find ${TMP_DIR}/* -delete 2>/dev/null
				;;
			'skip-once')
				# Re-enable skipped clean build
				DO_BUILD_CLEAN=y
				;;
			esac
	
			## Do incremental build
			case "${DO_BUILD_INCREMENTAL}" in
			'y')
				# Switch tmpdir to archive
				export TMP_DIR="${INC_TMP_DIR}"
				mkdir -p "${TMP_DIR}"
	
				tb_log " + Starting incremental builds for machines {$MLIST}"
				tb_build_machines i "$MLIST" $bbimages
				;;
			esac
		done #SETS
	done #DISTROS
) done #BUILDS

# Upload logs
if [ -n "$DO_UPLOAD_LOGS" ]; then
	tb_log "Uploading logs to ${UPLOAD_DESTDIR}"
	eval ${UPLOAD_CMD} ${UPLOAD_OPTS} "${LOG_DIR}/*" "${UPLOAD_DESTDIR}"
fi

tb_log "All done."

exit 0