update_jdn.sh 24.2 KB
Newer Older
1
#!/bin/bash
2
# vim: set noexpandtab:
3
# Copyright 2012-2018 Holger Levsen <holger@layer-acht.org>
4
#         ©      2018 Mattia Rizzolo <mattia@debian.org>
5
6
# released under the GPLv=2

Holger Levsen's avatar
Holger Levsen committed
7
# puppet / salt / ansible / fai / chef / deployme.app - disclaimer
Holger Levsen's avatar
Holger Levsen committed
8
9
10
11
12
13
14
15
16
17
18
# (IOW: this script has been grown in almost 500 commits and it shows…)
#
# yes, we know… and: "it" should probably still be done.
#
# It just unclear, how/what, and what we have actually mostly works.
#
# Switching to jenkins.debian.org is probably an opportunity
# to write (refactor this into) *yet another deployment script*
# (interacting with the DSA machine setup which is in puppet…),
# thus obsoleting this script gradually, though this is used on
# 47 hosts currently (of which quite some were initially installed
Edward Betts's avatar
Edward Betts committed
19
# manually…)
Holger Levsen's avatar
Holger Levsen committed
20
21
22
#
# so, yes, patches welcome. saying this is crap alone is not helpful,
# nor is just suggesting some new or old technology. patches most welcome!
23
24
#
# that said, there's a new one: init_node ;)
Holger Levsen's avatar
Holger Levsen committed
25

26
27
set -e

28
BASEDIR="$(dirname "$(readlink -e $0)")"
29
STAMP=/var/log/jenkins/update-jenkins.stamp
30
31
# The $@ below means that command line args get passed on to j-j-b
# which allows one to specify --flush-cache or --ignore-cache
32
JJB="jenkins-jobs $@"
33
DPKG_ARCH="$(dpkg --print-architecture)"
Holger Levsen's avatar
Holger Levsen committed
34

35
# so we can later run some commands only if $0 has been updated…
Philip Hands's avatar
Philip Hands committed
36
if [ -f $STAMP ] && [ $STAMP -nt $BASEDIR/$0 ] ; then
Holger Levsen's avatar
Holger Levsen committed
37
	UP2DATE=true
Philip Hands's avatar
Philip Hands committed
38
39
else
	UP2DATE=false
40
41
42
fi


Holger Levsen's avatar
Holger Levsen committed
43
explain() {
Holger Levsen's avatar
Holger Levsen committed
44
	echo "$HOSTNAME: $1"
Holger Levsen's avatar
Holger Levsen committed
45
46
}

47
48
49
50
51
set_correct_date() {
		# set correct date
		sudo service ntp stop || true
		sudo ntpdate -b de.pool.ntp.org
}
52

53
disable_dsa_check_packages() {
54
	# disable check for outdated packages as someday in the future
55
	# packages from security.d.o will appear outdated always…
56
57
	echo -e "#!/bin/sh\n# disabled dsa-check by update_jdn.sh\nexit 0" | sudo tee /usr/local/bin/dsa-check-packages
	sudo chmod a+rx /usr/local/bin/dsa-check-packages
58
59
}

60
echo "--------------------------------------------"
Holger Levsen's avatar
Holger Levsen committed
61
explain "$(date) - begin deployment update."
62

63
64
65
66
#
# temporarily test to check which hosts don't use systemd
#
if [ -z "$(dpkg -l|grep systemd-sysv||true)" ] ; then 
67
	echo "no systemd-sysv installed on $(hostname), please enter to continue…"
68
69
70
	read
fi

71
# some nodes need special treatment…
72
case $HOSTNAME in
73
	profitbricks-build4-amd64|profitbricks-build5-amd64|profitbricks-build6-i386|profitbricks-build15-amd64|profitbricks-build16-i386)
Holger Levsen's avatar
Holger Levsen committed
74
		# set correct date
75
		set_correct_date
76
		;;
77
	codethink-sled9*|codethink-sled11*|codethink-sled13*|codethink-sled15*)
78
		# set correct date
79
		set_correct_date
Holger Levsen's avatar
Holger Levsen committed
80
		;;
81
82
83
	*)	;;
esac

84
85
86
87
88
89
90
91
92
93
94
# ubuntu decided to change kernel perms in the middle of LTS…
case $HOSTNAME in
	codethink-sled*)
		# fixup perms
		sudo chmod +r /boot/vmlinuz-*
		;;
	*)	;;
esac



95
96
97
#
# set up users and groups
#
98
declare -A user_host_groups u_shell
99
sudo_groups='jenkins,jenkins-adm,sudo,adm'
100
101
102

# if there's a need for host groups, a case statement on $HOSTNAME here that sets $GROUPNAME, say, should do the trick
# then you can define user_host_groups['phil','lvm_group']=... below
Edward Betts's avatar
Edward Betts committed
103
# and add checks for the GROUP version wherever the HOSTNAME is checked in the following code
104
105
106
107
108

user_host_groups['helmut','*']="$sudo_groups"
user_host_groups['holger','*']="$sudo_groups"
user_host_groups['holger','jenkins']="reproducible,${user_host_groups['holger','*']}"
user_host_groups['mattia','*']="$sudo_groups"
109
user_host_groups['mattia','jenkins']="reproducible,${user_host_groups['mattia','*']}"
110
user_host_groups['phil','jenkins-test-vm']="$sudo_groups,libvirt,libvirt-qemu"
111
user_host_groups['phil','jenkins']="$sudo_groups"
112
user_host_groups['lunar','jenkins']='reproducible'
113
114
user_host_groups['lynxis','profitbricks-build3-amd64']="$sudo_groups"
user_host_groups['lynxis','profitbricks-build4-amd64']="$sudo_groups"
115
user_host_groups['hans','profitbricks-build7-amd64']="$sudo_groups"
116
117
user_host_groups['vagrant','armhf']="$sudo_groups"
user_host_groups['vagrant','arm64']="$sudo_groups"
118

119
120

u_shell['mattia']='/bin/zsh'
121
u_shell['lynxis']='/bin/fish'
122
u_shell['jenkins-adm']='/bin/bash'
123

124
125
126
# get the users out of the user_host_groups array's index
users=$(for i in ${!user_host_groups[@]}; do echo ${i%,*} ; done | sort -u)

127
( $UP2DATE && [ -z "$(find authorized_keys -newer $0)" ] ) || for user in ${users}; do
128
	# -v is a bashism to check for set variables, used here to see if this user is active on this host
129
	if [ ! -v user_host_groups["$user","$HOSTNAME"] ] && [ ! -v user_host_groups["$user",'*'] ] && [ ! -v user_host_groups["$user","$DPKG_ARCH"] ] ; then
Holger Levsen's avatar
Holger Levsen committed
130
131
		continue
	fi
132

133
134
135
136
137
138
	# create the user
	if ! getent passwd $user > /dev/null ; then
		# adduser, defaulting to /bin/bash as shell
		sudo adduser --gecos "" --shell "${u_shell[$user]:-/bin/bash}" --disabled-password $user
	fi
	# add groups: first try the specific host, or if unset fall-back to default '*' setting
139
	for h in "$HOSTNAME" "$DPKG_ARCH" '*' ; do
140
		if [ -v user_host_groups["$user","$h"] ] ; then
Holger Levsen's avatar
Holger Levsen committed
141
			sudo usermod -G "${user_host_groups["$user","$h"]}" $user
142
			break
143
		fi
144
145
146
147
148
	done
	# add the user's keys (if any)
	if ls authorized_keys/${user}@*.pub >/dev/null 2>&1 ; then
		[ -d /var/lib/misc/userkeys ] || sudo mkdir -p /var/lib/misc/userkeys
		cat authorized_keys/${user}@*.pub | sudo tee /var/lib/misc/userkeys/${user} > /dev/null
149
	fi
150
151
done

152
153
154
155
156
sudo mkdir -p /srv/workspace
[ -d /srv/schroots ] || sudo mkdir -p /srv/schroots
[ -h /chroots ] || sudo ln -s /srv/workspace/chroots /chroots
[ -h /schroots ] || sudo ln -s /srv/schroots /schroots

Mattia Rizzolo's avatar
Mattia Rizzolo committed
157
if [ "$HOSTNAME" = "jenkins-test-vm" ] || [ "$HOSTNAME" = "profitbricks-build7-amd64" ] ; then
158
	# jenkins needs access to libvirt
159
	sudo adduser jenkins kvm
160
161
	sudo adduser jenkins libvirt
	sudo adduser jenkins libvirt-qemu
162
fi
163

164
165
# prepare tmpfs on some hosts
case $HOSTNAME in
Mattia Rizzolo's avatar
Mattia Rizzolo committed
166
167
168
169
	jenkins)
		TMPFSSIZE=100
		TMPSIZE=15
		;;
Holger Levsen's avatar
Holger Levsen committed
170
	profitbricks-build9-amd64)
171
		TMPFSSIZE=40
Mattia Rizzolo's avatar
Mattia Rizzolo committed
172
173
		TMPSIZE=8
		;;
174
175
176
177
	profitbricks-build3-amd64|profitbricks-build4-amd64)
		TMPFSSIZE=200
		TMPSIZE=30
		;;
Holger Levsen's avatar
Holger Levsen committed
178
	profitbricks-build*)
Mattia Rizzolo's avatar
Mattia Rizzolo committed
179
180
181
		TMPFSSIZE=200
		TMPSIZE=15
		;;
182
183
184
185
	codethink*)
		TMPFSSIZE=100
		TMPSIZE=15
		;;
186
187
188
	*) ;;
esac
case $HOSTNAME in
189
190
191
192
193
194
	profitbricks-build*i386)
		if ! grep -q '/srv/workspace' /etc/fstab; then
			echo "Warning: you need to manually create a /srv/workspace partition on i386 nodes, exiting."
			exit 1
		fi
		;;
195
	jenkins|profitbricks-build*amd64|codethink*)
196
		if ! grep -q '^tmpfs\s\+/srv/workspace\s' /etc/fstab; then
197
			echo "tmpfs		/srv/workspace	tmpfs	defaults,size=${TMPFSSIZE}g	0	0" | sudo tee -a /etc/fstab >/dev/null  
198
		fi
Mattia Rizzolo's avatar
Mattia Rizzolo committed
199
200
201
		if ! grep -q '^tmpfs\s\+/tmp\s' /etc/fstab; then
			echo "tmpfs		/tmp	tmpfs	defaults,size=${TMPSIZE}g	0	0" | sudo tee -a /etc/fstab >/dev/null
		fi
202
203
		if ! mountpoint -q /srv/workspace; then
			if test -z "$(ls -A /srv/workspace)"; then
204
				sudo mount /srv/workspace
205
			else
206
				explain "WARNING: mountpoint /srv/workspace is non-empty."
207
208
209
210
211
			fi
		fi
		;;
	*) ;;
esac
212

213
# make sure needed directories exists - some directories will not be needed on all hosts...
214
for directory in /schroots /srv/reproducible-results /srv/d-i /srv/udebs /srv/live-build /var/log/jenkins/ /srv/jenkins /srv/jenkins/pseudo-hosts /srv/workspace/chroots ; do
215
216
217
	if [ ! -d $directory ] ; then
		sudo mkdir $directory
	fi
Holger Levsen's avatar
Holger Levsen committed
218
	sudo chown jenkins.jenkins $directory
219
done
Holger Levsen's avatar
Holger Levsen committed
220
for directory in /srv/jenkins ; do
221
222
223
224
225
	if [ ! -d $directory ] ; then
		sudo mkdir $directory
		sudo chown jenkins-adm.jenkins-adm $directory
	fi
done
226

227
if ! test -h /chroots; then
Holger Levsen's avatar
Holger Levsen committed
228
	sudo rmdir /chroots || sudo rm -f /chroots # do not recurse
229
	if test -e /chroots; then
Holger Levsen's avatar
Holger Levsen committed
230
		explain "/chroots could not be cleared."
231
	else
232
		sudo ln -s /srv/workspace/chroots /chroots
233
234
235
	fi
fi

236
237
238
239
240
# only on Debian systems
if [ -f /etc/debian_version ] ; then
	#
	# install packages we need
	#
241
	if [ $BASEDIR/$0 -nt $STAMP ] || [ ! -f $STAMP ] ; then
Holger Levsen's avatar
Holger Levsen committed
242
243
		DEBS=" 
			bash-completion 
244
245
			bc
			bsd-mailx
246
247
			curl
			debian-archive-keyring
248
			debootstrap 
249
250
			devscripts
			eatmydata
251
			etckeeper
252
			figlet
Holger Levsen's avatar
Holger Levsen committed
253
			git
254
			haveged
Holger Levsen's avatar
Holger Levsen committed
255
			htop
256
			less
257
			locales-all
Mattia Rizzolo's avatar
Mattia Rizzolo committed
258
			lsof
259
260
			molly-guard
			moreutils
261
262
263
			munin-node/stretch-backports
			munin-plugins-core/stretch-backports
			munin-plugins-extra/stretch-backports
264
			netcat-traditional
265
266
			ntp
			ntpdate
267
			pbuilder/stretch-backports
268
			pigz 
269
			postfix
270
			procmail
271
			psmisc
272
			python3-psycopg2 
Holger Levsen's avatar
Holger Levsen committed
273
			schroot 
274
275
			screen
			slay
276
			stunnel
Holger Levsen's avatar
Holger Levsen committed
277
278
			subversion 
			subversion-tools 
279
			systemd-sysv
Holger Levsen's avatar
Holger Levsen committed
280
281
282
			sudo 
			unzip 
			vim 
283
			zsh
284
			"
285
		# install squid on a few nodes only
286
		case $HOSTNAME in
287
			profitbricks-build1-a*|profitbricks-build10*|profitbricks-build7*|codethink-sled16*) DEBS="$DEBS
288
289
290
291
292
293
				squid" ;;
			*) ;;
		esac
		# notifications are only done from a view nodes
		case $HOSTNAME in
			jenkins|jenkins-test-vm|profitbricks-build*) DEBS="$DEBS
294
295
				kgb-client
				python3-yaml" ;;
296
297
			*) ;;
		esac
298
299
300
		# install debootstrap from stretch-backports on ubuntu nodes as since 20180927 debootstrap 1.0.78+nmu1ubuntu1.6 cannot install sid anymore
		case $HOSTNAME in
			codethink*) DEBS="$DEBS
301
				debootstrap/stretch-backports" ;;
302
303
			*) 	;;
		esac
304
305
		# needed to run the 2nd reproducible builds nodes in the future...
		case $HOSTNAME in
306
			profitbricks-build4-amd64|profitbricks-build5-amd64|profitbricks-build6-i386|profitbricks-build15-amd64|profitbricks-build16-i386) DEBS="$DEBS ntpdate" ;;
307
			codethink-sled9*|codethink-sled11*|codethink-sled13*|codethink-sled15*) DEBS="$DEBS ntpdate" ;;
308
309
			*) ;;
		esac
310
		# needed to run coreboot/openwrt/netbsd/fedora jobs
311
		case $HOSTNAME in
312
		profitbricks-build3-amd64|profitbricks-build4-amd64) DEBS="$DEBS
Holger Levsen's avatar
Holger Levsen committed
313
				bison
314
				ca-certificates
Holger Levsen's avatar
Holger Levsen committed
315
316
317
				cmake
				diffutils
				findutils
318
				fish
Holger Levsen's avatar
Holger Levsen committed
319
320
321
322
323
324
325
326
327
328
329
330
331
				flex
				g++
				gawk
				gcc
				grep
				iasl
				libc6-dev
				libncurses5-dev
				libssl-dev
				locales-all
				kgb-client
				m4
				make
332
333
				python3-clint
				python3-git
334
				python3-pystache
335
				python3-requests
Holger Levsen's avatar
Holger Levsen committed
336
337
				python3-yaml
				subversion
338
				tree
Holger Levsen's avatar
Holger Levsen committed
339
340
341
				unzip
				util-linux
				zlib1g-dev"
342
343
344
			;;
			*) ;;
		esac
345
346
347
		# needed to run fdroid jobs
		case $HOSTNAME in
			profitbricks-build7-amd64) DEBS="$DEBS
348
				androguard/stretch-backports
349
				android-sdk
350
351
				bzr
				git-svn
352
				fdroidserver/stretch-backports
353
				libvirt-clients
354
355
				libvirt-daemon
				libvirt-daemon-system
356
				mercurial
357
358
				python3-babel
				python3-defusedxml
359
				python3-libvirt
360
				python3-mwclient/stretch-backports
361
362
				python3-qrcode
				python3-ruamel.yaml
363
				python3-setuptools
364
				python3-vagrant
365
				qemu-kvm
366
				subversion
367
368
369
				vagrant
				vagrant-mutate
				vagrant-libvirt"
370
			;;
371
			*) ;;
372
		esac
373
374
375
376
		if [ "$HOSTNAME" = "jenkins-test-vm" ] ; then
			# for phil only
			DEBS="$DEBS postfix-pcre"
		fi
377
		if [ "$HOSTNAME" = "jenkins" ] || [ "$HOSTNAME" = "jenkins-test-vm" ] ; then
Holger Levsen's avatar
Holger Levsen committed
378
379
380
381
			MASTERDEBS=" 
				apache2 
				apt-file 
				apt-listchanges 
382
				asciidoc
Holger Levsen's avatar
Holger Levsen committed
383
				binfmt-support 
384
385
				bison
				botch
Holger Levsen's avatar
Holger Levsen committed
386
387
388
389
390
391
392
				build-essential 
				cmake 
				cron-apt 
				csvtool 
				dnsmasq-base 
				dstat 
				figlet 
Holger Levsen's avatar
Holger Levsen committed
393
				flex
Holger Levsen's avatar
Holger Levsen committed
394
				gawk 
395
				ghc
396
				git-lfs
397
				git-notifier 
Holger Levsen's avatar
Holger Levsen committed
398
399
400
401
				gocr 
				graphviz 
				iasl 
				imagemagick 
402
403
				ip2host
				jekyll
404
				kgb-client
Holger Levsen's avatar
Holger Levsen committed
405
406
407
408
409
410
411
412
413
				libcap2-bin 
				libfile-touch-perl 
				libguestfs-tools 
				libjson-rpc-perl 
				libsoap-lite-perl 
				libxslt1-dev 
				moreutils 
				mr 
				mtr-tiny 
414
				munin/stretch-backports
Holger Levsen's avatar
Holger Levsen committed
415
				ntp 
416
				obfs4proxy
Holger Levsen's avatar
Holger Levsen committed
417
418
				openbios-ppc 
				openbios-sparc 
419
				openjdk-8-jre 
420
				pandoc
421
				postgresql
422
				postgresql-autodoc
423
				postgresql-client 
Holger Levsen's avatar
Holger Levsen committed
424
425
426
				poxml 
				procmail 
				python3-debian 
427
				python3-pystache
428
				python3-requests
429
430
431
				python3-sqlalchemy
				python3-xdg
				python3-yaml
Holger Levsen's avatar
Holger Levsen committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
				python-arpy 
				python-hachoir-metadata 
				python-imaging 
				python-lzma 
				python-pip 
				python-rpy2 
				python-setuptools 
				python-twisted 
				python-yaml 
				qemu 
				qemu-kvm 
				qemu-system-x86 
				qemu-user-static 
				radvd 
446
447
				ruby-rspec
				rustc
Holger Levsen's avatar
Holger Levsen committed
448
449
450
451
				seabios 
				shorewall 
				shorewall6 
				sqlite3 
452
453
				syslinux
				systemd/stretch-backports
454
				thin-provisioning-tools
455
				tor
Holger Levsen's avatar
Holger Levsen committed
456
				vncsnapshot 
457
458
				vnstat
				whohas
Holger Levsen's avatar
Holger Levsen committed
459
				x11-apps 
460
				xtightvncviewer
461
				xvfb
462
				xvkbd
463
				zutils"
464
465
466
		else
			MASTERDEBS=""
		fi
467
		$UP2DATE || sudo apt-get update
468
		$UP2DATE || sudo apt-get install $DEBS $MASTERDEBS
Edward Betts's avatar
Edward Betts committed
469
		# don't (re-)install pbuilder if it's on hold
470
		if [ "$(dpkg-query -W -f='${db:Status-Abbrev}\n' pbuilder)" != "hi " ] ; then
471
			$UP2DATE || sudo apt-get install pbuilder
472
		fi
473
		# remove unattended-upgrades if it's installed
474
		if [ "$(dpkg-query -W -f='${db:Status-Abbrev}\n' unattended-upgrades 2>/dev/null || true)" = "ii "  ] ; then
475
			 sudo apt-get -y purge unattended-upgrades
476
		fi
477
		# we need mock to build fedora
478
		if [ "$HOSTNAME" = "profitbricks-build3-amd64" ] || [ "$HOSTNAME" = "profitbricks-build4-amd64" ] || [ "$HOSTNAME" = "jenkins" ] ; then
479
			$UP2DATE || sudo apt-get install mock
480
		fi
481
		# for varying kernels:
482
		# - we use bpo kernels on pb-build5+15 (and the default amd64 kernel on pb-build6+16-i386)
483
484
485
		# - we also use the bpo kernel on pb-build4 (but not pb-build3)
		if [ "$HOSTNAME" = "profitbricks-build5-amd64" ] || [ "$HOSTNAME" = "profitbricks-build15-amd64" ] \
			|| [ "$HOSTNAME" = "profitbricks-build4-amd64" ] ; then
486
			$UP2DATE || sudo apt-get install -t stretch-backports linux-image-amd64
487
488
489
		elif [ "$HOSTNAME" = "profitbricks-build6-i386" ] || [ "$HOSTNAME" = "profitbricks-build16-i386" ] \
			|| [ "$HOSTNAME" = "profitbricks-build2-i386" ] || [ "$HOSTNAME" = "profitbricks-build12-i386" ] ; then
			# we dont vary the kernel on i386 atm, see #875990 + #876035
490
			$UP2DATE || sudo apt-get install linux-image-amd64:amd64
491
		fi
Holger Levsen's avatar
Holger Levsen committed
492
		# only needed on the main nodes
Holger Levsen's avatar
Holger Levsen committed
493
		if [ "$HOSTNAME" = "jenkins-test-vm" ] ; then
494
			$UP2DATE || sudo apt-get install jenkins-job-builder/stretch-backports
Holger Levsen's avatar
Holger Levsen committed
495
		elif [ "$HOSTNAME" = "jenkins" ] ; then
496
			$UP2DATE || sudo apt-get install ffmpeg libav-tools python3-popcon jenkins-job-builder/stretch-backports dose-extra
497
		fi
498
		sudo apt-get clean
Holger Levsen's avatar
Holger Levsen committed
499
		explain "packages installed."
500
	else
Holger Levsen's avatar
Holger Levsen committed
501
		explain "no new packages to be installed."
502
	fi
503
fi
504

505
#
Holger Levsen's avatar
Holger Levsen committed
506
# deploy package configuration in /etc and /usr
507
#
Holger Levsen's avatar
Holger Levsen committed
508
cd $BASEDIR
509
510
511
512
513
514
515
516
517
for h in common common-amd64 common-i386 common-arm64 common-armhf "$HOSTNAME" ; do
	# $HOSTNAME has precedence over common-$DPKG_ARCH over common
	case $h in
		common-amd64) [ $DPKG_ARCH = "amd64" ] || continue ;;
		common-i386)  [ $DPKG_ARCH = "i386" ] || continue ;;
		common-arm64) [ $DPKG_ARCH = "arm64" ] || continue ;;
		common-armhf) [ $DPKG_ARCH = "armhf" ] || continue ;;
		*) ;;
	esac
518
519
	if [ -d "hosts/$h/etc/sudoers.d/" ]; then
		for f in "hosts/$h/etc/sudoers.d/"* ; do
520
			/usr/sbin/visudo -c -f "$f" > /dev/null
521
522
523
		done
	fi
	for d in etc usr ; do
Mattia Rizzolo's avatar
Mattia Rizzolo committed
524
		if [ -d "hosts/$h/$d" ]; then
525
			sudo cp --preserve=mode,timestamps -r "hosts/$h/$d/"* "/$d"
526
527
528
		fi
	done
done
529
530
# we ship one or two service files…
sudo systemctl daemon-reload &
531
532
533
534
535

#
# more configuration than a simple cp can do
#
sudo chown root.root /etc/sudoers.d/jenkins ; sudo chmod 700 /etc/sudoers.d/jenkins
536
sudo chown root.root /etc/sudoers.d/jenkins-adm ; sudo chmod 700 /etc/sudoers.d/jenkins-adm
Holger Levsen's avatar
Holger Levsen committed
537
[ -f /etc/mailname ] || ( echo $HOSTNAME.debian.net | sudo tee /etc/mailname )
538
539

if [ "$HOSTNAME" = "jenkins" ] ; then
540
	if ! $UP2DATE || [ $BASEDIR/hosts/$HOSTNAME/etc/apache2 -nt $STAMP ]  ; then
541
542
543
544
545
546
547
548
549
550
551
		if [ ! -e /etc/apache2/mods-enabled/proxy.load ] ; then
			sudo a2enmod proxy
			sudo a2enmod proxy_http
			sudo a2enmod rewrite
			sudo a2enmod ssl
			sudo a2enmod headers
			sudo a2enmod macro
			sudo a2enmod filter
		fi
		sudo a2ensite -q jenkins.debian.net
		sudo a2enconf -q munin
552
		sudo chown jenkins-adm.jenkins-adm /etc/apache2/sites-enabled/jenkins.debian.net.conf
553
554
555
		# for reproducible.d.n url rewriting:
		[ -L /var/www/userContent ] || sudo ln -sf /var/lib/jenkins/userContent /var/www/userContent
		sudo service apache2 reload
556
	fi
557
558
559
560
561
562
	if ! grep controlmaster ~jenkins/.ssh/config || ! grep controlpath ~jenkins/.ssh/config ; then
		echo
		echo "Please define controlmaster and controlpath in ~jenkins/.ssh/config manually, see https://debian-administration.org/article/290/Reusing_existing_OpenSSH_v4_connections"
		echo
		exit 1
	fi
563
564
fi

565
if ! $UP2DATE || [ $BASEDIR/hosts/$HOSTNAME/etc/munin -nt $STAMP ] ; then
566
	cd /etc/munin/plugins
Holger Levsen's avatar
Holger Levsen committed
567
	sudo rm -f postfix_* open_inodes interrupts irqstats threads proc_pri vmstat if_err_* exim_* netstat fw_forwarded_local fw_packets forks open_files users nfs* iostat_ios ntp* 2>/dev/null
568
	case $HOSTNAME in
569
			profitbricks-build7*|profitbricks-build1-a*|profitbricks-build10*|codethink-sled16*) [ -L /etc/munin/plugins/squid_cache ] || for i in squid_cache squid_objectsize squid_requests squid_traffic ; do sudo ln -s /usr/share/munin/plugins/$i $i ; done ;;
570
571
			*)	;;
	esac
Holger Levsen's avatar
Holger Levsen committed
572
573
574
575
	case $HOSTNAME in
			jenkins) [ -L /etc/munin/plugins/postfix_mailstats ] || for i in postfix_mailstats postfix_mailvolume postfix_mailqueue ; do sudo ln -s /usr/share/munin/plugins/$i $i ; done ;;
			*)	;;
	esac
576
577
578
	if [ "$HOSTNAME" != "jenkins" ] && [ -L /etc/munin/plugins/iostat ] ; then
		sudo rm /etc/munin/plugins/iostat
	fi
579
580
	if [ "$HOSTNAME" = "jenkins" ] && [ ! -L /etc/munin/plugins/apache_accesses ] ; then
		for i in apache_accesses apache_volume ; do sudo ln -s /usr/share/munin/plugins/$i $i ; done
581
		sudo ln -s /usr/share/munin/plugins/loggrep jenkins_oom
582
	fi
583
	sudo service munin-node restart
584
fi
Holger Levsen's avatar
Holger Levsen committed
585
explain "packages configured."
Holger Levsen's avatar
Holger Levsen committed
586
587
588
589

#
# install the heart of jenkins.debian.net
#
Holger Levsen's avatar
Holger Levsen committed
590
cd $BASEDIR
591
[ -d /srv/jenkins/features ] && sudo rm -rf /srv/jenkins/features
592
# check for bash syntax *before* actually deploying anything
593
shopt -s nullglob
594
for f in bin/*.sh bin/**/*.sh ; do bash -n "$f" ; done
595
shopt -u nullglob
596
for dir in bin logparse live mustache-templates ; do
597
598
	sudo mkdir -p /srv/jenkins/$dir
	sudo rsync -rpt --delete $dir/ /srv/jenkins/$dir/
Holger Levsen's avatar
Holger Levsen committed
599
	sudo chown -R jenkins-adm.jenkins-adm /srv/jenkins/$dir
600
done
601
602
HOST_JOBS="hosts/$HOSTNAME/job-cfg"
if [ -e "$HOST_JOBS" ] ; then
603
	sudo -u jenkins-adm rsync -rpt --copy-links --delete "$HOST_JOBS/" /srv/jenkins/job-cfg/
604
else
Philip Hands's avatar
Philip Hands committed
605
606
	# tidying up ... assuming that we don't want clutter on peripheral servers
	[ -d /srv/jenkins/job-cfg ] && sudo rm -rf /srv/jenkins/job-cfg
607
608
fi

609

610
sudo mkdir -p -m 700 /var/lib/jenkins/.ssh
611
if [ "$HOSTNAME" = "jenkins" ] ; then
612
613
	sudo -u jenkins install -m 600 jenkins-home/authorized_keys /var/lib/jenkins/.ssh/authorized_keys
	sudo -u jenkins cp jenkins-home/procmailrc /var/lib/jenkins/.procmailrc
614
	sudo -u jenkins cp jenkins-home/offline_nodes /var/lib/jenkins/offline_nodes
615
616
617
else
	sudo cp jenkins-nodes-home/authorized_keys /var/lib/jenkins/.ssh/authorized_keys
fi
Holger Levsen's avatar
Holger Levsen committed
618
explain "scripts and configurations for jenkins updated."
619

620
if [ "$HOSTNAME" = "jenkins" ] ; then
Holger Levsen's avatar
Holger Levsen committed
621
	sudo cp -pr README INSTALL TODO CONTRIBUTING d-i-preseed-cfgs /var/lib/jenkins/userContent/
622
	TMPFILE=$(mktemp)
623
	git log | grep ^Author| cut -d " " -f2-|sort -u -f > $TMPFILE
624
	echo "----" >> $TMPFILE
625
	sudo tee /var/lib/jenkins/userContent/THANKS > /dev/null < THANKS.head
Edward Betts's avatar
Edward Betts committed
626
	# samuel, lunar, josch and phil committed with several committers, only display one
Holger Levsen's avatar
Holger Levsen committed
627
	grep -v -e "samuel.thibault@ens-lyon.org" -e Lunar -e "j.schauer@email.de" -e "mattia@mapreri.org" -e "phil@jenkins-test-vm" $TMPFILE | sudo tee -a /var/lib/jenkins/userContent/THANKS > /dev/null
628
	rm $TMPFILE
629
	TMPDIR=$(mktemp -d -t update-jdn-XXXXXXXX)
630
	sudo cp -pr userContent $TMPDIR/
Holger Levsen's avatar
Holger Levsen committed
631
632
633
	sudo chown -R jenkins.jenkins $TMPDIR
	sudo cp -pr $TMPDIR/userContent  /var/lib/jenkins/
	sudo rm -r $TMPDIR > /dev/null
634
635
636
637
638
639
640
641
642
	cd /var/lib/jenkins/userContent/
	ASCIIDOC_PARAMS="-a numbered -a data-uri -a iconsdir=/etc/asciidoc/images/icons -a scriptsdir=/etc/asciidoc/javascripts -b html5 -a toc -a toclevels=4 -a icons -a stylesheet=$(pwd)/theme/debian-asciidoc.css"
	[ about.html -nt README ] || asciidoc $ASCIIDOC_PARAMS -o about.html README
	[ todo.html -nt TODO ] || asciidoc $ASCIIDOC_PARAMS -o todo.html TODO
	[ setup.html -nt INSTALL ] || asciidoc $ASCIIDOC_PARAMS -o setup.html INSTALL
	[ contributing.html -nt CONTRIBUTING ] || asciidoc $ASCIIDOC_PARAMS -o contributing.html CONTRIBUTING
	diff THANKS .THANKS >/dev/null || asciidoc $ASCIIDOC_PARAMS -o thanks.html THANKS
	mv THANKS .THANKS
	rm TODO README INSTALL CONTRIBUTING
643
	sudo chown jenkins.jenkins /var/lib/jenkins/userContent/*html
Holger Levsen's avatar
Holger Levsen committed
644
	explain "user content for jenkins updated."
645
fi
646

647
if [ "$HOSTNAME" = "jenkins" ] || [ "$HOSTNAME" = "jenkins-test-vm" ] ; then
648
649
650
651
652
653
	#
	# run jenkins-job-builder to update jobs if needed
	#     (using sudo because /etc/jenkins_jobs is root:root 700)
	#
	cd /srv/jenkins/job-cfg
	for metaconfig in *.yaml.py ; do
Holger Levsen's avatar
Holger Levsen committed
654
655
656
657
658
659
		if [ -f $metaconfig ] ; then
			TMPFILE=$(sudo -u jenkins-adm mktemp)
			./$metaconfig | sudo -u jenkins-adm tee "$TMPFILE" >/dev/null
			if ! sudo -u jenkins-adm cmp -s ${metaconfig%.py} "$TMPFILE" ; then
				sudo -u jenkins-adm mv "$TMPFILE" "${metaconfig%.py}"
			fi
660
661
662
		fi
	done
	for config in *.yaml ; do
663
664
665
666
667
668
669
		# do update, if
		# no stamp file exist or
		# no .py file exists and config is newer than stamp or
		# a .py file exists and .py file is newer than stamp
		if [ ! -f $STAMP ] || \
		 ( [ ! -f $config.py ] && [ $config -nt $STAMP ] ) || \
		 ( [ -f $config.py ] && [ $config.py -nt $STAMP ] ) ; then
670
			echo "$config has changed, executing updates."
671
			$JJB update $config
672
673
		fi
	done
Holger Levsen's avatar
Holger Levsen committed
674
	explain "jenkins jobs updated."
675
fi
676

677
#
Holger Levsen's avatar
Holger Levsen committed
678
# configure git for jenkins
679
#
Holger Levsen's avatar
Holger Levsen committed
680
681
682
683
if [ "$(sudo su - jenkins -c 'git config --get user.email')" != "jenkins@jenkins.debian.net" ] ; then
	sudo su - jenkins -c "git config --global user.email jenkins@jenkins.debian.net"
	sudo su - jenkins -c "git config --global user.name Jenkins"
fi
Holger Levsen's avatar
Holger Levsen committed
684

685
686
687
#
# generate the kgb-client configurations
#
688
if [ "$HOSTNAME" = "jenkins" ] || [ "$HOSTNAME" = "profitbricks-build3-amd64" ] || [ "$HOSTNAME" = "profitbricks-build4-amd64" ] || [ "$HOSTNAME" = "profitbricks-build7-amd64" ] || [ "$HOSTNAME" = "profitbricks-build2-i386" ] || [ "$HOSTNAME" = "profitbricks-build12-i386" ] ; then
689
690
691
	cd $BASEDIR
	KGB_SECRETS="/srv/jenkins/kgb/secrets.yml"
	if [ -f "$KGB_SECRETS" ] && [ $(stat -c "%a:%U:%G" "$KGB_SECRETS") = "640:jenkins-adm:jenkins-adm" ] ; then
Philip Hands's avatar
Philip Hands committed
692
		# the last condition is to assure the files are owned by the right user/team
693
		if [ "$KGB_SECRETS" -nt $STAMP ] || [ "deploy_kgb.py" -nt "$STAMP" ] || [ ! -f $STAMP ] ; then
Philip Hands's avatar
Philip Hands committed
694
695
696
697
			sudo -u jenkins-adm "./deploy_kgb.py"
		else
			explain "kgb-client configuration unchanged, nothing to do."
		fi
698
	else
Philip Hands's avatar
Philip Hands committed
699
700
701
		figlet -f banner Warning
		echo "Warning: $KGB_SECRETS either does not exist or has bad permissions. Please fix. KGB configs not generated"
		echo "We expect the secrets file to be mode 640 and owned by jenkins-adm:jenkins-adm."
Holger Levsen's avatar
Holger Levsen committed
702
703
		echo "/srv/jenkins/kgb should be mode 755 and owned by jenkins-adm:root."
		echo "/srv/jenkins/kgb/client-status should be mode 755 and owned by jenkins:jenkins."
704
	fi
705
706
fi

707
708
709
#
# Create GPG key for jenkins user if they do not already exist (eg. to sign .buildinfo files)
#
710
if sudo -H -u jenkins gpg --with-colons --fixed-list-mode --list-secret-keys | cut -d: -f1 | grep -qsFx 'sec' >/dev/null 2>&1 ; then
Holger Levsen's avatar
Holger Levsen committed
711
	explain "$(date) - Not generating GPG key as one already exists for jenkins user."
712
else
Holger Levsen's avatar
Holger Levsen committed
713
	explain "$(date) - Generating GPG key for jenkins user."
714

715
	sudo -H -u jenkins gpg --no-tty --batch --gen-key <<EOF
716
717
718
Key-Type: RSA
Key-Length: 4096
Key-Usage: sign
719
Name-Real: $HOSTNAME
720
721
722
723
724
725
Name-Comment: Automatically generated key for signing .buildinfo files
Expire-Date: 0
%no-ask-passphrase
%no-protection
%commit
EOF
726

727
	GPG_KEY_ID="$(sudo -H -u jenkins gpg --with-colons --fixed-list-mode --list-secret-keys | grep '^sec' | cut -d: -f5 | tail -n1)"
728
729
730
731
732
733

	if [ "$GPG_KEY_ID" = "" ]
	then
		explain "$(date) - Generated GPG key but could not parse key ID"
	else
		explain "$(date) - Generated GPG key $GPG_KEY_ID - submitting to keyserver"
734
		sudo -H -u jenkins gpg --send-keys $GPG_KEY_ID
735
	fi
736
737
fi

Holger Levsen's avatar
Holger Levsen committed
738
739
740
741
742
#
# There's always some work left...
#	echo FIXME is ignored so check-jobs scripts can output templates requiring manual work
#
if [ "$HOSTNAME" = "jenkins" ] || [ "$HOSTNAME" = "jenkins-test-vm" ] ; then
743
	TMPFILE=$(mktemp)
Holger Levsen's avatar
Holger Levsen committed
744
745
746
	rgrep FI[X]ME $BASEDIR/* | grep -v echo > $TMPFILE || true
	if [ -s $TMPFILE ] ; then
		echo
747
		cat $TMPFILE
Holger Levsen's avatar
Holger Levsen committed
748
749
		echo
	fi
750
	rm -f $TMPFILE
Holger Levsen's avatar
Holger Levsen committed
751
752
fi

753
#
754
# almost finally…
755
#
756
sudo touch $STAMP	# so on the next run, only configs newer than this file will be updated
Holger Levsen's avatar
Holger Levsen committed
757
explain "$(date) - finished deployment."
758

759
# finally!
760
case $HOSTNAME in
761
	# set time back to the future
762
	profitbricks-build4-amd64|profitbricks-build5-amd64|profitbricks-build6-i386|profitbricks-build15-amd64|profitbricks-build16-i386)
763
		disable_dsa_check_packages
764
765
		sudo date --set="+398 days +6 hours + 23 minutes"
		;;
766
	codethink-sled9*|codethink-sled11*|codethink-sled13*|codethink-sled15*)
767
		disable_dsa_check_packages
768
769
		sudo date --set="+398 days +6 hours + 23 minutes"
		;;
770
	jenkins)
771
		# notify irc on updates of jenkins.d.n
772
773
		MESSAGE="jenkins.d.n updated to $(cd $BASEDIR ; git describe --always)."
		kgb-client --conf /srv/jenkins/kgb/debian-qa.conf --relay-msg "$MESSAGE"
Holger Levsen's avatar
Holger Levsen committed
774
		;;
775
776
	*)	;;
esac
777
778
779
780

echo
figlet ok
echo
Holger Levsen's avatar
Holger Levsen committed
781
782
echo "__$HOSTNAME=ok__"