Merge "Increase packer job timeout to 90 minutes"
[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 ensure_kernel_install() {
9     # Workaround for mkinitrd failing on occassion.
10     # On CentOS 7 it seems like the kernel install can fail it's mkinitrd
11     # run quietly, so we may not notice the failure. This script retries for a
12     # few times before giving up.
13     initramfs_ver=$(rpm -q kernel | tail -1 | sed "s/kernel-/initramfs-/")
14     grub_conf="/boot/grub/grub.conf"
15     # Public cloud does not use /boot/grub/grub.conf and uses grub2 instead.
16     if [ ! -e "$grub_conf" ]; then
17         echo "$grub_conf not found. Using Grub 2 conf instead."
18         grub_conf="/boot/grub2/grub.cfg"
19     fi
20
21     for i in $(seq 3); do
22         if grep "$initramfs_ver" "$grub_conf"; then
23             break
24         fi
25         echo "Kernel initrd missing. Retrying to install kernel..."
26         yum reinstall -y kernel
27     done
28     if ! grep "$initramfs_ver" "$grub_conf"; then
29         cat /boot/grub/grub.conf
30         echo "ERROR: Failed to install kernel."
31         exit 1
32     fi
33 }
34
35 ensure_ubuntu_install() {
36     # Workaround for mirrors occassionally failing to install a package.
37     # On Ubuntu sometimes the mirrors fail to install a package. This wrapper
38     # checks that a package is successfully installed before moving on.
39
40     packages=($@)
41
42     for pkg in "${packages[@]}"
43     do
44         # Retry installing package 5 times if necessary
45         for i in {0..5}
46         do
47             if [ "$(dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
48                 apt-cache policy "$pkg"
49                 apt-get install "$pkg"
50                 continue
51             else
52                 echo "$pkg already installed."
53                 break
54             fi
55         done
56     done
57 }
58
59 rh_systems() {
60     # Handle the occurance where SELINUX is actually disabled
61     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
62     MODE=$(echo "$SELINUX" | cut -f 2 -d '=')
63     case "$MODE" in
64         permissive)
65             echo "************************************"
66             echo "** SYSTEM ENTERING ENFORCING MODE **"
67             echo "************************************"
68             # make sure that the filesystem is properly labelled.
69             # it could be not fully labeled correctly if it was just switched
70             # from disabled, the autorelabel misses some things
71             # skip relabelling on /dev as it will generally throw errors
72             restorecon -R -e /dev /
73
74             # enable enforcing mode from the very start
75             setenforce enforcing
76
77             # configure system for enforcing mode on next boot
78             sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
79         ;;
80         disabled)
81             sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
82             touch /.autorelabel
83
84             echo "*******************************************"
85             echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **"
86             echo "*******************************************"
87         ;;
88         enforcing)
89             echo "*********************************"
90             echo "** SYSTEM IS IN ENFORCING MODE **"
91             echo "*********************************"
92         ;;
93     esac
94
95     # Allow jenkins access to alternatives command to switch java version
96     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
97 Defaults:jenkins !requiretty
98 jenkins ALL = NOPASSWD: /usr/sbin/alternatives
99 EOF
100
101     echo "---> Updating operating system"
102     yum clean all
103     yum install -y deltarpm
104     yum update -y
105
106     ensure_kernel_install
107
108     # add in components we need or want on systems
109     echo "---> Installing base packages"
110     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
111     # separate group installs from package installs since a non-existing
112     # group with dnf based systems (F21+) will fail the install if such
113     # a group does not exist
114     yum install -y unzip xz puppet git git-review perl-XML-XPath
115
116     # All of our systems require Java (because of Jenkins)
117     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
118     # default
119
120     echo "---> Configuring OpenJDK"
121     yum install -y 'java-*-openjdk-devel'
122
123     FACTER_OS=$(/usr/bin/facter operatingsystem)
124     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
125     case "$FACTER_OS" in
126         Fedora)
127             if [ "$FACTER_OSVER" -ge "21" ]
128             then
129                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
130             else
131                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
132                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
133             fi
134         ;;
135         RedHat|CentOS)
136             if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
137             then
138                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
139             else
140                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
141                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
142             fi
143         ;;
144         *)
145             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
146             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
147         ;;
148     esac
149
150     ########################
151     # --- START LFTOOLS DEPS
152
153     # Used by various scripts to push patches to Gerrit
154     yum install -y git-review
155
156     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
157     # to initialize Heat template based systems.
158     yum install -y jq
159
160     # Used by lftools scripts to parse XML
161     yum install -y xmlstarlet
162
163     # Haskel Packages
164     # Cabal update fails on a 1G system so workaround that with a swap file
165     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
166     mkswap /tmp/swap
167     swapon /tmp/swap
168
169     yum install -y cabal-install
170     cabal update
171     cabal install "Cabal<1.18"  # Pull Cabal version that is capable of building shellcheck
172     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
173
174     # --- END LFTOOLS DEPS
175     ######################
176
177     # install haveged to avoid low entropy rejecting ssh connections
178     yum install -y haveged
179     systemctl enable haveged.service
180 }
181
182 ubuntu_systems() {
183     # Ignore SELinux since slamming that onto Ubuntu leads to
184     # frustration
185
186     # Allow jenkins access to update-alternatives command to switch java version
187     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
188 Defaults:jenkins !requiretty
189 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
190 EOF
191
192     export DEBIAN_FRONTEND=noninteractive
193     cat <<EOF >> /etc/apt/apt.conf
194 APT {
195   Get {
196     Assume-Yes "true";
197     allow-change-held-packages "true";
198     allow-downgrades "true";
199     allow-remove-essential "true";
200   };
201 };
202
203 Dpkg::Options {
204   "--force-confdef";
205   "--force-confold";
206 };
207
208 EOF
209
210     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
211     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
212
213     echo "---> Updating operating system"
214
215     # add additional repositories
216     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
217
218     echo "---> Installing base packages"
219     apt-get clean
220     apt-get update -m
221     apt-get upgrade -m
222     apt-get dist-upgrade -m
223
224     ensure_ubuntu_install unzip xz-utils puppet git libxml-xpath-perl
225
226     # install Java 7
227     echo "---> Configuring OpenJDK"
228     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
229     case "$FACTER_OSVER" in
230         14.04)
231             apt-get install openjdk-7-jdk
232             # make jdk8 available
233             add-apt-repository -y ppa:openjdk-r/ppa
234             apt-get update
235             # We need to force openjdk-8-jdk to install
236             apt-get install openjdk-8-jdk
237             # make sure that we still default to openjdk 7
238             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
239             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
240         ;;
241         16.04)
242             apt-get install openjdk-8-jdk
243         ;;
244         *)
245             echo "---> Unknown Ubuntu version $FACTER_OSVER"
246             exit 1
247         ;;
248     esac
249
250     ########################
251     # --- START LFTOOLS DEPS
252
253     # Used by various scripts to push patches to Gerrit
254     ensure_ubuntu_install git-review
255
256     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
257     # to initialize Heat template based systems.
258     ensure_ubuntu_install jq
259
260     # Used by lftools scripts to parse XML
261     ensure_ubuntu_install xmlstarlet
262
263     # Haskel Packages
264     # Cabal update fails on a 1G system so workaround that with a swap file
265     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
266     mkswap /tmp/swap
267     swapon /tmp/swap
268
269     ensure_ubuntu_install cabal-install
270     cabal update
271     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
272
273     # --- END LFTOOLS DEPS
274     ######################
275
276     # install haveged to avoid low entropy rejecting ssh connections
277     apt-get install haveged
278     update-rc.d haveged defaults
279
280     # disable unattended upgrades & daily updates
281     echo '---> Disabling automatic daily upgrades'
282     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
283     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
284 }
285
286 all_systems() {
287     # To handle the prompt style that is expected all over the environment
288     # with how use use robotframework we need to make sure that it is
289     # consistent for any of the users that are created during dynamic spin
290     # ups
291     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
292
293     # Do any Distro specific installations here
294     echo "Checking distribution"
295     FACTER_OS=$(/usr/bin/facter operatingsystem)
296     case "$FACTER_OS" in
297         RedHat|CentOS)
298             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
299                 echo
300                 echo "---> CentOS 7"
301                 echo "No extra steps currently for CentOS 7"
302                 echo
303             else
304                 echo "---> CentOS 6"
305                 echo "Installing ODL YUM repo"
306                 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
307             fi
308         ;;
309         *)
310             echo "---> $FACTER_OS found"
311             echo "No extra steps for $FACTER_OS"
312         ;;
313     esac
314 }
315
316 echo "---> Attempting to detect OS"
317 # upstream cloud images use the distro name as the initial user
318 ORIGIN=$(if [ -e /etc/redhat-release ]
319     then
320         echo redhat
321     else
322         echo ubuntu
323     fi)
324 #ORIGIN=$(logname)
325
326 case "${ORIGIN}" in
327     fedora|centos|redhat)
328         echo "---> RH type system detected"
329         rh_systems
330     ;;
331     ubuntu)
332         echo "---> Ubuntu system detected"
333         ubuntu_systems
334     ;;
335     *)
336         echo "---> Unknown operating system"
337     ;;
338 esac
339
340 # execute steps for all systems
341 all_systems