-
Notifications
You must be signed in to change notification settings - Fork 4
/
ec2ctl.py
executable file
·180 lines (154 loc) · 6.48 KB
/
ec2ctl.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
178
179
180
#!/usr/bin/env python3
# ----------------------------------------------------------------------------
# Purpose : Start/stop instances. Status of instance or all instances in topology.
# Author: Denny Vettom
# Dependencies: PYTHON -3, Boto3, Aws cli profiles. CMDB File, dvclass
#
# ----------------------------------------------------------------------------
# Name Date Comment Version
# ----------------------------------------------------------------------------
# DV 25/02/2020 Initial Version V 1.0
# DV 29/02/2020 rewrited for dvclass V 2.0
#
'''
Take hostname or instance ID as argument and perform action
Accepts Topoogy as argument and show status of all hosts in topology.
'''
import boto3,argparse,sys,time,re,os
import dvclass
argparser = argparse.ArgumentParser(description='Perform common instance tasks')
argparser.add_argument('Task', choices=['start','stop','restart','status', 'topo'], help='Instance action to be performed list/start/stop/restart/status')
argparser.add_argument('-i', '--instance', nargs='+', help='Hostnames or Aws instance IDs' )
argparser.add_argument('-t', '--topology', help='Topology name to get status of all instances' )
argparser.add_argument('-p', '--profile', default="default", help='If no profile provided, assumes default')
# argparser.add_argument('--region', default="eu-west-1", help='Default is eu-west-1, or provide as argument')
args = argparser.parse_args()
def InstanceStatus(Instance, Host, Region):
# Set ec2 client
ec2client =DVboto3.SetEC2Client(Profile, Region)
try:
# Get instance details
Data = ec2client.describe_instances(InstanceIds=[Instance])
# Process the json to print the status. Using Tag CMDB_hostname to get hostname, update if required.
except Exception as ERR:
print(f' ERROR: Failed to gent instance details, please ensure right profile and is AWS topology \n Exception is {ERR}')
exit()
for X in Data['Reservations']:
for Y in X["Instances"]:
InstID = Y.get("InstanceId", "NULL")
InstanceType = Y.get("InstanceType", "NULL")
Status = Y.get('State').get('Name')
try:
print (f' {Host} \t {Status}' )
except:
print (" ERROR: Failed to get status for Host ", Host )
def StopInstance(Instance, Host, Region):
# Set ec2 client
ec2client =DVboto3.SetEC2Client(Profile, Region)
try:
# print " INFO : Stoppign instance " + Instance
print(f' INFO: Executing stop on {Host}.')
ec2client.stop_instances(InstanceIds=[Instance])
print(f' INFO: Waiting for {Host} to stop.')
waiter = ec2client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[Instance])
print (f' INFO: {Host} stopped.')
except:
print (" ERROR : Failed to Stop instance ", Host)
def StartInstance(Instance, Host, Region):
# Set ec2 client
ec2client =DVboto3.SetEC2Client(Profile, Region)
try:
# print " INFO : Stoppign instance " + Instance
print(f' INFO: Executing start on {Host}.')
ec2client.start_instances(InstanceIds=[Instance])
print(f' INFO: Waiting for {Host} to start.')
waiter = ec2client.get_waiter('system_status_ok')
waiter.wait(InstanceIds=[Instance])
print (f' INFO: {Host} started.')
except:
print (" ERROR : Failed to Start instance ", Host)
def VerifyInstArgument(Arg):
if Arg is None:
print(" ERROR: Instance name must be provided as argument. -h for help ")
argparser.print_help()
exit()
def main():
global Profile
global DVboto3
global ams
Profile = args.profile
DVboto3 = dvclass.AWSBoto3()
ams = dvclass.AMSCMDB()
# Ready to take action based on input
if args.Task == "status":
VerifyInstArgument(args.instance)
print("\n")
# Loop through instance and take action
for X in args.instance:
Result = ams.GetInstAWS(X)
# instance_id, Host, Region, AZ, VPC
InstID = Result[0]
HostName = Result[1]
Region = Result[2]
InstanceStatus(InstID,HostName,Region)
print("\n")
elif args.Task == "stop":
VerifyInstArgument(args.instance)
print("\n")
# Loop through instance and take action
for X in args.instance:
Result = ams.GetInstAWS(X)
# instance_id, Host, Region, AZ, VPC
InstID = Result[0]
HostName = Result[1]
Region = Result[2]
StopInstance(InstID,HostName,Region)
print("\n")
elif args.Task == "start":
VerifyInstArgument(args.instance)
print("\n")
# Loop through instance and take action
for X in args.instance:
Result = ams.GetInstAWS(X)
# instance_id, Host, Region, AZ, VPC
InstID = Result[0]
HostName = Result[1]
Region = Result[2]
StartInstance(InstID,HostName,Region)
print("\n")
elif args.Task == "restart":
VerifyInstArgument(args.instance)
print("\n")
for X in args.instance:
Result = ams.GetInstAWS(X)
# instance_id, Host, Region, AZ, VPC
InstID = Result[0]
HostName = Result[1]
Region = Result[2]
StopInstance(InstID,HostName,Region)
time.sleep(3)
StartInstance(InstID,HostName,Region)
elif args.Task == "topo":
# Print status of all instances in topology
# Topology as argument required
if args.topology is None:
print( " ERROR: Topology argument must be provided. -h for help")
exit()
# Get instance information for topology and get status.
Result = ams.GetInstanceforTopo(args.topology)
if len(Result) is 0:
print(f' ERROR: No instances found for {args.topology}, verify topology name and is it AWS topology? \n')
exit()
print("\n")
# Loop through instance and take action
for X in Result:
Result = ams.GetInstAWS(X)
# instance_id, Host, Region, AZ, VPC
InstID = Result[0]
HostName = Result[1]
Region = Result[2]
InstanceStatus(InstID,HostName,Region)
print("\n")
if __name__ == "__main__":
main()