Install GNU parallel on baseline
[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             echo "$i: Installing $pkg"
83             if [ "$(dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
84                 apt-cache policy "$pkg"
85                 apt-get install "$pkg"
86                 continue
87             else
88                 echo "$pkg already installed."
89                 break
90             fi
91         done
92     done
93 }
94
95 rh_systems() {
96     # Handle the occurance where SELINUX is actually disabled
97     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
98     MODE=$(echo "$SELINUX" | cut -f 2 -d '=')
99     case "$MODE" in
100         permissive)
101             echo "************************************"
102             echo "** SYSTEM ENTERING ENFORCING MODE **"
103             echo "************************************"
104             # make sure that the filesystem is properly labelled.
105             # it could be not fully labeled correctly if it was just switched
106             # from disabled, the autorelabel misses some things
107             # skip relabelling on /dev as it will generally throw errors
108             restorecon -R -e /dev /
109
110             # enable enforcing mode from the very start
111             setenforce enforcing
112
113             # configure system for enforcing mode on next boot
114             sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
115         ;;
116         disabled)
117             sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
118             touch /.autorelabel
119
120             echo "*******************************************"
121             echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **"
122             echo "*******************************************"
123         ;;
124         enforcing)
125             echo "*********************************"
126             echo "** SYSTEM IS IN ENFORCING MODE **"
127             echo "*********************************"
128         ;;
129     esac
130
131     # Allow jenkins access to alternatives command to switch java version
132     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
133 Defaults:jenkins !requiretty
134 jenkins ALL = NOPASSWD: /usr/sbin/alternatives
135 EOF
136
137     echo "---> Updating operating system"
138     yum clean all
139     yum install -y deltarpm
140     yum update -y
141
142     ensure_kernel_install
143
144     # add in components we need or want on systems
145     echo "---> Installing base packages"
146     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
147     # separate group installs from package installs since a non-existing
148     # group with dnf based systems (F21+) will fail the install if such
149     # a group does not exist
150     yum install -y unzip xz puppet git git-review perl-XML-XPath
151
152     # All of our systems require Java (because of Jenkins)
153     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
154     # default
155
156     echo "---> Configuring OpenJDK"
157     yum install -y 'java-*-openjdk-devel'
158
159     FACTER_OS=$(/usr/bin/facter operatingsystem)
160     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
161     case "$FACTER_OS" in
162         Fedora)
163             if [ "$FACTER_OSVER" -ge "21" ]
164             then
165                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
166             else
167                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
168                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
169             fi
170         ;;
171         RedHat|CentOS)
172             if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
173             then
174                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
175             else
176                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
177                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
178             fi
179         ;;
180         *)
181             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
182             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
183         ;;
184     esac
185
186     ########################
187     # --- START LFTOOLS DEPS
188
189     # Used by various scripts to push patches to Gerrit
190     yum install -y git-review
191
192     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
193     # to initialize Heat template based systems.
194     yum install -y jq
195
196     # Used py lftools to speend up some scripts
197     wget http://ftp.riken.jp/Linux/cern/centos/7/cern/x86_64/Packages/parallel-20150522-1.el7.cern.noarch.rpm
198     yum localinstall -y parallel-20150522-1.el7.cern.noarch.rpm
199
200     # Used by lftools scripts to parse XML
201     yum install -y xmlstarlet
202
203     # Haskel Packages
204     # Cabal update fails on a 1G system so workaround that with a swap file
205     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
206     mkswap /tmp/swap
207     swapon /tmp/swap
208
209     yum install -y cabal-install
210     cabal update
211     cabal install "Cabal<1.18"  # Pull Cabal version that is capable of building shellcheck
212     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
213
214     # --- END LFTOOLS DEPS
215     ######################
216
217     # install haveged to avoid low entropy rejecting ssh connections
218     yum install -y haveged
219     systemctl enable haveged.service
220
221     # Install sysstat
222     yum install -y sysstat
223     enable_service sysstat
224
225     # Install python3 and dependencies, needed for Coala linting at least
226     yum install -y python34
227     yum install -y python34-{devel,virtualenv,setuptools,pip}
228
229     # Install python dependencies, useful generally
230     yum install -y python-{devel,virtualenv,setuptools,pip}
231 }
232
233 ubuntu_systems() {
234     # Ignore SELinux since slamming that onto Ubuntu leads to
235     # frustration
236
237     # Allow jenkins access to update-alternatives command to switch java version
238     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
239 Defaults:jenkins !requiretty
240 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
241 EOF
242
243     export DEBIAN_FRONTEND=noninteractive
244     cat <<EOF >> /etc/apt/apt.conf
245 APT {
246   Get {
247     Assume-Yes "true";
248     allow-change-held-packages "true";
249     allow-downgrades "true";
250     allow-remove-essential "true";
251   };
252 };
253
254 Dpkg::Options {
255   "--force-confdef";
256   "--force-confold";
257 };
258
259 EOF
260
261     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
262     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
263
264     echo "---> Updating operating system"
265
266     # add additional repositories
267     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
268
269     echo "---> Installing base packages"
270     apt-get clean
271     apt-get update -m
272     apt-get upgrade -m
273     apt-get dist-upgrade -m
274
275     ensure_ubuntu_install unzip xz-utils puppet git libxml-xpath-perl
276
277     # Install python3 and dependencies, needed for Coala linting
278     ensure_ubuntu_install python3
279     ensure_ubuntu_install python3-{dev,setuptools,pip}
280
281     # Install python and dependencies
282     ensure_ubuntu_install python-{dev,virtualenv,setuptools,pip}
283
284     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
285     case "$FACTER_OSVER" in
286         14.04)
287             echo "---> Installing OpenJDK"
288             apt-get install openjdk-7-jdk
289             # make jdk8 available
290             add-apt-repository -y ppa:openjdk-r/ppa
291             apt-get update
292             # We need to force openjdk-8-jdk to install
293             apt-get install openjdk-8-jdk
294             echo "---> Configuring OpenJDK"
295             # make sure that we still default to openjdk 7
296             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
297             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
298         ;;
299         16.04)
300             echo "---> Installing OpenJDK"
301             apt-get install openjdk-8-jdk
302
303             echo "---> Installing python3 virtualenv"
304             # python3-virtualenv is available starting with 16.04.
305             ensure_ubuntu_install python3-virtualenv
306         ;;
307         *)
308             echo "---> Unknown Ubuntu version $FACTER_OSVER"
309             exit 1
310         ;;
311     esac
312
313     ########################
314     # --- START LFTOOLS DEPS
315
316     # Used by various scripts to push patches to Gerrit
317     ensure_ubuntu_install git-review
318
319     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
320     # to initialize Heat template based systems.
321     ensure_ubuntu_install jq
322
323     # Used py lftools to speend up some scripts
324     ensure_ubuntu_install parallel
325
326     # Used by lftools scripts to parse XML
327     ensure_ubuntu_install xmlstarlet
328
329     # Haskel Packages
330     # Cabal update fails on a 1G system so workaround that with a swap file
331     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
332     mkswap /tmp/swap
333     swapon /tmp/swap
334
335     ensure_ubuntu_install cabal-install
336     cabal update
337     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
338
339     # --- END LFTOOLS DEPS
340     ######################
341
342     # Install sysstat
343     ensure_ubuntu_install sysstat
344     sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
345     enable_service sysstat
346
347     # install haveged to avoid low entropy rejecting ssh connections
348     apt-get install haveged
349     update-rc.d haveged defaults
350
351     # disable unattended upgrades & daily updates
352     echo '---> Disabling automatic daily upgrades'
353     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
354     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
355
356     # Install packaging job dependencies for building debs
357     ensure_ubuntu_install  build-essential devscripts equivs dh-systemd python-yaml \
358                     python-jinja2 gdebi
359
360 }
361
362 all_systems() {
363     # To handle the prompt style that is expected all over the environment
364     # with how use use robotframework we need to make sure that it is
365     # consistent for any of the users that are created during dynamic spin
366     # ups
367     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
368
369     # Do any Distro specific installations here
370     echo "Checking distribution"
371     FACTER_OS=$(/usr/bin/facter operatingsystem)
372     case "$FACTER_OS" in
373         RedHat|CentOS)
374             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
375                 echo
376                 echo "---> CentOS 7"
377                 echo "No extra steps currently for CentOS 7"
378                 echo
379             else
380                 echo "---> CentOS 6"
381                 echo "Installing ODL YUM repo"
382                 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
383             fi
384         ;;
385         *)
386             echo "---> $FACTER_OS found"
387             echo "No extra steps for $FACTER_OS"
388         ;;
389     esac
390 }
391
392 echo "---> Attempting to detect OS"
393 # upstream cloud images use the distro name as the initial user
394 ORIGIN=$(if [ -e /etc/redhat-release ]
395     then
396         echo redhat
397     else
398         echo ubuntu
399     fi)
400 #ORIGIN=$(logname)
401
402 case "${ORIGIN}" in
403     fedora|centos|redhat)
404         echo "---> RH type system detected"
405         rh_systems
406     ;;
407     ubuntu)
408         echo "---> Ubuntu system detected"
409         ubuntu_systems
410     ;;
411     *)
412         echo "---> Unknown operating system"
413     ;;
414 esac
415
416 # execute steps for all systems
417 all_systems