This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagger.sh
36 lines (30 loc) · 1.82 KB
/
tagger.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
#!/bin/bash
taginstance () { # $1 node name, $2 node label, $3 node value
cat > patch.json <<EOF
[
{
"op": "add", "path": "/metadata/labels/$2", "value": "$3"
}
]
EOF
curl --request PATCH --data "$(cat patch.json)" -H "Content-Type:application/json-patch+json" http://k8s.mbst.tv:8080/api/v1/nodes/$1 >> /dev/null
}
LOCAL_HOSTNAME=`curl -L http://169.254.169.254/latest/meta-data/local-hostname`
if [[ `aws ec2 describe-instances --region eu-west-1 --instance-ids=$(curl -L http://169.254.169.254/latest/meta-data/instance-id) | jq '.Reservations[].Instances[].Tags[] | select ( .Key | contains("elastic-ip-id") )'` == *eipalloc* ]]; then
ELASTIC_IP_ID=`aws ec2 describe-instances --region eu-west-1 --instance-ids=$(curl -L http://169.254.169.254/latest/meta-data/instance-id) | jq '.Reservations[].Instances[].Tags[] | select ( .Key | contains("elastic-ip-id") ).Value' | sed 's/\"//g'`
ELASTIC_IP=`aws ec2 describe-addresses --region eu-west-1 --allocation-ids eipalloc-eecdcf86 | jq '.Addresses[].PublicIp' | sed 's/\"//g'`
CURRENT_IP=`curl -L http://169.254.169.254/latest/meta-data/public-ipv4`
if [[ "$ELASTIC_IP" != "$CURRENT_IP" ]]; then
#aws ec2 associate-address --region eu-west-1 --allocation-id=$ELASTIC_IP_ID --instance-id=$(curl -L http://169.254.169.254/latest/meta-data/instance-id)
taginstance $LOCAL_HOSTNAME elastic-ip-instance true
else
echo "Instance is already tagged" # this IF statement avoids a LOT of AWS eip remap costs
fi
else
taginstance $LOCAL_HOSTNAME elastic-ip-instance false
fi
if [[ `aws ec2 describe-instances --region eu-west-1 --instance-ids=$(curl -L http://169.254.169.254/latest/meta-data/instance-id) | jq '.Reservations[].Instances[].InstanceLifecycle'` == *spot* ]]; then
taginstance $LOCAL_HOSTNAME spot-instance true
else
taginstance $LOCAL_HOSTNAME spot-instance false
fi