-
Notifications
You must be signed in to change notification settings - Fork 5
/
cbox.in
555 lines (490 loc) · 14 KB
/
cbox.in
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
#!/bin/bash
set -e
[ "$(whoami)" != "root" ] && {
echo "Need root baby"
exit 1
}
[ -f /root/.cbox ] && {
. /root/.cbox
echo "Cluster: $cboxclustername appears to be installed on the system"
echo ""
# TODO: would like to be able to skip this question if -f is used:
read -p "Do you want to totally destroy $cboxclustername (y/N)? " ans
[ -z "$ans" ] || [ "$ans" != "y" ] && {
echo "Aborting"
exit 1
}
# go as far as we can without failing
set +e
echo "Destroying nodes:"
for i in $(virsh list --all | grep $cboxclustername- | awk '{print $2}'); do
virsh destroy $i
virsh undefine $i
done
echo "Destroying virtual network:"
for i in 0 1; do
virsh net-destroy $cboxclustername-br$i
virsh net-undefine $cboxclustername-br$i
done
for i in $(mount | grep $cboxclustername | awk '{print $3}'); do
echo "Unmounting $i"
umount $i
done
echo "Purging nodes disks $cboxvmsdir/$cboxclustername"
rm -rf $cboxvmsdir/$cboxclustername
echo "Removing keys from /root/.ssh and update known_hosts"
cat /root/.ssh/known_hosts | \
grep -v "^$cboxclustername-node" > /root/.ssh/known_hosts.new
mv /root/.ssh/known_hosts.new /root/.ssh/known_hosts
echo "Restore /etc/hosts"
mv -f /etc/hosts.cbox /etc/hosts
echo "Restore /etc/fence_virt.conf"
mv -f /etc/fence_virt.conf.cbox /etc/fence_virt.conf
echo "Stopping fence_virtd"
service fence_virtd stop
rm -f /root/.cbox
echo "We have done our best to restore original configs and destroy"
echo "$cboxclustername cluster."
echo ""
echo "May the force be with you!"
exit 0
}
# cbox data dir
[ -z "$cboxdatadir" ] && cboxdatadir="DATADIR"
# cbox hooks dir
[ -z "$cboxhooksdir" ] && cboxhooksdir="$cboxdatadir/hooks"
# cbox emulator
[ -z "$cboxemulator" ] && cboxemulator="EMULATOR"
# basic logging
[ -z "$cboxlogdir" ] && cboxlogdir="LOGDIR"
cboxlogfile=$cboxlogdir/cbox-$(date +%F_%T).log
function print_usage() {
echo "$(basename $0) ver: VERSION"
echo ""
echo "usage:"
echo ""
echo "$(basename $0) [options]"
echo ""
echo "Options:"
echo " -c cluster_name set clustername (default: testcluster)"
echo " -d development mode. Will repeat failed hook."
echo " -f force switch to skip all questions. Use with care!"
echo " -k kickstart use custom kickstart file. This overrides both -O and -r."
echo " -m [pacemaker|rgmanager|none] use a resource manager (default: none)"
echo " -n number_of_nodes a value between 2 and 16 (default: 2)"
echo " -o path_to_storage_dir set path where to store VMs (default: /srv/cbox)"
echo " -O osname use kickstart without fedora prefix distributed with cbox. This overrides -r"
echo " -q enable qdiskd for cman clusters (default: no)"
echo " -r fedora_release force fedora release to install on the nodes (defaults: autodetect/same as host)"
echo " -t [cman|corosync|ceph] define cluster type (default: cman)"
echo " -u repo_URL use additional custom yum repository"
echo " -h this help"
echo " -E regexp exclude given script (regexp is matched by =~ bash operator)"
echo " -D enable creation of driver node"
echo " -v virt_opts define virtual nodes and storage size in a comma separated list of parameters"
echo " valid parameters are:"
echo " ram=XXX amount of RAM (for each node) in MB (min. 512 - default 2048)"
echo " cpus=XXX number of virtual cpus"
echo " root=XXX size of the root partition in MB (min. 2048 - default 4096)"
echo " swap=XXX size of the swap partition in MB (min. 512 - default is 1024)"
echo " share=XXX size of the shared storage disk in MB (min. 20544 - default 20544)"
echo ""
}
function is_integer() {
[ "$1" -eq "$1" ] > /dev/null 2>&1
return $?
}
function parse_virt_opts() {
OLD_IFS=${IFS}
IFS=,
for i in $@; do
opt=$(echo $i | sed -e 's#=.*##g')
val=$(echo $i | sed -e 's#.*=##g')
case $opt in
ram)
cboxvirtram=$val
! is_integer $cboxvirtram && {
echo "ERROR: virt ram size needs to be an integer"
exit 1
}
[ "$cboxvirtram" -lt "512" ] && {
echo "ERROR: virt ram size too small to complete the installation (min 512 MB)"
exit 1
}
;;
cpus)
cboxvirtcpus=$val
! is_integer $cboxvirtcpus && {
echo "ERROR: virt cpus needs to be an integer"
exit 1
}
[ "$cboxvirtcpus" -lt "1" ] && {
echo "ERROR: virt cpus needs to be a reasonable value (hint: greater than 0)"
exit 1
}
;;
root)
cboxvirtroot=$val
! is_integer $cboxvirtroot && {
echo "ERROR: virt root size needs to be an integer"
exit 1
}
[ "$cboxvirtroot" -lt "2048" ] && {
echo "ERROR: virt root size too small to complete the installation (min 2048 MB)"
exit 1
}
;;
swap)
cboxvirtswap=$val
! is_integer $cboxvirtswap && {
echo "ERROR: virt swap size needs to be an integer"
exit 1
}
[ "$cboxvirtswap" -lt "512" ] && {
echo "ERROR: virt swap size needs to be a reasonable value (hint: greater than 512 MB)"
exit 1
}
;;
share)
cboxvirtshare=$val
! is_integer $cboxvirtshare && {
echo "ERROR: virt shared disk size needs to be an integer"
exit 1
}
[ "$cboxclustertype" != "ceph" -a "$cboxvirtshare" -lt "20544" ] && {
echo "ERROR: virt shared disk size needs to be a reasonable value (hint: greater than 20544 MB)"
exit 1
}
;;
*)
echo "ERROR Unknown virtopt $opt=$val"
exit 1
;;
esac
done
IFS=${OLD_IFS}
}
# parse command line options
#
# c -> cluster name (cboxclustername)
# h -> help
# m -> resource manager (cboxrasmngr) rgmanager|pacemaker|none
# n -> number of nodes (cboxnumnodes)
# o -> output dir (cboxvmsdir)
# q -> enable qdiskd (cboxqdiskd)
# r -> fedora release (13 / 14 / rawhide)
# t -> type (cboxclustertype) cman|corosync|ceph
# v -> virt params (ram/cpu/root/swap)
# l -> disable guestmount and use loop and kpartx
# clear envvars (when adding, make sure to export them below)
cboxclustername=""
cboxforce=""
cboxdevel=""
cboxnumnodes=""
cboxminnodes=""
cboxclustertype=""
cboxkickstart=""
cboxos=""
cboxfedrelease=""
cboxrasmngr=""
cboxqdiskd=""
cboxvmsdir="/srv/cbox"
cboxrepourl=""
cboxexclude=""
cboxdrivernode="no"
cboxvirt=""
cboxvirtram=2048
cboxvirtcpus=2
cboxvirtroot=4096
cboxvirtswap=$((cboxvirtram / 2))
cboxvirtshare=20544
while getopts ":c:dfhk:lm:n:o:O:qr:t:u:v:E:D" optflag; do
case "$optflag" in
c)
cboxclustername="$OPTARG"
;;
d)
cboxdevel="yes"
;;
D)
cboxdrivernode="yes"
;;
E)
cboxexclude="$OPTARG"
;;
f)
cboxforce="yes"
;;
k)
cboxkickstart="$OPTARG"
;;
l)
cboxuseloop="yes"
;;
m)
cboxrasmngr="$OPTARG"
if [ "$cboxrasmngr" != "rgmanager" ] && \
[ "$cboxrasmngr" != "pacemaker" ] && \
[ "$cboxrasmngr" != "none" ]; then
echo "ERROR: cluster resource manager has to be rgmanager, pacemaker or none"
exit 1
fi
;;
n)
cboxnumnodes="$OPTARG"
! is_integer $cboxnumnodes && {
echo "ERROR: number of nodes needs to be an integer (min 2, max 16)"
exit 1
}
;;
o)
cboxvmsdir="$OPTARG"
;;
O)
cboxos="$OPTARG"
;;
q)
cboxqdiskd="yes"
;;
r)
cboxfedrelease="$OPTARG"
;;
t)
cboxclustertype="$OPTARG"
if [ "$cboxclustertype" != "cman" ] && \
[ "$cboxclustertype" != "corosync" ] && \
[ "$cboxclustertype" != "ceph" ]; then
echo "ERROR: cluster type has to be cman, corosync or ceph"
exit 1
fi
;;
u)
cboxrepourl="$OPTARG"
;;
v)
cboxvirt="$OPTARG"
;;
h)
print_usage
exit 0
;;
\?|:)
print_usage
exit 1
;;
esac
done
parse_virt_opts "$cboxvirt"
## ask question
echo "***** WARNING *****"
echo ""
echo "cbox is an experimental script to build virtual TEST cluster"
echo "and it performs actions and configurations that might not be"
echo "considered safe on both the host and the guests."
if [ -z "$cboxforce" ]; then
echo "Please abort the execution now unless you fully understand"
echo "what you are doing."
fi
echo "The resulting setup has to be used only for TESTING."
echo "Do NOT place this cluster into production."
echo ""
echo "cbox can only be executed once on each host to create a virtual cluster."
echo "Second execution will safely request to destroy the previously"
echo "created cluster, and start all over again."
echo ""
echo "***** END OF WARNING *****"
echo ""
echo "YOU HAVE BEEN WARNED"
echo ""
if [ -z "$cboxforce" ]; then
echo "If you agree, and want to proceed, please type:"
echo "Yes, I fully understand the risks of what I am doing"
echo ""
read -p "type here: " ans
[ "$ans" = "Yes, I fully understand the risks of what I am doing" ] || {
echo "Wise choice.. or you simply didn't type it right"
exit 0
}
fi
echo ""
[ ! -d "$cboxdatadir" ] && {
echo "ERROR: no valid data directory specified: $cboxdatadir"
exit 1
}
[ ! -d "$cboxhooksdir" ] && {
echo "ERROR: no valid hooks directory specified: $cboxhooksdir"
exit 1
}
[ ! -d "$cboxlogdir" ] && {
echo "ERROR: no valid log directory specified: $cboxlogdir"
exit 1
}
touch $cboxlogfile || {
echo "ERROR: unable to write to logfile: $cboxlogfile"
exit 1
}
[ -z "$cboxclustername" ] && cboxclustername=testcluster
[ "$(echo ${#cboxclustername})" -gt "12" ] && {
echo "ERROR: clustername has be 12 or less chars"
exit 1
}
[ -z "$cboxnumnodes" ] && cboxnumnodes=2
# waiting for cboxforce - let's do the check here
if [ -z "$cboxforce" ]; then
cboxminnodes="2"
else
cboxminnodes="1"
fi
if [ "$cboxnumnodes" -lt "$cboxminnodes" ] || \
[ "$cboxnumnodes" -gt "16" ]; then
echo "ERROR: invalid number of nodes specified (min $cboxminnodes, max 16)"
exit 1
fi
if [ -z "$cboxfedrelease" ]; then
if [ -n "$cboxos" ] || [ -n "$cboxkickstart" ]; then
cboxfedrelease="custom"
else
if [ ! -f "/etc/fedora-release" ]; then
echo "ERROR: can't detect Fedora release"
exit 1
fi
cboxfedrelease=$(cat /etc/fedora-release | awk '{print $3}')
grep -q -i rawhide /etc/fedora-release && cboxfedrelease=rawhide
fi
fi
[ -z "$cboxclustertype" ] && cboxclustertype=corosync
#[ "$cboxfedrelease" != "rawhide" ] && [ "$cboxfedrelease" != "custom" ] && \
# [ "$cboxfedrelease" -gt 16 ] && cboxclustertype=corosync
#[ "$cboxfedrelease" = "rawhide" ] && cboxclustertype=corosync
[ -z "$cboxrasmngr" ] && cboxrasmngr=none
if [ -z "$cboxkickstart" ]; then
if [ -n "$cboxos" ]; then
cboxkickstart=$cboxdatadir/$cboxos.ks
else
cboxkickstart=$cboxdatadir/fedora-$cboxfedrelease.ks
fi
fi
[ ! -f "$cboxkickstart" ] && {
echo "ERROR: no kickstart file $cboxkickstart"
exit 1
}
[ -d $cboxvmsdir/$cboxclustername ] && {
echo "ERROR: $cboxvmsdir/$cboxclustername appears to already exist"
exit 1
}
## cross checks
[ "$cboxclustertype" = "corosync" ] && [ "$cboxrasmngr" = "rgmanager" ] && {
echo "ERROR: you can't select corosync and rgmanager together."
exit 1
}
[ "$cboxclustertype" = "corosync" ] && [ -n "$cboxqdiskd" ] && {
echo "ERROR: you can't select corosync and qdiskd togetehr."
exit 1
}
## show details:
echo "***** requested configuration *****"
echo ""
echo "cbox version: VERSION"
echo ""
echo "cluster name: $cboxclustername"
echo "cluster nodes: $cboxnumnodes"
echo "cluster type: $cboxclustertype"
echo "cluster qdiskd: $cboxqdiskd"
echo "cluster resource manager: $cboxrasmngr"
echo "cluster fedora release: $cboxfedrelease"
echo ""
echo "node capacity (for each node):"
echo ""
echo "RAM: $cboxvirtram (MB)"
echo "CPU(s): $cboxvirtcpus"
echo "root partition size: $cboxvirtroot (MB)"
echo "swap partition size: $cboxvirtswap (MB)"
echo ""
echo "shared disk size: $cboxvirtshare (MB)"
echo ""
echo "under the hood info"
echo "cbox data dir: $cboxdatadir"
echo "cbox hooks dir: $cboxhooksdir"
echo "cbox log dir: $cboxlogdir"
echo "cbox log file: $cboxlogfile"
echo "cbox VMs storage dir: $cboxvmsdir"
echo ""
if [ -z "$cboxforce" ]; then
read -p "Proceed (Y/n)? " ans
[ -n "$ans" ] && [ "$ans" != "Y" ] && {
echo "Aborting"
exit 1
}
fi
echo "cboxclustername=$cboxclustername" > /root/.cbox
echo "cboxvmsdir=$cboxvmsdir" >> /root/.cbox
cboxtempdir=$cboxvmsdir/$cboxclustername/temp
mkdir -p $cboxtempdir
# export all into enviroment
export cboxdatadir
export cboxhooksdir
export cboxclustername
export cboxnumnodes
export cboxclustertype
export cboxlogdir
export cboxlogfile
export cboxfedrelease
export cboxos
export cboxkickstart
export cboxrasmngr
export cboxqdiskd
export cboxvmsdir
export cboxtempdir
export cboxvirtram
export cboxvirtcpus
export cboxvirtroot
export cboxvirtswap
export cboxvirtshare
export cboxuseloop
export cboxemulator
export cboxrepourl
export cboxdrivernode
echo ""
echo "Creating cluster... this might take several minutes"
echo ""
# we catch errors here
set +e
for i in $(LC_ALL=C; echo $cboxhooksdir/*[^~,]); do
[ -d $i ] && continue
# skip know scripts
[ "${i%.cfsaved}" != "${i}" ] && continue
[ "${i%.rpmsave}" != "${i}" ] && continue
[ "${i%.rpmorig}" != "${i}" ] && continue
[ "${i%.rpmnew}" != "${i}" ] && continue
[ "${i%.swp}" != "${i}" ] && continue
[ "${i%,v}" != "${i}" ] && continue
[ "${i%.dpkg-old}" != "${i}" ] && continue
[ "${i%.dpkg-dist}" != "${i}" ] && continue
[ "${i%.dpkg-new}" != "${i}" ] && continue
if [ -x $i ]; then
if [ -n "$cboxexclude" ] && [[ "$(basename $i)" =~ $cboxexclude ]];then
echo "Skipping $(basename $i)" >> $cboxlogfile
continue
fi
echo -n "Executing $(basename $i): "
echo "Executing $(basename $i)" >> $cboxlogfile
while true; do
$i >> $cboxlogfile 2>&1 || {
echo "ERROR $? from $(basename $i). Please check logs"
if [ -z $cboxdevel ]; then
exit 1
else
echo "Run 'screen -rS cboxfixme' and exit when config fixed."
screen -DmS cboxfixme
continue
fi
}
break
done
echo "OK"
echo "Done with $(basename $i)" >> $cboxlogfile
fi
done
echo "cluster $cboxclustername successfully created and running"
exit 0
# vim:shiftwidth=8:noexpandtab