-
Notifications
You must be signed in to change notification settings - Fork 12
/
fix-instance-id.sh
executable file
Β·392 lines (352 loc) Β· 12 KB
/
fix-instance-id.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
# -*- coding: UTF-8 -*-
# aws cli v2 - https://github.com/aws/aws-cli/issues/4992
export AWS_PAGER=""
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[38;5;214m'
NC='\033[0m' # No Color
# env vars
DRYRUN=${DRYRUN:-}
KUBECONFIG=${KUBECONFIG:-}
BASE_DOMAIN=${BASE_DOMAIN:-}
CLUSTER_NAME=${CLUSTER_NAME:-}
readonly RUN_DIR=$(pwd)
# prog vars
region=
instance_id=
find_region() {
if [ ! -z "$AWS_DEFAULT_REGION" ]; then
region="$AWS_DEFAULT_REGION"
else
region=$(aws configure get region)
fi
if [ -z "$region" ]; then
echo -e "π±${RED}Failed - could not find aws region ?${NC}"
exit 1
else
echo "π΄ Region set to $region"
fi
}
find_instance_id() {
local tag_value="$1"
instance_id=$(aws ec2 describe-instances \
--region=${region} \
--query "Reservations[].Instances[].InstanceId" \
--filters "Name=tag-value,Values=$tag_value" "Name=instance-state-name,Values=running" \
--output text)
if [ -z "$instance_id" ]; then
echo -e "π±${RED}Failed - could not find instance id associated with tag: $tag_value ?${NC}"
exit 1
else
echo "π΄ InstanceId set to $instance_id"
fi
}
restart_instance() {
set -o pipefail
aws ec2 stop-instances \
${DRYRUN:---dry-run} \
--region=${region} \
--instance-ids=$instance_id 2>&1 | tee /tmp/aws-error-file
if [ "$?" != 0 ]; then
if egrep -q "DryRunOperation" /tmp/aws-error-file; then
echo -e "${GREEN}Ignoring - restart_instance stop instance - dry run set${NC}"
else
echo -e "π±${RED}Failed - could not stop $instance_id ?${NC}"
exit 1
fi
else
echo -e "${GREEN} -> restart_instance stopping [ $instance_id ] OK${NC}"
fi
echo -e "${GREEN} -> wait instance-stopped ... [ $instance_id ]${NC}"
aws ec2 wait instance-stopped \
${DRYRUN:---dry-run} \
--region=${region} \
--instance-ids $instance_id
if [ ! -z "$DRYRUN" ]; then
sleep 120 # fix me spot restart is not elegant
echo -e "${GREEN} -> instance stopped [ $instance_id ] OK${NC}"
fi
aws ec2 start-instances \
${DRYRUN:---dry-run} \
--region=${region} \
--instance-ids=$instance_id 2>&1 | tee /tmp/aws-error-file
if [ "$?" != 0 ]; then
if egrep -q "DryRunOperation" /tmp/aws-error-file; then
echo -e "${GREEN}Ignoring - restart_instance start instance - dry run set${NC}"
else
until [ "$?" == 0 ]
do
echo -e "${GREEN} -> trying to start-instances ... [ $instance_id ]${NC}"
((i=i+1))
if [ $i -gt 5 ]; then
echo -e "π±${RED}Failed - could not start $instance_id ?${NC}"
exit 1
fi
sleep 10
aws ec2 start-instances \
${DRYRUN:---dry-run} \
--region=${region} \
--instance-ids=$instance_id 2>&1 | tee /tmp/aws-error-file
done
echo -e "${GREEN} -> restart_instance starting [ $instance_id ] OK${NC}"
fi
else
echo -e "${GREEN} -> restart_instance starting [ $instance_id ] OK${NC}"
fi
set +o pipefail
}
find_vpc_id() {
local tag_value="$1"
vpc_id=$(aws ec2 describe-vpcs --region=${region} \
--query "Vpcs[].VpcId" \
--filters "Name=tag-value,Values=$tag_value" \
--output text)
if [ -z "$vpc_id" ]; then
echo -e "π±${RED}Failed - could not find vpc id associated with tag: $tag_value ?${NC}"
exit 1
else
echo "π΄ VpcId set to $vpc_id"
fi
}
find_router_lb() {
query='LoadBalancerDescriptions[?VPCId==`'${vpc_id}'`]|[].LoadBalancerName'
router_load_balancer=$(aws elb describe-load-balancers \
--region=${region} \
--query $query \
--output text)
if [ -z "$router_load_balancer" ]; then
echo -e "π±${RED}Warning - could not find router load balancer ?${NC}"
exit 1
else
echo "π΄ RouterLoadBalancer set to $router_load_balancer"
fi
}
associate_router_instance() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - associate_router_instance - dry run set${NC}"
return
fi
if [ ! -z "$router_load_balancer" ]; then
aws elb register-instances-with-load-balancer \
--load-balancer-name $router_load_balancer \
--instances $instance_id \
--region=${region}
if [ "$?" != 0 ]; then
echo -e "π±${RED}Failed - could not associate router lb $router_load_balancer with instance $instance_id ?${NC}"
exit 1
else
echo -e "${GREEN} -> associate_router_instance [ $router_load_balancer, $instance_id ] OK${NC}"
fi
fi
}
find_node_providerid() {
node_provider_id=$(${RUN_DIR}/oc get nodes -o jsonpath='{.items[0].spec.providerID}')
if [ -z "$node_provider_id" ]; then
echo -e "π±${RED}Failed - could not find openshift node providerid ?${NC}"
exit 1
else
echo "π΄ NodeProviderId set to $node_provider_id"
fi
}
update_providerid_on_node() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - update_providerid_on_node - dry run set${NC}"
return
fi
${RUN_DIR}/oc -n default debug -T $(${RUN_DIR}/oc get node -o name) -- chroot /host bash -c "sed -i \"s|aws:///.*|aws:///$region/$instance_id\\\"|\" /etc/systemd/system/kubelet.service.d/20-aws-providerid.conf"
}
delete_node() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - delete_node - dry run set${NC}"
return
fi
${RUN_DIR}/oc delete $(${RUN_DIR}/oc get node -o name)
}
patch_machine_load_balancers() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - patch_machine_load_balancers - dry run set${NC}"
return
fi
${RUN_DIR}/oc -n openshift-machine-api patch $(${RUN_DIR}/oc -n openshift-machine-api get machine -l machine.openshift.io/cluster-api-machine-role=master -o name) -p '{"spec":{"providerSpec":{"value": {"loadBalancers" : []}}}}' --type=merge
if [ "$?" != 0 ]; then
echo -e "π±${RED}Failed to patch_machine_load_balancers ?${NC}"
exit 1
fi
echo -e "${GREEN} -> patch_machine_load_balancers OK${NC}"
}
patch_network_load_balancer() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - patch_network_load_balancer - dry run set${NC}"
return
fi
${RUN_DIR}/oc -n openshift-ingress-operator patch ingresscontroller default --type=merge --patch '
apiVersion: operator.openshift.io/v1
kind: IngressController
metadata:
name: default
namespace: openshift-ingress-operator
spec:
endpointPublishingStrategy:
loadBalancer:
scope: External
providerParameters:
type: AWS
aws:
type: NLB
type: LoadBalancerService'
if [ "$?" != 0 ]; then
echo -e "π±${RED}Failed to patch_network_load_balancer ?${NC}"
exit 1
fi
echo -e "${GREEN} -> patch_network_load_balancer OK${NC}"
}
delete_classic_load_balancers() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - delete_classic_load_balancers - dry run set${NC}"
return
fi
query='LoadBalancerDescriptions[?VPCId==`'${vpc_id}'`]|[].LoadBalancerName'
load_balancers_name=$(aws elb describe-load-balancers \
--region=${region} \
--query $query \
--output text)
if [ -z "$load_balancers_name" ]; then
echo -e "π${ORANGE}Warning - could not find load balancers by name - continuing, they may have been deleted already ?${NC}"
return
else
echo "π΄ LoadBalancersName set to $load_balancers_name"
fi
if [ ! -z "$load_balancers_name" ]; then
for x in $load_balancers_name; do
aws elb delete-load-balancer \
--region=${region} \
--load-balancer-name $x
if [ "$?" != 0 ]; then
echo -e "π±${RED}Failed - could not delete load balancer $x ?${NC}"
exit 1
else
echo -e "${GREEN} -> delete_classic_load_balancers [ $x ] OK${NC}"
fi
done
fi
}
wait_for_openshift_api() {
local i=0
HOST=https://api.${CLUSTER_NAME}.${BASE_DOMAIN}:6443/healthz
until [ $(curl -k -s -o /dev/null -w %{http_code} ${HOST}) = "200" ]
do
echo -e "${GREEN}Waiting for 200 response from openshift api ${HOST}.${NC}"
sleep 5
((i=i+1))
if [ $i -gt 100 ]; then
echo -e "π±${RED}Failed - OpenShift api ${HOST} never ready?.${NC}"
exit 1
fi
done
}
approve_all_certificates() {
if [ -z "$DRYRUN" ]; then
echo -e "${GREEN}Ignoring - approve_all_certificates - dry run set${NC}"
return
fi
local i=0
${RUN_DIR}/oc get csr
until [ "$?" == 0 ]
do
echo -e "${GREEN}Waiting for 0 rc from oc commands.${NC}"
((i=i+1))
if [ $i -gt 100 ]; then
echo -e "π±${RED}Failed - oc never ready?.${NC}"
exit 1
fi
sleep 5
${RUN_DIR}/oc get csr
done
${RUN_DIR}/oc get csr -o name | xargs ${RUN_DIR}/oc adm certificate approve
if [ "$?" != 0 ]; then
echo -e "π±${RED}Failed to approve ?${NC}"
exit 1
fi
echo -e "${GREEN} -> approve_csr OK${NC}"
}
# do it all
all() {
echo "π΄ BASE_DOMAIN set to $BASE_DOMAIN"
echo "π΄ CLUSTER_NAME set to $CLUSTER_NAME"
echo "π΄ KUBECONFIG set to $KUBECONFIG"
find_region
find_instance_id "$CLUSTER_NAME-*-master-0"
find_vpc_id "$CLUSTER_NAME-*-vpc"
find_router_lb
associate_router_instance
wait_for_openshift_api
find_node_providerid
if [[ "$node_provider_id" != *"$instance_id"* ]]; then
update_providerid_on_node
delete_node
restart_instance
wait_for_openshift_api
else
echo -e "${GREEN} -> $instance_id already set on node $node_provider_id, so not redoing it. OK${NC}"
fi
patch_machine_load_balancers
patch_network_load_balancer
delete_classic_load_balancers
approve_all_certificates
}
usage() {
cat <<EOF 2>&1
usage: $0 [ -d ]
Fix SNO Instance Id's
-d do it ! no dry run - else we print out whats going to happen and any non desructive lookups
Optional arguments if not set in environment:
-b BASE_DOMAIN - openshift base domain (or export BASE_DOMAIN env var)
-c CLUSTER_NAME - openshift cluster name (or export CLUSTER_NAME env var)
-k KUBECONFIG - full path to the kubeconfig file
This script is rerunnable.
Environment Variables:
Export these in your environment.
AWS_PROFILE use a pre-configured aws profile (~/.aws/config and ~/.aws/credentials)
OR export these in your environment:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
Optionally if not set on command line:
BASE_DOMAIN
CLUSTER_NAME
KUBECONFIG
EOF
exit 1
}
while getopts db:c:k: opts; do
case $opts in
b)
BASE_DOMAIN=$OPTARG
;;
c)
CLUSTER_NAME=$OPTARG
;;
d)
DRYRUN="--no-dry-run"
;;
k)
KUBECONFIG=$OPTARG
;;
*)
usage
;;
esac
done
shift `expr $OPTIND - 1`
# Check for EnvVars
[ ! -z "$AWS_PROFILE" ] && echo "π΄ Using AWS_PROFILE: $AWS_PROFILE"
[ -z "$BASE_DOMAIN" ] && echo "π± Error: must supply BASE_DOMAIN in env or cli" && exit 1
[ -z "$CLUSTER_NAME" ] && echo "π± Error: must supply CLUSTER_NAME in env or cli" && exit 1
[ -z "$AWS_PROFILE" ] && [ -z "$AWS_ACCESS_KEY_ID" ] && echo "π± Error: AWS_ACCESS_KEY_ID not set in env" && exit 1
[ -z "$AWS_PROFILE" ] && [ -z "$AWS_SECRET_ACCESS_KEY" ] && echo "π± Error: AWS_SECRET_ACCESS_KEY not set in env" && exit 1
[ -z "$AWS_PROFILE" ] && [ -z "$AWS_DEFAULT_REGION" ] && echo "π± Error: AWS_DEFAULT_REGION not set in env" && exit 1
[ -z "$KUBECONFIG" ] && [ -z "KUBECONFIG" ] && echo "π± Error: KUBECONFIG not set in env or cli" && exit 1
all
echo -e "\nπ»${GREEN}AWS SNO Reconfigured OK.${NC}π»\n"
exit 0