Cache NLTK data in build minions
[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 git git-review perl-XML-XPath
151
152     # facter is not installed by default on the base image and facter package
153     # gets removed when puppet4 is updated. To fix this use version of facter
154     # shipped with puppet4.
155     yum install -y facter
156
157     # ensure facter is available in $PATH avoid failures in retry loop
158     export PATH="/opt/puppetlabs/bin/:$PATH"
159
160     # Install puppet4
161     rpm -ivh https://yum.puppetlabs.com/puppetlabs-release-pc1-el-7.noarch.rpm
162     yum -y install -y puppet-agent
163
164     # Create symlink for facter and puppet
165     ln -sf /opt/puppetlabs/bin/facter /usr/bin/facter
166     ln -sf /opt/puppetlabs/puppet/bin/puppet /usr/bin/puppet
167
168     # All of our systems require Java (because of Jenkins)
169     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
170     # default
171
172     echo "---> Configuring OpenJDK"
173     yum install -y 'java-*-openjdk-devel'
174
175     FACTER_OS=$(/usr/bin/facter operatingsystem)
176     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
177     case "$FACTER_OS" in
178         Fedora)
179             if [ "$FACTER_OSVER" -ge "21" ]
180             then
181                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
182             else
183                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
184                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
185             fi
186         ;;
187         RedHat|CentOS)
188             if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
189             then
190                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
191             else
192                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
193                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
194             fi
195         ;;
196         *)
197             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
198             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
199         ;;
200     esac
201
202     ########################
203     # --- START LFTOOLS DEPS
204
205     # Used by various scripts to push patches to Gerrit
206     yum install -y git-review
207
208     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
209     # to initialize Heat template based systems.
210     yum install -y jq
211
212     # Used py lftools to speend up some scripts
213     wget http://ftp.riken.jp/Linux/cern/centos/7/cern/x86_64/Packages/parallel-20150522-1.el7.cern.noarch.rpm
214     yum localinstall -y parallel-20150522-1.el7.cern.noarch.rpm
215
216     # Used by lftools scripts to parse XML
217     yum install -y xmlstarlet
218
219     # Haskel Packages
220     # Cabal update fails on a 1G system so workaround that with a swap file
221     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
222     mkswap /tmp/swap
223     swapon /tmp/swap
224
225     yum install -y cabal-install
226     cabal update
227     cabal install "Cabal<1.18"  # Pull Cabal version that is capable of building shellcheck
228     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
229
230     # openldap dev headers are required for lftools
231     yum install -y openldap-devel
232
233     # NLTK_DATA Cache: many jobs that use coala pull down nltk_data
234     wget -nv -O /opt/nltk_data.zip https://github.com/nltk/nltk_data/archive/gh-pages.zip
235
236     # --- END LFTOOLS DEPS
237     ######################
238
239     # install haveged to avoid low entropy rejecting ssh connections
240     yum install -y haveged
241     systemctl enable haveged.service
242
243     # Install sysstat
244     yum install -y sysstat
245     enable_service sysstat
246
247     # Install python3 and dependencies, needed for Coala linting at least
248     yum install -y python34
249     yum install -y python34-{devel,virtualenv,setuptools,pip}
250
251     # Install python dependencies, useful generally
252     yum install -y python-{devel,virtualenv,setuptools,pip}
253 }
254
255 ubuntu_systems() {
256     # Ignore SELinux since slamming that onto Ubuntu leads to
257     # frustration
258
259     # Allow jenkins access to update-alternatives command to switch java version
260     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
261 Defaults:jenkins !requiretty
262 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
263 EOF
264
265     export DEBIAN_FRONTEND=noninteractive
266     cat <<EOF >> /etc/apt/apt.conf
267 APT {
268   Get {
269     Assume-Yes "true";
270     allow-change-held-packages "true";
271     allow-downgrades "true";
272     allow-remove-essential "true";
273   };
274 };
275
276 Dpkg::Options {
277   "--force-confdef";
278   "--force-confold";
279 };
280
281 EOF
282
283     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
284     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
285
286     echo "---> Updating operating system"
287
288     # add additional repositories
289     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
290
291     # facter is not installed by default on the base image and facter package
292     # gets removed when puppet4 is updated. To fix this use version of facter
293     # shipped with puppet4.
294     ensure_ubuntu_install facter
295     # ensure facter is available in $PATH avoid failures in retry loop
296     export PATH="/opt/puppetlabs/bin/:$PATH"
297
298     echo "---> Installing base packages"
299     apt-get clean
300     apt-get update -m
301     apt-get upgrade -m
302     apt-get dist-upgrade -m
303
304     ensure_ubuntu_install unzip xz-utils git libxml-xpath-perl
305
306     # Install python3 and dependencies, needed for Coala linting
307     ensure_ubuntu_install python3
308     ensure_ubuntu_install python3-{dev,setuptools,pip}
309
310     # Install python and dependencies
311     ensure_ubuntu_install python-{dev,virtualenv,setuptools,pip}
312
313     FACTER_OSVER=$(facter operatingsystemrelease)
314     case "$FACTER_OSVER" in
315         14.04)
316             echo "---> Installing OpenJDK"
317             apt-get install openjdk-7-jdk
318             # make jdk8 available
319             add-apt-repository -y ppa:openjdk-r/ppa
320             apt-get update
321             # We need to force openjdk-8-jdk to install
322             apt-get install openjdk-8-jdk
323             echo "---> Configuring OpenJDK"
324             # make sure that we still default to openjdk 7
325             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
326             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
327
328             echo "---> Install puppet repository for 14.04"
329             apt-get purge puppet puppet-common
330             wget https://apt.puppetlabs.com/puppetlabs-release-pc1-trusty.deb
331             dpkg -i puppetlabs-release-pc1-trusty.deb
332
333             echo "---> Installing puppet"
334             apt-get update -m
335             ensure_ubuntu_install puppet-agent
336             # Create symlink for facter and puppet
337             ln -sf /opt/puppetlabs/bin/facter /usr/bin/facter
338             ln -sf /opt/puppetlabs/puppet/bin/puppet /usr/bin/puppet
339         ;;
340         16.04)
341             echo "---> Installing OpenJDK"
342             apt-get install openjdk-8-jdk
343
344             echo "---> Install puppet4 repository for 16.04"
345             apt-get purge puppet puppet-common
346             wget https://apt.puppetlabs.com/puppetlabs-release-pc1-xenial.deb
347             dpkg -i puppetlabs-release-pc1-xenial.deb
348
349             echo "---> Installing puppet"
350             apt-get update -m
351             ensure_ubuntu_install puppet-agent
352
353             # Create symlink for facter and puppet
354             ln -sf /opt/puppetlabs/bin/facter /usr/bin/facter
355             ln -sf /opt/puppetlabs/puppet/bin/puppet /usr/bin/puppet
356
357             echo "---> Installing python3 virtualenv"
358             # python3-virtualenv is available starting with 16.04.
359             ensure_ubuntu_install python3-virtualenv
360         ;;
361         *)
362             echo "---> Unknown Ubuntu version $FACTER_OSVER"
363             exit 1
364         ;;
365     esac
366
367     ########################
368     # --- START LFTOOLS DEPS
369
370     # Used by various scripts to push patches to Gerrit
371     ensure_ubuntu_install git-review
372
373     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
374     # to initialize Heat template based systems.
375     ensure_ubuntu_install jq
376
377     # Used py lftools to speend up some scripts
378     ensure_ubuntu_install parallel
379
380     # Used by lftools scripts to parse XML
381     ensure_ubuntu_install xmlstarlet
382
383     # Haskel Packages
384     # Cabal update fails on a 1G system so workaround that with a swap file
385     dd if=/dev/zero of=/tmp/swap bs=1M count=1024
386     mkswap /tmp/swap
387     swapon /tmp/swap
388
389     ensure_ubuntu_install cabal-install
390     cabal update
391     cabal install --bindir=/usr/local/bin "shellcheck-0.4.6"  # Pin shellcheck version
392
393     # openldap dev headers are required for lftools
394     ensure_ubuntu_install libldap2-dev libssl-dev libsasl2-dev
395
396     # NLTK_DATA Cache: many jobs that use coala pull down nltk_data
397     wget -nv -O /opt/nltk_data.zip https://github.com/nltk/nltk_data/archive/gh-pages.zip
398
399     # --- END LFTOOLS DEPS
400     ######################
401
402     # Install sysstat
403     ensure_ubuntu_install sysstat
404     sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
405     enable_service sysstat
406
407     # Install haveged to avoid low entropy rejecting ssh connections
408     apt-get install haveged
409     update-rc.d haveged defaults
410
411     # Disable unattended upgrades & daily updates
412     echo '---> Disabling automatic daily upgrades'
413     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
414     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
415
416     # Install packaging job dependencies for building debs
417     ensure_ubuntu_install  build-essential devscripts equivs dh-systemd python-yaml \
418                     python-jinja2 gdebi
419 }
420
421 all_systems() {
422     # To handle the prompt style that is expected all over the environment
423     # with how use use robotframework we need to make sure that it is
424     # consistent for any of the users that are created during dynamic spin
425     # ups
426     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
427
428     # Do any Distro specific installations here
429     echo "Checking distribution"
430     FACTER_OS=$(/usr/bin/facter operatingsystem)
431     case "$FACTER_OS" in
432         RedHat|CentOS)
433             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
434                 echo
435                 echo "---> CentOS 7"
436                 echo "No extra steps currently for CentOS 7"
437                 echo
438             else
439                 echo "---> CentOS 6"
440                 echo "Installing ODL YUM repo"
441                 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
442             fi
443         ;;
444         *)
445             echo "---> $FACTER_OS found"
446             echo "No extra steps for $FACTER_OS"
447         ;;
448     esac
449 }
450
451 echo "---> Attempting to detect OS"
452 # upstream cloud images use the distro name as the initial user
453 ORIGIN=$(if [ -e /etc/redhat-release ]
454     then
455         echo redhat
456     else
457         echo ubuntu
458     fi)
459 #ORIGIN=$(logname)
460
461 case "${ORIGIN}" in
462     fedora|centos|redhat)
463         echo "---> RH type system detected"
464         rh_systems
465     ;;
466     ubuntu)
467         echo "---> Ubuntu system detected"
468         ubuntu_systems
469     ;;
470     *)
471         echo "---> Unknown operating system"
472     ;;
473 esac
474
475 # execute steps for all systems
476 all_systems