Merge "Add git-review to base system"
[ci-management.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     echo "---> Updating operating system"
42     yum clean all -q
43     yum install -y -q deltarpm
44     yum update -y -q
45
46     # add in components we need or want on systems
47     echo "---> Installing base packages"
48     yum install -y -q @base https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
49     # separate group installs from package installs since a non-existing
50     # group with dnf based systems (F21+) will fail the install if such
51     # a group does not exist
52     yum install -y -q unzip xz puppet git git-review perl-XML-XPath wget make
53
54     # All of our systems require Java (because of Jenkins)
55     # Install all versions of the OpenJDK devel but force 1.7.0 to be the
56     # default
57
58     echo "---> Configuring OpenJDK"
59     yum install -y -q 'java-*-openjdk-devel'
60
61     FACTER_OS=$(/usr/bin/facter operatingsystem)
62     FACTER_OSVER=$(/usr/bin/facter operatingsystemrelease)
63     case "$FACTER_OS" in
64         Fedora)
65             if [ "$FACTER_OSVER" -ge "21" ]
66             then
67                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
68             else
69                 alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
70                 alternatives --set java_sdk_openjdk /usr/lib/jvm/java-1.7.0-openjdk.x86_64
71             fi
72         ;;
73         *)
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         ;;
77     esac
78
79     # Needed to parse OpenStack commands used by infra
80     # stack commands to initialize Heat template based systems.
81     yum install -y jq
82 }
83
84 ubuntu_systems() {
85     # Ignore SELinux since slamming that onto Ubuntu leads to
86     # frustration
87
88     export DEBIAN_FRONTEND=noninteractive
89     cat <<EOF >> /etc/apt/apt.conf
90 APT {
91   Get {
92     Assume-Yes "true";
93     allow-change-held-packages "true";
94     allow-downgrades "true";
95     allow-remove-essential "true";
96   };
97 };
98
99 Dpkg::Options {
100   "--force-confdef";
101   "--force-confold";
102 };
103
104 EOF
105
106     echo "---> Updating operating system"
107     apt-get update -qq > /dev/null
108     apt-get upgrade -qq > /dev/null
109
110     # add in stuff we know we need
111     echo "---> Installing base packages"
112     apt-get install -qq unzip xz-utils puppet git git-review libxml-xpath-perl make wget > /dev/null
113
114     # install Java 7
115     echo "---> Configuring OpenJDK"
116     apt-get install -qq openjdk-7-jdk > /dev/null
117
118     # make jdk8 available
119     add-apt-repository -y ppa:openjdk-r/ppa > /dev/null
120     apt-get update -qq > /dev/null
121     # We need to force openjdk-8-jdk to install
122     apt-get install -qq openjdk-8-jdk > /dev/null
123
124     # make sure that we still default to openjdk 7
125     update-alternatives --set java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
126     update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
127
128     # Needed to parse OpenStack commands used by infra
129     # stack commands to initialize Heat template based systems.
130     apt-get install -qq jq > /dev/null
131
132     # disable unattended upgrades & daily updates
133     echo '---> Disabling automatic daily upgrades'
134     sed -ine 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
135     echo 'APT::Periodic::Unattended-Upgrade "0";' >> /etc/apt/apt.conf.d/10periodic
136 }
137
138 all_systems() {
139     # Allow jenkins access to update-alternatives command to switch java version
140     cat <<EOF >/etc/sudoers.d/89-jenkins-user-defaults
141 Defaults:jenkins !requiretty
142 jenkins ALL = NOPASSWD: /usr/bin/update-alternatives
143 EOF
144
145     # Do any Distro specific installations here
146     echo "Checking distribution"
147     FACTER_OS=$(/usr/bin/facter operatingsystem)
148     case "$FACTER_OS" in
149         *)
150             echo "---> $FACTER_OS found"
151             echo "No extra steps for $FACTER_OS"
152         ;;
153     esac
154 }
155
156 echo "---> Attempting to detect OS"
157 # upstream cloud images use the distro name as the initial user
158 ORIGIN=$(if [ -e /etc/redhat-release ]
159     then
160         echo redhat
161     else
162         echo ubuntu
163     fi)
164 #ORIGIN=$(logname)
165
166 case "${ORIGIN}" in
167     fedora|centos|redhat)
168         echo "---> RH type system detected"
169         rh_systems
170     ;;
171     ubuntu)
172         echo "---> Ubuntu system detected"
173         ubuntu_systems
174     ;;
175     *)
176         echo "---> Unknown operating system"
177     ;;
178 esac
179
180 # execute steps for all systems
181 all_systems