common-functions.sh 6.97 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 61 62 63 64 65
	# - for remote jobs we need to check against $SSH_ORIGINAL_COMMAND
	# - for local jobs this would be $JOB_NAME
	if [ -n "$JOB_NAME" ] ; then
		WHOAREWE=$JOB_NAME
	else
		WHOAREWE=${SSH_ORIGINAL_COMMAND}
	fi
66
	# abort certain jobs if we know they will fail due to certain bugs…
67
	case $WHOAREWE in
68 69 70 71
		#chroot-installation_*_install_design-desktop-*)
		#	for BLOCKER in 869155 867695 ; do
		#		abort_if_bug_is_still_open $BLOCKER
		#	done ;;
72
		chroot-installation_sid_install_parl*|chroot-installation_buster_install_parl-*)
73
			abort_if_bug_is_still_open 886676 ;;
74 75
		chroot-installation_buster_install_design*)
			# technically these two bugs dont affect design-desktop
76
			# but just a depends of it, however I don't think it's likely
77
			# design-desktop will enter buster without these two bugs being fixed
78 79
			abort_if_bug_is_still_open 890754
			abort_if_bug_is_still_open 879034 ;;
80 81
		chroot-installation_sid_install_design-desktop-graphics)
			abort_if_bug_is_still_open 910812 ;;
82 83
		chroot-installation_stretch_install_education-mathematics_upgrade_to_buster)
			abort_if_bug_is_still_open 910819 ;;
84 85
		*) ;;
	esac
86
	# mktemp some place for us...
87
	TTT=$(mktemp --tmpdir=/tmp jenkins-script-XXXXXXXX)
88 89 90 91
	if [ -z "$TTT" ] ; then
		echo "Failed to create tmpfile, aborting. (Probably due to read-only filesystem…)"
		exit 1
	fi
92 93 94 95 96
	# prepare cleanup
	trap common_cleanup INT TERM EXIT
	# cp $0 to /tmp and run it from there
	cp $0 $TTT
	chmod +x $TTT
97 98
	echo "===================================================================================="
	echo
99
	echo "$(date -u) - running $0 (for job $WHOAREWE) on $(hostname) now."
100
	echo
101
	echo "To learn to understand this, git clone https://salsa.debian.org/qa/jenkins.debian.net.git"
102
	echo "and then have a look at the files README, INSTALL, CONTRIBUTING and maybe TODO."
103
	echo
104 105 106
	echo "This invocation of this script, which is located in bin/$(basename $0),"
	echo "has been called using \"$@\" as arguments." 
	echo
107
	echo "Please send feedback about jenkins to qa-jenkins-dev@lists.alioth.debian.org,"
108
	echo "or as a bug against the 'jenkins.debian.org' pseudo-package,"
109
	echo "feedback about specific job results should go to their respective lists and/or the BTS."
110
	echo 
111
	echo "===================================================================================="
112
	echo "$(date -u) - start running \"$0\" (md5sum $(md5sum $0|cut -d ' ' -f1)) as \"$TTT\" on $(hostname)."
Holger Levsen's avatar
Holger Levsen committed
113
	echo
114
	# this is the "hack": call ourself as a copy in /tmp again
115
	$TTT "$@"
Holger Levsen's avatar
Holger Levsen committed
116 117
	exit $?
	# cleanup is done automatically via trap
118
else
119 120
	# this directory resides on tmpfs, so it might be gone after reboots...
	mkdir -p /srv/workspace/chroots
121 122
	# default settings used for the jenkins.debian.net environment
	if [ -z "$LC_ALL" ]; then
123
		export LC_ALL=C.UTF-8
124
	fi
125

126
	if [ -z "$MIRROR" ]; then
127
		case $HOSTNAME in
128
			jenkins|jenkins-test-vm|profitbricks-build*)
129
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
130
			bbx15|cb3*|cbxi4*|wbq0|odxu4*|odu3*|odc*|ff*|ff4*|opi2*|jt?1*|p64*)
131
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
132
			codethink*)
133
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
134 135 136 137
			spectrum)
				export MIRROR=none ;;
			*)
				echo "unsupported host, exiting." ; exit 1 ;;
138
		esac
139 140
	fi
	if [ -z "$http_proxy" ]; then
141
		case $HOSTNAME in
142
			jenkins|jenkins-test-vm|profitbricks-build*)
143
				export http_proxy="http://localhost:3128" ;;
144 145
			codethink*)
				export http_proxy="http://192.168.101.16:3128" ;;
146
			bbx15|cb3*|cbxi4*|wbq0|odxu4*|odu3*|odc*|ff*|ff4*|opi2*|jt?1*|p64*)
147 148 149 150 151
				export http_proxy="http://10.0.0.15:8000/" ;;
			spectrum)
				export MIRROR=none ;;
			*)
				echo "unsupported host, exiting." ; exit 1 ;;
152
		esac
153 154 155 156 157 158 159
	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
160 161 162 163
	if [ ! -d "$SCHROOT_BASE" ]; then
		echo "Directory $SCHROOT_BASE does not exist, aborting."
		exit 1
	fi
164 165
	# use these settings in the scripts in the (s)chroots too
	export SCRIPT_HEADER="#!/bin/bash
166 167 168
	if $DEBUG ; then
		set -x
	fi
169 170 171 172 173
	set -e
	export DEBIAN_FRONTEND=noninteractive
	export LC_ALL=$LC_ALL
	export http_proxy=$http_proxy
	export MIRROR=$MIRROR"
174 175 176 177 178
	# be more verbose, maybe
	if $DEBUG ; then
		export
		set -x
	fi
179 180 181 182
	set -e
fi
}

183
publish_changes_to_userContent() {
184 185 186 187 188 189 190 191 192
	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"
193
	NEW_CONTENT=$(mktemp -d -t new-content-XXXXXXXX)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	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
}
Holger Levsen's avatar
Holger Levsen committed
216 217 218 219

write_page() {
	echo "$1" >> $PAGE
}