-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2Control.py
120 lines (100 loc) · 3.86 KB
/
ec2Control.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
#!c:/Python34/python.exe -u
import argparse
import sys
import json
import ec2lib
from subprocess import Popen, PIPE, STDOUT
# Setup commandline arguments and check that the tags are matched
parser = argparse.ArgumentParser(description='ec2 instance handler based on groups as defined by tag name and value')
parser.add_argument('Action')
parser.add_argument('Tag', nargs='*', type=str)
args = parser.parse_args()
# Check the tags and store in dictionary Name:Value
TagDict = []
TagDict = ec2lib.checktags(args.Tag)
# Open connection ----------------------------------------------------------------------
# Connect to AWS and check on all instances where the name:value pairs match
cmd = "aws ec2 describe-instances"
if len(args.Tag) != 0:
cmd = cmd + " --filters"
for key in TagDict:
cmd = cmd + " \"Name=tag:" + key + ",Values=" + TagDict[key] + "\""
print("cmd = " + cmd)
p = Popen(cmd, shell=True,
stdout=PIPE,
stderr=STDOUT)
myjson = "" #Convert io.BufferedReader, byte data to json
for line in p.stdout:
line = line.decode(encoding='utf-8')
myjson = myjson + line
myj = json.loads(myjson)
# Check the return, if empty will be in format {'Reservations': []}
if len(myj["Reservations"]) == 0:
print("ERROR: The return value is empty, no instances meet the criteria entered")
sys.exit(2)
# Examine the returned ec2 descriptions for name, status, and Tags:Name,Value
count = 0
InstanceId = ""
Status = ""
PublicIp = ""
InstanceList = []
for i in myj['Reservations']:
InstanceId = myj['Reservations'][count]['Instances'][0]['InstanceId']
try:
PublicIp = myj['Reservations'][count]['Instances'][0]['PublicIpAddress']
except:
PublicIp = 'NONE'
Status = myj['Reservations'][count]['Instances'][0]['State']['Name']
for tag in myj['Reservations'][count]['Instances'][0]['Tags']:
if (tag['Key'] == 'Name'):
NameTag = tag['Value']
ec2lib.addInstance(InstanceId, Status, NameTag, PublicIp, InstanceList)
count = count + 1
# Process AWS CLI calls ---------------------------------------
# Start, stop depending on status or get instance id and status
if args.Action == 'status':
for ec2info in InstanceList:
ec2lib.printec2Info(ec2info)
elif args.Action == 'stop':
cmd = 'aws ec2 stop-instances --instance-ids'
for ec2info in InstanceList:
if ec2info[1] != 'running':
print(ec2info[0] + ' is not in a state to stop [current status: ' + ec2info[1] + ']')
sys.exit(2)
else:
cmd = cmd + ' ' + ec2info[0]
print("Stopping: " + cmd)
p = Popen(cmd, shell=True,
stdout=PIPE,
stderr=STDOUT)
ec2lib.formatReturn(p.communicate()) # Display return values
elif args.Action == 'start':
cmd = 'aws ec2 start-instances --instance-ids'
for ec2info in InstanceList:
if ec2info[1] != 'stopped':
print(ec2info[0] + ' is not in a state to start [current status: ' + ec2info[1] + ']')
sys.exit(2)
else:
cmd = cmd + ' ' + ec2info[0]
print("Starting: " + cmd)
p = Popen(cmd, shell=True,
stdout=PIPE,
stderr=STDOUT)
ec2lib.formatReturn(p.communicate()) # Display return values
elif args.Action == 'getip':
if len(InstanceList) == 1:
print(InstanceList[0][3])
else:
for ec2info in InstanceList:
if ec2info[1] != 'stopped':
ec2Name = '{:15.15}'.format(ec2info[2])
print(ec2info[0] + ": (" + ec2Name + ') ' + ec2info[3])
elif args.Action == 'reboot':
cmd = 'aws ec2 reboot-instances --instance-ids'
for ec2info in InstanceList:
cmd = cmd + ' ' + ec2info[0]
print("rebooting: " + cmd)
p = Popen(cmd, shell=True,
stdout=PIPE,
stderr=STDOUT)
ec2lib.formatReturn(p.communicate()) # Display return values