-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2bot.py
178 lines (143 loc) · 5.41 KB
/
ec2bot.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
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
import boto3
import botocore
from slackclient import SlackClient
from collections import namedtuple
import configparser
import json
import threading
import time
import queue
import sys
import re
if len(sys.argv) < 2:
print("ERROR: missing path to slackers.cfg")
sys.exit(1)
CONFIGS = configparser.ConfigParser()
CONFIGS.read(sys.argv[1])
CONFIG = CONFIGS['ec2bot']
REQUIRED_TAGS = set(CONFIG['REQUIRED_TAGS'].split(','))
name_regex = CONFIG['IGNORED_INSTANCE_NAME_REGEX']
if name_regex:
IGNORED_INSTANCE_NAME_REGEX = re.compile(name_regex)
else:
IGNORED_INSTANCE_NAME_REGEX = None
IGNORED_STATES = CONFIG['IGNORED_STATES'].split(',')
InstanceState = namedtuple(
"InstanceState", ["instance_id", "state", "missing_tags", "found_tags"])
SHUTDOWN = False
def get_instance_state():
ec2 = boto3.client('ec2')
instances = ec2.describe_instances()
missing = []
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
instance_id = instance['InstanceId']
state = instance['State']['Name']
tags = instance.get('Tags', [])
tag_map = {}
for tag in tags:
tag_map[tag['Key']] = tag['Value']
missing_tags = REQUIRED_TAGS - set(tag_map)
missing.append(InstanceState(instance_id, state, missing_tags,
tag_map))
return missing
def parse_event(event):
ec2 = boto3.client('ec2')
instance_id = event['detail']['instance-id']
event_state = event['detail']['state']
if event_state in IGNORED_STATES:
return None
try:
desc = ec2.describe_instances(InstanceIds=[instance_id])
except botocore.exceptions.ClientError:
desc = {'Reservations': []}
#print(desc)
# Only non-terminated instances will have this
if desc['Reservations']:
instance = desc['Reservations'][0]['Instances'][0]
tags = instance.get('Tags', [])
tag_map = {}
for tag in tags:
tag_map[tag['Key']] = tag['Value']
missing_tags = REQUIRED_TAGS - set(tag_map)
if 'Name' in tag_map and IGNORED_INSTANCE_NAME_REGEX:
matches = IGNORED_INSTANCE_NAME_REGEX.search(tag_map['Name'])
if matches:
return None
msg = 'ec2 event: {}, id: {}, public_ip: {}, private_ip: {}, tags: {}' \
.format(
event_state,
instance_id,
instance.get('PublicIpAddress'),
instance.get('PrivateIpAddress'),
', '.join(['{}={}'.format(k,v) for k,v in tag_map.items()]))
if missing_tags:
msg += ' *missing tags: {}*'.format(', '.join(sorted(missing_tags)))
return msg
return 'ec2 event: {}, id: {}'.format(
event['detail']['state'],
instance_id)
def get_ec2_events(msg_queue):
sqs = boto3.resource('sqs')
sqs_queue = sqs.get_queue_by_name(QueueName=CONFIG['EC2_EVENT_QUEUE_NAME'])
while not SHUTDOWN:
print("event loop iteration")
processed_messages = []
for i, message in enumerate(
sqs_queue.receive_messages(WaitTimeSeconds=10)):
processed_messages.append(
{'Id': '{}'.format(i),
'ReceiptHandle': message.receipt_handle})
body = json.loads(message.body)
print("Incoming ec2 event: ", body)
msg_queue.put(body)
if processed_messages:
sqs_queue.delete_messages(Entries=processed_messages)
def main(msg_queue, channel):
LOOP_TIMEOUT=.5
slack_client = SlackClient(CONFIG['SLACK_TOKEN'])
if slack_client.rtm_connect():
print("Connected to Slack")
#my_id = slack_client.server.login_data['self']['id']
# try:
# my_channels = [x for x in slack_client.api_call("channels.list")['channels'] if x['is_member']]
# except (KeyError, json.decoder.JSONDecodeError):
# # sometimes channels isn't there?
# pass
while not SHUTDOWN:
try:
event = msg_queue.get_nowait()
print("New event:", event)
message = parse_event(event)
if message:
slack_client.api_call(
"chat.postMessage", channel=channel, text=message,
as_user=True)
except queue.Empty:
pass
slack_msgs = slack_client.rtm_read()
if slack_msgs:
print(slack_msgs)
time.sleep(LOOP_TIMEOUT)
if __name__ == '__main__':
#instances = get_instance_state()
# for missing in instances:
# print('{} instance {} missing tags: {} found: {}'.format(
# missing.state, missing.instance_id, ','.join(missing.missing_tags), missing.found_tags))
while not SHUTDOWN:
try:
msg_bus = queue.Queue()
eventsThread = threading.Thread(name="ec2events", target=get_ec2_events, args=[msg_bus])
eventsThread.start()
main(msg_bus, CONFIG['CHANNEL'])
eventsThread.join()
except KeyboardInterrupt as e:
print("Shutting down")
SHUTDOWN = True
except Exception as e:
print("well, this is embarassing")
print(e)
SHUTDOWN = True
eventsThread.join()
SHUTDOWN = False
time.sleep(5000)