Merge "Update Ubuntu 14.04 mininet images"
[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)
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             if [ "$(dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
83                 apt-cache policy "$pkg"
84                 apt-get install "$pkg"
85                 continue
86             else
87                 echo "$pkg already installed."
88                 break
89             fi
90         done
91     done
92 }
93
94 rh_systems() {
95     # Handle the occurance where SELINUX is actually disabled
96     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
97     MODE=$(echo "$SELINUX" | cut -f 2 -d '=')
98     case "$MODE" in
99         permissive)
100             echo "************************************"
101             echo "** SYSTEM ENTERING ENFORCING MODE **"
102             echo "************************************"
103             # make sure that the filesystem is properly labelled.
104             # it could be not fully labeled correctly if it was just switched
105             # from disabled, the autorelabel misses some things
106             # skip relabelling on /dev as it will generally throw errors
107             restorecon -R -e /dev /
108
109             # enable enforcing mode from the very start
110             setenforce enforcing
111
112             # configure system for enforcing mode on next boot
113             sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
114         ;;
115         disabled)
116             sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
117             touch /.autorelabel
118
119             echo "*******************************************"
120             echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **"
121             echo "*******************************************"
122         ;;
123         enforcing)
124             echo "*********************************"
125             echo "** SYSTEM IS IN ENFORCING MODE **"
126             echo "*********************************"
127         ;;
128     esac
129
130     # Allow jenkins access to alternatives command to switch java version
131     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
132 Defaults:jenkins !requiretty
133 jenkins ALL = NOPASSWD: /usr/sbin/alternatives
134 EOF
135
136     echo "---> Updating operating system"
137     yum clean all
138     yum install -y deltarpm
139     yum update -y
140
141     ensure_kernel_install
142
143     # add in components we need or want on systems
144     echo "---> Installing base packages"
145     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
146     # separate group installs from package installs since a non-existing
147     # group with dnf based systems (F21+) will fail the install if such
148     # a group does not exist
149     yum install -y unzip xz puppet git git-review perl-XML-XPath
150
151     # All of our systems require Java (because of Jenkins)
152     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
153     # default
154
155     echo "---> Configuring OpenJDK"
156     yum install -y 'java-*-openjdk-devel'
157
158     FACTER_OS=$(/usr/bin/facter operatingsystem)
159     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
160     case "$FACTER_OS" in
161         Fedora)
162             if [ "$FACTER_OSVER" -ge "21" ]
163             then
164                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
165             else
166                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
167                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
168             fi
169         ;;
170         RedHat|CentOS)
171             if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
172             then
173                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
174             else
175                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
176                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
177             fi
178         ;;
179         *)
180             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
181             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
182         ;;
183     esac
184
185     ########################
186     # --- START LFTOOLS DEPS
187
188     # Used by various scripts to push patches to Gerrit
189     yum install -y git-review
190
191     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
192     # to initialize Heat template based systems.
193     yum install -y jq
194
195     # Used by lftools scripts to parse XML
196     yum install -y xmlstarlet
197
198     # Haskel Packages
199     # Cabal update fails on a 1G system so workaround that with a swap file
200     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
201     mkswap /tmp/swap
202     swapon /tmp/swap
203
204     yum install -y cabal-install
205     cabal update
206     cabal install "Cabal<1.18"  # Pull Cabal version that is capable of building shellcheck
207     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
208
209     # --- END LFTOOLS DEPS
210     ######################
211
212     # install haveged to avoid low entropy rejecting ssh connections
213     yum install -y haveged
214     systemctl enable haveged.service
215
216     # Install sysstat
217     yum install -y sysstat
218     enable_service sysstat
219
220     # Install python3 and dependencies, needed for Coala linting at least
221     yum install -y python34
222     yum install -y python34-{devel,virtualenv,setuptools,pip}
223
224     # Install python dependencies, useful generally
225     yum install -y python-{devel,virtualenv,setuptools,pip}
226 }
227
228 ubuntu_systems() {
229     # Ignore SELinux since slamming that onto Ubuntu leads to
230     # frustration
231
232     # Allow jenkins access to update-alternatives command to switch java version
233     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
234 Defaults:jenkins !requiretty
235 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
236 EOF
237
238     export DEBIAN_FRONTEND=noninteractive
239     cat <<EOF >> /etc/apt/apt.conf
240 APT {
241   Get {
242     Assume-Yes "true";
243     allow-change-held-packages "true";
244     allow-downgrades "true";
245     allow-remove-essential "true";
246   };
247 };
248
249 Dpkg::Options {
250   "--force-confdef";
251   "--force-confold";
252 };
253
254 EOF
255
256     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
257     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
258
259     echo "---> Updating operating system"
260
261     # add additional repositories
262     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
263
264     echo "---> Installing base packages"
265     apt-get clean
266     apt-get update -m
267     apt-get upgrade -m
268     apt-get dist-upgrade -m
269
270     ensure_ubuntu_install unzip xz-utils puppet git libxml-xpath-perl
271
272     # Install python3 and dependencies, needed for Coala linting
273     ensure_ubuntu_install python3
274     ensure_ubuntu_install python3-{dev,setuptools,pip}
275
276     # Install python and dependencies
277     ensure_ubuntu_install python-{dev,virtualenv,setuptools,pip}
278
279     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
280     case "$FACTER_OSVER" in
281         14.04)
282             echo "---> Installing OpenJDK"
283             apt-get install openjdk-7-jdk
284             # make jdk8 available
285             add-apt-repository -y ppa:openjdk-r/ppa
286             apt-get update
287             # We need to force openjdk-8-jdk to install
288             apt-get install openjdk-8-jdk
289             echo "---> Configuring OpenJDK"
290             # make sure that we still default to openjdk 7
291             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
292             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
293         ;;
294         16.04)
295             echo "---> Installing OpenJDK"
296             apt-get install openjdk-8-jdk
297
298             echo "---> Installing python3 virtualenv"
299             # python3-virtualenv is available starting with 16.04.
300             ensure_ubuntu_install python3-virtualenv
301         ;;
302         *)
303             echo "---> Unknown Ubuntu version $FACTER_OSVER"
304             exit 1
305         ;;
306     esac
307
308     ########################
309     # --- START LFTOOLS DEPS
310
311     # Used by various scripts to push patches to Gerrit
312     ensure_ubuntu_install git-review
313
314     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
315     # to initialize Heat template based systems.
316     ensure_ubuntu_install jq
317
318     # Used by lftools scripts to parse XML
319     ensure_ubuntu_install xmlstarlet
320
321     # Haskel Packages
322     # Cabal update fails on a 1G system so workaround that with a swap file
323     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
324     mkswap /tmp/swap
325     swapon /tmp/swap
326
327     ensure_ubuntu_install cabal-install
328     cabal update
329     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
330
331     # --- END LFTOOLS DEPS
332     ######################
333
334     # Install sysstat
335     ensure_ubuntu_install sysstat
336     sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
337     enable_service sysstat
338
339     # install haveged to avoid low entropy rejecting ssh connections
340     apt-get install haveged
341     update-rc.d haveged defaults
342
343     # disable unattended upgrades & daily updates
344     echo '---> Disabling automatic daily upgrades'
345     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
346     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
347 }
348
349 all_systems() {
350     # To handle the prompt style that is expected all over the environment
351     # with how use use robotframework we need to make sure that it is
352     # consistent for any of the users that are created during dynamic spin
353     # ups
354     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
355
356     # Do any Distro specific installations here
357     echo "Checking distribution"
358     FACTER_OS=$(/usr/bin/facter operatingsystem)
359     case "$FACTER_OS" in
360         RedHat|CentOS)
361             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
362                 echo
363                 echo "---> CentOS 7"
364                 echo "No extra steps currently for CentOS 7"
365                 echo
366             else
367                 echo "---> CentOS 6"
368                 echo "Installing ODL YUM repo"
369                 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
370             fi
371         ;;
372         *)
373             echo "---> $FACTER_OS found"
374             echo "No extra steps for $FACTER_OS"
375         ;;
376     esac
377 }
378
379 echo "---> Attempting to detect OS"
380 # upstream cloud images use the distro name as the initial user
381 ORIGIN=$(if [ -e /etc/redhat-release ]
382     then
383         echo redhat
384     else
385         echo ubuntu
386     fi)
387 #ORIGIN=$(logname)
388
389 case "${ORIGIN}" in
390     fedora|centos|redhat)
391         echo "---> RH type system detected"
392         rh_systems
393     ;;
394     ubuntu)
395         echo "---> Ubuntu system detected"
396         ubuntu_systems
397     ;;
398     *)
399         echo "---> Unknown operating system"
400     ;;
401 esac
402
403 # execute steps for all systems
404 all_systems