-
Notifications
You must be signed in to change notification settings - Fork 4
/
elbctl.py
executable file
·282 lines (223 loc) · 11.2 KB
/
elbctl.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
# ----------------------------------------------------------------------------
# Purpose : Perform routine ELB tasks using Boto3. Can accept mutiple ELB and Instances as arguments for attach/detach.
# Author: Denny Vettom
# Dependencies: PYTHON -3, Boto3, Aws cli and profiles. CMDB File, dvclass
#
# ----------------------------------------------------------------------------
# Name Date Comment Version
# ----------------------------------------------------------------------------
# DV 09/09/2019 Initial Version V 1.0
# DV 16/09/2019 Region info from Document V 1.1
#
"""
Script for list/status/attach/detach tasks. Default variables Profile=default, Region=eu-west-1
list : can take Topology as argument and display all ELB for the topology
Status : Accepts Topology or one or more ELB as argument and shows status
At/Detach : Accepts 1 or more ELB and Hostsor instance ID as argument. For each elb same set of actions performed with hosts
All ELB must be in same region.
"""
import boto3,argparse,sys,time,re,os
import dvclass
SCR_HOME = os.path.dirname(os.path.realpath(__file__))
# Argument Parser with Subparse for each actopn
NOTE = "ELB Status"
P = argparse.ArgumentParser(description='ELB Operations tasks', epilog=NOTE)
Sub = P.add_subparsers(title="Required arguments", dest="Task")
# List Sub Parse, requires single input which is Topology
List = Sub.add_parser("list",help="Show list of all ELB configured for Topology")
List.add_argument('-t', '--topology', help='Topology name for which ELB status to be displayed.', required=True)
List.add_argument('-p', '--profile', default="default", help='If no profile provided, assumes default')
List.add_argument('-r', '--region', default="eu-west-1", help='Default is eu-west-1, or provide as argument')
Status = Sub.add_parser("status",help="Show status of all ELB configured for Topology")
Status.add_argument('-t', '--topology', help='Topology name for which ELB status to be displayed.')
Status.add_argument('-e', '--elb', nargs='+', help='Name/s of Aws ELB')
Status.add_argument('-p', '--profile', default="default", help='If no profile provided, assumes default')
Status.add_argument('-r', '--region', default="eu-west-1", help='Default is eu-west-1, or provide as argument')
Attach = Sub.add_parser("attach",help="Attach instance/s to ELB")
Attach.add_argument('-e', '--elb', nargs='+', help='Name/s of Aws ELB', required=True)
Attach.add_argument('-i', '--instances', nargs='+', help='Dispatcher Hostnames or Instance ID/s', required=True)
Attach.add_argument('-p', '--profile', default="default", help='If no profile provided, assumes default')
Attach.add_argument('-r', '--region', default="eu-west-1", help='Default is eu-west-1, or provide as argument')
Detach = Sub.add_parser("detach",help="Detach instance/s to ELB")
Detach.add_argument('-e','--elb', nargs='+', help='Name/s of Aws ELB', required=True)
Detach.add_argument('-i', '--instances', nargs='+', help='Dispatcher Hostnames or Instance ID/s', required=True)
Detach.add_argument('-p', '--profile', default="default", help='If no profile provided, assumes default')
Detach.add_argument('-r', '--region', default="eu-west-1", help='Default is eu-west-1, or provide as argument')
# Parse Arguments
args = P.parse_args()
# Eit programme if no arguments provided. Print help
if args.Task is None:
P.print_help()
exit()
# Setting Default variables
Profile = args.profile
Region = args.region
# Start of Functions. -----********------
def ListallELB(VPC, Region):
# List and display all ELB in provided region. If VPC argument provided, will only show ELB attached to those VPC
ec2client = boto3.SetELBClient(Profile, Region)
try: Result = ec2client.describe_load_balancers()
except exception as ERR: print (" ERROR : Failed to get ELB list ", ERR)
# Process the output and print the list
try:
print ("\n ELB's in {} in {} region using profile {}. " .format(VPC,Region,Profile))
for X in Result['LoadBalancerDescriptions']:
ELB = X.get('LoadBalancerName', 'NULL')
VPCID = X.get('VPCId', 'NULL')
if VPC == VPCID:
print (" {} " .format(ELB))
print ("\n")
except: print("Failed to process Result to list ELB. ")
def Elbstatus(Var,Region):
# Var = ELB name
ec2client = boto3.SetELBClient(Profile, Region)
try:
print("\n ELB {} in {} " .format(Var, Region))
Result = ec2client.describe_instance_health(LoadBalancerName=Var)
# Process Result and display
for X in Result['InstanceStates']:
InstID = X.get('InstanceId')
InstName = ams.GetHostname(InstID)
print (InstName, '\t', InstID, '\t', X.get('State'))
except Exception as ERR:
print(" ERROR : Failed to get ELB status using ELB={}, Region={}, Profile={}. \n ERROR : {}" .format(Var,Region,Profile,ERR))
#
# Now process Result and Get hostname to print.
def GetElbList(VPC, Region):
# Return list of ELB
ec2client = boto3.SetELBClient(Profile, Region)
try: Result = ec2client.describe_load_balancers()
except exception as ERR: print (" ERROR : Failed to get ELB list ", ERR)
# Process the output and print the list
LIST = []
try:
for X in Result['LoadBalancerDescriptions']:
ELB = X.get('LoadBalancerName', 'NULL')
VPCID = X.get('VPCId', 'NULL')
if VPC == VPCID:
LIST.append(ELB)
return LIST
except: print("Failed to process Result to list ELB. ")
def AtachinstancetoELB(ELB, Instances):
# Loop through Instances and issue attach command
for I in Instances:
try:
# Call function that gets instance information like ID,host, Region, AZ, VPC
global Region
AWSinfo = ams.GetInstAWS(I)
InstID = AWSinfo[0]
Host = AWSinfo[1]
Region = AWSinfo[2]
if InstID is None:
print (" \nERROR : Failed to get Instance ID for {}. Please chek Instance ID/Hostname provided. " .format(I))
exit()
except Exception as ERR:
print (" ERROR : Failed to get Instance ID for {} \n {} " .format(I,ERR))
try:
ec2client = boto3.SetELBClient(Profile, Region)
Result = ec2client.register_instances_with_load_balancer(\
LoadBalancerName=ELB, \
Instances=[{'InstanceId': InstID},])
print (" INFO : Attached {} to ELB {}. " .format(Host,ELB))
except Exception as ERR:
print (" \nERROR: Failed to attach Host {} {} to {} \n {}" .format(Host,InstID,ELB,ERR))
def DetachfromELB(ELB, Instances):
# Loop through Instances and issue attach command
for I in Instances:
try:
# Call function that gets instance information like ID,host, Region, AZ, VPC
global Region
AWSinfo = ams.GetInstAWS(I)
InstID = AWSinfo[0]
Host = AWSinfo[1]
Region = AWSinfo[2]
if InstID is None:
print (" \nERROR : Failed to get Instance ID for {}. Please chek Instance ID/Hostname provided. " .format(I))
exit()
except Exception as ERR:
print (" ERROR : Failed to get Instance ID for {} \n {} " .format(I,ERR))
try:
ec2client = boto3.SetELBClient(Profile, Region)
Result = ec2client.deregister_instances_from_load_balancer(\
LoadBalancerName=ELB, \
Instances=[{'InstanceId': InstID},])
print (" INFO : Detached {} from ELB {}. " .format(Host,ELB))
except Exception as ERR:
print (" \nERROR: Failed to detach Host {} {} to {} \n {}" .format(Host,InstID,ELB,ERR))
# Start of Main Section. -----********------
def main():
global boto3
global ams
boto3 = dvclass.AWSBoto3()
ams = dvclass.AMSCMDB()
# Set ec2 client from Class
ec2client = boto3.SetELBClient(Profile, Region)
if args.Task == "list":
# Accepts Topology as cumpulsary argiment.
# Find all ELB for the Topology mentioned
# . First find all VPC and region, then find all ELB
try:
Result = ams.GetVpcReg(args.topology)
# Make sure Result is valid, if not exit. If topology not found or not AWS, then will be empty
if not Result:
print ("\n ERROR : Topology not found in CMDB or it is not AWS Topology \n")
exit()
# At this stage we have Result with values, so continue
# Result can have multiple entries for multiple regions
for X in Result:
Y = X.split()
VPC = Y[0]
REGION = Y[1]
ListallELB(VPC, REGION)
except Exception as ERR:
print (" ERROR : Failed to find {} in Database. Errors is {}" .format(args.topology,ERR))
elif args.Task == "status":
# Accepts Topology or list of ELB as argument, and at least one should be proided.
# If elb as argument have to specify Profile and region if different.
if args.topology is None and args.elb is None:
print ("\n ERROR : Please provide \'-t Topology\' or \'--elb Elbname\' as argument\n")
print (" INFO : Run \"status -h\" for help")
exit ()
# If Topology is provided process it and ignore ELB
if args.topology:
# Get status of all elb from all region
ec2client = boto3.SetELBClient(Profile, Region)
Result = ams.GetVpcReg(args.topology)
if not Result:
print ("\n ERROR : Topology not found in CMDB or it is not AWS Topology \n")
exit()
for X in Result:
Y = X.split()
VPC = Y[0]
REGION = Y[1]
# Now for each VPC get list of ELB and status
Result = GetElbList(VPC,REGION)
for ELB in Result:
Elbstatus(ELB, REGION)
exit()
if args.elb:
for ELB in args.elb:
# Call the elbstatus function
Elbstatus(ELB,Region)
exit()
elif args.Task == "attach":
#Loop through the ELB provided and call attach function. At end wait few seconds before issuing status
for ELB in args.elb:
AtachinstancetoELB(ELB, args.instances)
# At end sleep for few seconds then status
print ("\n INFO : Waiting 20 seconds before checking status...")
time.sleep(20)
for ELB in args.elb:
Elbstatus(ELB,Region)
elif args.Task == "detach":
#Loop through the ELB provided and call attach function. At end wait few seconds before issuing status
for ELB in args.elb:
DetachfromELB(ELB, args.instances)
# At end sleep for few seconds then status
print ("\n INFO : Waiting 20 seconds before checking status...")
time.sleep(20)
for ELB in args.elb:
Elbstatus(ELB,Region)
if __name__ == "__main__":
main()