Fix if-statement for pkg retry loop
[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 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     # Allow jenkins access to alternatives command to switch java version
45     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
46 Defaults:jenkins !requiretty
47 jenkins ALL = NOPASSWD: /usr/sbin/alternatives
48 EOF
49
50     echo "---> Updating operating system"
51     yum clean all
52     yum install -y deltarpm
53
54     # Workaround for kernel panic issue that appears sometimes after kernel update
55     #     https://www.centos.org/forums/viewtopic.php?t=22425
56     yum remove -y kernel
57     yum update -y
58     yum install -y kernel
59
60     # add in components we need or want on systems
61     echo "---> Installing base packages"
62     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
63     # separate group installs from package installs since a non-existing
64     # group with dnf based systems (F21+) will fail the install if such
65     # a group does not exist
66     yum install -y unzip xz puppet git git-review perl-XML-XPath ShellCheck
67
68     # All of our systems require Java (because of Jenkins)
69     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
70     # default
71
72     echo "---> Configuring OpenJDK"
73     yum install -y 'java-*-openjdk-devel'
74
75     FACTER_OS=$(/usr/bin/facter operatingsystem)
76     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
77     case "$FACTER_OS" in
78         Fedora)
79             if [ "$FACTER_OSVER" -ge "21" ]
80             then
81                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
82             else
83                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
84                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
85             fi
86         ;;
87         RedHat|CentOS)
88             if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
89             then
90                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
91             else
92                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
93                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
94             fi
95         ;;
96         *)
97             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
98             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
99         ;;
100     esac
101
102     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
103     # to initialize Heat template based systems.
104     yum install -y jq
105
106     # install haveged to avoid low entropy rejecting ssh connections
107     yum install -y haveged
108     systemctl enable haveged.service
109
110 }
111
112 ubuntu_systems() {
113     # Ignore SELinux since slamming that onto Ubuntu leads to
114     # frustration
115
116     # Allow jenkins access to update-alternatives command to switch java version
117     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
118 Defaults:jenkins !requiretty
119 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
120 EOF
121
122     export DEBIAN_FRONTEND=noninteractive
123     cat <<EOF >> /etc/apt/apt.conf
124 APT {
125   Get {
126     Assume-Yes "true";
127     allow-change-held-packages "true";
128     allow-downgrades "true";
129     allow-remove-essential "true";
130   };
131 };
132
133 Dpkg::Options {
134   "--force-confdef";
135   "--force-confold";
136 };
137
138 EOF
139
140     # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
141     sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
142
143     echo "---> Updating operating system"
144
145     # add additional repositories
146     sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
147
148     echo "---> Installing base packages"
149     # Use retry loop to install packages for failing mirrors
150     for i in {0..5}
151     do
152         echo "Attempt $i of installing base packages..."
153         apt-get clean
154         apt-get update -m
155         apt-get upgrade -m
156         apt-get dist-upgrade -m
157
158         for pkg in unzip xz-utils puppet git git-review libxml-xpath-perl
159         do
160             if dpkg-query -W -f='${Status}' $pkg 2>/dev/null | grep -c "ok installed"; then
161                 apt-get install $pkg
162             fi
163         done
164     done
165
166     # install Java 7
167     echo "---> Configuring OpenJDK"
168     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
169     case "$FACTER_OSVER" in
170         14.04)
171             apt-get install openjdk-7-jdk
172             # make jdk8 available
173             add-apt-repository -y ppa:openjdk-r/ppa
174             apt-get update
175             # We need to force openjdk-8-jdk to install
176             apt-get install openjdk-8-jdk
177             # make sure that we still default to openjdk 7
178             update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
179             update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
180         ;;
181         16.04)
182             apt-get install openjdk-8-jdk
183         ;;
184         *)
185             echo "---> Unknown Ubuntu version $FACTER_OSVER"
186             exit 1
187         ;;
188     esac
189
190     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
191     # to initialize Heat template based systems.
192     apt-get install jq
193
194     # install haveged to avoid low entropy rejecting ssh connections
195     apt-get install haveged
196     update-rc.d haveged defaults
197
198     # disable unattended upgrades & daily updates
199     echo '---> Disabling automatic daily upgrades'
200     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
201     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
202 }
203
204 all_systems() {
205     # To handle the prompt style that is expected all over the environment
206     # with how use use robotframework we need to make sure that it is
207     # consistent for any of the users that are created during dynamic spin
208     # ups
209     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
210
211     # Do any Distro specific installations here
212     echo "Checking distribution"
213     FACTER_OS=$(/usr/bin/facter operatingsystem)
214     case "$FACTER_OS" in
215         RedHat|CentOS)
216             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
217                 echo
218                 echo "---> CentOS 7"
219                 echo "No extra steps currently for CentOS 7"
220                 echo
221             else
222                 echo "---> CentOS 6"
223                 echo "Installing ODL YUM repo"
224                 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
225             fi
226         ;;
227         *)
228             echo "---> $FACTER_OS found"
229             echo "No extra steps for $FACTER_OS"
230         ;;
231     esac
232 }
233
234 echo "---> Attempting to detect OS"
235 # upstream cloud images use the distro name as the initial user
236 ORIGIN=$(if [ -e /etc/redhat-release ]
237     then
238         echo redhat
239     else
240         echo ubuntu
241     fi)
242 #ORIGIN=$(logname)
243
244 case "${ORIGIN}" in
245     fedora|centos|redhat)
246         echo "---> RH type system detected"
247         rh_systems
248     ;;
249     ubuntu)
250         echo "---> Ubuntu system detected"
251         ubuntu_systems
252     ;;
253     *)
254         echo "---> Unknown operating system"
255     ;;
256 esac
257
258 # execute steps for all systems
259 all_systems