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