-
-
Notifications
You must be signed in to change notification settings - Fork 556
/
cluster_manager.sh
executable file
·171 lines (158 loc) · 5.55 KB
/
cluster_manager.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
#!/bin/bash
# Function to create a Kafka cluster
create_cluster() {
local response
local create_cluster_url="https://api.instaclustr.com/cluster-management/v2/resources/applications/kafka/clusters/v2"
response=$(curl --fail --show-error -s -w "%{http_code}" -X POST -u "${INSTACLUSTR_USER_NAME}:${INSTACLUSTR_API_KEY}" "${create_cluster_url}" \
-H 'Content-Type: application/json' \
-d '{
"allowDeleteTopics": true,
"autoCreateTopics": true,
"clientToClusterEncryption": false,
"dataCentres": [
{
"cloudProvider": "AWS_VPC",
"name": "AWS_VPC_US_EAST_1",
"network": "10.0.0.0/16",
"nodeSize": "KFK-DEV-t4g.small-5",
"numberOfNodes": 3,
"region": "US_EAST_1"
}
],
"dedicatedZookeeper": [],
"defaultNumberOfPartitions": 3,
"defaultReplicationFactor": 3,
"kafkaVersion": "3.6.1",
"name": "DemoKafkaCluster",
"pciComplianceMode": false,
"privateNetworkCluster": false,
"slaTier": "NON_PRODUCTION"
}')
echo "${response}"
}
# Function to wait for cluster to be fully operational
wait_for_cluster() {
local cluster_id=$1
local status
local response
while true; do
local url="https://api.instaclustr.com/cluster-management/v2/resources/applications/kafka/clusters/v2/${cluster_id}"
response=$(curl --fail --show-error -s -X GET -H "Content-Type: application/json" -u "${INSTACLUSTR_USER_NAME}:${INSTACLUSTR_API_KEY}" "${url}")
status=$(echo "${response}" | jq -r '.status')
case "${status}" in
RUNNING)
echo "Cluster is now running. Waiting for 60 seconds to be fully available"
sleep 60
break
;;
ERROR)
echo "Cluster creation failed with status: ${status}"
echo "Error details: $(echo "${response}" | jq -r '.errorMessage')"
exit 1
;;
DEFERRED)
echo "Cluster creation has been deferred. Exiting..."
exit 1
;;
*)
echo "Current status: ${status}. Waiting for cluster to be in RUNNING status..."
sleep 60
;;
esac
done
}
# Function to retrieve cluster details
get_cluster_details() {
local cluster_id=$1
local response
response=$(curl --fail --show-error -s -X GET -H "Content-Type: application/json" -u "${INSTACLUSTR_USER_NAME}:${INSTACLUSTR_API_KEY}" \
"https://api.instaclustr.com/cluster-management/v2/resources/applications/kafka/clusters/v2/${cluster_id}")
echo "${response}"
}
# Function to add a firewall rule
add_firewall_rule() {
local cluster_id=$1
local response
response=$(curl -s --fail --show-error -X POST -H "Content-Type: application/json" -u "${INSTACLUSTR_USER_NAME}:${INSTACLUSTR_API_KEY}" \
"https://api.instaclustr.com/cluster-management/v2/resources/network-firewall-rules/v2" \
-d '{
"clusterId": "'"${cluster_id}"'",
"network": "'"0.0.0.0/0"'",
"type":"KAFKA"
}')
#echo "${response}"
local status=$(echo "${response}" | jq -r '.status')
if [[ "${status}" == "GENESIS" ]]; then
echo "Firewall rule applied successfully"
else
echo "Firewall rule application failed"
fi
}
# Function to create a user for the Kafka cluster
create_user() {
local cluster_id=$1
local response
response=$(curl --fail --show-error -s -X POST -u "${INSTACLUSTR_USER_NAME}:${INSTACLUSTR_API_KEY}" \
"https://api.instaclustr.com/cluster-management/v2/resources/applications/kafka/users/v2" \
-H 'Content-Type: application/json' \
-d '{
"clusterId": "'"${cluster_id}"'",
"initialPermissions": "standard",
"options": {
"overrideExistingUser": false,
"saslScramMechanism": "SCRAM-SHA-256"
},
"password": "myPassword1.",
"username": "myKafkaUser"
}')
# echo "${response}"
if [[ $(echo "${response}" | jq -r '.id') != "null" && $(echo "${response}" | jq -r '.username') == "myKafkaUser" ]]; then
echo "User created successfully"
else
echo "User creation failed"
fi
}
# Function to delete the Kafka cluster
delete_cluster() {
local cluster_id=$1
local response
echo "Deleting Kafka cluster"
response=$(curl --fail --show-error -s -X DELETE -u "${INSTACLUSTR_USER_NAME}:${INSTACLUSTR_API_KEY}" \
"https://api.instaclustr.com/cluster-management/v2/resources/applications/kafka/clusters/v2/${cluster_id}")
echo "${response}"
}
# Main function
main() {
if [[ "$1" == "cleanup" ]]; then
local cluster_id
cluster_id=$(cat cluster_id.txt)
delete_cluster "${cluster_id}"
rm cluster_id.txt
unset INSTACLUSTR_USER_NAME
unset INSTACLUSTR_API_KEY
else
local response=$(create_cluster)
local http_code=$(echo "${response}" | tail -c 4)
local response_body=$(echo "${response}" | sed '$ s/...$//')
if [[ $http_code -eq 401 ]]; then
echo "Unauthorized: Please check your username or API key"
exit 1
fi
local cluster_id=$(echo "${response_body}" | jq -r '.id')
if [ -z "$cluster_id" ]; then
echo "Error: Cluster creation failed"
exit 1
fi
echo "Creating a trial cluster. Cluster setup usually take around 5-10 minutes"
echo "Cluster ID: ${cluster_id}"
echo "${cluster_id}" > cluster_id.txt
wait_for_cluster "${cluster_id}"
create_user "${cluster_id}"
add_firewall_rule "${cluster_id}"
local cluster_details=$(get_cluster_details "${cluster_id}")
local bootstrapserver=$(echo "${cluster_details}" | jq -r '.dataCentres[].nodes[-2].publicAddress')
echo "BOOTSTRAP_SERVER:$bootstrapserver"
fi
}
# Run the main script logic
main "$@"