maintenance.sh 8.12 KB
Newer Older
1
#!/bin/bash
2

3
# Copyright 2012-2022 Holger Levsen <holger@layer-acht.org>
Holger Levsen's avatar
Holger Levsen committed
4
# released under the GPLv2
5

6
DEBUG=false
7
8
. /srv/jenkins/bin/common-functions.sh
common_init "$@"
9

10
check_for_mounted_chroots() {
Holger Levsen's avatar
Holger Levsen committed
11
	CHROOT_PATTERN="/chroots/${1}-*"
12
	OUTPUT=$(mktemp)
Holger Levsen's avatar
Holger Levsen committed
13
	ls $CHROOT_PATTERN 2>/dev/null > $OUTPUT || true
14
	if [ -s $OUTPUT ] ; then
15
16
17
18
19
20
21
		figlet "Warning:"
		echo
		echo "Probably manual cleanup needed:"
		echo
		echo "$ ls -la $CHROOT_PATTERN"
		# List the processes using the partition
		echo
22
		fuser -mv $CHROOT_PATTERN
23
24
		cat $OUTPUT
		rm $OUTPUT
25
26
		exit 1
	fi
27
	rm $OUTPUT
28
}
Holger Levsen's avatar
cleanup    
Holger Levsen committed
29

30
31
32
33
34
35
36
chroot_checks() {
	check_for_mounted_chroots $1
	report_disk_usage /chroots
	report_disk_usage /schroots
	echo "WARNING: should remove directories in /(s)chroots which are older than a month."
}

37
38
39
compress_old_jenkins_logs() {
	local COMPRESSED
	# compress logs to save space
40
	COMPRESSED=$(find /var/lib/jenkins/jobs/*/builds/ -maxdepth 2 -mindepth 2 -mtime +1 -name log -exec gzip -9 -v {} \;)
41
42
43
44
45
46
47
48
	if [ ! -z "$COMPRESSED" ] ; then
		echo "Logs have been compressed:"
		echo
		echo "$COMPRESSED"
		echo
	fi
}

49
remove_old_rebootstrap_logs() {
50
	local OLDSTUFF
Holger Levsen's avatar
Holger Levsen committed
51
52
	# delete old html logs to save space
	OLDSTUFF=$(find /var/lib/jenkins/jobs/rebootstrap_* -maxdepth 3 -mtime +7 -name log_content.html  -exec rm -v {} \;)
53
54
	if [ ! -z "$OLDSTUFF" ] ; then
		echo "Old html logs have been deleted:"
55
		echo
56
		echo "$OLDSTUFF"
57
		echo
58
	fi
59
60
}

61
62
63
remove_old_d-i_manual_builds() {
	local OLDSTUFF
	# delete builds of d-i manual
64
	OLDSTUFF=$(find /srv/d-i/ -maxdepth 1 -name "d-i-manual-????" -mtime +3 -exec rm -r {} \;)
65
66
67
68
69
70
71
72
	if [ ! -z "$OLDSTUFF" ] ; then
		echo "Old builds of d-i manuals have been deleted:"
		echo
		echo "$OLDSTUFF"
		echo
	fi
}

73
74
report_old_directories() {
	# find and warn about old temp directories
75
76
	if [ -z "$3" ] ; then
		OLDSTUFF=$(find $1/* -maxdepth 0 -type d -mtime +$2 -exec ls -lad {} \;)
77
	elif [ -z "$4" ] ; then
78
79
		# if $3 is given, ignore it
		OLDSTUFF=$(find $1/* -maxdepth 0 -type d -mtime +$2 ! -path "$3*" -exec ls -lad {} \;)
80
81
82
	else
		# if $3 + $4 are given, ignore them
		OLDSTUFF=$(find $1/* -maxdepth 0 -type d -mtime +$2 ! -path "$3*" ! -path "$4*" -exec ls -lad {} \;)
83
	fi
84
	if [ ! -z "$OLDSTUFF" ] ; then
Holger Levsen's avatar
Holger Levsen committed
85
		echo "Warning: old temp directories found in $1"
86
		echo
87
88
89
90
91
92
		echo "$OLDSTUFF"
		echo "Please cleanup manually."
		echo
	fi
}

93
report_disk_usage() {
94
95
96
97
98
	if [ -z "$WATCHED_JOBS" ] ; then
		echo "File system usage for all ${1} jobs:"
	else
		echo "File system usage for all ${1} jobs (including those currently running):"
	fi
99
	du -schx /var/lib/jenkins/jobs/${1}* |grep total |sed -s "s#total#${1} jobs#"
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	echo
	if [ ! -z "$WATCHED_JOBS" ] ; then
		TMPFILE=$(mktemp)
		for JOB in $(cat $WATCHED_JOBS) ; do
			du -shx --exclude='*/archive/*' $JOB | grep G >> $TMPFILE || true
		done
		if [ -s $TMPFILE ] ; then
			echo
			echo "${1} jobs with filesystem usage over 1G, excluding their archives and those currently running:"
			cat $TMPFILE
			echo
		fi
		rm $TMPFILE
	fi
114
}
115

Holger Levsen's avatar
Holger Levsen committed
116
report_filetype_usage() {
117
	OUTPUT=$(mktemp)
118
	for JOB in $(cat $WATCHED_JOBS) ; do
119
		if [ "$2" != "bak" ] && [ "$2" != "png" ] ; then
120
121
			find /var/lib/jenkins/jobs/$JOB -type f -name "*.${2}" ! -path "*/archive/*" 2>/dev/null|xargs -r du -sch |grep total |sed -s "s#total#$JOB .$2 files#" >> $OUTPUT
		else
122
			# find archived .bak + .png files too
123
124
125
126
127
			find /var/lib/jenkins/jobs/$JOB -type f -name "*.${2}" 2>/dev/null|xargs -r du -sch |grep total |sed -s "s#total#$JOB .$2 files#" >> $OUTPUT
		fi
	done
	if [ -s $OUTPUT ] ; then
		echo "File system use in $1 for $2 files:"
128
		cat $OUTPUT
129
130
131
		if [ "$3" = "warn" ] ; then
			echo "Warning: there are $2 files and there should not be any."
		fi
Holger Levsen's avatar
Holger Levsen committed
132
		echo
133
134
	fi
	rm $OUTPUT
Holger Levsen's avatar
Holger Levsen committed
135
136
}

137
138
139
140
141
142
143
144
wait4idle() {
	echo "Waiting until no $1.sh process runs.... $(date)"
	while [ $(ps fax | grep -c $1.sh) -gt 1 ] ; do
		sleep 30
	done
	echo "Done waiting: $(date)"
}

145
general_maintenance() {
146
	uptime
Holger Levsen's avatar
Holger Levsen committed
147

148
	echo
Holger Levsen's avatar
Holger Levsen committed
149
150
	# ignore unreadable /media fuse mountpoints from guestmount
	df -h 2>/dev/null || true
151

152
	echo
153
	for DIR in /var/cache/apt/archives/ /var/cache/pbuilder/build/ /var/lib/jenkins/jobs/ /chroots /schroots ; do
154
155
156
157
158
159
160
		sudo du -shx $DIR 2>/dev/null
	done
	JOB_PREFIXES=$(ls -1 /var/lib/jenkins/jobs/|cut -d "_" -f1|sort -f -u)
	for PREFIX in $JOB_PREFIXES ; do
		report_disk_usage $PREFIX
	done

Holger Levsen's avatar
Holger Levsen committed
161
	echo
162
163
	vnstat

Holger Levsen's avatar
Holger Levsen committed
164
	(df 2>/dev/null || true ) | grep tmpfs > /dev/null || ( echo ; echo "Warning: no tmpfs mounts in use. Please investigate the host system." ; exit 1 )
165
}
166

167
168
build_jenkins_job_health_page() {
	#
169
	# jenkins job health page
170
	#
171
	echo "$(date -u) - starting to write jenkins_job_health page."
172
173
	# these are simple egrep filters. however, if they contain a colon,
	# the filter is split in two, see $category and $avoid below
Holger Levsen's avatar
Holger Levsen committed
174
175
176
177
178
179
180
181
182
183
184
	FILTER=("maintenance"
		"udd"
		"lintian"
		"piuparts"
		"policy_"
		"debsums"
		"dpkg"
		"transitional"
		"edu-packages"
		"haskell"
		"chroot-installation_sid"
185
		"chroot-installation_bookworm"
Holger Levsen's avatar
Holger Levsen committed
186
187
188
189
190
191
192
193
194
195
		"chroot-installation_bullseye"
		"chroot-installation_buster"
		"d-i_overview"
		"d-i_manual"
		"d-i_build"
		"d-i_schroot"
		"d-i_:(overview|manual|build|schroot)"
		"rebootstrap"
		"debian-archive-keyring")
	numfilters=$(( ${#FILTER[@]} -1 ))
196
197
	write_page "<!DOCTYPE html><html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
	write_page "<title>Jenkins job health</title/></head><body>"
198
	for CATEGORY in $(seq 0 $numfilters) ; do
199
200
201
202
203
204
205
206
		# $FILTER with a colon are split into $category and $filter
		if $(echo "${FILTER[$CATEGORY]}" | grep -q ":") ; then
			category=$(echo "${FILTER[$CATEGORY]}" | cut -d ":" -f1)
			filter=$(echo "${FILTER[$CATEGORY]}" | cut -d ":" -f2)
		else
			category="${FILTER[$CATEGORY]}"
			filter=""
		fi
207
		write_page "<p style=\"clear:both;\">"
208
		write_page "<h3>$(echo $category | sed 's#\.\*##g')</h3>"
209
		for JOB in $(cat ${JOBS} | grep -v reproducible_ | egrep "$category" | sort ) ; do
210
211
212
			if [ -n "$filter" ] && [[ -n $(echo "$JOB" | egrep "$filter") ]] ; then
				continue
			fi
213
214
215
216
217
218
			URL="https://jenkins.debian.net/job/$JOB"
			BADGE="$URL/badge/icon"
			write_page "<a href='$URL'><img src='$BADGE' /></a> "
		done
		write_page "</p>"
	done
219
	# find jobs not present in jenkins_job_health.html
220
	for JOB in $(cat ${JOBS} | egrep -v '(reproducible_|lost\+found)' | sort ) ; do
221
222
		found=false
		for CATEGORY in $(seq 0 $numfilters) ; do
223
224
225
226
227
228
			if $(echo "${FILTER[$CATEGORY]}" | grep -q ":") ; then
				category=$(echo "${FILTER[$CATEGORY]}" | cut -d ":" -f1)
			else
				category="${FILTER[$CATEGORY]}"
			fi
			if [ -n "$(echo $JOB | egrep "$category" 2>/dev/null|| true )" ] ; then
229
230
231
232
233
				found=true
				continue
			fi
		done
		if ! $found ; then
234
235
236
237
238
			if $empty ; then
				empty=false
				write_page "<p style=\"clear:both;\">"
				write_page "<h3>Other jobs</h3>"
			fi
239
240
241
242
243
244
			echo "$(date -u) - job $JOB not present in in existing filters for jenkins_job_health page..."
			URL="https://jenkins.debian.net/job/$JOB"
			BADGE="$URL/badge/icon"
			write_page "<a href='$URL'><img src='$BADGE' /></a> "
		fi
	done
245
246
	if ! $empty ; then
		write_page "</p>"
247
		write_page "<p><small>This page was generated by <a href=\"$JOB_URL\">$(basename $JOB_URL)</a> at $(date -u).</small></p>"
248
249
		write_page "</body></html>"
	fi
250
251
252
253
254
	mv $PAGE ~/userContent/jenkins_job_health.html
	chmod 644 ~/userContent/jenkins_job_health.html
	echo "$(date -u) - updated https://jenkins.debian.net/userContent/jenkins_job_health.html"
}

255
#
256
# if $1 is empty, we do general maintenance, else for some subgroup of all jobs
257
#
Holger Levsen's avatar
Holger Levsen committed
258
if [ -z $1 ] ; then
259
	general_maintenance
260
	compress_old_jenkins_logs
261
	PAGE=$(mktemp)
262
263
264
265
266
267
268
269
270
271
272
	# only recreate jenkins job health page if jobs have changed
	JOBS=$(mktemp)
	(cd ~/jobs ; ls -1d * | sort > $JOBS)
	if [ ! -f ./joblist ] || ! (diff $JOBS ./joblist > /dev/null) ; then
		echo "$(date -u) - jobs have changed, recreating jenkins_job_health page."
		build_jenkins_job_health_page
		mv $JOBS ./joblist
	else
		echo "$(date -u) - jobs haven't changed, not recreating jenkins_job_health page."
		rm $JOBS
	fi
Holger Levsen's avatar
Holger Levsen committed
273
else
Holger Levsen's avatar
Holger Levsen committed
274
	case $1 in
275
		chroot-installation*)		wait4idle $1
276
						report_disk_usage $1
277
						chroot_checks $1
Holger Levsen's avatar
Holger Levsen committed
278
						;;
279
		d-i)				report_old_directories /srv/d-i 7 /srv/d-i/workspace /srv/d-i/isos
280
						remove_old_d-i_manual_builds
281
						;;
282
283
		rebootstrap)			remove_old_rebootstrap_logs
						;;
Holger Levsen's avatar
Holger Levsen committed
284
285
		*)				;;
	esac
286
fi
287

Holger Levsen's avatar
cleanup    
Holger Levsen committed
288
echo
289
echo "No (big) problems found, all seems good."
290
figlet "Ok."