This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws.py
84 lines (62 loc) · 2.35 KB
/
aws.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
from errbot import BotPlugin, botcmd
from optparse import OptionParser
import logging
import socket
import time
import boto3
log = logging.getLogger(name='errbot.plugins.AWS')
class AWS(BotPlugin):
def _ec2_find_instance(self, name):
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
for i in instance.tags:
if i['Key'] == 'Name':
insname = i['Value']
if name == insname or name == instance.id:
return instance
def _ec2_instance_details(self, name):
ec2 = boto3.resource('ec2')
instance = self._ec2_find_instance(name)
if instance is not None:
details = {
'id' : instance.id,
'status' : instance.state['Name'],
'ip_private' : instance.private_ip_address,
'ip_public' : instance.public_ip_address,
'instance_type': instance.instance_type,
}
else:
details = {'error': 'instance named {0} not found.'.format(name)}
return details
@botcmd(split_args_with=None)
def ec2_list(self, msg, args):
'''list all ec2 instances in account
option: None
!ec2 list
'''
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
#idlist.append(instance.id)
#yield instance.id
for i in instance.tags:
if i['Key'] == 'Name':
name = i['Value']
self.send(msg.frm,
'{0}: {1}'.format(instance.id, name),
message_type=msg.type,
in_reply_to=msg,
groupchat_nick_reply=True)
@botcmd(split_args_with=None)
def ec2_info(self, msg, args):
'''provide info of ec2 instances
option: instance id or name
Example: !ec2 info i-0aa26058860a003d4
!ec2 info docker-machine
'''
name = args.pop(0)
details = self._ec2_instance_details(name)
self.send(msg.frm,
'{0} : {1}'.format(name, details),
message_type=msg.type,
in_reply_to=msg,
groupchat_nick_reply=True)