fix(vpp/hs-test): fix hst debug job to build/test vpp debug image
[ci-management.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 rh_systems() {
9     # Handle the occurance where SELINUX is actually disabled
10     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
11     MODE=$(echo "$SELINUX" | cut -f 2 -d '=')
12     case "$MODE" in
13         permissive)
14             echo "************************************"
15             echo "** SYSTEM ENTERING ENFORCING MODE **"
16             echo "************************************"
17             # make sure that the filesystem is properly labelled.
18             # it could be not fully labeled correctly if it was just switched
19             # from disabled, the autorelabel misses some things
20             # skip relabelling on /dev as it will generally throw errors
21             restorecon -R -e /dev /
22
23             # enable enforcing mode from the very start
24             setenforce enforcing
25
26             # configure system for enforcing mode on next boot
27             sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
28         ;;
29         disabled)
30             sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
31             touch /.autorelabel
32
33             echo "*******************************************"
34             echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **"
35             echo "*******************************************"
36         ;;
37         enforcing)
38             echo "*********************************"
39             echo "** SYSTEM IS IN ENFORCING MODE **"
40             echo "*********************************"
41         ;;
42     esac
43
44     echo "---> Updating operating system"
45     yum clean all
46     yum install -y deltarpm
47     yum update -y
48
49     # add in components we need or want on systems
50     echo "---> Installing base packages"
51     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
52     # separate group installs from package installs since a non-existing
53     # group with dnf based systems (F21+) will fail the install if such
54     # a group does not exist
55     yum install -y unzip xz puppet git git-review perl-XML-XPath wget make
56
57     # All of our systems require Java (because of Jenkins)
58     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
59     # default
60
61     echo "---> Configuring OpenJDK"
62     yum install -y 'java-*-openjdk-devel'
63
64     FACTER_OS=$(/usr/bin/facter operatingsystem)
65     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
66     case "$FACTER_OS" in
67         Fedora)
68             if [ "$FACTER_OSVER" -ge "21" ]
69             then
70                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
71             else
72                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
73                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
74             fi
75         ;;
76         RedHat|CentOS)
77             if [ "$(echo $FACTER_OSVER | cut -d'.' -f1)" -ge "7" ]
78             then
79                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
80             else
81                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
82                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
83             fi
84         ;;
85         *)
86             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
87             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
88         ;;
89     esac
90
91     # Needed to parse OpenStack commands used by infra stack commands
92     # to initialize Heat template based systems.
93     yum install -y jq
94 }
95
96 ubuntu_systems() {
97     # Ignore SELinux since slamming that onto Ubuntu leads to
98     # frustration
99
100     export DEBIAN_FRONTEND=noninteractive
101     cat <<EOF >> /etc/apt/apt.conf
102 APT {
103   Get {
104     Assume-Yes "true";
105     allow-change-held-packages "true";
106     allow-downgrades "true";
107     allow-remove-essential "true";
108   };
109 };
110
111 Dpkg::Options {
112   "--force-confdef";
113   "--force-confold";
114 };
115
116 EOF
117
118     # Wrap apt-get to wait for the lock to become available for operation
119     # http://askubuntu.com/questions/132059/how-to-make-a-package-manager-wait-if-another-instance-of-apt-is-running
120     cat << 'EOF' >> /usr/local/bin/apt-get
121 #!/bin/bash
122
123 TTY=$(tty)
124 test -z "$TTY" && TERM=dumb
125
126 i=0
127 tput sc
128 LOCKFILES="/var/lib/dpkg/lock /var/lib/apt/lists/lock /var/cache/apt/archives/lock"
129 while fuser ${LOCKFILES} >/dev/null 2>&1 ; do
130     case $(($i % 4)) in
131         0 ) j="-" ;;
132         1 ) j="\\" ;;
133         2 ) j="|" ;;
134         3 ) j="/" ;;
135     esac
136     tput rc
137     echo -en "\r[$j] Waiting for other software managers to finish..."
138     sleep 0.5
139     ((i=i+1))
140 done
141
142 if [ $i==0 ]
143 then
144   /usr/bin/apt-get "$@"
145 else
146   sleep 1
147   exec /usr/local/bin/apt-get "$@"
148 fi
149
150 EOF
151     chmod +x /usr/local/bin/apt-get
152
153     echo "---> Updating operating system"
154     apt-get update
155     apt-get upgrade
156
157     # add in stuff we know we need
158     echo "---> Installing base packages"
159     apt-get install unzip xz-utils puppet git git-review libxml-xpath-perl make wget
160
161     # install Java 7
162     echo "---> Configuring OpenJDK"
163     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
164     case "$FACTER_OSVER" in
165         14.04)
166             apt-get install openjdk-7-jdk
167             # make jdk8 available
168             add-apt-repository -y ppa:openjdk-r/ppa
169             apt-get update
170             # We need to force openjdk-8-jdk to install
171             apt-get install openjdk-8-jdk
172             # make sure that we still default to openjdk 7
173             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
174             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
175
176             # disable auto-update service?
177             if [ -f /etc/cron.daily/apt ]
178             then
179                 rm -rf /etc/cron.daily/apt
180             fi
181         ;;
182         16.04)
183             apt-get install openjdk-8-jdk
184
185             # force auto-update services off and mask them so they can't
186             # be started
187             for i in apt-daily.{service,timer}
188             do
189                 systemctl disable ${i}
190                 systemctl mask ${i}
191             done
192         ;;
193         *)
194             echo "---> Unknown Ubuntu version $FACTER_OSVER"
195             exit 1
196         ;;
197     esac
198
199
200
201     # Needed to parse OpenStack commands used by infra stack commands
202     # to initialize Heat template based systems.
203     apt-get install jq
204
205     # disable unattended upgrades & daily updates
206     echo '---> Disabling automatic daily upgrades'
207     grep -lR 'APT::Periodic' /etc/apt/apt.conf.d/ | perl -pi -e 's/"1"/"0"/g'
208
209 }
210
211 opensuse_systems() {
212     # SELinux?
213
214     # Replacing cloud.cfg, it's not supported by cloud-init
215     cp /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.orig
216
217     # Clean and add repos and refresh
218     zypper clean -a
219     zypper --non-interactive --gpg-auto-import-keys ar \
220         http://download.opensuse.org/update/leap/42.3/oss/openSUSE:Leap:42.3:Update.repo
221     zypper --gpg-auto-import-keys ref
222     zypper --non-interactive --gpg-auto-import-keys ar \
223         http://download.opensuse.org/repositories/Cloud:/Tools/openSUSE_Leap_42.3/ Cloud:Tools.repo
224
225     # Add in components we need or want on systems
226     echo "---> Installing base packages"
227     zypper -n install unzip xz puppet perl-XML-XPath
228
229     # Instlal tools
230     echo "---> Installing tools packages"
231     zypper -n install git git-review wget libstdc++-devel ruby-devel
232
233     # All of our systems require Java (because of Jenkins)
234     echo "---> Configuring OpenJDK"
235     zypper -n install 'java-*-openjdk-devel'
236
237     # Needed to parse OpenStack commands used by infra stack commands
238     # to initialize Heat template based systems.
239     zypper -n install jq
240
241 }
242
243 all_systems() {
244     # Allow jenkins access to update-alternatives command to switch java version
245     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
246 Defaults:jenkins !requiretty
247 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
248 EOF
249
250     # Enable Hugepages
251     puppet module install thias-sysctl --version 1.0.6
252     puppet apply -e "sysctl {'vm.nr_hugepages': value => '128'}"
253     puppet apply -e "file { '/mnt/huge': ensure => directory }"
254     puppet apply -e "mount { '/mnt/huge': ensure => mounted, atboot => true, \
255       device => 'none', fstype => 'hugetlbfs', options => 'mode=01777' }"
256
257     # Do any Distro specific installations here
258     echo "Checking distribution"
259     FACTER_OS=$(/usr/bin/facter operatingsystem)
260     case "$FACTER_OS" in
261         *)
262             echo "---> $FACTER_OS found"
263             echo "No extra steps for $FACTER_OS"
264         ;;
265     esac
266 }
267
268 echo "---> Attempting to detect OS"
269 # upstream cloud images use the distro name as the initial user
270 ORIGIN=$(if [ -e /etc/redhat-release ]
271     then
272         echo redhat
273     else [ -e /etc/os-release ]
274       DIST="$(grep "\<ID\>" /etc/os-release)"
275       if [ $DIST = "ID=ubuntu" ]
276       then
277         echo ubuntu
278       else
279         echo opensuse
280       fi
281     fi)
282
283 case "${ORIGIN}" in
284     fedora|centos|redhat)
285         echo "---> RH type system detected"
286         rh_systems
287     ;;
288     ubuntu)
289         echo "---> Ubuntu system detected"
290         ubuntu_systems
291     ;;
292     opensuse)
293         echo "---> openSuSE system detected"
294         opensuse_systems
295     ;;
296     *)
297         echo "---> Unknown operating system"
298     ;;
299 esac
300
301 # execute steps for all systems
302 all_systems