-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreflight.sh
executable file
·306 lines (272 loc) · 10.9 KB
/
preflight.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
#!/bin/bash
###########################################################################################################################
# This script generates baremetal OCP configuration files and also validates physical environment is ready for deployment
# -Creates install-config.yml
# -Creates config_user.sh
# -Creates ironic_hosts.json
# -Validates DNS entries for api,ns1 and *.apps exists
# -Validates DHCP entries for external MAC addresses are present on network from DHCP server
# -Validates DNS for external host addresses are present
############################################################################################################################
howto(){
echo "Usage: preflight.sh -u username -p password -m master-0-ip,master-1-ip,master-2-ip -w worker-0-ip,worker-1-ip -o (power off nodes)"
echo "Example: preflight.sh -u root -p calvin -m 172.22.0.231,172.22.0.232,172.22.0.233 -w 172.22.0.234"
echo "Example: preflight.sh -u root -p calvin -d (use default settings where switches are not specified)"
}
df=0
power=0
while getopts u:p:m:w:doh option
do
case "${option}"
in
u) dracuser=${OPTARG};;
p) dracpassword=${OPTARG};;
m) mip=${OPTARG};;
w) wip=${OPTARG};;
d) df=1;;
o) power=1;;
h) howto; exit 0;;
\?) howto; exit 1;;
esac
done
SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
KNIUSER=`who am i|awk {'print $1'}`
export KNIUSER
chmod 755 check_dhcp.py
if [ $KNIUSER == "root" ]; then
echo "KNI-Preflight cannot be run as root and must use sudo"; exit 1
fi
if ([ -z "$dracuser" ] || [ -z "$dracpassword" ] || [ -z "$mip" ] || [ -z "$wip" ] && [ "$df" -eq "0" ]) then
howto
exit 1
fi
if ([ -z "$mip" ] && [ ! -z "$wip" ]) || ([ -z "$wip" ] && [ ! -z "$mip" ]) then
echo "When using -m or -w they must be specified together"; exit 1
fi
if ([ -z "$dracuser" ] && [ "$df" -eq "1" ]) then
dracuser="root"
fi
if ([ -z "$dracpassword" ] && [ "$df" -eq "1" ]) then
dracpassword="r3dh@tPW"
fi
if ([ -z "$mip" ] && [ "$df" -eq "1" ]) then
mip="172.22.0.231,172.22.0.232,172.22.0.233"
fi
if ([ -z "$wip" ] && [ "$df" -eq "1" ]) then
wip="172.22.0.234"
fi
if ([ "$power" -eq "1" ]) then
export power
powerstatus="Nodes will be powered off before deployment..."
else
powerstatus="Nodes will not be powered off before deployment..."
fi
if ([ "$df" -eq "1" ]) then
dfstatus="Using defaults where no arguments provided..."
else
dfstatus="Using user supplied arguments..."
fi
IFS=', ' read -r -a mipaddresses <<< "$mip"
IFS=', ' read -r -a wipaddresses <<< "$wip"
if ([ "${#mipaddresses[@]}" -ne "3" ]) then
echo "3 master nodes must be defined. Please try again."
exit 1
fi
if [ "${#wipaddresses[@]}" -lt "1" ]; then
echo "There needs to be at least 1 worker node defined. Please try again."
exit 1
fi
##################################################################
# Grab cluster and domain from discovery #
##################################################################
echo $dfstatus
echo $powerstatus
echo -n Discovering Cluster Name and Domain...
bootstrapip=`ip addr show baremetal| grep 'inet ' | cut -d/ -f1 | awk '{ print $2}'`
dnsname=`nslookup $bootstrapip|grep name| cut -d= -f2|sed s'/^ //'g|sed s'/.$//g'`
hostname=`echo $dnsname|awk -F. {'print $1'}`
clustername=`echo $dnsname|awk -F. {'print $2'}`
domain=`echo $dnsname|sed "s/$hostname.//g"|sed "s/$clustername.//g"`
echo "###">dhcps
echo "DiscoveryName DiscoveryValues">>dhcps
echo "-------------------- ---------------------">>dhcps
echo "Hostname_Long: $dnsname">>dhcps
echo "Hostname_Short: $hostname">>dhcps
echo "Clustername: $clustername">>dhcps
echo "Domain: $domain">>dhcps
echo "###">>dhcps
echo "Success"
##################################################################
# Build initial inventory file #
##################################################################
echo -n "Creating initial host inventory file..."
echo [bmcs]>hosts
mc=0
for ipaddr in "${mipaddresses[@]}"
do
echo "master-$mc bmcip=$ipaddr">>hosts
mc=$((mc+1))
done
wc=0
for ipaddr in "${wipaddresses[@]}"
do
echo "worker-$wc bmcip=$ipaddr">>hosts
wc=$((wc+1))
done
echo [bmcs:vars]>>hosts
echo bmcuser=$dracuser>>hosts
echo bmcpassword=$dracpassword>>hosts
echo domain=$domain>>hosts
echo cluster=$clustername>>hosts
echo mastercount=$mc>>hosts
echo workercount=$wc>>hosts
echo "Success"
#################################################################
# Determine External Network CIDR #
#################################################################
echo -n "Determining external network CIDR..."
BARNET=`/usr/bin/ipcalc -n "$(/usr/sbin/ip -o addr show|grep baremetal|grep -v inet6|awk {'print $4'})"|cut -f2 -d=`
BARCIDR=`/usr/bin/ipcalc -p "$(/usr/sbin/ip -o addr show|grep baremetal|grep -v inet6|awk {'print $4'})"|cut -f2 -d=`
wc=$((wc-1)) # reducing worker count by 1 since we don't use provisioning node
echo '[bootstrap]'>>hosts
echo localhost>>hosts
echo '[bootstrap:vars]'>>hosts
echo extcidrnet=$BARNET/$BARCIDR>>hosts
echo numworkers=$wc>>hosts
echo nummasters=$mc>>hosts
echo "Success"
##################################################################
# Determine provisioning interface and baremetal interface #
##################################################################
echo -n "Determining provisioning and baremetal interfaces..."
int_if=""
pro_if=""
lshw -quiet -class network | grep -A 1 "bus info" | grep name | awk -F': ' '{print $2}'|grep e | while read interface; do
if (`ip a|grep $interface|grep baremetal>/dev/null 2>&1`); then
echo "intif=$interface">>hosts
int_if="$interface"
elif (`ip a|grep $interface|grep provisioning>/dev/null 2>&1`); then
echo "proif=$interface">>hosts
pro_if="$interface"
else
if ((`ip addr show $interface| grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*">/dev/null 2>&1`) && (`ip link show $interface|grep "state UP">/dev/null 2>&1`) && [[ $int_if == "" ]]); then
echo "intif=$interface">>hosts
int_if="$interface"
fi
if ((! `ip addr show $interface| grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*">/dev/null 2>&1`) && (`ip link show $interface|grep "state UP">/dev/null 2>&1`) && [[ $pro_if == "" ]] ); then
echo "proif=$interface">>hosts
pro_if="$interface"
fi
fi
done
echo "Success"
##################################################################
# Run redfish.yml Playbook #
##################################################################
echo -n "Determining MAC addresses of baremetal nodes..."
firewall-cmd --zone=public --add-port=68/udp >/dev/null 2>&1
firewall-cmd --zone=public --add-port=67/udp >/dev/null 2>&1
if (ansible-playbook -i hosts redfish.yml >/dev/null 2>&1); then
echo "Success"
else
echo "Failed"; exit 1
fi
firewall-cmd --zone=public --remove-port=68/udp >/dev/null 2>&1
firewall-cmd --zone=public --remove-port=67/udp >/dev/null 2>&1
##################################################################
# Print Out Collected Data Allow for confirmation #
##################################################################
echo ""
head -n 7 dhcps | tail -n +2 | column -t
echo " "
tail -n +9 dhcps | column -t
echo ""
echo -n "Pausing for 30 seconds to confirm the information is correct (Y/N)?"
read -n 1 -r -t 30
if [[ $REPLY =~ ^[Nn]$ ]]
then
echo ""
echo "Information confirmed not correct exiting Preflight now"
exit 1
fi
echo ""
echo "Continuing with information above"
echo ""
##################################################################
# Run Make Configurations Playbook #
##################################################################
echo -n "Creating configuration files..."
if [ -f install-config.yaml ]; then
cp -f install-config.yaml install-config.yaml.orig
fi
if (ansible-playbook -i hosts make_configurations.yml >/dev/null 2>&1); then
echo "Success"
else
echo "Failed"; exit 1
fi
##################################################################
# Add pullsecret to install-config.yaml #
##################################################################
echo -n "Adding pullsecret to install-config.yaml..."
if [ -f $SCRIPTPATH/pull-secret ] && ( file pull-secret|grep ASCII>/dev/null 2>&1 ); then
PULLSECRET="pull-secret"
elif [ -f $SCRIPTPATH/pull-secret.txt ] && ( file pull-secret.txt|grep ASCII>/dev/null 2>&1 ); then
PULLSECRET="pull-secret.txt"
else
echo "Failed - pull-secret or pull-secret.txt file not found"; exit 1
fi
if [ -f "$PULLSECRET" ]; then
if ( ! grep registry.svc.ci.openshift.org $PULLSECRET>/dev/null 2>&1 ) || ( ! grep cloud.openshift.com $PULLSECRET>/dev/null 2>&1 ); then
echo "Failed - Invalid $PULLSECRET"; exit 1
fi
sed -i "s/^'//" $PULLSECRET
sed -i "s/'$//" $PULLSECRET
sed -i ':a;N;$!ba;s/\n//g' $PULLSECRET
sed -i "s/PULLSECRETHERE/$(sed 's:/:\\/:g' $PULLSECRET)/" install-config.yaml
sed -i "s/PULLSECRETHERE/$(sed 's:/:\\/:g' $PULLSECRET)/" config_$KNIUSER.sh
python -c 'import yaml, sys; yaml.safe_load(sys.stdin)' < install-config.yaml
if [ $? -ne 0 ]; then
echo "Failed"; exit 1
else
echo "Success"
fi
else
echo "Failed - Missing $PULLSECRET"; exit 1
fi
##################################################################
# Add sshkey to install-config.yaml #
##################################################################
SSHKEY=sshkey
DEFSSHKEY=/home/$KNIUSER/.ssh/id_rsa.pub
if [ -f "$SSHKEY" ]; then
ssh-keygen -l -f $SSHKEY >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "SSHkey addition to install-config.yaml: Failed"; exit 1
fi
echo "SSHkey addition to install-config.yaml: User Supplied"
sed -i "s/SSHKEYHERE/$(sed 's:/:\\/:g' $SSHKEY)/" install-config.yaml
python -c 'import yaml, sys; yaml.safe_load(sys.stdin)' < install-config.yaml
if [ $? -ne 0 ]; then
echo "SSHkey addition to install-config.yaml: Failed"; exit 1
else
echo "SSHkey addition to install-config.yaml: Success"
fi
else
if [ -f "$DEFSSHKEY" ]; then
ssh-keygen -l -f $DEFSSHKEY >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "SSHkey addition to install-config.yaml: Failed"; exit 1
fi
echo "SSHkey addition to install-config.yaml: Using Default"
sed -i "s/SSHKEYHERE/$(sed 's:/:\\/:g' $DEFSSHKEY)/" install-config.yaml
python -c 'import yaml, sys; yaml.safe_load(sys.stdin)' < install-config.yaml
if [ $? -ne 0 ]; then
echo "SSHkey addition to install-config.yaml: Failed"; exit 1
else
echo "SSHkey addition to install-config.yaml: Success"
fi
else
echo "SSHkey addition to install-config.yaml: Failed - No Key Found"; exit 1
fi
fi
echo "Preflight run complete!"