Merge "Download/install JDK9"
[releng/builder.git] / packer / provision / baseline.sh
1 #!/bin/bash
2
3 # vim: ts=4 sw=4 sts=4 et tw=72 :
4
5 # force any errors to cause the script and job to end in failure
6 set -xeu -o pipefail
7
8 enable_service() {
9     # Enable services for Ubuntu instances
10     services=($@)
11
12     for service in "${services[@]}"; do
13         echo "---> Enable service: $service"
14         FACTER_OS=$(/usr/bin/facter operatingsystem | tr '[:upper:]' '[:lower:]')
15         FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
16         if [ "$FACTER_OS" == "centos" ]; then
17             systemctl enable "$service"
18             systemctl start "$service"
19             systemctl status "$service"
20         elif [ "$FACTER_OS" == "ubuntu" ]; then
21             case "$FACTER_OSVER" in
22                 14.04)
23                     service "$service" start
24                     service "$service" status
25                 ;;
26                 16.04)
27                     systemctl enable "$service"
28                     systemctl start "$service"
29                     systemctl status "$service"
30                 ;;
31                 *)
32                     echo "---> Unknown Ubuntu version $FACTER_OSVER"
33                     exit 1
34                 ;;
35             esac
36         else
37             echo "---> Unknown OS $FACTER_OS"
38             exit 1
39         fi
40     done
41 }
42
43 ensure_kernel_install() {
44     # Workaround for mkinitrd failing on occassion.
45     # On CentOS 7 it seems like the kernel install can fail it's mkinitrd
46     # run quietly, so we may not notice the failure. This script retries for a
47     # few times before giving up.
48     initramfs_ver=$(rpm -q kernel | tail -1 | sed "s/kernel-/initramfs-/")
49     grub_conf="/boot/grub/grub.conf"
50     # Public cloud does not use /boot/grub/grub.conf and uses grub2 instead.
51     if [ ! -e "$grub_conf" ]; then
52         echo "$grub_conf not found. Using Grub 2 conf instead."
53         grub_conf="/boot/grub2/grub.cfg"
54     fi
55
56     for i in $(seq 3); do
57         if grep "$initramfs_ver" "$grub_conf"; then
58             break
59         fi
60         echo "Kernel initrd missing. Retrying to install kernel..."
61         yum reinstall -y kernel
62     done
63     if ! grep "$initramfs_ver" "$grub_conf"; then
64         cat /boot/grub/grub.conf
65         echo "ERROR: Failed to install kernel."
66         exit 1
67     fi
68 }
69
70 ensure_ubuntu_install() {
71     # Workaround for mirrors occassionally failing to install a package.
72     # On Ubuntu sometimes the mirrors fail to install a package. This wrapper
73     # checks that a package is successfully installed before moving on.
74
75     packages=($@)
76
77     for pkg in "${packages[@]}"
78     do
79         # Retry installing package 5 times if necessary
80         for i in {0..5}
81         do
82
83             # Wait for any background apt processes to finish before running apt
84             while pgrep apt > /dev/null; do sleep 1; done
85
86             echo "$i: Installing $pkg"
87             if [ "$(dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
88                 apt-cache policy "$pkg"
89                 apt-get install "$pkg"
90                 continue
91             else
92                 echo "$pkg already installed."
93                 break
94             fi
95         done
96     done
97 }
98
99 rh_systems() {
100     # Handle the occurance where SELINUX is actually disabled
101     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
102     MODE=$(echo "$SELINUX" | cut -f 2 -d '=')
103     case "$MODE" in
104         permissive)
105             echo "************************************"
106             echo "** SYSTEM ENTERING ENFORCING MODE **"
107             echo "************************************"
108             # make sure that the filesystem is properly labelled.
109             # it could be not fully labeled correctly if it was just switched
110             # from disabled, the autorelabel misses some things
111             # skip relabelling on /dev as it will generally throw errors
112             restorecon -R -e /dev /
113
114             # enable enforcing mode from the very start
115             setenforce enforcing
116
117             # configure system for enforcing mode on next boot
118             sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
119         ;;
120         disabled)
121             sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
122             touch /.autorelabel
123
124             echo "*******************************************"
125             echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **"
126             echo "*******************************************"
127         ;;
128         enforcing)
129             echo "*********************************"
130             echo "** SYSTEM IS IN ENFORCING MODE **"
131             echo "*********************************"
132         ;;
133     esac
134
135     # Allow jenkins access to alternatives command to switch java version
136     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
137 Defaults:jenkins !requiretty
138 jenkins ALL = NOPASSWD: /usr/sbin/alternatives
139 EOF
140
141     echo "---> Updating operating system"
142     yum clean all
143     yum install -y deltarpm
144     yum update -y
145
146     ensure_kernel_install
147
148     # add in components we need or want on systems
149     echo "---> Installing base packages"
150     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
151     # separate group installs from package installs since a non-existing
152     # group with dnf based systems (F21+) will fail the install if such
153     # a group does not exist
154     yum install -y unzip xz git git-review perl-XML-XPath
155
156     # facter is not installed by default on the base image and facter package
157     # gets removed when puppet4 is updated. To fix this use version of facter
158     # shipped with puppet4.
159     yum install -y facter
160
161     # ensure facter is available in $PATH avoid failures in retry loop
162     export PATH="/opt/puppetlabs/bin/:$PATH"
163
164     # Install puppet4
165     rpm -ivh https://yum.puppetlabs.com/puppetlabs-release-pc1-el-7.noarch.rpm
166     yum -y install -y puppet-agent
167
168     # Create symlink for facter and puppet
169     ln -sf /opt/puppetlabs/bin/facter /usr/bin/facter
170     ln -sf /opt/puppetlabs/puppet/bin/puppet /usr/bin/puppet
171
172     # All of our systems require Java (because of Jenkins)
173     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
174     # default
175
176     echo "---> Configuring OpenJDK"
177     yum install -y 'java-*-openjdk-devel'
178
179     MANUAL_JAVA9=1
180     FACTER_OS=$(/usr/bin/facter operatingsystem | tr '[:upper:]' '[:lower:]')
181     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
182     case "$FACTER_OS" in
183         fedora)
184             if [ "$FACTER_OSVER" -ge "21" ]
185             then
186                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
187             else
188                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
189                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
190             fi
191             # Java 9 is included in fedora-27
192             if [ "$FACTER_OSVER" -ge "27" ]
193             then
194                 MANUAL_JAVA9=0
195             fi
196         ;;
197         redhat|centos)
198             if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
199             then
200                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
201             else
202                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
203                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
204             fi
205         ;;
206         *)
207             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
208             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
209         ;;
210     esac
211
212     # Not all distributions ship JDK9 in their repositories, download
213     # and install Oracle JDK9 for those which are not known to provide
214     # their own package
215     if [ "$MANUAL_JAVA9" -eq "1" ]
216     then
217         echo "---> Fetching and installing JDK9"
218         wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/9.0.1+11/jdk-9.0.1_linux-x64_bin.rpm"
219         yum localinstall -y jdk-9.0.1_linux-x64_bin.rpm
220     fi
221
222     ########################
223     # --- START LFTOOLS DEPS
224
225     # Used by various scripts to push patches to Gerrit
226     yum install -y git-review
227
228     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
229     # to initialize Heat template based systems.
230     yum install -y jq
231
232     # Used py lftools to speend up some scripts
233     wget http://ftp.riken.jp/Linux/cern/centos/7/cern/x86_64/Packages/parallel-20150522-1.el7.cern.noarch.rpm
234     yum localinstall -y parallel-20150522-1.el7.cern.noarch.rpm
235
236     # Used by lftools scripts to parse XML
237     yum install -y xmlstarlet
238
239     # Haskel Packages
240     # Cabal update fails on a 1G system so workaround that with a swap file
241     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
242     mkswap /tmp/swap
243     swapon /tmp/swap
244
245     yum install -y cabal-install
246     cabal update
247     cabal install "Cabal<1.18"  # Pull Cabal version that is capable of building shellcheck
248     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
249
250     # NLTK_DATA Cache: many jobs that use coala pull down nltk_data
251     wget -nv -O /opt/nltk_data.zip https://github.com/nltk/nltk_data/archive/gh-pages.zip
252
253     # --- END LFTOOLS DEPS
254     ######################
255
256     # install haveged to avoid low entropy rejecting ssh connections
257     yum install -y haveged
258     systemctl enable haveged.service
259
260     # Install sysstat
261     yum install -y sysstat
262     enable_service sysstat
263
264     # Install python3 and dependencies, needed for Coala linting at least
265     yum install -y python34
266     yum install -y python34-{devel,virtualenv,setuptools,pip}
267
268     # Install python dependencies, useful generally
269     yum install -y python-{devel,virtualenv,setuptools,pip}
270 }
271
272 ubuntu_systems() {
273     # Ignore SELinux since slamming that onto Ubuntu leads to
274     # frustration
275
276     # Allow jenkins access to update-alternatives command to switch java version
277     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
278 Defaults:jenkins !requiretty
279 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
280 EOF
281
282     export DEBIAN_FRONTEND=noninteractive
283     cat <<EOF >> /etc/apt/apt.conf
284 APT {
285   Get {
286     Assume-Yes "true";
287     allow-change-held-packages "true";
288     allow-downgrades "true";
289     allow-remove-essential "true";
290   };
291 };
292
293 Dpkg::Options {
294   "--force-confdef";
295   "--force-confold";
296 };
297
298 EOF
299
300     echo "---> Updating operating system"
301
302     # add additional repositories
303     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
304
305     echo "---> Installing base packages"
306     apt-get clean
307     apt-get update -m
308     apt-get upgrade -m
309     apt-get dist-upgrade -m
310     apt-get install facter
311
312     # facter is not installed by default on the base image and facter package
313     # gets removed when puppet4 is updated. To fix this use version of facter
314     # shipped with puppet4.
315     # ensure facter is available in $PATH avoid failures in retry loop
316     export PATH="/opt/puppetlabs/bin/:$PATH"
317
318     ensure_ubuntu_install unzip xz-utils git libxml-xpath-perl
319
320     # Install python3 and dependencies, needed for Coala linting
321     ensure_ubuntu_install python3
322     ensure_ubuntu_install python3-{dev,setuptools,pip}
323
324     # Install python and dependencies
325     ensure_ubuntu_install python-{dev,virtualenv,setuptools,pip}
326
327     FACTER_OSVER=$(facter operatingsystemrelease)
328     case "$FACTER_OSVER" in
329         14.04)
330             echo "---> Installing OpenJDK"
331             apt-get install openjdk-7-jdk
332             # make jdk8 available
333             add-apt-repository -y ppa:openjdk-r/ppa
334             apt-get update
335             # We need to force openjdk-8-jdk to install
336             apt-get install openjdk-8-jdk
337             echo "---> Configuring OpenJDK"
338             # make sure that we still default to openjdk 7
339             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
340             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
341
342             echo "---> Install puppet repository for 14.04"
343             apt-get purge puppet puppet-common
344             wget https://apt.puppetlabs.com/puppetlabs-release-pc1-trusty.deb
345             dpkg -i puppetlabs-release-pc1-trusty.deb
346
347             echo "---> Installing puppet"
348             apt-get update -m
349             ensure_ubuntu_install puppet-agent
350             # Create symlink for facter and puppet
351             ln -sf /opt/puppetlabs/bin/facter /usr/bin/facter
352             ln -sf /opt/puppetlabs/puppet/bin/puppet /usr/bin/puppet
353         ;;
354         16.04)
355             echo "---> Installing OpenJDK"
356             apt-get install openjdk-8-jdk
357
358             echo "---> Install puppet4 repository for 16.04"
359             apt-get purge puppet puppet-common
360             wget https://apt.puppetlabs.com/puppetlabs-release-pc1-xenial.deb
361             dpkg -i puppetlabs-release-pc1-xenial.deb
362
363             echo "---> Installing puppet"
364             apt-get update -m
365             ensure_ubuntu_install puppet-agent
366
367             # Create symlink for facter and puppet
368             ln -sf /opt/puppetlabs/bin/facter /usr/bin/facter
369             ln -sf /opt/puppetlabs/puppet/bin/puppet /usr/bin/puppet
370
371             echo "---> Installing python3 virtualenv"
372             # python3-virtualenv is available starting with 16.04.
373             ensure_ubuntu_install python3-virtualenv
374         ;;
375         *)
376             echo "---> Unknown Ubuntu version $FACTER_OSVER"
377             exit 1
378         ;;
379     esac
380
381     ########################
382     # --- START LFTOOLS DEPS
383
384     # Used by various scripts to push patches to Gerrit
385     ensure_ubuntu_install git-review
386
387     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
388     # to initialize Heat template based systems.
389     ensure_ubuntu_install jq
390
391     # Used py lftools to speend up some scripts
392     ensure_ubuntu_install parallel
393
394     # Used by lftools scripts to parse XML
395     ensure_ubuntu_install xmlstarlet
396
397     # Haskel Packages
398     # Cabal update fails on a 1G system so workaround that with a swap file
399     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
400     mkswap /tmp/swap
401     swapon /tmp/swap
402
403     ensure_ubuntu_install cabal-install
404     cabal update
405     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
406
407     # NLTK_DATA Cache: many jobs that use coala pull down nltk_data
408     wget -nv -O /opt/nltk_data.zip https://github.com/nltk/nltk_data/archive/gh-pages.zip
409
410     # --- END LFTOOLS DEPS
411     ######################
412
413     # Install sysstat
414     ensure_ubuntu_install sysstat
415     sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
416     enable_service sysstat
417
418     # Install haveged to avoid low entropy rejecting ssh connections
419     apt-get install haveged
420     update-rc.d haveged defaults
421
422     # Disable unattended upgrades & daily updates
423     echo '---> Disabling automatic daily upgrades'
424     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
425     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
426
427     # Install packaging job dependencies for building debs
428     ensure_ubuntu_install  build-essential devscripts equivs dh-systemd python-yaml \
429                     python-jinja2 gdebi
430 }
431
432 all_systems() {
433     # To handle the prompt style that is expected all over the environment
434     # with how use use robotframework we need to make sure that it is
435     # consistent for any of the users that are created during dynamic spin
436     # ups
437     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
438
439     # Do any Distro specific installations here
440     echo "Checking distribution"
441     FACTER_OS=$(/usr/bin/facter operatingsystem | tr '[:upper:]' '[:lower:]')
442     case "$FACTER_OS" in
443         redhat|centos)
444             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
445                 echo
446                 echo "---> CentOS 7"
447                 echo "No extra steps currently for CentOS 7"
448                 echo
449             else
450                 echo "---> CentOS 6"
451                 echo "Installing ODL YUM repo"
452                 yum install -y https://nexus.opendaylight.org/content/repositories/opendaylight-yum-epel-6-x86_64/rpm/opendaylight-release/0.1.0-1.el6.noarch/opendaylight-release-0.1.0-1.el6.noarch.rpm
453             fi
454         ;;
455         *)
456             echo "---> $FACTER_OS found"
457             echo "No extra steps for $FACTER_OS"
458         ;;
459     esac
460
461     # Update /etc/nss-switch.conf to map hostname with IP instead of using `localhost`
462     # from /etc/hosts which is required by some of the Java API's to avoid
463     # Java UnknownHostException: "Name or service not known" error.
464     sed -i "/^hosts:/s/$/ myhostname/" /etc/nsswitch.conf
465 }
466
467 echo "---> Attempting to detect OS"
468 # upstream cloud images use the distro name as the initial user
469 ORIGIN=$(if [ -e /etc/redhat-release ]
470     then
471         echo redhat
472     else
473         echo ubuntu
474     fi)
475 #ORIGIN=$(logname)
476
477 case "${ORIGIN}" in
478     fedora|centos|redhat)
479         echo "---> RH type system detected"
480         rh_systems
481     ;;
482     ubuntu)
483         echo "---> Ubuntu system detected"
484         ubuntu_systems
485     ;;
486     *)
487         echo "---> Unknown operating system"
488     ;;
489 esac
490
491 # execute steps for all systems
492 all_systems