common-functions.sh 6.45 KB
Newer Older
1
#!/bin/bash
2
# vim: set noexpandtab:
3

4
# Copyright 2014-2018 Holger Levsen <holger@layer-acht.org>
5
#         © 2018      Mattia Rizzolo <mattia@mapreri.org>
6 7
# released under the GPLv=2

8
common_cleanup() {
Holger Levsen's avatar
Holger Levsen committed
9
	echo "$(date -u) - $0 stopped running as $TTT, which will now be removed."
10 11 12
	rm -f $TTT
}

13 14
abort_if_bug_is_still_open() {
	local TMPFILE=$(mktemp --tmpdir=/tmp jenkins-bugcheck-XXXXXXX)
15
	echo "$(date -u) - checking bug #$1 status."
16 17 18 19 20 21
	bts status $1 fields:done > $TMPFILE || true
	# if we get a valid response…
	if [ ! -z "$(grep done $TMPFILE)" ] ; then
		# if the bug is not done (by some email address containing a @)
		if [ -z "$(grep "@" $TMPFILE)" ] ; then
			rm $TMPFILE
Holger Levsen's avatar
Holger Levsen committed
22 23
			echo
			echo
24 25
			echo "########################################################################"
			echo "#                                                                      #"
Holger Levsen's avatar
Holger Levsen committed
26
			echo "#   https://bugs.debian.org/$1 is still open, aborting this job.   #"
27 28 29 30
			echo "#                                                                      #"
			echo "########################################################################"
			echo
			echo
31
			echo "Warning: aborting the job because of bug because #$1"
32
			echo
33 34 35 36
			echo "After having fixed the cause for #$1, remove the check from common_init()"
			echo "in bin/common-functions.sh and re-run the job. Close #$1 after the"
			echo "problem is indeed fixed."
			echo
37 38 39 40 41 42 43
			exec /srv/jenkins/bin/abort.sh
			exit 0
		fi
	fi
	rm $TMPFILE
}

44 45 46 47 48 49 50
#
# run ourself with the same parameter as we are running
# but run a copy from /tmp so that the source can be updated
# (Running shell scripts fail weirdly when overwritten when running,
#  this hack makes it possible to overwrite long running scripts
#  anytime...)
#
51 52 53
common_init() {
# check whether this script has been started from /tmp already
if [ "${0:0:5}" != "/tmp/" ] ; then
54 55 56 57 58
	# check that we are not root
	if [ $(id -u) -eq 0 ] ; then
		echo "Do not run this as root."
		exit 1
	fi
59 60
	# abort certain jobs if we know they will fail due to certain bugs…
	case $JOB_NAME in
61 62 63 64
		#chroot-installation_*_install_design-desktop-*)
		#	for BLOCKER in 869155 867695 ; do
		#		abort_if_bug_is_still_open $BLOCKER
		#	done ;;
65
		chroot-installation_sid_install_parl*|chroot-installation_buster_install_parl-*)
66
			abort_if_bug_is_still_open 886676 ;;
67 68
		chroot-installation_buster_install_design*)
			# technically these two bugs dont affect design-desktop
69
			# but just a depends of it, however I don't think it's likely
70
			# design-desktop will enter buster without these two bugs being fixed
71 72
			abort_if_bug_is_still_open 890754
			abort_if_bug_is_still_open 879034 ;;
73 74
		*) ;;
	esac
75
	# mktemp some place for us...
76
	TTT=$(mktemp --tmpdir=/tmp jenkins-script-XXXXXXXX)
77 78 79 80
	if [ -z "$TTT" ] ; then
		echo "Failed to create tmpfile, aborting. (Probably due to read-only filesystem…)"
		exit 1
	fi
81 82 83 84 85
	# prepare cleanup
	trap common_cleanup INT TERM EXIT
	# cp $0 to /tmp and run it from there
	cp $0 $TTT
	chmod +x $TTT
86 87
	echo "===================================================================================="
	echo
88
	echo "$(date -u) - running $0 ($JOB_NAME) on $(hostname) now."
89
	echo
90
	echo "To learn to understand this, git clone https://salsa.debian.org/qa/jenkins.debian.net.git"
91
	echo "and then have a look at the files README, INSTALL, CONTRIBUTING and maybe TODO."
92
	echo
93 94 95
	echo "This invocation of this script, which is located in bin/$(basename $0),"
	echo "has been called using \"$@\" as arguments." 
	echo
96
	echo "Please send feedback about jenkins to qa-jenkins-dev@lists.alioth.debian.org,"
97
	echo "or as a bug against the 'jenkins.debian.org' pseudo-package,"
98
	echo "feedback about specific job results should go to their respective lists and/or the BTS."
99
	echo 
100
	echo "===================================================================================="
101
	echo "$(date -u) - start running \"$0\" (md5sum $(md5sum $0|cut -d ' ' -f1)) as \"$TTT\" on $(hostname)."
Holger Levsen's avatar
Holger Levsen committed
102
	echo
103
	# this is the "hack": call ourself as a copy in /tmp again
104
	$TTT "$@"
Holger Levsen's avatar
Holger Levsen committed
105 106
	exit $?
	# cleanup is done automatically via trap
107
else
108 109
	# this directory resides on tmpfs, so it might be gone after reboots...
	mkdir -p /srv/workspace/chroots
110 111
	# default settings used for the jenkins.debian.net environment
	if [ -z "$LC_ALL" ]; then
112
		export LC_ALL=C.UTF-8
113
	fi
114

115
	if [ -z "$MIRROR" ]; then
116
		case $HOSTNAME in
117
			jenkins|jenkins-test-vm|profitbricks-build*)
118
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
119
			bbx15|cb3*|cbxi4*|wbq0|odxu4*|odu3*|odc*|ff*|ff4*|opi2*|jt?1*|p64*)
120
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
121
			codethink*)
122
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
123 124 125 126
			spectrum)
				export MIRROR=none ;;
			*)
				echo "unsupported host, exiting." ; exit 1 ;;
127
		esac
128 129
	fi
	if [ -z "$http_proxy" ]; then
130
		case $HOSTNAME in
131
			jenkins|jenkins-test-vm|profitbricks-build*|codethink*)
132
				export http_proxy="http://localhost:3128" ;;
133
			bbx15|cb3*|cbxi4*|wbq0|odxu4*|odu3*|odc*|ff*|ff4*|opi2*|jt?1*|p64*)
134 135 136 137 138
				export http_proxy="http://10.0.0.15:8000/" ;;
			spectrum)
				export MIRROR=none ;;
			*)
				echo "unsupported host, exiting." ; exit 1 ;;
139
		esac
140 141 142 143 144 145 146
	fi
	if [ -z "$CHROOT_BASE" ]; then
		export CHROOT_BASE=/chroots
	fi
	if [ -z "$SCHROOT_BASE" ]; then
		export SCHROOT_BASE=/schroots
	fi
Holger Levsen's avatar
Holger Levsen committed
147 148 149 150
	if [ ! -d "$SCHROOT_BASE" ]; then
		echo "Directory $SCHROOT_BASE does not exist, aborting."
		exit 1
	fi
151 152
	# use these settings in the scripts in the (s)chroots too
	export SCRIPT_HEADER="#!/bin/bash
153 154 155
	if $DEBUG ; then
		set -x
	fi
156 157 158 159 160
	set -e
	export DEBIAN_FRONTEND=noninteractive
	export LC_ALL=$LC_ALL
	export http_proxy=$http_proxy
	export MIRROR=$MIRROR"
161 162 163 164 165
	# be more verbose, maybe
	if $DEBUG ; then
		export
		set -x
	fi
166 167 168 169
	set -e
fi
}

170
publish_changes_to_userContent() {
171 172 173 174 175 176 177 178 179
	echo "Extracting contents from .deb files..."
	CHANGES=$1
	CHANNEL=$2
	SRCPKG=$(basename $CHANGES | cut -d "_" -f1)
	if [ -z "$SRCPKG" ] ; then
		exit 1
	fi
	VERSION=$(basename $CHANGES | cut -d "_" -f2)
	TARGET="/var/lib/jenkins/userContent/$SRCPKG"
180
	NEW_CONTENT=$(mktemp -d -t new-content-XXXXXXXX)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	for DEB in $(dcmd --deb $CHANGES) ; do
		dpkg --extract $DEB ${NEW_CONTENT} 2>/dev/null
	done
	rm -rf $TARGET
	mkdir $TARGET
	mv ${NEW_CONTENT}/usr/share/doc/${SRCPKG}-* $TARGET/
	rm -r ${NEW_CONTENT}
	if [ -z "$3" ] ; then
		touch "$TARGET/${VERSION}"
		FROM=""
	else
		touch "$TARGET/${VERSION}_$3"
		FROM=" from $3"
	fi
	MESSAGE="https://jenkins.debian.net/userContent/$SRCPKG/ has been updated${FROM}."
	echo
	echo $MESSAGE
	echo
	if [ ! -z "$CHANNEL" ] ; then
		kgb-client --conf /srv/jenkins/kgb/$CHANNEL.conf --relay-msg "$MESSAGE"
	fi
}