-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-leader.py
executable file
·93 lines (75 loc) · 2.2 KB
/
aws-leader.py
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
#!/usr/bin/python
from botocore.exceptions import ClientError, BotoCoreError
from requests import RequestException
import boto3
import logging
import requests
import sys
# https://hackernoon.com/do-as-i-say-not-as-i-do-get-your-ec2-instance-name-without-breaking-your-infrastructure-1da4a0963af0
def get_current_name_id_region():
try:
r = requests.get("http://169.254.169.254/latest/dynamic/instance-identity/document")
r.raise_for_status()
except RequestException as e:
logging.exception(e)
return None
try:
response_json = r.json()
except ValueError as e:
logging.exception(e)
return None
region = response_json.get('region')
instance_id = response_json.get('instanceId')
if not (region and instance_id):
logging.error('Invalid region: {} or instance_id: {}'.format(region, instance_id))
return None
try:
ec2 = boto3.resource('ec2', region_name=region)
instance = ec2.Instance(instance_id)
tags = instance.tags
except (ValueError, ClientError, BotoCoreError) as e:
logging.exception(e)
return None
tags = tags or []
names = [tag.get('Value') for tag in tags if tag.get('Key') == 'Name']
name = names[0] if names else None
if name is None:
return None
return {'name': name, 'id': instance_id, 'region': region}
def get_leader_id(name, region):
filters = [
{
'Name' : 'instance-state-name',
'Values' : ['running']
},
{
'Name': 'tag:Name',
'Values': [name]
}
]
try:
ec2 = boto3.client('ec2', region_name=region)
response = ec2.describe_instances(Filters=filters)
except (ValueError, ClientError, BotoCoreError) as e:
logging.exception(e)
return None
members = []
for reservation in response["Reservations"]:
for instance in reservation["Instances"]:
members.append(instance['InstanceId'])
members = sorted(members)
leader_id = members[0]
return leader_id
def main():
current = get_current_name_id_region()
if current is None:
sys.exit(1)
current_id = current['id']
leader_id = get_leader_id(current['name'], current['region'])
if leader_id is None:
sys.exit(1)
if current_id == leader_id:
sys.exit(0)
else:
sys.exit(1)
main()