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