Changes needed for vagrant in the ODL Priv Cloud
[releng/builder.git] / vagrant / lib / 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 -q
49     yum update -y -q
50
51     # add in components we need or want on systems
52     echo "---> Installing base packages"
53     yum install -y -q @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
54     # separate group installs from package installs since a non-existing
55     # group with dnf based systems (F21+) will fail the install if such
56     # a group does not exist
57     yum install -y -q unzip xz puppet git perl-XML-XPath
58
59     # All of our systems require Java (because of Jenkins)
60     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
61     # default
62
63     echo "---> Configuring OpenJDK"
64     yum install -y -q 'java-*-openjdk-devel'
65
66     FACTER_OS=$(/usr/bin/facter operatingsystem)
67     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
68     case "$FACTER_OS" in
69         Fedora)
70             if [ "$FACTER_OSVER" -ge "21" ]
71             then
72                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
73             else
74                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
75                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
76             fi
77         ;;
78         *)
79             alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
80             alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
81         ;;
82     esac
83 }
84
85 ubuntu_systems() {
86     # Ignore SELinux since slamming that onto Ubuntu leads to
87     # frustration
88
89     # Allow jenkins access to update-alternatives command to switch java version
90     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
91 Defaults:jenkins !requiretty
92 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
93 EOF
94
95     export DEBIAN_FRONTEND=noninteractive
96     cat <<EOF >> /etc/apt/apt.conf
97 APT {
98   Get {
99     Assume-Yes "true";
100     allow-change-held-packages "true";
101     allow-downgrades "true";
102     allow-remove-essential "true";
103   };
104 };
105
106 Dpkg::Options {
107   "--force-confdef";
108   "--force-confold";
109 };
110
111 EOF
112
113     echo "---> Updating operating system"
114     apt-get update
115     apt-get upgrade
116
117     # add in stuff we know we need
118     echo "---> Installing base packages"
119     apt-get install unzip xz-utils puppet git libxml-xpath-perl
120
121     # install Java 7
122     echo "---> Configuring OpenJDK"
123     apt-get install openjdk-7-jdk
124
125     # make jdk8 available
126     add-apt-repository -y ppa:openjdk-r/ppa
127     apt-get update
128     # We need to force openjdk-8-jdk to install
129     apt-get install openjdk-8-jdk
130
131     # make sure that we still default to openjdk 7
132     update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
133     update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
134 }
135
136 all_systems() {
137     # To handle the prompt style that is expected all over the environment
138     # with how use use robotframework we need to make sure that it is
139     # consistent for any of the users that are created during dynamic spin
140     # ups
141     echo 'PS1="[\u@\h \W]> "' >> /etc/skel/.bashrc
142
143     # Do any Distro specific installations here
144     echo "Checking distribution"
145     FACTER_OS=$(/usr/bin/facter operatingsystem)
146     case "$FACTER_OS" in
147         RedHat|CentOS)
148             if [ "$(/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1)" = "7" ]; then
149                 echo
150                 echo "---> CentOS 7"
151                 echo "No extra steps currently for CentOS 7"
152                 echo
153             else
154                 echo "---> CentOS 6"
155                 echo "Installing ODL YUM repo"
156                 yum install -q -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
157             fi
158         ;;
159         *)
160             echo "---> $FACTER_OS found"
161             echo "No extra steps for $FACTER_OS"
162         ;;
163     esac
164 }
165
166 echo "---> Attempting to detect OS"
167 # upstream cloud images use the distro name as the initial user
168 ORIGIN=$(logname)
169
170 case "${ORIGIN}" in
171     fedora|centos)
172         echo "---> RH type system detected"
173         rh_systems
174     ;;
175     ubuntu)
176         echo "---> Ubuntu system detected"
177         ubuntu_systems
178     ;;
179     *)
180         echo "---> Unknown operating system"
181     ;;
182 esac
183
184 # execute steps for all systems
185 all_systems