Merge "Make distribution-check job logic more strict"
[releng/builder.git] / packer / provision / baseline.sh
1 #!/bin/bash
2
3 # vim: ts=4 sw=4 sts=4 et tw=72 :
4
5 rh_systems() {
6     # Handle the occurance where SELINUX is actually disabled
7     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
8     MODE=$(echo "$SELINUX" | cut -f 2 -d '=')
9     case "$MODE" in
10         permissive)
11             echo "************************************"
12             echo "** SYSTEM ENTERING ENFORCING MODE **"
13             echo "************************************"
14             # make sure that the filesystem is properly labelled.
15             # it could be not fully labeled correctly if it was just switched
16             # from disabled, the autorelabel misses some things
17             # skip relabelling on /dev as it will generally throw errors
18             restorecon -R -e /dev /
19
20             # enable enforcing mode from the very start
21             setenforce enforcing
22
23             # configure system for enforcing mode on next boot
24             sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
25         ;;
26         disabled)
27             sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
28             touch /.autorelabel
29
30             echo "*******************************************"
31             echo "** SYSTEM REQUIRES A RESTART FOR SELINUX **"
32             echo "*******************************************"
33         ;;
34         enforcing)
35             echo "*********************************"
36             echo "** SYSTEM IS IN ENFORCING MODE **"
37             echo "*********************************"
38         ;;
39     esac
40
41     # Allow jenkins access to alternatives command to switch java version
42     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
43 Defaults:jenkins !requiretty
44 jenkins ALL = NOPASSWD: /usr/sbin/alternatives
45 EOF
46
47     echo "---> Updating operating system"
48     yum clean all
49     yum install -y deltarpm
50     yum update -y
51
52     # add in components we need or want on systems
53     echo "---> Installing base packages"
54     yum install -y @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
55     # separate group installs from package installs since a non-existing
56     # group with dnf based systems (F21+) will fail the install if such
57     # a group does not exist
58     yum install -y unzip xz puppet git git-review perl-XML-XPath
59
60     # All of our systems require Java (because of Jenkins)
61     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
62     # default
63
64     echo "---> Configuring OpenJDK"
65     yum install -y 'java-*-openjdk-devel'
66
67     FACTER_OS=$(/usr/bin/facter operatingsystem)
68     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
69     case "$FACTER_OS" in
70         Fedora)
71             if [ "$FACTER_OSVER" -ge "21" ]
72             then
73                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
74             else
75                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
76                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
77             fi
78         ;;
79         *)
80             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
81             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
82         ;;
83     esac
84
85     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
86     # to initialize Heat template based systems.
87     yum install -y jq
88
89     # install haveged to avoid low entropy rejecting ssh connections
90     yum install -y haveged
91     systemctl enable haveged.service
92
93 }
94
95 ubuntu_systems() {
96     # Ignore SELinux since slamming that onto Ubuntu leads to
97     # frustration
98
99     # Allow jenkins access to update-alternatives command to switch java version
100     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
101 Defaults:jenkins !requiretty
102 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
103 EOF
104
105     export DEBIAN_FRONTEND=noninteractive
106     cat <<EOF >> /etc/apt/apt.conf
107 APT {
108   Get {
109     Assume-Yes "true";
110     allow-change-held-packages "true";
111     allow-downgrades "true";
112     allow-remove-essential "true";
113   };
114 };
115
116 Dpkg::Options {
117   "--force-confdef";
118   "--force-confold";
119 };
120
121 EOF
122
123     echo "---> Updating operating system"
124     apt-get update
125     apt-get upgrade
126
127     # add in stuff we know we need
128     echo "---> Installing base packages"
129     apt-get install unzip xz-utils puppet git git-review libxml-xpath-perl
130
131     # install Java 7
132     echo "---> Configuring OpenJDK"
133     apt-get install openjdk-7-jdk
134
135     # make jdk8 available
136     add-apt-repository -y ppa:openjdk-r/ppa
137     apt-get update
138     # We need to force openjdk-8-jdk to install
139     apt-get install openjdk-8-jdk
140
141     # make sure that we still default to openjdk 7
142     update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
143     update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
144
145     # Needed to parse OpenStack commands used by opendaylight-infra stack commands
146     # to initialize Heat template based systems.
147     apt-get install jq
148
149     # install haveged to avoid low entropy rejecting ssh connections
150     apt-get install haveged
151     update-rc.d haveged defaults
152
153     # disable unattended upgrades & daily updates
154     echo '---> Disabling automatic daily upgrades'
155     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
156     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
157 }
158
159 all_systems() {
160     # To handle the prompt style that is expected all over the environment
161     # with how use use robotframework we need to make sure that it is
162     # consistent for any of the users that are created during dynamic spin
163     # ups
164     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
165
166     # Do any Distro specific installations here
167     echo "Checking distribution"
168     FACTER_OS=$(/usr/bin/facter operatingsystem)
169     case "$FACTER_OS" in
170         RedHat|CentOS)
171             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
172                 echo
173                 echo "---> CentOS 7"
174                 echo "No extra steps currently for CentOS 7"
175                 echo
176             else
177                 echo "---> CentOS 6"
178                 echo "Installing ODL YUM repo"
179                 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
180             fi
181         ;;
182         *)
183             echo "---> $FACTER_OS found"
184             echo "No extra steps for $FACTER_OS"
185         ;;
186     esac
187 }
188
189 echo "---> Attempting to detect OS"
190 # upstream cloud images use the distro name as the initial user
191 ORIGIN=$(if [ -e /etc/redhat-release ]
192     then
193         echo redhat
194     else
195         echo ubuntu
196     fi)
197 #ORIGIN=$(logname)
198
199 case "${ORIGIN}" in
200     fedora|centos|redhat)
201         echo "---> RH type system detected"
202         rh_systems
203     ;;
204     ubuntu)
205         echo "---> Ubuntu system detected"
206         ubuntu_systems
207     ;;
208     *)
209         echo "---> Unknown operating system"
210     ;;
211 esac
212
213 # execute steps for all systems
214 all_systems