-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00_create_ipibm_infra.sh
379 lines (349 loc) · 12.3 KB
/
00_create_ipibm_infra.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/bash
## ENV VARS ##
LIBVIRT_HOME=/var/lib/libvirt
LIBVIRT_IMGS=$LIBVIRT_HOME/images
INSTALLER_VM=ipibm-installer
IPIBM_VM=ipibm-master
IPIBM_NET=lab-ipibm
IPIBM_CIDR_IPV4=192.168.119.1/24
IPIBM_IPV4_IPROUTE=192.168.119.1
IPIBM_IPV4_PREFIX=24
IPIBM_IPV4_INSTALLER_IP=192.168.119.100
IPIBM_IPV4_API_IP=192.168.119.10
IPIBM_IPV4_INGRESS_IP=192.168.119.11
IPIBM_CIDR_IPV6=2620:52:0:1001::1/64
IPIBM_IPV6_IPROUTE=2620:52:0:1001::1
IPIBM_IPV6_PREFIX=64
IPIBM_IPV6_INSTALLER_IP=2620:52:0:1001::100
IPIBM_IPV6_API_IP=2620:52:0:1001::10
IPIBM_IPV6_INGRESS_IP=2620:52:0:1001::11
IPV4_RANGE_START=192.168.119.2
IPV4_RANGE_END=192.168.119.254
IPV6_RANGE_START=2620:52:0:1001::2
IPV6_RANGE_END=2620:52:0:1001::ffff
ID_RSA_PUB=$(cat /root/.ssh/id_rsa.pub)
MASTERS=(ipibm-master-01 ipibm-master-02 ipibm-master-03)
MASTERS_IPV4=(192.168.119.20 192.168.119.21 192.168.119.22)
MASTERS_IPV6=(2620:52:0:1001::20 2620:52:0:1001::21 2620:52:0:1001::22)
MASTERS_MAC_IPV4=(aa:aa:aa:aa:bc:01 aa:aa:aa:aa:bc:02 aa:aa:aa:aa:bc:03)
MASTERS_MAC_IPV6=(00:03:00:01:aa:aa:aa:aa:bc:01 00:03:00:01:aa:aa:aa:aa:bc:02 00:03:00:01:aa:aa:aa:aa:bc:03)
INSTALLER_MAC_IPV4=aa:aa:aa:aa:bc:00
INSTALLER_MAC_IPV6=00:03:00:01:aa:aa:aa:aa:bc:00
WORKER_MAC_IPV4=aa:aa:aa:aa:bd:0
WORKER_MAC_IPV6=00:03:00:01:aa:aa:aa:aa:bd:0
WORKER_IPV4=192.168.119.3
WORKER_IPV6=2620:52:0:1001::3
WORKER_NAME=ipibm-worker
RADVD_PREFIX=$(echo $IPIBM_CIDR_IPV6|sed 's/1\//\//g')
## ENV VARS ##
function set_vars () {
OCP_DOMAIN=${CLUSTER_NAME}.${DOMAIN}
IP_TYPE=$1
if [ "${IP_TYPE}" = "ipv4" ]; then
echo -e "+ Setting vars for a ipv4 cluster."
echo -e "+ The network range configured is: ${IPIBM_CIDR_IPV4}"
IPV="ip4"
IPFAMILY="ipv4"
IPIBM_CIDR=${IPIBM_CIDR_IPV4}
IPV4_METHOD="manual"
IPV6_METHOD="disabled"
IPROUTE=${IPIBM_IPV4_IPROUTE}
IPPREFIX=${IPIBM_IPV4_PREFIX}
INSTALLER_IP=${IPIBM_IPV4_INSTALLER_IP}
API_IP=${IPIBM_IPV4_API_IP}
INGRESS_IP=${IPIBM_IPV4_INGRESS_IP}
HOSTIDMAC="host mac"
IP_RANGE_START=${IPV4_RANGE_START}
IP_RANGE_END=${IPV4_RANGE_END}
MASTERS_IP=("${MASTERS_IPV4[@]}")
MASTERS_MAC=("${MASTERS_MAC_IPV4[@]}")
INSTALLER_MAC=${INSTALLER_MAC_IPV4}
WORKER_MAC_IP=${WORKER_MAC_IPV4}
WORKER_IP=${WORKER_IPV4}
elif [ "${IP_TYPE}" = "ipv6" ]; then
echo -e "+ Setting vars for a ipv6 cluster."
echo -e "+ The network range configured is: ${IPIBM_CIDR_IPV6}"
IPV="ip6"
IPFAMILY="ipv6"
IPIBM_CIDR=${IPIBM_CIDR_IPV6}
IPV4_METHOD="disabled"
IPV6_METHOD="manual"
IPROUTE=${IPIBM_IPV6_IPROUTE}
IPPREFIX=${IPIBM_IPV6_PREFIX}
INSTALLER_IP=${IPIBM_IPV6_INSTALLER_IP}
API_IP=${IPIBM_IPV6_API_IP}
INGRESS_IP=${IPIBM_IPV6_INGRESS_IP}
HOSTIDMAC="host id"
IP_RANGE_START=${IPV6_RANGE_START}
IP_RANGE_END=${IPV6_RANGE_END}
MASTERS_IP=("${MASTERS_IPV6[@]}")
MASTERS_MAC=("${MASTERS_MAC_IPV6[@]}")
INSTALLER_MAC=${INSTALLER_MAC_IPV6}
WORKER_MAC_IP=${WORKER_MAC_IPV6}
WORKER_IP=${WORKER_IPV6}
echo -e "+ Setting net.ipv6 required values..."
sysctl -w net.ipv6.conf.all.accept_ra=2
sysctl -w net.ipv6.conf.all.forwarding=1
else
echo -e "+ A valid network type value should be provided: ipv4/ipv6."
fi
}
## FUNCTIONS ##
function check_binary () {
BINARY=$1
# Check whether a specific binary exists or not
if [ "$(which $BINARY)" = "" ]; then
echo -e "\n+ $BINARY is not present in the $PATH or it is not installed"
echo -e "+ Look for $BINARY in custom PATHs or try to install it with dnf or yum"
exit 1
else
echo -e "\n+ $BINARY is already installed: $(which $BINARY)"
fi
}
function create_installer_image () {
# First of all check if the CentOS 8 Generic Cloud image is already downloaded
CENTOS_IMGS=$(ls $LIBVIRT_IMGS/CentOS-8-GenericCloud-8.*)
if [ -f "$CENTOS_IMGS" ]; then
echo "+ There is already an image, proceeding with that image..."
echo -e "\t\__>$CENTOS_IMGS"
else
echo "+ No CentOS image found, downloading a new image..."
curl https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2 > $LIBVIRT_IMGS/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2
chown qemu:qemu $LIBVIRT_IMGS/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2
fi
# Creating disk images for installer and master/worker node for SNO
check_binary qemu-img
qemu-img create -f qcow2 -F qcow2 -b ${LIBVIRT_IMGS}/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2 ${LIBVIRT_IMGS}/${INSTALLER_VM}.qcow2 500G
}
function create_image () {
SERVERNAME=$1
# Creating qcow images for server $SERVERNAME
check_binary qemu-img
qemu-img create -f qcow2 ${LIBVIRT_IMGS}/${SERVERNAME}.qcow2 100G
}
function create_cloud_init_config () {
# We need to create a temp dir to make the custom cloud init scripts and iso
echo -e "\n+ Creating temp dir and cloud-init config..."
mkdir /root/$INSTALLER_VM && cd /root/$INSTALLER_VM
# meta-data file
cat << EOF > meta-data
instance-id: ${INSTALLER_VM}
local-hostname: ${INSTALLER_VM}
EOF
#user-data file
cat << EOF > user-data
#cloud-config
preserve_hostname: False
hostname: ${INSTALLER_VM}
fqdn: ${INSTALLER_VM}.${OCP_DOMAIN}
user: test
password: test
chpasswd: {expire: False}
ssh_pwauth: True
ssh_authorized_keys:
- ${ID_RSA_PUB}
chpasswd:
list: |
root:test
test:test
expire: False
network:
config: disabled
runcmd:
- sed -i -e 's/^.*\(ssh-rsa.*\).*$/\1/' /root/.ssh/authorized_keys
EOF
# Time to create the new image including user-data and meta-data, this will be used to inject the cloud-init customizations.
genisoimage -output ${INSTALLER_VM}.iso -volid cidata -joliet -rock user-data meta-data
cp ${INSTALLER_VM}.iso ${LIBVIRT_IMGS}
}
function networks () {
echo -e "\n+ Defining virsh network and applying configuration..."
cat << EOF > ${IPIBM_NET}-network.xml
<network>
<name>${IPIBM_NET}</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='${IPIBM_NET}' stp='on' delay='0'/>
<mac address='52:54:00:eb:3a:aa'/>
<domain name='${IPIBM_NET}'/>
<dns>
<host ip='${API_IP}'>
<hostname>api</hostname>
<hostname>api-int.${OCP_DOMAIN}</hostname>
<hostname>api.${OCP_DOMAIN}</hostname>
</host>
<host ip='${INGRESS_IP}'>
<hostname>apps</hostname>
<hostname>console-openshift-console.apps.${OCP_DOMAIN}</hostname>
<hostname>oauth-openshift.apps.${OCP_DOMAIN}</hostname>
<hostname>prometheus-k8s-openshift-monitoring.apps.${OCP_DOMAIN}</hostname>
<hostname>canary-openshift-ingress-canary.apps.${OCP_DOMAIN}</hostname>
<hostname>assisted-service-open-cluster-management.apps.${OCP_DOMAIN}</hostname>
<hostname>assisted-service-assisted-installer.apps.${OCP_DOMAIN}</hostname>
</host>
</dns>
<ip family='${IPFAMILY}' address='${IPROUTE}' prefix='${IPPREFIX}'>
<dhcp>
<range start='${IP_RANGE_START}' end='${IP_RANGE_END}'/>
<${HOSTIDMAC}='${MASTERS_MAC[0]}' name='${MASTERS[0]}' ip='${MASTERS_IP[0]}'/>
<${HOSTIDMAC}='${MASTERS_MAC[1]}' name='${MASTERS[1]}' ip='${MASTERS_IP[1]}'/>
<${HOSTIDMAC}='${MASTERS_MAC[2]}' name='${MASTERS[2]}' ip='${MASTERS_IP[2]}'/>
</dhcp>
</ip>
</network>
EOF
virsh net-define ${IPIBM_NET}-network.xml
virsh net-autostart ${IPIBM_NET}
virsh net-start ${IPIBM_NET}
}
function create_workers () {
if [ ${NUM_WORKERS} -gt 0 ]; then
echo -e "\n+ ${NUM_WORKERS} workers will be deployed at installation time."
w=1
while [ "${w}" -le "${NUM_WORKERS}" ];
do
WORKER_VM=${WORKER_NAME}-0${w}
WORKER_MAC="aa:aa:aa:aa:bd:0${w}"
WORKER_MAC_DHCP="${WORKER_MAC_IP}${w}"
WORKER_IP_DHCP="${WORKER_IP}${w}"
virsh net-update ${IPIBM_NET} add ip-dhcp-host "<${HOSTIDMAC}='${WORKER_MAC_DHCP}' name='${WORKER_VM}' ip='${WORKER_IP_DHCP}'/>" --live --config
create_image ${WORKER_VM}
virt-install --virt-type=kvm --name=${WORKER_VM} --ram 16384 --vcpus 8 --hvm --network network=${IPIBM_NET},model=virtio,mac=${WORKER_MAC} --disk ${LIBVIRT_IMGS}/${WORKER_VM}.qcow2,device=disk,bus=scsi,format=qcow2 --os-type Linux --os-variant rhel8.0 --graphics none --import --noautoconsole
sleep 2
virsh destroy ${WORKER_VM}
let w++
done
else
echo -e "\n+ No workers will be deployed at installation time."
fi
}
function create_vms () {
# Check whether virt-install binary exists or not
check_binary virt-install
# Masters Installation
echo -e "\n+ Installing master servers..."
j=0
for m in ${MASTERS[@]}
do
create_image $m
virt-install --virt-type=kvm --name=${m} --ram 16384 --vcpus 8 --hvm --network network=${IPIBM_NET},model=virtio,mac=${MASTERS_MAC_IPV4[j]} --disk ${LIBVIRT_IMGS}/${m}.qcow2,device=disk,bus=scsi,format=qcow2 --os-type Linux --os-variant rhel8.0 --graphics none --import --noautoconsole
sleep 2
virsh destroy ${m}
let j++
done
# Installer deployment
echo -e "\n+ Installing installer server..."
virt-install --virt-type=kvm --name=${INSTALLER_VM} --ram 8192 --vcpus 8 --hvm --network network=default,model=virtio,mac=aa:aa:aa:aa:cc:00 --network network=${IPIBM_NET},model=virtio,mac=${INSTALLER_MAC_IPV4} --disk ${LIBVIRT_IMGS}/${INSTALLER_VM}.qcow2,device=disk,bus=scsi,format=qcow2 --disk ${LIBVIRT_IMGS}/${INSTALLER_VM}.iso,device=cdrom --os-type Linux --os-variant rhel8.0 --graphics none --import --noautoconsole
}
function config_dns_hosts () {
check_binary virsh
while [[ ${IP} = "" ]]
do
IP=$(virsh net-dhcp-leases ${IPIBM_NET} |grep ${INSTALLER_MAC_IPV4}|tail -1|awk '{print $5}'|cut -d "/" -f 1)
echo -e "+ Waiting to grab an IP from DHCP..."
sleep 5
done
echo -e "+ IP already assigned: ${IP}"
virsh net-update ${IPIBM_NET} add dns-host "<host ip='${IP}'> <hostname>${INSTALLER_VM}</hostname> <hostname>${INSTALLER_VM}.${OCP_DOMAIN}</hostname> </host>" --live --config
copy_id_rsa ${IP}
copy_install_files ${IP}
}
function copy_id_rsa () {
IP=$1
echo -e "\n+ Waiting 90seg to let the ${INSTALLER_VM} boot properly..."
sleep 90
scp /root/.ssh/id_rsa* root@[${IP}]:/root/.ssh/.
}
function copy_install_files () {
IP=$1
echo -e "\n+ Copying install files to ${INSTALLER_VM} with IP: ${IP} ..."
scp ${SCRIPT_PATH}/01_pre_reqs_ipibm.sh ${SCRIPT_PATH}/02_install_ipibm.sh ${SCRIPT_PATH}/find_redfish_host.sh root@[${IP}]:/root/.
}
function install_radvd () {
dnf -y install radvd
cat << EOF > /etc/radvd.conf
interface ${IPIBM_NET}
{
AdvManagedFlag on;
AdvSendAdvert on;
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
AdvDefaultLifetime 9000;
prefix ${RADVD_PREFIX}
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr on;
};
route ::/0 {
AdvRouteLifetime 9000;
AdvRoutePreference low;
RemoveRoute on;
};
};
EOF
sysctl -w net.ipv6.conf.all.accept_ra=2
sysctl -w net.ipv6.conf.all.forwarding=1
systemctl enable radvd --now
}
## FUNCTIONS ##
## MENU ##
if [[ -z "$@" ]]; then
echo -e "Missing arguments, run the following for help: $0 --help "
exit 1
fi
for i in "$@"; do
case $i in
-h|--help)
echo -e "+ Usage: $0 -n=<IP_TYPE> -w=<NUM_WORKERS> -d=<DOMAIN_NAME> -c=<CLUSTER_NAME>"
echo -e "Valid IP_TYPE values: ipv4/ipv6"
echo -e "Valid number of workers 1-9"
echo -e "Provide a valid domain name, if not present example.com will be set as the default domain"
echo -e "Provide a valid cluster name, if not present lab will be set as the default cluster name"
exit 0
;;
-n=*|--net=*)
IP_TYPE="${i#*=}"
shift
;;
-w=*|--workers=*)
NUM_WORKERS="${i#*=}"
shift
;;
-d=*|--domain=*)
DOMAIN="${i#*=}"
shift
;;
-c=*|--clustername=*)
CLUSTER_NAME="${i#*=}"
shift
;;
*)
echo -e "+ Usage: $0 -n=<IP_TYPE> -w=<NUM_WORKERS> -d=<DOMAIN_NAME> -c=<CLUSTER_NAME>"
echo -e "Valid IP_TYPE values: ipv4/ipv6"
echo -e "Valid number of workers 1-9"
echo -e "Provide a valid domain name, if not present example.com will be set as the default domain"
echo -e "Provide a valid cluster name, if not present lab will be set as the default cluster name"
exit 1
esac
done
if [[ -z "$DOMAIN" ]]; then
DOMAIN=example.com
fi
if [[ -z "$CLUSTER_NAME" ]]; then
CLUSTER_NAME=lab
fi
SCRIPT_PATH=$(dirname $(realpath $0))
## MENU ##
## MAIN ##
set_vars ${IP_TYPE}
create_installer_image
create_cloud_init_config
networks
create_vms
create_workers
config_dns_hosts
## MAIN ##