Merge "Remove 6wind/quagga build temporarily"
[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
221 ubuntu_systems() {
222     # Ignore SELinux since slamming that onto Ubuntu leads to
223     # frustration
224
225     # Allow jenkins access to update-alternatives command to switch java version
226     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
227 Defaults:jenkins !requiretty
228 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
229 EOF
230
231     export DEBIAN_FRONTEND=noninteractive
232     cat <<EOF >> /etc/apt/apt.conf
233 APT {
234   Get {
235     Assume-Yes "true";
236     allow-change-held-packages "true";
237     allow-downgrades "true";
238     allow-remove-essential "true";
239   };
240 };
241
242 Dpkg::Options {
243   "--force-confdef";
244   "--force-confold";
245 };
246
247 EOF
248
249     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
250     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
251
252     echo "---> Updating operating system"
253
254     # add additional repositories
255     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
256
257     echo "---> Installing base packages"
258     apt-get clean
259     apt-get update -m
260     apt-get upgrade -m
261     apt-get dist-upgrade -m
262
263     ensure_ubuntu_install unzip xz-utils puppet git libxml-xpath-perl
264
265     # install Java 7
266     echo "---> Configuring OpenJDK"
267     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
268     case "$FACTER_OSVER" in
269         14.04)
270             apt-get install openjdk-7-jdk
271             # make jdk8 available
272             add-apt-repository -y ppa:openjdk-r/ppa
273             apt-get update
274             # We need to force openjdk-8-jdk to install
275             apt-get install openjdk-8-jdk
276             # make sure that we still default to openjdk 7
277             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
278             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
279         ;;
280         16.04)
281             apt-get install openjdk-8-jdk
282         ;;
283         *)
284             echo "---> Unknown Ubuntu version $FACTER_OSVER"
285             exit 1
286         ;;
287     esac
288
289     ########################
290     # --- START LFTOOLS DEPS
291
292     # Used by various scripts to push patches to Gerrit
293     ensure_ubuntu_install git-review
294
295     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
296     # to initialize Heat template based systems.
297     ensure_ubuntu_install jq
298
299     # Used by lftools scripts to parse XML
300     ensure_ubuntu_install xmlstarlet
301
302     # Haskel Packages
303     # Cabal update fails on a 1G system so workaround that with a swap file
304     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
305     mkswap /tmp/swap
306     swapon /tmp/swap
307
308     ensure_ubuntu_install cabal-install
309     cabal update
310     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
311
312     # --- END LFTOOLS DEPS
313     ######################
314
315     # Install sysstat
316     ensure_ubuntu_install sysstat
317     sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
318     enable_service sysstat
319
320     # install haveged to avoid low entropy rejecting ssh connections
321     apt-get install haveged
322     update-rc.d haveged defaults
323
324     # disable unattended upgrades & daily updates
325     echo '---> Disabling automatic daily upgrades'
326     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
327     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
328 }
329
330 all_systems() {
331     # To handle the prompt style that is expected all over the environment
332     # with how use use robotframework we need to make sure that it is
333     # consistent for any of the users that are created during dynamic spin
334     # ups
335     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
336
337     # Do any Distro specific installations here
338     echo "Checking distribution"
339     FACTER_OS=$(/usr/bin/facter operatingsystem)
340     case "$FACTER_OS" in
341         RedHat|CentOS)
342             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
343                 echo
344                 echo "---> CentOS 7"
345                 echo "No extra steps currently for CentOS 7"
346                 echo
347             else
348                 echo "---> CentOS 6"
349                 echo "Installing ODL YUM repo"
350                 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
351             fi
352         ;;
353         *)
354             echo "---> $FACTER_OS found"
355             echo "No extra steps for $FACTER_OS"
356         ;;
357     esac
358 }
359
360 echo "---> Attempting to detect OS"
361 # upstream cloud images use the distro name as the initial user
362 ORIGIN=$(if [ -e /etc/redhat-release ]
363     then
364         echo redhat
365     else
366         echo ubuntu
367     fi)
368 #ORIGIN=$(logname)
369
370 case "${ORIGIN}" in
371     fedora|centos|redhat)
372         echo "---> RH type system detected"
373         rh_systems
374     ;;
375     ubuntu)
376         echo "---> Ubuntu system detected"
377         ubuntu_systems
378     ;;
379     *)
380         echo "---> Unknown operating system"
381     ;;
382 esac
383
384 # execute steps for all systems
385 all_systems