-
Notifications
You must be signed in to change notification settings - Fork 100
/
os-generic-bootstrap.sh
315 lines (275 loc) · 8.15 KB
/
os-generic-bootstrap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/sh
#
# Copyright (c) 2017 Cloudera, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# This script will bootstrap these OSes:
# - CentOS 6
# - CentOS 7
# - RHEL 6
# - RHEL 7
#
# Notes and notable differences between OSes:
# - CentOS and RHEL 6 use dhclient
# - CentOS and RHEL 7 use NetworkManager
#
#
# Functions
#
#
# CentOS and RHEL 6 use dhclient. Add a script to be automatically invoked when interface comes up.
# Function not indented so EOF works.
#
dhclient_6()
{
# dhclient-exit-hooks explained in dhclient-script man page: http://linux.die.net/man/8/dhclient-script
# cat a here-doc representation of the hooks to the appropriate file
cat > /etc/dhcp/dhclient-exit-hooks <<"EOF"
#!/bin/bash
printf "\ndhclient-exit-hooks running...\n\treason:%s\n\tinterface:%s\n" "${reason:?}" "${interface:?}"
# only execute on the primary nic
if [ "$interface" != "eth0" ]
then
exit 0;
fi
# when we have a new IP, perform nsupdate
if [ "$reason" = BOUND ] || [ "$reason" = RENEW ] || [ "$reason" = REBIND ] || [ "$reason" = REBOOT ]
then
printf "\tnew_ip_address:%s\n" "${new_ip_address:?}"
host=$(hostname -s)
domain=$(nslookup $(grep -i nameserver /etc/resolv.conf | head -n 1 | cut -d ' ' -f 2) | grep -i name | cut -d ' ' -f 3 | cut -d '.' -f 2- | rev | cut -c 2- | rev)
IFS='.' read -ra ipparts <<< "$new_ip_address"
ptrrec="$(printf %s "$new_ip_address." | tac -s.)in-addr.arpa"
nsupdatecmds=$(mktemp -t nsupdate.XXXXXXXXXX)
resolvconfupdate=$(mktemp -t resolvconfupdate.XXXXXXXXXX)
echo updating resolv.conf
grep -iv "search" /etc/resolv.conf > "$resolvconfupdate"
echo "search $domain" >> "$resolvconfupdate"
cat "$resolvconfupdate" > /etc/resolv.conf
echo "Attempting to register $host.$domain and $ptrrec"
{
echo "update delete $host.$domain a"
echo "update add $host.$domain 600 a $new_ip_address"
echo "send"
echo "update delete $ptrrec ptr"
echo "update add $ptrrec 600 ptr $host.$domain"
echo "send"
} > "$nsupdatecmds"
nsupdate "$nsupdatecmds"
fi
#done
exit 0;
EOF
chmod 755 /etc/dhcp/dhclient-exit-hooks
service network restart
# Confirm DNS record has been updated, retry if update did not work
i=0
until [ $i -ge 5 ]
do
sleep 5
i=$((i+1))
hostname | nslookup && break
service network restart
done
if [ $i -ge 5 ]; then
echo "DNS update failed"
exit 1
fi
}
centos_6()
{
echo "CentOS 6"
# execute the CentOS / RHEL 6 dhclient-exit-hooks setup
dhclient_6
}
rhel_6()
{
echo "RHEL 6"
# rewrite SELINUX config to disabled and turn off enforcement
sed -i.bak "s/^SELINUX=.*$/SELINUX=disabled/" /etc/selinux/config
setenforce 0
# stop firewall and disable
service iptables stop
chkconfig iptables off
# update config to disable IPv6 and disable
echo "# Disable IPv6" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1
# execute the CentOS / RHEL 6 dhclient-exit-hooks setup
dhclient_6
}
#
# CentOS and RHEL 7 use NetworkManager. Add a script to be automatically invoked when interface comes up.
# Function not indented so EOF works.
#
networkmanager_7()
{
cat > /etc/NetworkManager/dispatcher.d/12-register-dns <<"EOF"
#!/bin/bash
# NetworkManager Dispatch script
# Deployed by Cloudera Altus Director Bootstrap
#
# Expected arguments:
# $1 - interface
# $2 - action
#
# See for info: http://linux.die.net/man/8/networkmanager
# Register A and PTR records when interface comes up
# only execute on the primary nic
if [ "$1" != "eth0" ] || [ "$2" != "up" ]
then
exit 0;
fi
# when we have a new IP, perform nsupdate
new_ip_address="$DHCP4_IP_ADDRESS"
host=$(hostname -s)
domain=$(nslookup $(grep -i nameserver /etc/resolv.conf | head -n 1 | cut -d ' ' -f 2) | grep -i name | cut -d ' ' -f 3 | cut -d '.' -f 2- | rev | cut -c 2- | rev)
IFS='.' read -ra ipparts <<< "$new_ip_address"
ptrrec="$(printf %s "$new_ip_address." | tac -s.)in-addr.arpa"
nsupdatecmds=$(mktemp -t nsupdate.XXXXXXXXXX)
resolvconfupdate=$(mktemp -t resolvconfupdate.XXXXXXXXXX)
echo updating resolv.conf
grep -iv "search" /etc/resolv.conf > "$resolvconfupdate"
echo "search $domain" >> "$resolvconfupdate"
cat "$resolvconfupdate" > /etc/resolv.conf
echo "Attempting to register $host.$domain and $ptrrec"
{
echo "update delete $host.$domain a"
echo "update add $host.$domain 600 a $new_ip_address"
echo "send"
echo "update delete $ptrrec ptr"
echo "update add $ptrrec 600 ptr $host.$domain"
echo "send"
} > "$nsupdatecmds"
nsupdate "$nsupdatecmds"
exit 0;
EOF
chmod 755 /etc/NetworkManager/dispatcher.d/12-register-dns
service network restart
# Confirm DNS record has been updated, retry if update did not work
i=0
until [ $i -ge 5 ]
do
sleep 5
i=$((i+1))
hostname | nslookup && break
service network restart
done
if [ $i -ge 5 ]; then
echo "DNS update failed"
exit 1
fi
}
centos_7()
{
echo "CentOS 7"
# execute the CentOS / RHEL 7 network manager setup
networkmanager_7
}
rhel_7()
{
echo "RHEL 7"
# rewrite SELINUX config to disable and turn off enforcement
sed -i.bak "s/^SELINUX=.*$/SELINUX=disabled/" /etc/selinux/config
setenforce 0
# stop firewall and disable
systemctl stop iptables
systemctl iptables off
# RHEL 7 uses firewalld
systemctl stop firewalld
systemctl disable firewalld
# Disable tuned so it does not overwrite sysctl.conf
service tuned stop
systemctl disable tuned
# Disable chrony so it does not conflict with ntpd installed by Director
systemctl stop chronyd
systemctl disable chronyd
# update config to disable IPv6 and disable
echo "# Disable IPv6" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
# swappiness is set by Director in /etc/sysctl.conf
# Poke sysctl to have it pickup the config change.
sysctl -p
# execute the CentOS / RHEL 7 network manager setup
networkmanager_7
}
#
# Main workflow
#
# ensure user is root
if [ "$(id -u)" -ne 0 ]
then
echo "Please run as root."
exit 1
fi
# find the OS and release
os=""
major_release=""
# if it's there, use lsb_release
if rpm -q redhat-lsb
then
os=$(lsb_release -si)
major_release=$(lsb_release -sr | cut -d '.' -f 1)
# if lsb_release isn't installed, use /etc/redhat-release
else
if grep "CentOS.* 6\\." /etc/redhat-release
then
os="CentOS"
major_release="6"
fi
if grep "CentOS.* 7\\." /etc/redhat-release
then
os="CentOS"
major_release="7"
fi
if grep "Red Hat Enterprise Linux Server release 6\\." /etc/redhat-release
then
os="RedHatEnterpriseServer"
major_release="6"
fi
if grep "Red Hat Enterprise Linux Server release 7\\." /etc/redhat-release
then
os="RedHatEnterpriseServer"
major_release="7"
fi
fi
echo "OS: $os $major_release"
# select the OS and run the appropriate setup script
not_supported_msg="OS $os $major_release is not supported."
if [ "$os" = "CentOS" ]; then
if [ "$major_release" = "6" ]; then
centos_6
elif [ "$major_release" = "7" ]; then
centos_7
else
echo "$not_supported_msg"
exit 1
fi
elif [ "$os" = "RedHatEnterpriseServer" ]; then
if [ "$major_release" = "6" ]; then
rhel_6
elif [ "$major_release" = "7" ]; then
rhel_7
else
echo "$not_supported_msg"
exit 1
fi
else
echo "$not_supported_msg"
exit 1
fi