forked from pathbreak/storm-linode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzookeeper-cluster-linode.sh
2866 lines (2162 loc) · 86.5 KB
/
zookeeper-cluster-linode.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
. ./textfileops.sh
# Workaround to avoid "Agent admitted failure to sign using the key." ssh error despite
# using correct key, due to some conflict with gnone-keyring.
SSH_AUTH_SOCK=0
export SSH_AUTH_SOCK
# $1 -> Cluster name, or absolute or relative path of cluster directory, or
# absolute or relative path of cluster conf file in cluster directory.
load_cluster_conf() {
if [ -z "$1" ]; then
echo "Error: Provide a cluster name or cluster directory path or cluster configuration file path"
return 1
fi
local param_full_path=$(readlink -m "$1")
if [ -d "$param_full_path" ]; then
# Check that there is a $CLUSTER_NAME.conf in the directory and it is
# a proper cluster conf.
CLUSTER_NAME=$(basename "$param_full_path")
CLUSTER_CONF_FILE="$param_full_path/$CLUSTER_NAME.conf"
CLUSTER_CONF_DIR="$(dirname $CLUSTER_CONF_FILE)"
if [ ! -f "$CLUSTER_CONF_FILE" ]; then
echo "Error: '$1' does not seem to be a valid cluster name or cluster directory. Could not find cluster configuration file '$CLUSTER_CONF_FILE'"
return 1
fi
elif [ -f "$param_full_path" ]; then
# The given file may be any file at all, but what we want is a conf file with the correct cluster name.
CLUSTER_NAME=$(basename $(dirname "$param_full_path"))
CLUSTER_CONF_DIR=$(dirname "$param_full_path")
CLUSTER_CONF_FILE="$CLUSTER_CONF_DIR/$CLUSTER_NAME.conf"
local param_basename=$(basename "$param_full_path")
if [ "$param_basename" != "$CLUSTER_NAME.conf" ]; then
echo "Error: '$1' does not seem to be a valid cluster configuration filename. Expected cluster configuration filename '$CLUSTER_CONF_FILE'"
return 1
fi
if [ ! -f "$CLUSTER_CONF_FILE" ]; then
echo "Error: Cluster configuration file '$CLUSTER_CONF_FILE' does not exist."
return 1
fi
else
echo "Error: '$param_full_path' does not exist."
return 1
fi
# Absolute paths of this script's directory, and the image conf file's directory.
SCRIPT_DIR="$(pwd)"
echo "SCRIPT_DIR=$SCRIPT_DIR"
echo "CLUSTER_NAME=$CLUSTER_NAME"
echo "CLUSTER_CONF_DIR=$CLUSTER_CONF_DIR"
echo "CLUSTER_CONF_FILE=$CLUSTER_CONF_FILE"
# Include the cluster conf file
source $CLUSTER_CONF_FILE
# ZK_IMAGE_CONF may be a path that is relative to the cluster conf file, such
# as "../zk-image1/zk-image1.conf" or "../zk-image1" or "/home/clustermgr/storm-linode/zk-image1"
# We need to resolve it to its absolute path by prefixing it with $CLUSTER_CONF_DIR
# to get "$CLUSTER_CONF_DIR/../zk-image1/zk-image1.conf"
if [ -z "$ZK_IMAGE_CONF" ]; then
echo "Error: ZK_IMAGE_CONF should be a Zookeeper image directory or image configuration file"
return 1
fi
local zk_image_conf
if [ "${ZK_IMAGE_CONF:0:1}" == "/" ]; then
# It's an absolute path. Retain as it is.
zk_image_conf="$ZK_IMAGE_CONF"
else
# It's a relative path. Convert to absolute by prefixing with cluster conf dir.
zk_image_conf="$(readlink -m $CLUSTER_CONF_DIR/$ZK_IMAGE_CONF)"
fi
if ! load_image_conf "$zk_image_conf"; then
echo "Error: Change value of ZK_IMAGE_CONF to an existing Zookeeper image directory or image configuration file path."
return 1
fi
if ! validate_cluster_configuration; then
return 1
fi
NODE_USERNAME=root
if [ -z "$NODE_ROOT_SSH_PRIVATE_KEY" ]; then
NODE_ROOT_SSH_PRIVATE_KEY=$IMAGE_ROOT_SSH_PRIVATE_KEY
fi
return 0
}
# $1 : Image name, or a target directory as absolute or relative path
create_new_image_conf() {
if check_image_dir_exists "$1"; then
echo "Validation error: Image '$1' already exists. Provide a unique name or directory."
return 1
fi
echo "IMAGE_NAME=$IMAGE_NAME"
if ! validate_image_name "$IMAGE_NAME"; then
return 1
fi
mkdir -p "$1"
cp zk-image-example.conf "$1/$IMAGE_NAME.conf"
cp template_zoo.cfg "$1/zoo.cfg"
cp template_zk_log4j.properties "$1/log4j.properties"
cp template-zk-supervisord.conf "$1/zk-supervisord.conf"
chmod go-rwx $1/*
echo "Created image directory '$1'"
echo "Edit $1/$IMAGE_NAME.conf to enter mandatory properties, and then create the image with "
echo " zookeeper-cluster-linode.sh create-image $1 <API-ENV-CONF-FILE>"
}
# $1 : Name of image directory or Path of image configuration file.
# $2 : Name of API environment configuration file containing API endpoint and key.
create_zk_image() {
if ! load_image_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
NODE_USERNAME=root
local stfile=$(image_status_file)
if [ -f "$stfile" ]; then
# Status file already exist, which means image is partially/fully created.
echo "Image is already created or is being created. Nothing further to do..."
return 1
fi
local linout linerr linret
# There are limits on both number of images (max 3) and total size across
# images (10240MB). If these limits are reached or close to breaching, warn user
# and prevent disk imaging.
echo "Image prechecks"
linode_api linout linerr linret "imagestats"
if [ $linret -eq 1 ]; then
echo "Failed to get image statistics. Continuing, but there is a chance image creation may fail due to image count and disk size limits. Error:$linerr"
else
local num_images=$(echo $linout|cut -d ',' -f1)
local total_image_size=$(echo $linout|cut -d ',' -f2)
# The number of image limit does not seem to be consistent.
# Disabling the check.
#if [ $num_images -eq 3 ]; then
# printf "Error: Cannot create image because account limit of 3 images have already been created.\n"
# printf "Please delete an image using 'delete-image' or from 'Linode Manager' before retrying.\n"
# return 1
#fi
if [ $total_image_size -ge 9000 ]; then
printf "Warning: Total size of existing images $total_image_size MB is close to limit of 10240 MB.\n"
printf "There's a chance this image creation may fail.\n"
printf "If it does, delete an existing image and try again.\n"
fi
fi
create_image_status_file
# Create temporary linode of lowest cost plan in specified datacenter.
echo "Creating temporary linode"
linode_api linout linerr linret "create-node" 1 "$DATACENTER_FOR_IMAGE"
if [ $linret -eq 1 ]; then
echo "Failed to create temporary linode. Error:$linerr"
return 1
fi
local temp_linode_id=$linout
echo "Created temporary linode $temp_linode_id"
linode_api linout linerr linret "update-node" $temp_linode_id "zktmp-$temp_linode_id" "temporary"
if [ $linret -eq 1 ]; then
echo "Failed to update node label $temp_linode_id. Error:$linerr"
return 1
fi
# Create a disk from distribution.
echo "Creating disk"
linode_api linout linerr linret "create-disk-from-distribution" $temp_linode_id "$DISTRIBUTION_FOR_IMAGE" \
$IMAGE_DISK_SIZE "$IMAGE_ROOT_PASSWORD" "$IMAGE_ROOT_SSH_PUBLIC_KEY"
if [ $linret -eq 1 ]; then
echo "Failed to create disk. Error:$linerr"
return 1
fi
local disk_id=$(echo $linout|cut -d ',' -f1)
local create_disk_job_id=$(echo $linout|cut -d ',' -f2)
local disk_result
wait_for_job $create_disk_job_id $temp_linode_id
disk_result=$?
if [ $disk_result -eq 0 ]; then
echo "Create disk did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $disk_result -ge 2 ]; then
echo "Create disk failed."
return 1
fi
# Create a configuration profile with that disk.
echo "Creating a configuration"
linode_api linout linerr linret "create-config" $temp_linode_id "$KERNEL_FOR_IMAGE" \
$disk_id "template-configuration"
if [ $linret -eq 1 ]; then
echo "Failed to create configuration. Error:$linerr"
return 1
fi
local config_id=$linout
# Boot the linode.
echo "Booting the linode"
linode_api linout linerr linret "boot" $temp_linode_id $config_id
if [ $linret -eq 1 ]; then
echo "Failed to boot. Error:$linerr"
return 1
fi
local boot_job_id=$linout
# Wait for node to boot up.
local boot_result
wait_for_job $boot_job_id $temp_linode_id
boot_result=$?
if [ $boot_result -eq 0 ]; then
echo "Boot job did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $boot_result -ge 2 ]; then
echo "Booting failed."
return 1
fi
# Get public IP address of node.
echo "Getting IP address of linode"
linode_api linout linerr linret "public-ip" $temp_linode_id
if [ $linret -eq 1 ]; then
echo "Failed to get IP address. Error:$linerr"
return 1
fi
local ipaddr=$linout
echo "IP address: $ipaddr"
setup_users_and_authentication_for_image $ipaddr
#echo "Installing prerequisite software"
install_software_on_node $ipaddr $NODE_USERNAME
#echo "Installing Zookeeper software"
install_zookeeper_on_node $ipaddr $NODE_USERNAME
# Shutdown the linode.
echo "Shutting down the linode"
linode_api linout linerr linret "shutdown" $temp_linode_id
if [ $linret -eq 1 ]; then
echo "Failed to shutdown. Error:$linerr"
return 1
fi
local shutdown_job_id=$linout
# Wait for linode to shutdown.
local shutdown_result
wait_for_job $shutdown_job_id $temp_linode_id
shutdown_result=$?
if [ $shutdown_result -eq 0 ]; then
echo "Shutdown job did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $shutdown_result -ge 2 ]; then
echo "Shutdown failed."
return 1
fi
# create image of the disk
echo "Creating image of disk $disk_id"
linode_api linout linerr linret "create-image" $temp_linode_id $disk_id "$LABEL_FOR_IMAGE"
if [ $linret -eq 1 ]; then
echo "Failed to create image of disk. Error:$linerr"
return 1
fi
local image_id=$(echo $linout|cut -d ',' -f1)
local image_disk_job_id=$(echo $linout|cut -d ',' -f2)
local image_result
wait_for_job $image_disk_job_id $temp_linode_id
image_result=$?
if [ $image_result -eq 0 ]; then
echo "Image job did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $image_result -ge 2 ]; then
echo "Imaging failed."
return 1
fi
echo "Template image $image_id successfully created"
add_section $stfile "image"
insert_bottom_of_section $stfile "image" "$image_id"
# delete temporary linode. 'skipchecks' is 1 (true) because it's much
# easier than detaching disk from config and then deleting.
echo "Deleting the temporary linode $temp_linode_id"
linode_api linout linerr linret "delete-node" $temp_linode_id 1
if [ $linret -eq 1 ]; then
echo "Failed to delete temporary linode. Error:$linerr"
return 1
fi
printf "\n\nFinished creating Zookeeper template image $image_id\n"
return 0
}
# $1 : Name of image directory or Path of image configuration file.
# $2 : Name of API environment configuration file containing API endpoint and key.
delete_zk_image() {
if ! load_image_conf "$1"; then
return 1
fi
# Include API endpoint configuration file.
if ! load_api_env_configuration "$2"; then
return 1
fi
local stfile="$(image_status_file)"
if [ ! -f "$stfile" ]; then
# Status file does not exist, which means image is probably not created.
echo "Image does not exist. Nothing further to do..."
return 1
fi
local image_id=$(get_section $stfile "image")
echo "Deleting image $image_id"
local linout linerr linret
linode_api linout linerr linret "delete-image" $image_id
if [ $linret -eq 1 ]; then
echo "Failed to delete image $image_id. Error:$linerr"
return 1
fi
rm -f "$stfile"
printf "\n\nFinished deleting Zookeeper image $image_id\n"
return 0
}
# $1 : Cluster name, or a target directory as absolute or relative path
create_new_cluster_conf() {
if check_cluster_dir_exists "$1"; then
echo "Validation error: Cluster '$1' already exists. Provide a unique name or directory."
return 1
fi
echo "CLUSTER_NAME=$CLUSTER_NAME"
if ! validate_cluster_name "$CLUSTER_NAME"; then
return 1
fi
mkdir -p "$1"
cp zk-cluster-example.conf "$1/$CLUSTER_NAME.conf"
chmod go-rwx "$1/$CLUSTER_NAME.conf"
echo "Created cluster directory $1. "
echo "Edit $1/$CLUSTER_NAME.conf to enter mandatory properties, and then create the cluster with "
echo " zookeeper-cluster-linode.sh create $1 <API-ENV-CONF-FILE>"
}
# $1 : Name of cluster directory or Path of cluster configuration file.
# $2 : The API environment configuration file
# $3 : (Optional) If this is "--dontshutdown", then nodes are not stopped after creation. This option should be passed
# only by start cluster. By default, nodes are shutdown after creation
create_cluster() {
# Include the specified configuration file with cluster specific environment variables.
if ! load_cluster_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
local stfile=$(status_file)
if [ -f "$stfile" ]; then
# Status file already exist, which means cluster is partially/fully created.
echo "$CLUSTER_NAME is already created or is being created. Nothing further to do..."
return 1
fi
echo "Creating Zookeeper cluster $CLUSTER_NAME..."
create_status_file
update_cluster_status "creating"
create_new_nodes $CLUSTER_NAME
start_nodes $CLUSTER_NAME
# Wait for sometime after nodes have booted up for SSH daemon to come up.
sleep 15
# Since nodes created from an image retain the image's host keys, they should
# be changed to unique ones before doing anything else.
change_hostkeys $CLUSTER_NAME
set_hostnames $CLUSTER_NAME
distribute_hostsfile $CLUSTER_NAME
# zkdatadir/myid has to be created uniquely in each node.
# zoo.cfg should contain list of all zk nodes, and distributed to each node.
assign_zk_node_ids $CLUSTER_NAME
create_zk_configuration $CLUSTER_NAME
distribute_zk_configuration $CLUSTER_NAME
#configure_zookeeper_cluster $CLUSTER_NAME
# Configure firewall and security on all nodes.
create_cluster_security_configurations $CLUSTER_NAME
distribute_cluster_security_configurations $CLUSTER_NAME
update_security_status "unchanged"
# create may be called either independently or while starting cluster.
# When called independently, after creation, we can shutdown all nodes.
# When called by cluster startup, don't shutdown nodes since it's wasteful.
if [ "$3" != "--dontshutdown" ]; then
sleep 5
stop_nodes $CLUSTER_NAME
fi
update_cluster_status "created"
echo "Zookeeper cluster successfully created"
}
# $1 : Name of cluster directory or Path of cluster configuration file.
# $2 : The API environment configuration file
start_cluster() {
echo "Starting Zookeeper cluster $1..."
if ! load_cluster_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
# If the cluster is not yet created, it should be first created along with all steps like
# - getting IP addresses
# - setting hostnames
# - distributing hosts file
# - distributing zookeeper configurations
#
# If cluster is already created but not started, then all the above steps should be done, except setting hostnames - that's a creation only operation.
#
# If cluster is already started, nothing to do.
local stfile=$(status_file)
if [ ! -f "$stfile" ]; then
# Status file does not exist, which means cluster is not created.
# So everything is done by create_cluster, and this function should just start zookeeper service on all nodes.
create_cluster "$1" "$2" "--dontshutdown"
else
local status=$(get_cluster_status)
if [ "$status" == "running" ]; then
echo "Cluster $CLUSTER_NAME is already running. Nothing further to do..."
return 0
elif [ "$status" == "creating" ]; then
echo "Cluster $CLUSTER_NAME is currently being created. Please wait for it to be created and then run start..."
return 1
elif [ "$status" == "starting" ]; then
echo "Cluster $CLUSTER_NAME is currently being started. Nothing further to do..."
return 1
elif [ "$status" == "stopping" ]; then
echo "Cluster $CLUSTER_NAME is currently stopping. Please wait for it to stop and then run start..."
return 1
elif [ "$status" == "destroying" ]; then
echo "Cluster $CLUSTER_NAME is currently being destroyed. Please wait for it to get destroyed and then run start or create..."
return 1
fi
echo "Cluster already created. Starting it..."
update_cluster_status "starting"
# So cluster is created, but it's "stopped". We need to start all nodes, get new IP addresses,
# and distribute hosts file.
start_nodes $CLUSTER_NAME
# Wait for sometime after nodes have booted up for SSH daemon to come up.
sleep 15
# The cluster zoo.cfg may have been changed by admin.
# If so, it should be distributed.
local conf_status=$(get_conf_status)
if [ "$conf_status" == "changed" ]; then
echo "Zoo.cfg has changed. Applying new configuration"
distribute_zk_configuration $CLUSTER_NAME
update_conf_status "unchanged"
fi
# The cluster security configuration may have changed if another cluster
# requested itself to be whitelisted, or if admin updated the firewall rules.
# If so, security configuration should be recreated and uploaded.
local security_status=$(get_security_status)
if [ "$security_status" == "changed" ]; then
echo "Security configuration has changed. Applying new configuration"
distribute_cluster_security_configurations $CLUSTER_NAME
update_security_status "unchanged"
fi
fi
# For newly created or restarted, we need to start the zookeeper services.
# zoo.cfg need not be updated even if node IP addresses have changed, because it uses hostnames, not IP addresses.
start_zookeeper $CLUSTER_NAME
update_cluster_status "running"
echo "Zookeeper cluster $CLUSTER_NAME is running"
return 0
}
# $1 : Name of cluster directory or Path of cluster configuration file.
# $2 : The API environment configuration file
stop_cluster() {
echo "Stopping cluster $1..."
if ! load_cluster_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
update_cluster_status "stopping"
# First stop zookeeper on all nodes, so that entire ensemble can save whatever state it should.
stop_zookeeper $CLUSTER_NAME
# Wait some time before stopping the nodes.
sleep 10
stop_nodes $CLUSTER_NAME
update_cluster_status "stopped"
echo "Zookeeper cluster $CLUSTER_NAME is stopped"
}
# $1: cluster name
start_nodes() {
local stfile="$(status_file)"
local nodes=$(get_section $stfile nodes)
local boot_jobs=''
for node in $nodes
do
echo "Starting linode $node..."
linode_api linout linerr linret "boot" $node
if [ $linret -eq 1 ]; then
echo "Failed to boot. Error:$linerr"
return 1
fi
local boot_job_id=$linout
boot_jobs="$boot_jobs $boot_job_id:$node"
done
echo "Waiting for nodes to boot"
for job in $boot_jobs
do
job_id=$(echo $job|cut -d':' -f1)
linode_id=$(echo $job|cut -d':' -f2)
# Wait for node to boot up.
local boot_result
wait_for_job $job_id $linode_id
boot_result=$?
if [ $boot_result -eq 0 ]; then
echo "Linode $linode_id did not boot up even after 4 minutes. Aborting"
return 1
fi
if [ $boot_result -ge 2 ]; then
echo "Linode $linode_id booting failed."
return 1
fi
done
echo "All nodes booted"
return 0
}
# $1: cluster name
create_new_nodes() {
local linout linerr linret
# Validate the datacenter.
linode_api linout linerr linret "datacenter-id" "$DATACENTER_FOR_CLUSTER"
if [ $linret -eq 1 ]; then
echo "Failed to find datacenter. Error:$linerr"
return 1
fi
local dc_id=$linout
echo "Datacenter ID=$dc_id"
# Get the image id from the image info file. If there is no image info file,
# then try to find using API.
local img_stfile="$(image_status_file)"
local image_id
if [ -f "$img_stfile" ]; then
image_id=$(get_section $img_stfile "image")
fi
if [ -z "$image_id" ]; then
echo "Unable to get image ID from image info file. Searching for image using label..."
linode_api linout linerr linret "image-id" "$LABEL_FOR_IMAGE"
if [ $linret -eq 1 ]; then
echo "Failed to find image. Error:$linerr"
return 1
fi
image_id=$(echo $linout|cut -d ',' -f1)
fi
if [ -z "$image_id" ]; then
echo "Failed to find image."
return 1
fi
echo "Image ID=$image_id"
# Get the kernel ID.
linode_api linout linerr linret "kernel-id" "$KERNEL_FOR_IMAGE"
if [ $linret -eq 1 ]; then
echo "Failed to find kernel. Error:$linerr"
return 1
fi
local kernel_id=$(echo $linout|cut -d ',' -f1)
echo "Kernel ID=$kernel_id"
local stfile="$(status_file)"
add_section $stfile "nodes"
add_section $stfile "ipaddresses"
echo "Creating $CLUSTER_SIZE new nodes in datacenter $dc_id based on image $image_id..."
for i in $CLUSTER_SIZE; do
plan=$(echo $i|cut -d ':' -f1)
count=$(echo $i|cut -d ':' -f2)
local plan_id
case $plan in
"2GB")
plan_id=1
;;
"4GB")
plan_id=2
;;
"8GB")
plan_id=4
;;
"12GB")
plan_id=6
;;
"24GB")
plan_id=7
;;
"48GB")
plan_id=8
;;
"64GB")
plan_id=9
;;
"80GB")
plan_id=10
;;
"120GB")
plan_id=12
;;
*)
echo "Invalid plan $plan. Aborting..."
return 1
;;
esac
echo "Creating $count $plan linodes (plan ID $plan_id)"
local node_count=1
while [ $node_count -le $count ]; do
# Create linode with specified plan and datacenter. The 0 at the end
# avoids validating datacenter id for every iteration.
linode_api linout linerr linret "create-node" $plan_id $dc_id 0
if [ $linret -eq 1 ]; then
echo "Failed to create $plan linode #$node_count. Error:$linerr"
return 1
fi
local linode_id=$linout
printf "\n\nCreated linode $linode_id\n"
# Store created linode's instance ID in status file.
# No need to store additional data like plan ID or datacenter ID
# because both are available from "linode.list" if required
write_status "nodes" $linode_id
node_count=$((node_count+1))
linode_api linout linerr linret "update-node" $linode_id "zk-$linode_id" "$CLUSTER_NAME"
if [ $linret -eq 1 ]; then
echo "Failed to update node label $linode_id. Error:$linerr"
return 1
fi
# Create a disk from distribution.
echo "Creating disk from Zookeeper image for linode $linode_id"
linode_api linout linerr linret "create-disk-from-image" $linode_id $image_id \
"Zookeeper" $NODE_DISK_SIZE "$NODE_ROOT_PASSWORD" "$NODE_ROOT_SSH_PUBLIC_KEY"
if [ $linret -eq 1 ]; then
echo "Failed to create image. Error:$linerr"
return 1
fi
local disk_id=$(echo $linout|cut -d ',' -f1)
local create_disk_job_id=$(echo $linout|cut -d ',' -f2)
local disk_result
wait_for_job $create_disk_job_id $linode_id
disk_result=$?
if [ $disk_result -eq 0 ]; then
echo "Create disk did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $disk_result -ge 2 ]; then
echo "Create disk failed."
return 1
fi
echo "Finished creating disk $disk_id from Zookeeper image for linode $linode_id"
# Create a swap disk sized according to the linode's RAM.
# The linode distributions are configured to expect /dev/sdb block device
# as swap space. If a swap disk is not part of the configuration, everything
# still succeeds but the dashboard shows a misconfiguration warning, and
# performance may also suffer.
echo "Creating swap disk"
linode_api linout linerr linret "create-swap-disk" $linode_id
if [ $linret -eq 1 ]; then
echo "Failed to create swap disk. Error:$linerr"
return 1
fi
local swap_disk_id=$(echo $linout|cut -d ',' -f1)
local create_swap_disk_job_id=$(echo $linout|cut -d ',' -f2)
local disk_result
wait_for_job $create_swap_disk_job_id $linode_id
disk_result=$?
if [ $disk_result -eq 0 ]; then
echo "Create swap disk did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $disk_result -ge 2 ]; then
echo "Create swap disk failed."
return 1
fi
# Create a configuration profile with that disk. The 0 at the end
# avoids validating kernel id for every iteration.
echo "Creating a configuration"
linode_api linout linerr linret "create-config" $linode_id $kernel_id \
"$disk_id,$swap_disk_id" "Zookeeper-configuration" 0
if [ $linret -eq 1 ]; then
echo "Failed to create configuration. Error:$linerr"
return 1
fi
local config_id=$linout
echo "Finished creating configuration $config_id for linode $linode_id"
# Add a private IP for this linode.
echo "Creating private IP for linode"
linode_api linout linerr linret "add-private-ip" $linode_id
if [ $linret -eq 1 ]; then
echo "Failed to add private IP address. Error:$linerr"
return 1
fi
local private_ip=$linout
echo "Private IP address $private_ip created for linode $linode_id"
# Get its public IP
linode_api linout linerr linret "public-ip" $linode_id
if [ $linret -eq 1 ]; then
echo "Failed to get public IP address. Error:$linerr"
return 1
fi
local public_ip=$linout
echo "Public IP address is $public_ip for linode $linode_id"
insert_or_replace_in_section $stfile "ipaddresses" $linode_id "$linode_id $private_ip $public_ip"
done
done
}
add_nodes() {
# One candidate implementation that can be tried out is described in https://gist.github.com/miketheman/6057930.
# Basically, add new nodes to zoo.cfg of followers, and restart followers.
# Finally, add new nodes to zoo.cfg of leader and restart leader.
# Another possibility is "dynamic reconfiguration" (https://zookeeper.apache.org/doc/trunk/zookeeperReconfig.html#ch_reconfig_format)
# but it's available only from >= 3.5.0
# Another possibility from http://stackoverflow.com/questions/11375126/zookeeper-adding-peers-dynamically ...
# setup a new cluster, snapshot the existing one, restore from snapshot on new cluster, update all clients.
return 1
}
# $1 : Name of cluster directory or Path of cluster configuration file.
# $2 : The API environment configuration file
destroy_cluster() {
if ! load_cluster_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
local stfile=$(status_file)
update_cluster_status "destroying"
local nodes=$(get_section $stfile "nodes")
local failures=0
while read node;
do
echo "Destroying $node..."
# Shutdown the linode.
echo "Shutting down the linode"
linode_api linout linerr linret "shutdown" $node
if [ $linret -eq 1 ]; then
echo "Failed to shutdown. Error:$linerr"
return 1
fi
local shutdown_job_id=$linout
# Wait for linode to shutdown.
local shutdown_result
wait_for_job $shutdown_job_id $node
shutdown_result=$?
if [ $shutdown_result -eq 0 ]; then
echo "Shutdown job did not complete even after 4 minutes. Aborting"
return 1
fi
if [ $shutdown_result -ge 2 ]; then
echo "Shutdown failed."
return 1
fi
sleep 2
# Delete node (and skip checks)
linode_api linout linerr linret "delete-node" $node 1
if [ $linret -eq 1 ]; then
echo "Failed to delete. Error:$linerr"
failures=1
continue
fi
# Remove entries for this node from all sections of status file
delete_line $stfile "nodes" $node
delete_line $stfile "ipaddresses" $node
delete_line $stfile "hostnames" $node
delete_line $stfile "myids" $node
done <<< "$nodes"
# Don't delete status file if there are any failures above
if [ $failures -eq 0 ]; then
echo "Deleting cluster status file..."
rm -f $stfile
echo "Deleting cluster hosts file..."
rm -f "$CLUSTER_CONF_DIR/$CLUSTER_NAME.hosts"
echo "Deleting cluster ZK cfg file..."
rm -f "$CLUSTER_CONF_DIR/zoo.cfg"
echo "Deleting security configuration files..."
rm -f "$CLUSTER_CONF_DIR/$CLUSTER_NAME-all-whitelists.ipsets"
rm -f "$CLUSTER_CONF_DIR/$CLUSTER_NAME-rules.v6"
rm -f "$CLUSTER_CONF_DIR/$CLUSTER_NAME-rules.ipsets"
rm -f "$CLUSTER_CONF_DIR/$CLUSTER_NAME-whitelist.ipsets"
rm -f "$CLUSTER_CONF_DIR/$CLUSTER_NAME-rules.v4"
# Delete the host entries from this cluster manager machine on which this script is running.
echo "$CLUSTER_MANAGER_NODE_PASSWORD"|sudo -S sh hostname_manager.sh "delete-cluster" $CLUSTER_NAME
echo "Zookeeper cluster $CLUSTER_NAME is destroyed"
else
echo "Leaving cluster status file intact, because some nodes could not be destroyed"
fi
}