Merge "Make Boron autorelease run weekly"
[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 Java 7
273     echo "---> Configuring OpenJDK"
274     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
275     case "$FACTER_OSVER" in
276         14.04)
277             apt-get install openjdk-7-jdk
278             # make jdk8 available
279             add-apt-repository -y ppa:openjdk-r/ppa
280             apt-get update
281             # We need to force openjdk-8-jdk to install
282             apt-get install openjdk-8-jdk
283             # make sure that we still default to openjdk 7
284             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
285             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
286         ;;
287         16.04)
288             apt-get install openjdk-8-jdk
289         ;;
290         *)
291             echo "---> Unknown Ubuntu version $FACTER_OSVER"
292             exit 1
293         ;;
294     esac
295
296     ########################
297     # --- START LFTOOLS DEPS
298
299     # Used by various scripts to push patches to Gerrit
300     ensure_ubuntu_install git-review
301
302     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
303     # to initialize Heat template based systems.
304     ensure_ubuntu_install jq
305
306     # Used by lftools scripts to parse XML
307     ensure_ubuntu_install xmlstarlet
308
309     # Haskel Packages
310     # Cabal update fails on a 1G system so workaround that with a swap file
311     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
312     mkswap /tmp/swap
313     swapon /tmp/swap
314
315     ensure_ubuntu_install cabal-install
316     cabal update
317     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
318
319     # --- END LFTOOLS DEPS
320     ######################
321
322     # Install sysstat
323     ensure_ubuntu_install sysstat
324     sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
325     enable_service sysstat
326
327     # install haveged to avoid low entropy rejecting ssh connections
328     apt-get install haveged
329     update-rc.d haveged defaults
330
331     # Install python3 and dependencies, needed for Coala linting at least
332     ensure_ubuntu_install python3
333     ensure_ubuntu_install python3-{dev,virtualenv,setuptools,pip}
334
335     # Install python and dependencies
336     ensure_ubuntu_install python-{dev,virtualenv,setuptools,pip}
337
338     # disable unattended upgrades & daily updates
339     echo '---> Disabling automatic daily upgrades'
340     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
341     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
342 }
343
344 all_systems() {
345     # To handle the prompt style that is expected all over the environment
346     # with how use use robotframework we need to make sure that it is
347     # consistent for any of the users that are created during dynamic spin
348     # ups
349     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
350
351     # Do any Distro specific installations here
352     echo "Checking distribution"
353     FACTER_OS=$(/usr/bin/facter operatingsystem)
354     case "$FACTER_OS" in
355         RedHat|CentOS)
356             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
357                 echo
358                 echo "---> CentOS 7"
359                 echo "No extra steps currently for CentOS 7"
360                 echo
361             else
362                 echo "---> CentOS 6"
363                 echo "Installing ODL YUM repo"
364                 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
365             fi
366         ;;
367         *)
368             echo "---> $FACTER_OS found"
369             echo "No extra steps for $FACTER_OS"
370         ;;
371     esac
372 }
373
374 echo "---> Attempting to detect OS"
375 # upstream cloud images use the distro name as the initial user
376 ORIGIN=$(if [ -e /etc/redhat-release ]
377     then
378         echo redhat
379     else
380         echo ubuntu
381     fi)
382 #ORIGIN=$(logname)
383
384 case "${ORIGIN}" in
385     fedora|centos|redhat)
386         echo "---> RH type system detected"
387         rh_systems
388     ;;
389     ubuntu)
390         echo "---> Ubuntu system detected"
391         ubuntu_systems
392     ;;
393     *)
394         echo "---> Unknown operating system"
395     ;;
396 esac
397
398 # execute steps for all systems
399 all_systems