reproducible_worker.sh 3.16 KB
Newer Older
1
#!/bin/bash
2
# vim: set noexpandtab:
3

4
# Copyright © 2017-2018 Holger Levsen (holger@layer-acht.org)
5
#           © 2018 Mattia Rizolo <mattia@debian.org>
6 7 8 9 10 11 12 13
# released under the GPLv=2

set -e

WORKER_NAME=$1
NODE1=$2
NODE2=$3

14 15 16
# normally defined by jenkins and used by reproducible_common.sh
JENKINS_URL=https://jenkins.debian.net

17 18 19 20 21 22 23
DEBUG=false
. /srv/jenkins/bin/common-functions.sh
common_init "$@"

# common code defining db access
. /srv/jenkins/bin/reproducible_common.sh

24 25 26
notify_log_of_failure() {
	tee -a /var/log/jenkins/reproducible-builder-errors.log <<-END

27
		$(date -u) - $WORKER_NAME/$BUILD_ID exited with an error.  Return code: $RETCODE
28 29 30 31 32
		Check out the log at $BUILD_URL

		END
}

33
# endless loop
34
while true ; do
35 36 37
	#
	# check if we really should be running
	#
38 39 40 41 42 43
	RUNNING=$(ps fax|grep -v grep|grep "$0 $1 ")
	if [ -z "$RUNNING" ] ; then
		echo "$(date --utc) - '$0 $1' already running, thus stopping this."
		break
	fi
	SERVICE="reproducible_build@startup.service"
44 45
	# try systemctl twice, but only output and thus log the 2nd attempt…
	RUNNING=$(systemctl show $SERVICE 2>/dev/null |grep ^SubState|cut -d "=" -f2)
46
	if [ "$RUNNING" != "running" ] ; then
47 48 49 50 51 52 53
		# sometimes systemctl requests time out… handle that gracefully
		sleep 23
		RUNNING=$(systemctl show $SERVICE|grep ^SubState|cut -d "=" -f2)
		if [ "$RUNNING" != "running" ] ; then
			echo "$(date --utc) - '$SERVICE' not running, thus stopping this."
			break
		fi
54
	fi
55
	LOCKFILE="/var/lib/jenkins/NO-RB-BUILDERS-PLEASE"
56
	if [ -f "$LOCKFILE" ]; then
57 58 59
		echo "The lockfile $LOCKFILE is present, thus stopping this"
		break
	fi
60 61 62 63 64 65 66 67
	if [ -f "$JENKINS_OFFLINE_LIST" ]; then
		for n in "$NODE1" "$NODE2"; do
			if grep -q "$n" "$JENKINS_OFFLINE_LIST"; then
				echo "$n is currently marked as offline, stopping the worker."
				break
			fi
		done
	fi
68 69 70 71

	# sleep up to 2.3 seconds (additionally to the random sleep reproducible_build.sh does anyway)
	/bin/sleep $(echo "scale=1 ; $(shuf -i 1-23 -n 1)/10" | bc )

72 73 74
	#
	# increment BUILD_ID
	#
75 76 77 78 79 80 81
	BUILD_BASE=/var/lib/jenkins/userContent/reproducible/debian/build_service/$WORKER_NAME
	OLD_ID=$(ls -1rt $BUILD_BASE|egrep -v "(latest|worker.log)" |sort -n|tail -1)
	let BUILD_ID=OLD_ID+1
	mkdir -p $BUILD_BASE/$BUILD_ID
	rm -f $BUILD_BASE/latest
	ln -sf $BUILD_ID $BUILD_BASE/latest

82 83 84
	#
	# prepare variables for export
	#
85
	export BUILD_URL=https://jenkins.debian.net/userContent/reproducible/debian/build_service/$WORKER_NAME/$BUILD_ID/
86 87 88 89
	export BUILD_ID=$BUILD_ID
	export JOB_NAME="reproducible_builder_$WORKER_NAME"
	export

90 91 92
	#
	# actually run reproducible_build.sh
	#
93 94 95
	echo
	echo "================================================================================================"
	echo "$(date --utc) - running build #$BUILD_ID for $WORKER_NAME on $NODE1 and $NODE2."
96
	echo "                               see https://tests.reproducible-builds.org/cgi-bin/nph-logwatch?$WORKER_NAME/$BUILD_ID"
97 98
	echo "================================================================================================"
	echo
99 100
	RETCODE=0
	/srv/jenkins/bin/reproducible_build.sh $NODE1 $NODE2 >$BUILD_BASE/$BUILD_ID/console.log 2>&1 || RETCODE=$?
101
	gzip $BUILD_BASE/$BUILD_ID/console.log || true
102
	echo
103

104
	[ "$RETCODE" -eq 0 ] || notify_log_of_failure
105

106
done