Also install git-review on Ubuntu systems
[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 ShellCheck
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     # --- END LFTOOLS DEPS
164     ######################
165
166     # install haveged to avoid low entropy rejecting ssh connections
167     yum install -y haveged
168     systemctl enable haveged.service
169 }
170
171 ubuntu_systems() {
172     # Ignore SELinux since slamming that onto Ubuntu leads to
173     # frustration
174
175     # Allow jenkins access to update-alternatives command to switch java version
176     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
177 Defaults:jenkins !requiretty
178 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
179 EOF
180
181     export DEBIAN_FRONTEND=noninteractive
182     cat <<EOF >> /etc/apt/apt.conf
183 APT {
184   Get {
185     Assume-Yes "true";
186     allow-change-held-packages "true";
187     allow-downgrades "true";
188     allow-remove-essential "true";
189   };
190 };
191
192 Dpkg::Options {
193   "--force-confdef";
194   "--force-confold";
195 };
196
197 EOF
198
199     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
200     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
201
202     echo "---> Updating operating system"
203
204     # add additional repositories
205     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
206
207     echo "---> Installing base packages"
208     apt-get clean
209     apt-get update -m
210     apt-get upgrade -m
211     apt-get dist-upgrade -m
212
213     ensure_ubuntu_install unzip xz-utils puppet git libxml-xpath-perl
214
215     # install Java 7
216     echo "---> Configuring OpenJDK"
217     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
218     case "$FACTER_OSVER" in
219         14.04)
220             apt-get install openjdk-7-jdk
221             # make jdk8 available
222             add-apt-repository -y ppa:openjdk-r/ppa
223             apt-get update
224             # We need to force openjdk-8-jdk to install
225             apt-get install openjdk-8-jdk
226             # make sure that we still default to openjdk 7
227             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
228             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
229         ;;
230         16.04)
231             apt-get install openjdk-8-jdk
232         ;;
233         *)
234             echo "---> Unknown Ubuntu version $FACTER_OSVER"
235             exit 1
236         ;;
237     esac
238
239     ########################
240     # --- START LFTOOLS DEPS
241
242     # Used by various scripts to push patches to Gerrit
243     ensure_ubuntu_install git-review
244
245     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
246     # to initialize Heat template based systems.
247     ensure_ubuntu_install jq
248
249     # Used by lftools scripts to parse XML
250     ensure_ubuntu_install xmlstarlet
251
252     # --- END LFTOOLS DEPS
253     ######################
254
255     # install haveged to avoid low entropy rejecting ssh connections
256     apt-get install haveged
257     update-rc.d haveged defaults
258
259     # disable unattended upgrades & daily updates
260     echo '---> Disabling automatic daily upgrades'
261     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
262     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
263 }
264
265 all_systems() {
266     # To handle the prompt style that is expected all over the environment
267     # with how use use robotframework we need to make sure that it is
268     # consistent for any of the users that are created during dynamic spin
269     # ups
270     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
271
272     # Do any Distro specific installations here
273     echo "Checking distribution"
274     FACTER_OS=$(/usr/bin/facter operatingsystem)
275     case "$FACTER_OS" in
276         RedHat|CentOS)
277             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
278                 echo
279                 echo "---> CentOS 7"
280                 echo "No extra steps currently for CentOS 7"
281                 echo
282             else
283                 echo "---> CentOS 6"
284                 echo "Installing ODL YUM repo"
285                 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
286             fi
287         ;;
288         *)
289             echo "---> $FACTER_OS found"
290             echo "No extra steps for $FACTER_OS"
291         ;;
292     esac
293 }
294
295 echo "---> Attempting to detect OS"
296 # upstream cloud images use the distro name as the initial user
297 ORIGIN=$(if [ -e /etc/redhat-release ]
298     then
299         echo redhat
300     else
301         echo ubuntu
302     fi)
303 #ORIGIN=$(logname)
304
305 case "${ORIGIN}" in
306     fedora|centos|redhat)
307         echo "---> RH type system detected"
308         rh_systems
309     ;;
310     ubuntu)
311         echo "---> Ubuntu system detected"
312         ubuntu_systems
313     ;;
314     *)
315         echo "---> Unknown operating system"
316     ;;
317 esac
318
319 # execute steps for all systems
320 all_systems