forked from patrick-ogrady/avalanche-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-cli.sh
executable file
·393 lines (360 loc) · 12.5 KB
/
deploy-cli.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
#!/bin/bash
# adopted from here:https://medium.com/@Drew_Stokes/bash-argument-parsing-54f3b81a6a8f
set -e
function usage() {
echo -e 'Usage: deploy-cli.sh <cmd> <params>'
echo -e '\tdeploy <app>\t- deploy app: prometheus, avalanche, staking'
echo -e '\t[--staking-key|-k <key-dir>] deploy all\t- deploy prometheus, avalanche'
echo -e '\tupgrade <app>\t- upgrade app: avalanche'
echo -e '\tdeploy staking <staking-key-dir>\t- deploy staking key'
echo -e '\tdeploy dashboards <dashboard-dir>\t- deploy or update dashboards'
echo -e '\tcreate-alerts <kind> <channel-id> <bottoken>\t- create grafana alerts, kind: telegram'
echo -e '\tdelete <app>\t- delete app: prometheus, avalanche, staking'
echo -e '\treset-pwd <app> <pwd>\t- reset app pwd. app: grafana'
echo -e '\tcreate-cluster <cluster-name> [<gcloud-project-name>] [<zone>]\t- create k8s cluster'
}
# install password for grafana. Will be changed at the first login
grafana_pwd=prom-operator
ava_staking_secret=ava-staking
ava_namespace=avalanche
prom_namespace=prometheus
staking_key=""
function check_namespace() {
set +e
kubectl get namespace "$1" >/dev/null
local err=$?
set -e
echo $err
}
function deploy_prometheus() {
echo 'Deploying prometheus stack'
err=$(check_namespace "$prom_namespace")
if [ $err != 0 ]; then
echo "Create namespace $prom_namespace"
kubectl create namespace "$prom_namespace"
fi
# install from helm repo
# helm install -n "$prom_namespace" prometheus-stack prometheus-community/kube-prometheus-stack
# install from this repo
helm install -n "$prom_namespace" prometheus-stack kube-prometheus-stack-10.1.0 -f k8s-prom-values.yaml
wait_for_pod grafana "$prom_namespace"
pod=$(kubectl get pod -n "$prom_namespace" --selector="app.kubernetes.io/instance=prometheus-stack,app.kubernetes.io/name=grafana" --output jsonpath='{.items[0].metadata.name}')
kubectl exec -n "$prom_namespace" -it "$pod" -c grafana -- grafana-cli admin reset-admin-password "$grafana_pwd"
echo "Prometheus node deployed. cli_pod/password admin/$grafana_pwd to login"
}
function deploy_avalanche() {
echo 'Deploying avalanche node'
if [ "$staking_key" != "" ]; then
echo "Use staking key from $staking_key"
deploy_staking_key "$staking_key"
fi
err=$(check_namespace "$ava_namespace")
if [ $err != 0 ]; then
echo "Create namespace $ava_namespace"
kubectl create namespace "$ava_namespace"
fi
# check staking key
set +e
kubectl get secret "$ava_staking_secret" -n "$ava_namespace" 1>/dev/null
err=$?
set -e
if [ "$err" == 0 ]; then
echo "Found staking key $ava_staking_secret. Will use it for the node"
opts="--set-string stakingKey=$ava_staking_secret"
else
echo -e "Staking key $ava_staking_secret is not found. Will create a new key.\nto extract the key use 'avax-cli.sh staking get <dest-file.tgz>'"
fi
helm install -n "$ava_namespace" $opts ava-node kube-avax -f kube-avax-values.yaml
# install utilities into cli container
wait_for_pod cli "$ava_namespace"
cli_pod=$(kubectl get pods -n "$ava_namespace" | grep cli | awk '{print $1}')
kubectl exec -it "$cli_pod" -n "$ava_namespace" -- apt-get update
kubectl exec -it "$cli_pod" -n "$ava_namespace" -- apt-get install -y jq
echo 'Avalanche node deployed'
}
function deploy_staking_key() {
err=$(check_namespace "$ava_namespace")
if [ $err != 0 ]; then
kubectl create namespace "$ava_namespace"
fi
echo "Create $ava_staking_secret staking secret"
kubectl create secret generic "$ava_staking_secret" --from-file="$1"/staker.key \
--from-file="$1"/staker.crt -n "$ava_namespace"
}
function wait_for_pod() {
svc=
for i in {1..20}
do
n=$(kubectl get pod -n "$2" |grep "$1" | grep Running | awk '{print $1}')
if [ "$n" == "" ]; then
sleep 5
else
# wait untill pod starts
sleep 30
break
fi
done
if [ "$n" == "" ]; then
echo "Wait for $1 timeout"
exit 1
fi
}
function wait_for_svc() {
svc=
for i in {1..20}
do
n=$(kubectl get service -n "$2" |grep "$1" | awk '{print $1}')
if [ "$n" == "" ]; then
sleep 5
else
break
fi
done
if [ "$n" == "" ]; then
echo "Wait for $1 timeout"
exit 1
fi
}
function deploy_avalanche_dashboards() {
echo 'Deploying avalanche dashboards'
wait_for_svc grafana "$prom_namespace"
grafana_ip=$(kubectl get service -n "$prom_namespace" |grep grafana | awk '{print $3}')
cli_pod=$(kubectl get pods -n "$ava_namespace" | grep cli | awk '{print $1}')
tar cfz /tmp/dashboards.tgz -C $1 .
tmp_dir=$(kubectl exec "$cli_pod" -n "$ava_namespace" -- mktemp -d -t ci-XXXXXXXXXX)
kubectl cp /tmp/dashboards.tgz "$cli_pod:$tmp_dir" -n "$ava_namespace"
set +e
kubectl exec -it "$cli_pod" -n "$ava_namespace" -- mkdir "$tmp_dir"/dashboards
set -e
kubectl exec -it "$cli_pod" -n "$ava_namespace" -- tar xvf "$tmp_dir"/dashboards.tgz -C "$tmp_dir"/dashboards
kubectl cp .create-dashboards.sh "$cli_pod:$tmp_dir" -n "$ava_namespace"
kubectl exec -it "$cli_pod" -n "$ava_namespace" -- "$tmp_dir"/.create-dashboards.sh "$tmp_dir/dashboards" "$grafana_ip" "$2"
}
function delete_app() {
echo "Will delete $1"
helm delete $1 -n "$2"
echo "$1 is deleted"
}
function delete_staking_key() {
echo "Will delete $ava_staking_secret"
set +e
kubectl get secret "$ava_staking_secret" -n "$ava_namespace" 2&>/dev/null
err=$?
set -e
if [ "$err" == 0 ]; then
kubectl delete secret "$ava_staking_secret" -n "$ava_namespace"
echo "$ava_staking_secret is deleted"
else
echo "Staking key $ava_staking_secret is not found"
fi
}
function upgrade_avalanche() {
echo 'Upgrading avalanche node'
# check staking key
set +e
kubectl get secret "$ava_staking_secret" -n "$ava_namespace" 1>/dev/null
err=$?
set -e
if [ "$err" == 0 ]; then
echo "Found staking key $ava_staking_secret. Will use it for the node"
opts="--set-string stakingKey=$ava_staking_secret"
fi
helm upgrade -n "$ava_namespace" $opts ava-node kube-avax -f kube-avax-values.yaml
echo 'Avalanche node is upgraded'
}
function create_alerts() {
provider=$1
if [ "$provider" != "telegram" ]; then
echo "only telegram provider is supported"
exit 1
fi
channel=$2
if [ "$channel" == "" ]; then
echo "missing channel id"
exit 1
fi
bottoken=$3
if [ "$bottoken" == "" ]; then
echo "missing bot token"
exit 1
fi
pod=$(kubectl get pods -n "$ava_namespace" | grep cli | awk '{print $1}')
ip=$(kubectl get service -n "$prom_namespace" |grep prometheus-stack-grafana | awk '{print $3}')
cmd="kubectl exec -it $pod -n $ava_namespace -- curl -X POST --data '{
\"name\": \"telegram\",
\"type\": \"telegram\",
\"settings\": {
\"chatid\": \"-$channel\",
\"bottoken\": \"$bottoken\"
}
}' -H 'content-type:application/json;' http://admin:$grafana_pwd@$ip/api/alert-notifications"
eval "$cmd"
}
function create_cluster() {
cluster=$1
project=$2
if [ "$project" == "" ]; then
project=$(gcloud config get-value project)
fi
zone="$3"
if [ "$zone" == "" ]; then
zone="us-central1-c"
fi
gcloud beta container --project "$project" \
clusters create "$cluster" --zone "$zone" \
--no-enable-basic-auth --cluster-version "1.16.15-gke.4300" \
--machine-type "e2-standard-2" --image-type "COS" --disk-type "pd-standard" \
--disk-size "100" --metadata disable-legacy-endpoints=true \
--scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
--num-nodes "1" --enable-stackdriver-kubernetes \
--enable-ip-alias \
--network "projects/$project/global/networks/default" \
--subnetwork "projects/$project/regions/us-central1/subnetworks/default" \
--default-max-pods-per-node "110" --no-enable-master-authorized-networks \
--addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade \
--enable-autorepair --max-surge-upgrade 1 --max-unavailable-upgrade 0
gcloud container clusters \
get-credentials "$cluster" --zone "$zone" --project "$project"
}
if [ $# == 0 ]; then
usage
exit 1
fi
PARAMS=""
while (( "$#" )); do
case "$1" in
-h|--help)
usage
exit 0
;;
-k|--staking-key)
shift
staking_key=$1
if [ "$staking_key" == "" ];then
echo "Error: Missing staking key" >&2
exit 1
fi
shift
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
case "$1" in
deploy)
if [ -n "$2" ]; then
app=$2
if [ "$app" == "prometheus" ]; then
deploy_prometheus
elif [ "$app" == "avalanche" ]; then
deploy_avalanche
elif [ "$app" == "staking" ]; then
deploy_staking_key $3
elif [ "$app" == "dashboards" ]; then
deploy_avalanche_dashboards "$3" "$grafana_pwd"
elif [ "$app" == "all" ]; then
deploy_prometheus
deploy_avalanche
deploy_avalanche_dashboards "node-monitoring/dashboards" "$grafana_pwd"
else
echo "Error: Unsupported '$app'. Supported: 'prometheus', 'avalanche' or 'all'" >&2
exit 1
fi
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
delete)
set +e # ignore failures
if [ -n "$2" ]; then
app=$2
if [ "$app" == "prometheus" ]; then
delete_app prometheus-stack "$prom_namespace"
kubectl delete namespace "$prom_namespace"
elif [ "$app" == "avalanche" ]; then
delete_app ava-node "$ava_namespace"
kubectl delete namespace "$ava_namespace"
elif [ "$app" == "staking" ]; then
delete_staking_key
elif [ "$app" == "all" ]; then
delete_app prometheus-stack "$prom_namespace"
delete_app ava-node "$ava_namespace"
delete_staking_key
err=$(check_namespace "$prom_namespace")
if [ $err == 0 ]; then
echo "Delete namespace $prom_namespace"
kubectl delete namespace "$prom_namespace"
fi
err=$(check_namespace "$ava_namespace")
if [ $err == 0 ]; then
echo "Delete namespace $ava_namespace"
kubectl delete namespace "$ava_namespace"
fi
else
echo "Error: Unsupported '$app'. Supported: 'prometheus' or 'avalanche'" >&2
exit 1
fi
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
upgrade)
if [ -n "$2" ]; then
app=$2
if [ "$app" == "avalanche" ]; then
upgrade_avalanche
else
echo "Error: Unsupported '$app'. Supported: 'avalanche'" >&2
exit 1
fi
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
reset-pwd)
if [ -n "$2" ]; then
app=$2
if [ "$app" == "grafana" ]; then
if [ "$3" == "" ]; then
echo "Missing pwd"
exit 1
fi
wait_for_pod grafana "$prom_namespace"
pod=$(kubectl get pod -n "$prom_namespace" --selector="app.kubernetes.io/instance=prometheus-stack,app.kubernetes.io/name=grafana" --output jsonpath='{.items[0].metadata.name}')
kubectl exec -n "$prom_namespace" -it "$pod" -c grafana -- grafana-cli admin reset-admin-password "$3"
else
echo "Error: Unsupported '$app'. Supported: 'grafana'" >&2
exit 1
fi
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
create-alerts)
create_alerts "$2" "$3" "$4"
;;
create-cluster)
if [ -n "$2" ]; then
create_cluster "$2" "$3" "$4"
else
echo "Error: Cluster name is missing" >&2
exit 1
fi
;;
*)
echo "Error: Unsupported command $1" >&2
exit 1
;;
esac