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

4
# Copyright 2014-2022 Holger Levsen <holger@layer-acht.org>
5
#         © 2018      Mattia Rizzolo <mattia@mapreri.org>
Holger Levsen's avatar
Holger Levsen committed
6
# released under the GPLv2
7

8
common_cleanup() {
9
	echo "$(date -u) - $(basename $0) stopped running as $TTT, removing."
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
Holger Levsen's avatar
Holger Levsen committed
24
25
			echo "########################################################################"
			echo "#                                                                      #"
Holger Levsen's avatar
Holger Levsen committed
26
			echo "#   https://bugs.debian.org/$1 is still open, aborting this job.   #"
Holger Levsen's avatar
Holger Levsen committed
27
28
29
30
			echo "#                                                                      #"
			echo "########################################################################"
			echo
			echo
31
			echo "Warning: aborting the job because of bug because #$1"
Holger Levsen's avatar
Holger Levsen committed
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
common_init() {
52
53
54
55
# some sensible default
if [ "${LC_ALL+x}" != "x" ] ; then
	export LC_ALL=C.UTF-8
fi
56
57
# check whether this script has been started from /tmp already
if [ "${0:0:5}" != "/tmp/" ] ; then
58
59
60
61
62
	# check that we are not root
	if [ $(id -u) -eq 0 ] ; then
		echo "Do not run this as root."
		exit 1
	fi
63
64
	# - for remote jobs we need to check against $SSH_ORIGINAL_COMMAND
	# - for local jobs this would be $JOB_NAME
65
	if [ "${JOB_NAME+x}" = "x" ] ; then
66
67
		WHOAREWE=$JOB_NAME
	else
68
		# $JOB_NAME is undefined
69
		WHOAREWE=${SSH_ORIGINAL_COMMAND/%\ */}
70
	fi
71
	# abort certain jobs if we know they will fail due to certain bugs…
72
	case $WHOAREWE in
73
74
75
76
		#chroot-installation_*_install_design-desktop-*)
		#	for BLOCKER in 869155 867695 ; do
		#		abort_if_bug_is_still_open $BLOCKER
		#	done ;;
77
78
		chroot-installation_buster_install_design*)
			# technically these two bugs dont affect design-desktop
79
			# but just a depends of it, however I don't think it's likely
80
			# design-desktop will enter buster without these two bugs being fixed
81
			abort_if_bug_is_still_open 890754 ;;
82
83
		chroot-installation_stretch_install_education-desktop-gnome_upgrade_to_buster|chroot-installation_stretch_install_education-desktop-xfce_upgrade_to_buster|chroot-installation_stretch_install_education-networked_upgrade_to_buster)
			abort_if_bug_is_still_open 928429 ;;
84
85
		chroot-installation_buster_install_parl-desktop-world*)
			abort_if_bug_is_still_open 945930 ;;
86
87
		*) ;;
	esac
88
	# mktemp some place for us...
89
90
	TTT="$(mktemp --tmpdir=/tmp jenkins-script-XXXXXXXX)"
	if [ -z "$TTT" ] ; then
91
92
93
		echo "Failed to create tmpfile, aborting. (Probably due to read-only filesystem…)"
		exit 1
	fi
94
95
96
97
98
	# prepare cleanup
	trap common_cleanup INT TERM EXIT
	# cp $0 to /tmp and run it from there
	cp $0 $TTT
	chmod +x $TTT
99
	echo "===================================================================================="
100
101
	echo "$(date -u) - running $0 (for job $WHOAREWE) on $(hostname), called using \"$@\" as arguments."
	echo "$(date -u) - actually running \"$(basename $0)\" (md5sum $(md5sum $0|cut -d ' ' -f1)) as \"$TTT\""
102
	echo
103
	echo "$ git clone https://salsa.debian.org/qa/jenkins.debian.net.git ; more CONTRIBUTING"
104
	echo
105
	# this is the "hack": call ourself as a copy in /tmp again
106
	$TTT "$@"
Holger Levsen's avatar
Holger Levsen committed
107
108
	exit $?
	# cleanup is done automatically via trap
109
else
110
111
	# this directory resides on tmpfs, so it might be gone after reboots...
	mkdir -p /srv/workspace/chroots
112

113
	if [ "${MIRROR+x}" != "x" ] ; then
114
		case $HOSTNAME in
115
			jenkins|ionos*|osuosl*|snapshot)
116
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
117
			cbxi4*|wbq0|ff*|jt?1*|virt32[a-z]|virt64[a-z])
118
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
119
			codethink*)
120
				export MIRROR=http://cdn-fastly.deb.debian.org/debian ;;
121
			spectrum|warren)  # warren is mattia's laptop
122
123
124
				export MIRROR=none ;;
			*)
				echo "unsupported host, exiting." ; exit 1 ;;
125
		esac
126
	fi
127
128
	# force http_proxy as we want it
	case $HOSTNAME in
129
		jenkins|ionos1-a*|ionos2*|ionos9*|ionos11*|ionos12*)
Holger Levsen's avatar
Holger Levsen committed
130
			# IONOS datacenter in karlsruhe uses ionos1 as proxy:
131
			export http_proxy="http://78.137.99.97:3128" ;;
132
		ionos3*|ionos5*|ionos6*|ionos7*|ionos10*|ionos15*|ionos16*)
Holger Levsen's avatar
Holger Levsen committed
133
			# IONOS datacenter in frankfurt uses ionos10 as proxy:
134
			export http_proxy="http://85.184.249.68:3128" ;;
135
		osuosl*)
136
137
			# all nodes at OSUOSL use themself as proxy:
			export http_proxy="http://127.0.0.1:3128" ;;
138
139
		codethink*)
			export http_proxy="http://192.168.101.16:3128" ;;
140
		cbxi4*|wbq0|ff*|jt?1*|virt64[a-z]|virt32[a-z])
141
			export http_proxy="http://10.0.0.15:3142/" ;;
142
		spectrum)
143
			export http_proxy="http://127.0.0.1:3128" ;;
144
		snapshot) : ;;
145
		warren) : ;;  # warren is mattia's laptop
146
147
148
		*)
			echo "unsupported host, exiting." ; exit 1 ;;
	esac
149
	if [ "${CHROOT_BASE+x}" != "x" ] ; then
150
151
		export CHROOT_BASE=/chroots
	fi
152
	if [ "${SCHROOT_BASE+x}" != "x" ] ; then
153
154
		export SCHROOT_BASE=/schroots
	fi
Holger Levsen's avatar
Holger Levsen committed
155
156
157
158
	if [ ! -d "$SCHROOT_BASE" ]; then
		echo "Directory $SCHROOT_BASE does not exist, aborting."
		exit 1
	fi
159
160
	# use these settings in the scripts in the (s)chroots too
	export SCRIPT_HEADER="#!/bin/bash
161
162
163
	if $DEBUG ; then
		set -x
	fi
164
165
166
167
168
	set -e
	export DEBIAN_FRONTEND=noninteractive
	export LC_ALL=$LC_ALL
	export http_proxy=$http_proxy
	export MIRROR=$MIRROR"
169
170
171
172
173
	# be more verbose, maybe
	if $DEBUG ; then
		export
		set -x
	fi
174
175
176
177
	set -e
fi
}

178
publish_changes_to_userContent() {
179
180
181
	echo "Extracting contents from .deb files..."
	CHANGES=$1
	CHANNEL=$2
182
183
	SRCPKG="$(basename $CHANGES | cut -d "_" -f1)"
	if [ -z "$SRCPKG" ] ; then
184
		echo '$SRCPKG is empty, exiting with error.'
185
186
187
188
		exit 1
	fi
	VERSION=$(basename $CHANGES | cut -d "_" -f2)
	TARGET="/var/lib/jenkins/userContent/$SRCPKG"
189
	NEW_CONTENT=$(mktemp -d -t new-content-XXXXXXXX)
190
191
192
193
194
	for DEB in $(dcmd --deb $CHANGES) ; do
		dpkg --extract $DEB ${NEW_CONTENT} 2>/dev/null
	done
	rm -rf $TARGET
	mkdir $TARGET
195
	mv ${NEW_CONTENT}/usr/share/doc/${SRCPKG}* $TARGET/
196
	rm -r ${NEW_CONTENT}
197
	if [ "${3+x}" != "x" ] ; then
198
199
200
201
202
203
204
205
206
207
		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
208
	if [ "${CHANNEL+x}" != "x" ] ; then
209
210
211
		kgb-client --conf /srv/jenkins/kgb/$CHANNEL.conf --relay-msg "$MESSAGE"
	fi
}
Holger Levsen's avatar
Holger Levsen committed
212
213
214
215

write_page() {
	echo "$1" >> $PAGE
}
Holger Levsen's avatar
Holger Levsen committed
216

Holger Levsen's avatar
Holger Levsen committed
217
218
219
220
221
222
223
224
225
226
227
output_echo() {
	set +x
	echo "###########################################################################################"
	echo
	echo -e "$(date -u) - $1"
	echo
	if $DEBUG; then
		set -x
	fi
}

Holger Levsen's avatar
Holger Levsen committed
228
229
230
231
232
233
jenkins_zombie_check() {
	#
	# sometimes deleted jobs come back as zombies
	# and we dont know why and when that happens,
	# so just report those zombies here.
	#
234
	# this has last happened on 2021-08-09, with a jenkins.deb
235
236
237
	# upgrade on 2021-07-28, thus likely unrelated. what seems
	# related however is that I issued a reboot (via running 
	# /sbin/reboot) right before the zombies appeared...
238
	#
239
	ZOMBIES="$(ls -1d /var/lib/jenkins/jobs/* | egrep 'strip-nondeterminism|reprotest|reproducible_(builder_(amd64|i386|armhf|arm64)|setup_(pbuilder|schroot)_testing)|chroot-installation_wheezy|aptdpkg|stretch_install_education-thin-client-server|jessie_multiarch_versionskew|dpkg_stretch_find_trigger_cycles|sid_install_education-services|buster_install_education-services|lvc|chroot-installation_stretch_.*_upgrade_to_sid|chroot-installation_buster_.*_upgrade_to_sid|piuparts_.*_jessie|udd_stretch|d-i_pu-build|debsums-tests_stretch|debian-archive-keyring-tests_stretch|chroot-installation_jessie|chroot-installation_.*education-lang-|kirkwoot|rebootstrap_.*_gcc[5-9]($|_)|rebootstrap_.*_gcc1[01]($|_)|brcm47xx|rebootstrap_kfreebsd|diffoscope_from_git_|disorderfs_from_git_master|diffoscope_pypi|diffoscope_freebsd|diffoscope_netbsd|diffoscope_macports|diffoscope_archlinux|openwrt-target-ath97|profitbricks|pool_buildinfos_suites|g-i-installation|reproducible_compare_Debian_sha1sums|bbx15|cb3a|ff2a|ff2b|jtk1a|jtk1b|odxu4a|odxu4b|odu3a|opi2a|opi2c|p64b|p64c|ar71xx|reproducible_debian_live_build$|chroot-installation_stretch|chroot-installation_bullseye*upgrade_to_sid|rebuilder_prototype|osuosl169' || true)"
240
	if [ ! -z "$ZOMBIES" ] ; then
241
		DIRTY=true
Holger Levsen's avatar
Holger Levsen committed
242
		figlet 'zombies!!!'
Holger Levsen's avatar
Holger Levsen committed
243
		echo "Warning: rise of the jenkins job zombies has started again, these jobs should not exist:"
Holger Levsen's avatar
Holger Levsen committed
244
245
246
247
248
249
250
251
252
253
254
255
256
		for z in $ZOMBIES ; do

			echo $(basename $z)
		done
		echo
	fi
}

jenkins_logsize_check() {
	#
	# /var/log/jenkins/jenkins.log sometimes grows very fast
	# and we don't yet know why, so let's monitor this for now.
	JENKINSLOG="$(find /var/log/jenkins -name jenkins.log -size +42G)"
257
	if [ ! -z "$JENKINSLOG" ] ; then
258
		figlet 'jenkins.log size'
Holger Levsen's avatar
Holger Levsen committed
259
		echo "Warning: jenkins.log is larger than 42G, please fix, erroring out now."
Holger Levsen's avatar
Holger Levsen committed
260
261
262
		exit 1
	else
		JENKINSLOG="$(find /var/log/jenkins -name jenkins.log -size +23G)"
263
		if [ ! -z "$JENKINSLOG" ] ; then
264
			DIRTY=true
Holger Levsen's avatar
Holger Levsen committed
265
			figlet 'jenkins.log size'
Holger Levsen's avatar
Holger Levsen committed
266
			echo "Warning: jenkins.log is larger than 23G, please do something…"
Holger Levsen's avatar
Holger Levsen committed
267
268
269
		fi
	fi
}
Holger Levsen's avatar
Holger Levsen committed
270
271
272
273
274

jenkins_bugs_check() {
	jenkins_zombie_check
	jenkins_logsize_check
}