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