-
Notifications
You must be signed in to change notification settings - Fork 3
/
storm-cluster-linode.sh
3520 lines (2677 loc) · 113 KB
/
storm-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
# Prerequisites:
# - ssh should be installed
# - python2 should be installed (for JSON reading/writing)
# - curl should be installed
. ./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
# STORM_IMAGE_CONF may be a path that is relative to the cluster conf file, such
# as "../storm-image1/storm-image1.conf" or "../storm-image1" or "/home/clustermgr/storm-linode/storm-image1"
# We need to resolve it to its absolute path by prefixing it with $CLUSTER_CONF_DIR
# to get "$CLUSTER_CONF_DIR/../storm-image1/storm-image1.conf
if [ -z "$STORM_IMAGE_CONF" ]; then
echo "Error: STORM_IMAGE_CONF should be a Storm image directory or image configuration file"
return 1
fi
local storm_image_conf
if [ "${STORM_IMAGE_CONF:0:1}" == "/" ]; then
# It's an absolute path. Retain as it is.
storm_image_conf="$STORM_IMAGE_CONF"
else
# It's a relative path. Convert to absolute by prefixing with cluster conf dir.
storm_image_conf="$(readlink -m $CLUSTER_CONF_DIR/$STORM_IMAGE_CONF)"
fi
if ! load_image_conf "$storm_image_conf"; then
echo "Error: Change value of STORM_IMAGE_CONF to an existing Storm image directory or image configuration file path."
return 1
fi
# We need Zookeeper cluster name as well as cluster directory for many operations in this script.
# So validate the path pointed by ZOOKEEPER_CLUSTER.
# ZOOKEEPER_CLUSTER may be a path that is relative to the cluster conf file, such
# as "../zk-cluster1/zk-cluster1.conf" or "../zk-cluster1" or "/home/clustermgr/storm-linode/zk-cluster1"
# We need to resolve it to its absolute path by prefixing it with $CLUSTER_CONF_DIR
# to get "$CLUSTER_CONF_DIR/../zk-cluster1/zk-cluster1.conf"
if [ -z "$ZOOKEEPER_CLUSTER" ]; then
echo "Error: ZOOKEEPER_CLUSTER should be a Zookeeper cluster directory or cluster configuration file"
return 1
fi
local zk_cluster_conf="$ZOOKEEPER_CLUSTER"
if [ "${ZOOKEEPER_CLUSTER:0:1}" != "/" ]; then
# It's a relative path. Convert to absolute by prefixing with cluster conf dir.
zk_cluster_conf="$(readlink -m $CLUSTER_CONF_DIR/$ZOOKEEPER_CLUSTER)"
fi
if ! load_zk_conf "$zk_cluster_conf"; then
echo "Error: Change value of ZOOKEEPER_CLUSTER to an existing Zookeeper cluster directory or cluster 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 -> ZK cluster name, or absolute or relative path of cluster directory, or
# absolute or relative path of cluster conf file in cluster directory.
load_zk_conf() {
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.
ZK_CLUSTER_NAME=$(basename "$param_full_path")
ZK_CLUSTER_CONF_FILE="$param_full_path/$ZK_CLUSTER_NAME.conf"
ZK_CLUSTER_CONF_DIR="$(dirname $ZK_CLUSTER_CONF_FILE)"
if [ ! -f "$ZK_CLUSTER_CONF_FILE" ]; then
printf "Error: '$1' does not seem to be a valid Zookeeper cluster name or cluster directory\n."
printf "Could not find cluster configuration file '$ZK_CLUSTER_CONF_FILE'\n"
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.
ZK_CLUSTER_NAME=$(basename $(dirname "$param_full_path"))
ZK_CLUSTER_CONF_DIR=$(dirname "$param_full_path")
ZK_CLUSTER_CONF_FILE="$ZK_CLUSTER_CONF_DIR/$ZK_CLUSTER_NAME.conf"
local param_basename=$(basename "$param_full_path")
if [ "$param_basename" != "$ZK_CLUSTER_NAME.conf" ]; then
printf "Error: '$1' does not seem to be a valid Zookeeper cluster configuration filename.\n"
printf "Expected cluster configuration filename '$ZK_CLUSTER_CONF_FILE'\n"
return 1
fi
if [ ! -f "$ZK_CLUSTER_CONF_FILE" ]; then
echo "Error: Zookeeper Cluster configuration file '$ZK_CLUSTER_CONF_FILE' does not exist."
return 1
fi
else
echo "Error: Zookeeper Cluster configuration '$param_full_path' does not exist."
return 1
fi
echo "ZK_CLUSTER_NAME=$ZK_CLUSTER_NAME"
echo "ZK_CLUSTER_CONF_DIR=$ZK_CLUSTER_CONF_DIR"
echo "ZK_CLUSTER_CONF_FILE=$ZK_CLUSTER_CONF_FILE"
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 storm-image-example.conf "$1/$IMAGE_NAME.conf"
cp template-storm.yaml "$1/"
cp template-storm-supervisord.conf "$1/"
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 " storm-cluster-linode.sh create-image $1 <API-ENV-CONF-FILE>"
}
# $1 : Name of configuration file containing base node spec, template node spec, install flags and any other
# common configuration options.
# $2 : Name of API environment configuration file containing API endpoint and key.
create_storm_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 it.
#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 maximum 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 "stormtmp-$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
install_software_on_node $ipaddr $NODE_USERNAME
install_storm_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 Storm template image $image_id\n"
return 0
}
# $1 : Name of configuration file containing base node spec, template node spec, install flags and any other
# common configuration options.
# $2 : Name of API environment configuration file containing API endpoint and key.
delete_storm_image() {
if ! load_image_conf "$1"; then
return 1
fi
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 Storm image $image_id\n"
return 0
}
# $1 : Cluster directory name
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 storm-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 " storm-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 storm cluster $CLUSTER_NAME..."
# Some steps like hosts file distribution require hostnames and IP addresses of ZK nodes. If ZK cluster status file
# does not exist, then create the cluster.
# TODO handle zookeeper cluster creation errors better. Its possible outcomes are
# created | not created because it already exists | creation failed
./zookeeper-cluster-linode.sh create "$ZK_CLUSTER_CONF_FILE" "$2"
create_status_file
update_cluster_status "creating"
create_new_nodes $CLUSTER_NAME
if [ $? -ne 0 ]; then
echo "Node creation failed. Aborting"
return 1
fi
start_nodes $CLUSTER_NAME
if [ $? -ne 0 ]; then
echo "Could not start all nodes. Aborting"
return 1
fi
# 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
# Storm cluster nodes should know hostnames of all the ZK nodes too.
distribute_hostsfile $CLUSTER_NAME
# storm.yaml should contain nimbus hostname and list of all ZK nodes, and should be distributed to each node.
create_storm_configuration $CLUSTER_NAME
distribute_storm_configuration $CLUSTER_NAME
# Install apache reverse proxy on client node. It should be under supervisor control (autostart)
# This reverse proxy acts as a gateway to both the storm-ui web app and the logviewer URLs
# of each supervisor node, which are served only via their private hostnames and hence not
# accessible from developer/sysadmin machines outside the cluster.
install_client_reverse_proxy $CLUSTER_NAME
configure_client_reverse_proxy $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
stop_nodes $CLUSTER_NAME
fi
update_cluster_status "created"
echo "Storm cluster successfully created"
}
# $1 : Name of cluster directory or Path of cluster configuration file.
# $2 : The API environment configuration file
start_cluster() {
if ! load_cluster_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
echo "Starting Storm cluster $CLUSTER_NAME..."
# 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 storm 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"
./zookeeper-cluster-linode.sh start "$ZK_CLUSTER_CONF_FILE" "$2"
start_nodes $CLUSTER_NAME
# Wait for sometime after nodes have booted up for SSH daemon to come up.
sleep 15
# If nodes were added when cluster was stopped, we need all nodes
# to know about all other nodes.
distribute_hostsfile $CLUSTER_NAME
# The cluster security configuration may have changed if nodes were added or
# admin updated the firewall rules while cluster was stopped.
# If so, security configuration should distributed.
local security_status=$(get_security_status)
if [ "$security_status" == "changed" ]; then
echo "Security configuration has changed. Applying new configuration"
# If any new supervisor nodes were added when cluster was stopped,
# they have to be integrated with reverse web proxy running on client
# node, to make their log files available via storm web UI.
configure_client_reverse_proxy $CLUSTER_NAME
distribute_cluster_security_configurations $CLUSTER_NAME
update_security_status "unchanged"
fi
# The cluster storm configuration may have been changed by admin.
# If so, it should be distributed.
local conf_status=$(get_conf_status)
if [ "$conf_status" == "changed" ]; then
echo "Storm configuration has changed. Applying new configuration"
distribute_storm_configuration $CLUSTER_NAME
update_conf_status "unchanged"
fi
fi
# For newly created or restarted, we need to start the storm services.
# storm.yaml need not be updated even if node IP addresses have changed, because it uses hostnames, not IP addresses.
start_storm $CLUSTER_NAME
update_cluster_status "running"
echo "Storm 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() {
if ! load_cluster_conf "$1"; then
return 1
fi
if ! load_api_env_configuration "$2"; then
return 1
fi
echo "Stopping cluster $CLUSTER_NAME..."
update_cluster_status "stopping"
# First stop storm services cleanly on all nodes, so that entire cluster can save whatever state it should.
stop_storm $CLUSTER_NAME
# Wait some time before stopping the nodes.
sleep 20
stop_nodes $CLUSTER_NAME
update_cluster_status "stopped"
echo "Storm cluster $CLUSTER_NAME is stopped"
}
# $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"
######################################################################################
printf "\n\nCreating nimbus node in datacenter $dc_id based on image $image_id...\n"
local nimbus_plan_id
get_plan_id $NIMBUS_NODE
nimbus_plan_id=$?
if [ $nimbus_plan_id -eq -1 ]; then
echo "Invalid plan $NIMBUS_NODE for Nimbus node. It should be one of 2GB|4GB|8GB|...|120GB.See https://www.linode.com/pricing for all plan names"
return 1
fi
local nimbus_linode_id
create_single_node $1 $nimbus_plan_id $dc_id $image_id $kernel_id nimbus_linode_id 'nimbus'
if [ $? -eq 1 ]; then
echo "Nimbus node creation failed. Aborting"
return 1
fi
# We need to know role(nimbus/supervisor/client) of each node later on to build storm.yaml
# start the correct services on each node.
write_status "nodes" "$nimbus_linode_id:nimbus"
#####################################################################################
create_supervisor_nodes $1 $SUPERVISOR_NODES $dc_id $image_id $kernel_id
if [ $? -eq 1 ]; then
echo "Error during Supervisor nodes creation. Aborting"
return 1
fi
#####################################################################################
printf "\n\nCreating client node in datacenter $dc_id based on image $image_id...\n"
local client_plan_id
get_plan_id $CLIENT_NODE
client_plan_id=$?
if [ $client_plan_id -eq -1 ]; then
echo "Invalid plan $CLIENT_NODE for client node. It should be one of 2GB|4GB|8GB|...|120GB.See https://www.linode.com/pricing for all plan names"
return 1
fi
local client_linode_id
create_single_node $1 $client_plan_id $dc_id $image_id $kernel_id client_linode_id 'client'
if [ $? -eq 1 ]; then
echo "Client node creation failed. Aborting"
return 1
fi
write_status "nodes" "$client_linode_id:client"
return 0
}
# $1 : Cluster name
# $2 : Plan for supervisor nodes (ex: "2GB:1 4GB:1 8GB:1")
# $3 : Datacenter ID
# $4 : Image ID
# $5 : Kernel ID
# $6 : (Optional) Role suffix for the supervisor nodes. If not empty, it'll be appended as "supervisor:$6"
create_supervisor_nodes() {
# Create the supervisor nodes.
printf "\n\nCreating $2 new supervisor nodes...\n"
local stfile="$(status_file)"
local role="supervisor"
if [ ! -z "$6" ]; then
role="$role:$6"
fi
local total_sup_count=1
for i in $2; do
local plan=$(echo $i|cut -d ':' -f1)
local count=$(echo $i|cut -d ':' -f2)
local plan_id
get_plan_id $plan
plan_id=$?
if [ $plan_id -eq -1 ]; then
echo "Invalid plan $plan for supervisor nodes. It should be one of 2GB|4GB|8GB|...|120GB.See https://www.linode.com/pricing for all plan names"
return 1
fi
echo "Creating $count supervisor linodes (plan $plan ID $plan_id)"
local sup_node_count=1
local supervisor_linode_id
while [ $sup_node_count -le $count ]; do
echo "Creating supervisor #$total_sup_count (plan $plan ID $plan_id)"
create_single_node $1 $plan_id $3 $4 $5 supervisor_linode_id 'sup'
if [ $? -eq 1 ]; then
echo "Supervisor node creation failed. Aborting"
return 1
fi
write_status "nodes" "$supervisor_linode_id:$role"
sup_node_count=$((sup_node_count+1))
total_sup_count=$((total_sup_count+1))
done
done
return 0
}
# $1 : The plan name ("2GB | 4GB | 8GB ....")
get_plan_id() {
local plan_id
case $1 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
;;
*)
plan_id=-1
;;
esac
return $plan_id
}
# $1 : Cluster name
# $2 : The Plan ID
# $3 : The datacenter ID (this has to be already validated by caller)
# $4 : The image ID
# $5 : The kernel ID
# $6 : Name of a variable that'll receive the created linode ID.
# $7 : Label prefix for linode
create_single_node() {
local stfile="$(status_file)"
local plan_id=$2
local dc_id=$3
local image_id=$4
local kernel_id=$5
# 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 linode. Error:$linerr"
return 1
fi
local __linode_id=$linout
eval $6="$__linode_id"
echo "Created linode $__linode_id"
linode_api linout linerr linret "update-node" $__linode_id "$7-$__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 Storm image for linode $__linode_id"
linode_api linout linerr linret "create-disk-from-image" $__linode_id $image_id \
"Storm" $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 Storm 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