-
Notifications
You must be signed in to change notification settings - Fork 339
/
startup-cdk.sh
executable file
·97 lines (87 loc) · 2.43 KB
/
startup-cdk.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
#!/bin/bash
set -x
IP=$(ip route show |grep -o src.* |cut -f2 -d" ")
# kubernetes sets routes differently -- so we will discover our IP differently
if [[ ${IP} == "" ]]; then
IP=$(hostname -i)
fi
SUBNET=$(echo ${IP} | cut -f1 -d.)
NETWORK=$(echo ${IP} | cut -f3 -d.)
case "${SUBNET}" in
10)
orchestrator=ecs
;;
192)
orchestrator=kubernetes
;;
*)
orchestrator=unknown
;;
esac
if [[ "${orchestrator}" == 'ecs' ]]; then
case "${NETWORK}" in
100)
zone=a
color=Crimson
;;
101)
zone=b
color=CornflowerBlue
;;
102)
zone=c
color=LightGreen
;;
*)
zone=unknown
color=Yellow
;;
esac
fi
if [[ "${orchestrator}" == 'kubernetes' ]]; then
if ((0<=${NETWORK} && ${NETWORK}<32))
then
zone=a
elif ((32<=${NETWORK} && ${NETWORK}<64))
then
zone=b
elif ((64<=${NETWORK} && ${NETWORK}<96))
then
zone=c
elif ((96<=${NETWORK} && ${NETWORK}<128))
then
zone=a
elif ((128<=${NETWORK} && ${NETWORK}<160))
then
zone=b
elif ((160<=${NETWORK}))
then
zone=c
else
zone=unknown
fi
fi
if [[ ${orchestrator} == 'unknown' ]]; then
zone=$(curl -m2 -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.availabilityZone' | grep -o .$)
fi
# Am I on ec2 instances?
if [[ ${zone} == "unknown" ]]; then
zone=$(curl -m2 -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.availabilityZone' | grep -o .$)
fi
# Still no luck? Perhaps we're running fargate!
if [[ -z ${zone} ]]; then
export AWS_DEFAULT_REGION=$REGION
ip_addr=$(curl -m2 -s ${ECS_CONTAINER_METADATA_URI} | jq -r '.Networks[].IPv4Addresses[]')
declare -a subnets=( $(aws ec2 describe-subnets | jq -r .Subnets[].CidrBlock| sed ':a;N;$!ba;s/\n/ /g') )
for sub in "${subnets[@]}"; do
ip_match=$(echo -e "from netaddr import IPNetwork, IPAddress\nif IPAddress('$ip_addr') in IPNetwork('$sub'):\n print('true')" | python3)
if [[ $ip_match == "true" ]];then
zone=$(aws ec2 describe-subnets | jq -r --arg sub "$sub" '.Subnets[] | select(.CidrBlock==$sub) | .AvailabilityZone' | grep -o .$)
fi
done
fi
export CODE_HASH="$(cat code_hash.txt)"
export IP
export AZ="${IP} in AZ-${zone}"
# exec container command
exec node server.js