update_jdn.sh 13.6 KB
Newer Older
1
2
#!/bin/bash

Philip Hands's avatar
set -e    
Philip Hands committed
3
4
set -e

5
# Copyright 2012-2015 Holger Levsen <holger@layer-acht.org>
6
7
# released under the GPLv=2

Holger Levsen's avatar
Holger Levsen committed
8
BASEDIR=$HOME/jenkins.debian.net
9
10
PVNAME=/dev/vdb      # LVM physical volume for jobs
VGNAME=jenkins01     # LVM volume group
11
STAMP=/var/log/jenkins/update-jenkins.stamp
12
TMPFILE=$(mktemp)
13
JJB=jenkins-job-builder
Holger Levsen's avatar
Holger Levsen committed
14

Holger Levsen's avatar
Holger Levsen committed
15
explain() {
Holger Levsen's avatar
Holger Levsen committed
16
	echo "$HOSTNAME: $1"
Holger Levsen's avatar
Holger Levsen committed
17
18
}

19
echo "--------------------------------------------"
Holger Levsen's avatar
Holger Levsen committed
20
explain "$(date) - begin deployment update."
21
22
23
24
25
26
27
28
29
30

# run update at current date
case $HOSTNAME in
	profitbricks-build5-amd64|profitbricks-build6-amd64)
		# set correct date
		sudo ntpdate -b de.pool.ntp.org
		;;
	*)	;;
esac

31
32
33
#
# set up users and groups
#
34
if ! getent passwd jenkins > /dev/null ; then
35
	sudo addgroup --system jenkins
36
	sudo adduser --system --shell /bin/bash --home /var/lib/jenkins --ingroup jenkins --disabled-login jenkins
37
fi
Holger Levsen's avatar
Holger Levsen committed
38
if ! getent group jenkins-adm > /dev/null ; then
39
40
	sudo addgroup --system jenkins-adm
fi
41
if ! getent passwd jenkins-adm > /dev/null  ; then
42
	sudo adduser --system --shell /bin/bash --no-create-home --ingroup jenkins-adm --disabled-login --no-create-home jenkins-adm
Holger Levsen's avatar
Holger Levsen committed
43
	sudo usermod -G jenkins jenkins-adm
44
fi
Holger Levsen's avatar
Holger Levsen committed
45
for user in helmut holger mattia lunar philh ; do
46
47
48
49
	if [ "$user" = "lunar" ] && [ "$HOSTNAME" != "jenkins" ] ; then
		# lunar only wants to configure jekyll
		continue
	fi
Holger Levsen's avatar
Holger Levsen committed
50
51
52
53
	if [ "$user" = "philh" ] && [ "$HOSTNAME" != "jenkins-test-vm" ] ; then
		# philh only wants to test stuff
		continue
	fi
54
	if ! getent passwd $user > /dev/null ; then
55
		if [ "$user" = "mattia" ] ; then
56
			usershell=/bin/zsh
57
		else
58
			usershell=/bin/bash
59
		fi
60
		sudo adduser --gecos "" --shell "$usershell" --disabled-password $user
Holger Levsen's avatar
Holger Levsen committed
61
		if [ "$user" = "holger" ] ; then
62
			sudo usermod -G jenkins,jenkins-adm,sudo,adm $user
63
		elif [ "$user" != "lunar" ] ; then
64
65
			sudo usermod -G jenkins,jenkins-adm $user
		fi
66
67
68
	fi
done

69
sudo mkdir -p /srv/workspace
70
[ -d /srv/schroots ] || sudo mkdir -p /srv/schroots
71
[ -h /chroots ] || sudo ln -s /srv/workspace/chroots /chroots
72
[ -h /schroots ] || sudo ln -s /srv/schroots /schroots
73

74
75
# prepare tmpfs on some hosts
case $HOSTNAME in
Mattia Rizzolo's avatar
Mattia Rizzolo committed
76
77
78
79
80
81
82
83
84
85
86
87
	jenkins)
		TMPFSSIZE=100
		TMPSIZE=15
		;;
	profitbricks-build4-amd64)
		TMPFSSIZE=32
		TMPSIZE=8
		;;
	profitbricks-build?-amd64)
		TMPFSSIZE=200
		TMPSIZE=15
		;;
88
89
90
91
92
	*) ;;
esac
case $HOSTNAME in
	jenkins|profitbricks-build?-amd64)
		if ! grep -q '^tmpfs\s\+/srv/workspace\s' /etc/fstab; then
93
			echo "tmpfs		/srv/workspace	tmpfs	defaults,size=${TMPFSSIZE}g	0	0" | sudo tee -a /etc/fstab >/dev/null  
94
		fi
Mattia Rizzolo's avatar
Mattia Rizzolo committed
95
96
97
		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
98
99
		if ! mountpoint -q /srv/workspace; then
			if test -z "$(ls -A /srv/workspace)"; then
100
				sudo mount /srv/workspace
101
			else
102
				explain "WARNING: mountpoint /srv/workspace is non-empty."
103
104
105
106
107
			fi
		fi
		;;
	*) ;;
esac
108

109
# make sure needed directories exists - some directories will not be needed on all hosts...
110
for directory in /schroots /srv/reproducible-results /srv/d-i /srv/live-build /var/log/jenkins/ /srv/jenkins /srv/jenkins/pseudo-hosts /srv/workspace/chroots ; do
111
112
113
	if [ ! -d $directory ] ; then
		sudo mkdir $directory
	fi
Holger Levsen's avatar
Holger Levsen committed
114
	sudo chown jenkins.jenkins $directory
115
done
Holger Levsen's avatar
Holger Levsen committed
116
for directory in /srv/jenkins ; do
117
118
119
120
121
	if [ ! -d $directory ] ; then
		sudo mkdir $directory
		sudo chown jenkins-adm.jenkins-adm $directory
	fi
done
122

123
if ! test -h /chroots; then
Holger Levsen's avatar
Holger Levsen committed
124
	sudo rmdir /chroots || sudo rm -f /chroots # do not recurse
125
	if test -e /chroots; then
Holger Levsen's avatar
Holger Levsen committed
126
		explain "/chroots could not be cleared."
127
	else
128
		sudo ln -s /srv/workspace/chroots /chroots
129
130
131
	fi
fi

132
133
134
135
136
137
# only on Debian systems
if [ -f /etc/debian_version ] ; then
	#
	# install packages we need
	#
	if [ ./$0 -nt $STAMP ] || [ ! -f $STAMP ] ; then
Holger Levsen's avatar
Holger Levsen committed
138
139
		DEBS=" 
			bash-completion 
140
141
			bc
			bsd-mailx
Holger Levsen's avatar
Holger Levsen committed
142
143
144
			curl 
			debootstrap 
			devscripts 
145
			etckeeper
146
			figlet
Holger Levsen's avatar
Holger Levsen committed
147
			git
148
			haveged
Holger Levsen's avatar
Holger Levsen committed
149
			htop
150
			less
151
152
			molly-guard
			moreutils
153
154
			munin-node
			munin-plugins-extra 
155
			pigz 
156
			postfix
157
			procmail
158
			psmisc
159
			python3-psycopg2 
Holger Levsen's avatar
Holger Levsen committed
160
			schroot 
161
162
			screen
			slay
Holger Levsen's avatar
Holger Levsen committed
163
164
165
166
167
			subversion 
			subversion-tools 
			sudo 
			unzip 
			vim 
168
			zsh
169
			"
170
171
172
173
		case $HOSTNAME in
			jenkins|profitbricks-build?-amd64) DEBS="$DEBS squid3" ;;
			*) ;;
		esac
174
175
		# needed to run the 2nd reproducible builds nodes in the future...
		case $HOSTNAME in
176
			bpi0|hb0|profitbricks-build5-amd64|profitbricks-build6-amd64) DEBS="$DEBS ntpdate" ;;
177
178
			*) ;;
		esac
179
		# needed to run coreboot/openwrt/netbsd/fedora jobs
180
		case $HOSTNAME in
Holger Levsen's avatar
Holger Levsen committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
			profitbricks-build3-amd64) DEBS="$DEBS
				bison
				cmake
				diffutils
				findutils
				flex
				g++
				gawk
				gcc
				git
				grep
				iasl
				libc6-dev
				libncurses5-dev
				libssl-dev
				locales-all
				kgb-client
				m4
				make
200
				mock
Holger Levsen's avatar
Holger Levsen committed
201
202
				python3-yaml
				subversion
203
				sysvinit-core
204
				tree
Holger Levsen's avatar
Holger Levsen committed
205
206
207
208
				unzip
				util-linux
				zlib1g-dev"
			   ;;
209
210
			*) ;;
		esac
211
		if [ "$HOSTNAME" = "jenkins" ] ; then
Holger Levsen's avatar
Holger Levsen committed
212
213
214
215
			MASTERDEBS=" 
				apache2 
				apt-file 
				apt-listchanges 
216
				asciidoc
Holger Levsen's avatar
Holger Levsen committed
217
218
219
220
221
222
223
224
225
226
227
228
				binfmt-support 
				bison 
				build-essential 
				calamaris 
				cmake 
				cron-apt 
				csvtool 
				cucumber 
				dnsmasq-base 
				dose-extra 
				dstat 
				figlet 
Holger Levsen's avatar
Holger Levsen committed
229
230
				flex
				ffmpeg
Holger Levsen's avatar
Holger Levsen committed
231
				gawk 
232
233
				ghc
				git-notifier 
Holger Levsen's avatar
Holger Levsen committed
234
235
236
237
				gocr 
				graphviz 
				iasl 
				imagemagick 
238
239
				ip2host
				jekyll
240
				kgb-client
Holger Levsen's avatar
Holger Levsen committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
				libapache2-mod-macro 
				libav-tools 
				libcap2-bin 
				libfile-touch-perl 
				libguestfs-tools 
				libjson-rpc-perl 
				libsikuli-script-java 
				libsoap-lite-perl 
				libvirt0 
				libvirt-bin 
				libvirt-dev 
				libvpx1 
				libxslt1-dev 
				linux-image-amd64 
				moreutils 
				mr 
				mtr-tiny 
				munin 
				ntp 
				openbios-ppc 
				openbios-sparc 
				openjdk-7-jre 
263
264
				ovmf
				pandoc
Holger Levsen's avatar
Holger Levsen committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
				postgresql-client-9.4 
				poxml 
				procmail 
				python3-debian 
				python3-yaml 
				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 
				ruby-json 
				ruby-libvirt 
				ruby-packetfu 
				ruby-rjb 
				ruby-rspec 
				seabios 
				shorewall 
				shorewall6 
				sqlite3 
				syslinux 
294
295
				tcpdump
				tor
Holger Levsen's avatar
Holger Levsen committed
296
297
298
299
300
301
				unclutter 
				virt-viewer 
				vncsnapshot 
				vnstat 
				x11-apps 
				x11vnc 
302
303
304
				xtightvncviewer
				xvkbd
				xvfb
305
				zutils"
306
307
308
		else
			MASTERDEBS=""
		fi
Holger Levsen's avatar
Holger Levsen committed
309
		sudo apt-get update
Holger Levsen's avatar
Holger Levsen committed
310
		sudo apt-get install $DEBS $MASTERDEBS
311
312
313
		sudo apt-get install -t jessie-backports \
				pbuilder
		#		botch
314
		# to vary kernels
315
		if [ "$HOSTNAME" = "profitbricks-build5-amd64" ] || [ "$HOSTNAME" = "profitbricks-build6-amd64" ]; then
316
317
			sudo apt-get install -t jessie-backports linux-image-amd64
		fi
Holger Levsen's avatar
Holger Levsen committed
318
		explain "packages installed."
319
	else
Holger Levsen's avatar
Holger Levsen committed
320
		explain "no new packages to be installed."
321
	fi
322
fi
323

324
325
326
327
328
#
# prepare mock to build rpms
#
if [ "$HOSTNAME" = "profitbricks-build3-amd64" ] ; then
	if ! grep mock /etc/group ; then
329
330
		sudo groupadd --system mock
		sudo usermod -a -G mock jenkins
331
332
333
	fi
fi

334
#
335
# deploy package configuration in /etc and /usr/local/
336
#
Holger Levsen's avatar
Holger Levsen committed
337
cd $BASEDIR
338
sudo cp --preserve=mode,timestamps -r hosts/$HOSTNAME/etc/* /etc
339
sudo cp --preserve=mode,timestamps -r hosts/$HOSTNAME/usr/* /usr/
340
341
342
343
344

#
# more configuration than a simple cp can do
#
sudo chown root.root /etc/sudoers.d/jenkins ; sudo chmod 700 /etc/sudoers.d/jenkins
345
sudo chown root.root /etc/sudoers.d/jenkins-adm ; sudo chmod 700 /etc/sudoers.d/jenkins-adm
346
347

if [ "$HOSTNAME" = "jenkins" ] ; then
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
	if [ $BASEDIR/hosts/$HOSTNAME/etc/apache2 -nt $STAMP ] || [ ! -f $STAMP ] ; then
		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
		sudo chown jenkins-adm.jenkins-adm /etc/apache2/sites-enabled/jenkins.conf
		# for reproducible.d.n url rewriting:
		[ -L /var/www/userContent ] || sudo ln -sf /var/lib/jenkins/userContent /var/www/userContent
		sudo service apache2 reload
364
365
366
	fi
fi

367
if [ $BASEDIR/hosts/$HOSTNAME/etc/munin -nt $STAMP ] || [ ! -f $STAMP ] ; then
368
	cd /etc/munin/plugins
369
	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 2>/dev/null
370
371
372
373
	case $HOSTNAME in
			jenkins|profitbricks-build?-amd64) [ -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 ;;
			*)	;;
	esac
374
375
	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
376
		sudo ln -s /usr/share/munin/plugins/loggrep jenkins_oom
377
378
	fi
	sudo service munin-node force-reload
379
fi
Holger Levsen's avatar
Holger Levsen committed
380
explain "packages configured."
Holger Levsen's avatar
Holger Levsen committed
381
382
383
384

#
# install the heart of jenkins.debian.net
#
Holger Levsen's avatar
Holger Levsen committed
385
cd $BASEDIR
386
for dir in bin logparse job-cfg features live ; do
387
	sudo cp --preserve=mode,timestamps -r $dir /srv/jenkins/
Holger Levsen's avatar
Holger Levsen committed
388
	sudo chown -R jenkins-adm.jenkins-adm /srv/jenkins/$dir
389
done
Holger Levsen's avatar
Holger Levsen committed
390
sudo mkdir -p /var/lib/jenkins/.ssh
391
392
393
394
395
396
if [ "$HOSTNAME" = "jenkins" ] ; then
	sudo cp jenkins-home/procmailrc /var/lib/jenkins/.procmailrc
	sudo cp jenkins-home/authorized_keys /var/lib/jenkins/.ssh/authorized_keys
else
	sudo cp jenkins-nodes-home/authorized_keys /var/lib/jenkins/.ssh/authorized_keys
fi
Holger Levsen's avatar
Holger Levsen committed
397
398
399
sudo chown -R jenkins:jenkins /var/lib/jenkins/.ssh
sudo chmod 700 /var/lib/jenkins/.ssh
sudo chmod 600 /var/lib/jenkins/.ssh/authorized_keys
Holger Levsen's avatar
Holger Levsen committed
400
explain "scripts and configurations for jenkins updated."
401

402
if [ "$HOSTNAME" = "jenkins" ] ; then
Holger Levsen's avatar
Holger Levsen committed
403
	sudo cp -pr README INSTALL TODO CONTRIBUTING d-i-preseed-cfgs /var/lib/jenkins/userContent/
404
405
	git log | grep ^Author| cut -d " " -f2-|sort -u > $TMPFILE
	echo "----" >> $TMPFILE
406
	sudo tee /var/lib/jenkins/userContent/THANKS > /dev/null < THANKS.head
407
	# samuel, lunar and josch committed with several commiters, only display one
408
	grep -v -e "samuel.thibault@ens-lyon.org" -e Lunar -e "j.schauer@email.de" -e "mattia@mapreri.org" $TMPFILE | sudo tee -a /var/lib/jenkins/userContent/THANKS > /dev/null
409
	rm $TMPFILE
Holger Levsen's avatar
Holger Levsen committed
410
	sudo cp -pr userContent /var/lib/jenkins/
411
412
413
414
415
416
417
418
419
	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
420
	sudo chown -R jenkins.jenkins /var/lib/jenkins/userContent
Holger Levsen's avatar
Holger Levsen committed
421
	explain "user content for jenkins updated."
422
fi
423

424
if [ "$HOSTNAME" = "jenkins" ] || [ "$HOSTNAME" = "jenkins-test-vm" ] ; then
425
426
427
428
429
430
431
432
	#
	# 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
	# there are both python2 and python3 scripts here
		./$metaconfig > $TMPFILE
433
434
		if ! sudo -u jenkins-adm cmp -s ${metaconfig%.py} - < $TMPFILE ; then
			sudo -u jenkins-adm tee ${metaconfig%.py} > /dev/null < $TMPFILE
435
436
		fi
	done
Holger Levsen's avatar
Holger Levsen committed
437
	rm -f $TMPFILE
438
439
	for config in *.yaml ; do
		if [ $config -nt $STAMP ] || [ ! -f $STAMP ] ; then
440
			$JJB update $config
441
442
443
444
		else
			echo "$config has not changed, nothing to do."
		fi
	done
Holger Levsen's avatar
Holger Levsen committed
445
	explain "jenkins jobs updated."
446
fi
447

448
#
Holger Levsen's avatar
Holger Levsen committed
449
# configure git for jenkins
450
#
Holger Levsen's avatar
Holger Levsen committed
451
452
453
454
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
455

456
457
458
459
460
461
if [ "$HOSTNAME" = "jenkins" ] ; then
	#
	# creating LVM volume group for jobs
	#
	if [ "$PVNAME" = "" ]; then
	    figlet -f banner Error
Holger Levsen's avatar
Holger Levsen committed
462
	    explain "you must set \$PVNAME to physical volume pathname, exiting."
463
464
465
466
467
468
469
470
471
	    exit 1
	else
	    if ! sudo pvs $PVNAME >/dev/null 2>&1; then
	        sudo pvcreate $PVNAME
	    fi
	    if ! sudo vgs $VGNAME >/dev/null 2>&1; then
	        sudo vgcreate $VGNAME $PVNAME
	    fi
	fi
472
473
474
475
476
477
fi

#
# generate the kgb-client configurations
#
if [ "$HOSTNAME" = "jenkins" ] || [ "$HOSTNAME" = "profitbricks-build3-amd64" ] ; then
478
479
480
481
482
483
484
485
486
487
	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
	    # the last condition is to assure the files are owned by the right user/team
	    if [ "$KGB_SECRETS" -nt $STAMP ] || [ ! -f $STAMP ] ; then
	        sudo -u jenkins-adm "./deploy_kgb.py"
	    else
	        explain "kgb-client configuration unchanged, nothing to do."
	    fi
	else
Holger Levsen's avatar
Holger Levsen committed
488
	    figlet -f banner Warning
489
490
491
	    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."
	fi
492
493
fi

Holger Levsen's avatar
Holger Levsen committed
494
495
496
497
#
# There's always some work left...
#	echo FIXME is ignored so check-jobs scripts can output templates requiring manual work
#
Philip Hands's avatar
set -e    
Philip Hands committed
498
rgrep FI[X]ME $BASEDIR/* | grep -v echo > $TMPFILE || true
Holger Levsen's avatar
Holger Levsen committed
499
500
501
502
503
504
if [ -s $TMPFILE ] ; then
	echo
	cat $TMPFILE
	echo
fi
rm -f $TMPFILE
Holger Levsen's avatar
Holger Levsen committed
505

506
507
508
#
# finally
#
509
sudo touch $STAMP	# so on the next run, only configs newer than this file will be updated
Holger Levsen's avatar
Holger Levsen committed
510
explain "$(date) - finished deployment."
511

512
513
514
515
516
517
518
# set time back to the future
case $HOSTNAME in
	profitbricks-build5-amd64|profitbricks-build6-amd64)
		sudo date --set="+398 days +6 hours + 23 minutes"
		;;
	*)	;;
esac