-
Notifications
You must be signed in to change notification settings - Fork 6
/
oct.sh
executable file
·609 lines (503 loc) · 17.9 KB
/
oct.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#!/bin/bash
OC_DOWNLOAD_URL="https://mirror.openshift.com/pub/openshift-v4/clients/oc/latest/linux/oc.tar.gz"
GOVC_DOWNLOAD_URL="https://github.com/vmware/govmomi/releases/download/v0.24.0/govc_linux_amd64.gz"
BIN_DIR=${HOME}/bin
die() {
echo "$1"
exit 1
}
show_help() {
echo -e "\n-h|-?|--help
--auto-secret
Automatically use the "dummy" pull secret instead of prompting for one.
--install-tools
Calls the install_cluster_tools() function. This flag should be used in conjunction with the --release flag.
--prerun
Call the launch_prerun() function.
--provision-infrastructure
Calls the provision_cluster_infrastructure() function. This flag should be accompanied by the --template-name, --library, --cluster-name, --cluster-folder, --network-name, --installation-folder, --master-node-count, and --worker-node-count flags with their appropriate values.
--destroy
Calls the destroy-cluster() function. This should be accompanied by the --cluster-name, --master-node-count, and --worker-node-count flags with their appropriate values.
--clean
Calls the clean() function, which removes all generated files from an installation.
--release version
The release version you wish to install the OKD/OpenShift tools for. This can be the complete release version (e.g. "4.7.0-0.okd-2021-03-07-090821") or the just the major.minor version, in which case the latest build of that version will be used (e.g. "4.7")
--cluster-power [on/off]
Calls the manage_power()function. Values are "on" and "off". This should be accompanied by the --cluster-name, --master-node-count, and --worker-node-count flags with their appropriate values.
--library name
The name of the vSphere Content Library where the VM template can be found
--template-name name
The name of the VM template to use when deploying nodes
--cluster-name name
The name of the cluster. This is used for assembling the node names and URLs (e.g. worker-1.*name.example.com)
--master-node-count number
The desired number of master nodes. This flag should be used in conjunction with --worker-node-count, --build, --destroy, and --cluster-power.
--worker-node-count number
The desired number of worker nodes. This flag should be used in conjunction with --master-node-count, --build, --destroy, and --cluster-power.
--cluster-folder folder
The folder on vSphere where the VMs will be deployed into.
--network-name name
The name of the vSphere network that the deployed VMs should use (e.g. the default "VM Network")
--installation-folder path
The path to the folder with the installation materials.
-v|--verbose
Set the verbosity level."
}
while :; do
case $1 in
-h|-\?|--help)
show_help
exit
;;
--auto-secret)
auto_secret=1
;;
--install-tools)
install_tools=1
;;
--install-config-template)
if [ "$2" ]; then
install_config_template_path=$2
shift
else
die 'ERROR: "--install-config-template" requires a non-empty option argument.'
fi
;;
--prerun)
prerun=1
;;
--provision-infrastructure)
provision_infrastructure=1
;;
--destroy)
destroy=1
;;
--deploy-node)
deploy_single_node=1
;;
--clean)
clean=1
;;
--import-template)
import_template=1
;;
--template-url)
if [ "$2" ]; then
template_url=$2
shift
else
die 'ERROR: "--template-url" requires a non-empty option argument.'
fi
;;
--release)
if [ "$2" ]; then
release=$2
shift
else
die 'ERROR: "--release" requires a non-empty option argument.'
fi
;;
--cluster-power)
if [ "$2" ]; then
cluster_power_action=$2
shift
else
die 'ERROR: "--power" requires a non-empty option argument.'
fi
;;
--library)
if [ "$2" ]; then
library=$2
shift
else
die 'ERROR: "--library" requires a non-empty option argument.'
fi
;;
--template-name)
if [ "$2" ]; then
template_name=$2
shift
else
die 'ERROR: "--template-name" requires a non-empty option argument.'
fi
;;
--cluster-name)
if [ "$2" ]; then
cluster_name=$2
shift
else
die 'ERROR: "--cluster-name" requires a non-empty option argument.'
fi
;;
--cluster-folder)
if [ "$2" ]; then
cluster_folder=$2
shift
else
die 'ERROR: "--vm-folder" requires a non-empty option argument.'
fi
;;
--network-name)
if [ "$2" ]; then
network_name=$2
shift
else
die 'ERROR: "--network-name" requires a non-empty option argument.'
fi
;;
--installation-folder)
if [ "$2" ]; then
installation_folder=$2
shift
else
die 'ERROR: "--installation_folder" requires a non-empty option argument.'
fi
;;
--master-node-count)
if [ "$2" ]; then
master_node_count=$2
shift
else
die 'ERROR: "--master-node-count" requires a non-empty option argument.'
fi
;;
--worker-node-count)
if [ "$2" ]; then
worker_node_count=$2
shift
else
die 'ERROR: "--worker-node-count" requires a non-empty option argument.'
fi
;;
--vm-name)
if [ "$2" ]; then
vm_name=$2
shift
else
die 'ERROR: "--vm-name" requires a non-empty option argument.'
fi
;;
--vm-cpu)
if [ "$2" ]; then
vm_cpu=$2
shift
else
die 'ERROR: "--vm-cpu" requires a non-empty option argument.'
fi
;;
--vm-memory)
if [ "$2" ]; then
vm_memory=$2
shift
else
die 'ERROR: "--vm-memory" requires a non-empty option argument.'
fi
;;
--vm-disk)
if [ "$2" ]; then
vm_disk=$2
shift
else
die 'ERROR: "--vm-disk" requires a non-empty option argument.'
fi
;;
--ignition-file)
if [ "$2" ]; then
ignition_file_path=$2
shift
else
die 'ERROR: "--ignition-file" requires a non-empty option argument.'
fi
;;
--ipcfg)
if [ "$2" ]; then
ipcfg=$2
shift
else
die 'ERROR: "--ipcfg" requires a non-empty option argument.'
fi
;;
--boot)
boot_vm=1
;;
--query-fcos)
query_fcos=1
;;
--stream-name)
if [ "$2" ]; then
stream_name=$2
shift
else
die 'ERROR: "--stream-name" requires a non-empty option argument.'
fi
;;
-v|--verbose)
verbose=$((verbose + 1))
;;
--)
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*)
break
esac
shift
done
check_oc() {
if ! command -v oc &> /dev/null
then
while true; do
read -p "The oc command could not be found. Would you like to download it? " yn
case $yn in
[Yy]* )
install_oc;break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
}
install_oc() {
curl -L ${OC_DOWNLOAD_URL} > /tmp/oc.tar.gz
tar xvf /tmp/oc.tar.gz -C /tmp
mv /tmp/oc ${BIN_DIR}
mv /tmp/kubectl ${BIN_DIR}
echo "The oc and kubectl applications have been downloaded to directory ${BIN_DIR}"
}
check_govc() {
if ! command -v govc &> /dev/null
then
while true; do
read -p "The govc command could not be found. Would you like to download it? " yn
case $yn in
[Yy]* )
install_govc;break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
}
install_govc() {
curl -L ${GOVC_DOWNLOAD_URL} | gunzip > ${BIN_DIR}/govc
chmod +x ${BIN_DIR}/govc
echo "The govc application has been downloaded to the directory ${BIN_DIR}"
}
import_template_from_url(){
file_name=$(basename ${template_url})
template_name="${file_name%.*}"
template_exists=false
echo "Checking for ${template_name} in library ${library}"
library_items=$(govc library.ls "${library}/*")
while IFS= read -r line; do
library_item_name=$(echo ${line} | cut -d'/' -f 3)
echo "Library Item: $library_item_name"
if [[ "$library_item_name" == "$template_name" ]]; then
echo "Template already exists in library"
template_exists=true
break
fi
done <<< "$library_items"
if [[ "$template_exists" == false ]]; then
echo "Importing template to library"
curl -s -i -O ${template_url}
govc library.import -k=true "${library}" "${template_url}"
echo "template_name"
fi
}
install_cluster_tools(){
if [[ -v release ]]; then
release_info=$(oc adm release info registry.ci.openshift.org/origin/release:"${release}")
release_version=$(echo "${release_info}" | grep Name | awk '{print $2}')
pull_url=$(echo "${release_info}" | grep "Pull From:" | awk '{print $3}')
else
echo "Please use the --release flag to denote what version you'd like to install."
fi
installer_file_name="openshift-install-linux-${release_version}.tar.gz"
client_file_name="openshift-client-linux-${release_version}.tar.gz"
if [ ! -d bin ]; then
mkdir bin
fi
echo "Downloading the cluster tools for ${release_version}..."
oc adm release extract --to bin --tools ${pull_url}
tar xvf bin/${installer_file_name} -C bin
tar xvf bin/${client_file_name} -C bin
rm bin/${installer_file_name}
rm bin/${client_file_name}
}
create_install_config_from_template() {
echo "Creating an install config from ${install_config_template_path}"
if [ -z ${auto_secret} ]; then
echo "Please enter your pullSecret"
read pullSecret
else
pullSecret='{"auths":{"fake":{"auth":"aWQ6cGFzcwo="}}}'
fi
cp "${install_config_template_path}" install-config.yaml
echo "pullSecret: '${pullSecret}'" >> install-config.yaml
}
launch_prerun() {
bin/openshift-install create manifests --dir=$(pwd)
rm -f openshift/99_openshift-cluster-api_master-machines-*.yaml openshift/99_openshift-cluster-api_worker-machineset-*.yaml
sed -i -e s/true/false/g manifests/cluster-scheduler-02-config.yml
bin/openshift-install create ignition-configs --dir=$(pwd)
# Change timeouts for Master
sed -i "s/\"timeouts\":{}/\"timeouts\":{\"httpResponseHeaders\":50,\"httpTotal\":600}/g" master.ign
# Change timeouts for Worker
sed -i "s/\"timeouts\":{}/\"timeouts\":{\"httpResponseHeaders\":50,\"httpTotal\":600}/g" worker.ign
echo "Copying bootstrap.ign to /var/www/html/..."
sudo /usr/bin/cp bootstrap.ign /var/www/html/bootstrap.ign
sudo /usr/bin/chown apache:apache /var/www/html/bootstrap.ign
sudo /usr/sbin/restorecon -Rv /var/www/html
}
deploy_node() {
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc library.deploy --folder "${cluster_folder}" "${library}/${template_name}" "${vm_name}"
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.change -vm "${vm_name}" \
-c="${vm_cpu}" \
-m="${vm_memory}"
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.change -vm "${vm_name}" -e guestinfo.ignition.config.data="$(cat ${ignition_file_path} | base64 -w0)" -e guestinfo.ignition.config.data.encoding="base64"
## Set the network ##
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.network.change -vm "${vm_name}" -net "${network_name}" -net.address - ethernet-0
##
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.disk.change -vm "${vm_name}" -disk.label "Hard disk 1" -size ${vm_disk}G
if [[ ! -z "${ipcfg}" ]]; then
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.change -vm "${vm_name}" -e "guestinfo.afterburn.initrd.network-kargs=${ipcfg}"
fi
if [[ ! -z "${vm_mac}" ]]; then
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.network.change -vm ${vm_name} -net "${cluster_network}" -net.address ${vm_mac} ethernet-0
fi
if [[ ! -z "${boot_vm}" ]]; then
podman run -e GOVC_URL="${GOVC_URL}" -e GOVC_USERNAME="${GOVC_USERNAME}" -e GOVC_PASSWORD="${GOVC_PASSWORD}" -e GOVC_INSECURE=true --rm -it docker.io/vmware/govc /govc vm.power -on "${vm_name}"
fi
}
provision_cluster_infrastructure(){
bootstrap_cpu=4
bootstrap_memory=16384
bootstrap_disk=120
master_cpu=4
master_memory=16384
master_disk=120
worker_cpu=4
worker_memory=16384
worker_disk=120
echo "Cluster: ${cluster_name}"
echo "Template: ${template_name}"
echo "Cluster Folder: ${cluster_folder}"
echo "Network: ${network_name}"
echo "Installation Folder: ${installation_folder}"
# Create the bootstrap node
echo "Creating a bootstrap node with ${bootstrap_cpu} cpus and ${bootstrap_memory} MB of memory"
vm_name="bootstrap.${cluster_name}"
vm_cpu="${bootstrap_cpu}"
vm_memory="${bootstrap_memory}"
vm_disk="${bootstrap_disk}"
vm_name="bootstrap.${cluster_name}"
ignition_file_path="${installation_folder}/append-bootstrap.ign"
deploy_node
# Create the master nodes
echo "Creating ${master_node_count} master nodes with ${master_cpu} cpus and ${master_memory} MB of memory"
vm_cpu="${master_cpu}"
vm_memory="${master_memory}"
vm_disk="${master_disk}"
ignition_file_path="${installation_folder}/master.ign"
for (( i=0; i<${master_node_count}; i++ )); do
vm_name="master-${i}.${cluster_name}"
deploy_node
done
# Create the worker nodes
echo "Creating ${worker_node_count} worker nodes with ${worker_cpu} cpus and ${worker_memory} MB of memory"
vm_cpu="${worker_cpu}"
vm_memory="${worker_memory}"
vm_disk="${worker_disk}"
ignition_file_path="${installation_folder}/worker.ign"
for (( i=0; i<${worker_node_count}; i++ )); do
vm_name="worker-${i}.${cluster_name}"
deploy_node
done
}
run_installer() {
bin/openshift-install create cluster --dir=$(pwd) --log-level=info
}
destroy_cluster() {
echo "If you really want to delete the cluster ${cluster_name}, type its name again:"
read response
if [[ "${response}" == "${cluster_name}" ]]; then
echo "Destroying cluster: ${cluster_name}"
# Destroy the master nodes
for (( i=0; i<${master_node_count}; i++ )); do
vm="master-${i}.${cluster_name}"
echo "master: $vm"
govc vm.destroy $vm
done
# Destroy the worker nodes
for (( i=0; i<${worker_node_count}; i++ )); do
vm="worker-${i}.${cluster_name}"
govc vm.destroy $vm
done
# Destroy the bootstrap node
vm="bootstrap.${cluster_name}"
govc vm.destroy $vm
else
echo "OK, I'll forget you ever mentioned it."
fi
}
clean() {
rm -rf master.ign worker.ign metadata.json .openshift_install* auth/ bootstrap.ign
}
manage_cluster_power() {
echo "Turning cluster ${power_action}..."
vm="bootstrap.${cluster_name}"
govc vm.power -${cluster_power_action} "${vm}"
# Manage power for the master nodes
for (( i=0; i<${master_node_count}; i++ )); do
vm="master-${i}.${cluster_name}"
govc vm.power -${cluster_power_action} "${vm}"
done
# Manage power for the worker nodes
for (( i=0; i<${worker_node_count}; i++ )); do
vm="worker-${i}.${cluster_name}"
govc vm.power -${cluster_power_action} "${vm}"
done
}
query_fcos_stream() {
stream_data=$(curl -s https://builds.coreos.fedoraproject.org/streams/${stream_name}.json)
file_url=$(echo ${stream_data} | jq -r '.architectures.x86_64.artifacts.vmware.formats.ova.disk.location')
file_name=$(basename ${file_url})
template_name="${file_name%.*}"
echo "${file_url}"
}
check_oc
#check_govc
if [ ! -z ${install_tools} ]; then
install_cluster_tools
fi
if [ ! -z ${import_template} ]; then
import_template_from_url
fi
if [ ! -z ${install_config_template_path} ]; then
create_install_config_from_template
fi
if [ ! -z ${prerun} ]; then
launch_prerun
fi
if [ ! -z ${provision_infrastructure} ]; then
provision_cluster_infrastructure
fi
if [ ! -z ${deploy_single_node} ]; then
deploy_node
fi
if [ ! -z ${destroy} ]; then
destroy_cluster
fi
if [ ! -z ${cluster_power_action} ]; then
manage_cluster_power
fi
if [ ! -z ${clean} ]; then
clean
fi
if [ ! -z ${query_fcos} ]; then
query_fcos_stream
fi