comparison planemo/bin/elbadmin @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
1 #!/Users/guerler/spring/springsuite/planemo/bin/python3
2 # Copyright (c) 2009 Chris Moyer http://coredumped.org/
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish, dis-
8 # tribute, sublicense, and/or sell copies of the Software, and to permit
9 # persons to whom the Software is furnished to do so, subject to the fol-
10 # lowing conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
22 #
23 # Elastic Load Balancer Tool
24 #
25 VERSION = "0.2"
26 usage = """%prog [options] [command]
27 Commands:
28 list|ls List all Elastic Load Balancers
29 delete <name> Delete ELB <name>
30 get <name> Get all instances associated with <name>
31 create <name> Create an ELB; -z and -l are required
32 add <name> <instances> Add <instances> in ELB <name>
33 remove|rm <name> <instances> Remove <instances> from ELB <name>
34 reap <name> Remove terminated instances from ELB <name>
35 enable|en <name> <zone> Enable Zone <zone> for ELB <name>
36 disable <name> <zone> Disable Zone <zone> for ELB <name>
37 addl <name> Add listeners (specified by -l) to the ELB
38 <name>
39 rml <name> <port> Remove Listener(s) specified by the port on
40 the ELB <name>
41 """
42
43
44 def find_elb(elb, name):
45 try:
46 elbs = elb.get_all_load_balancers(name)
47 except boto.exception.BotoServerError as se:
48 if se.code == 'LoadBalancerNotFound':
49 elbs = []
50 else:
51 raise
52
53 if len(elbs) < 1:
54 print "No load balancer by the name of %s found" % name
55 return None
56 elif len(elbs) > 1:
57 print "More than one elb matches %s?" % name
58 return None
59
60 # Should not happen
61 if name not in elbs[0].name:
62 print "No load balancer by the name of %s found" % name
63 return None
64
65 return elbs[0]
66
67
68 def list(elb):
69 """List all ELBs"""
70 print "%-20s %s" % ("Name", "DNS Name")
71 print "-" * 80
72 for b in elb.get_all_load_balancers():
73 print "%-20s %s" % (b.name, b.dns_name)
74
75 def check_valid_region(conn, region):
76 if conn is None:
77 print 'Invalid region (%s)' % region
78 sys.exit(1)
79
80 def get(elb, name):
81 """Get details about ELB <name>"""
82
83 b = find_elb(elb, name)
84 if b:
85 print "=" * 80
86 print "Name: %s" % b.name
87 print "DNS Name: %s" % b.dns_name
88 if b.canonical_hosted_zone_name:
89 chzn = b.canonical_hosted_zone_name
90 print "Canonical hosted zone name: %s" % chzn
91 if b.canonical_hosted_zone_name_id:
92 chznid = b.canonical_hosted_zone_name_id
93 print "Canonical hosted zone name id: %s" % chznid
94 print
95
96 print "Health Check: %s" % b.health_check
97 print
98
99 print "Listeners"
100 print "---------"
101 print "%-8s %-8s %s" % ("IN", "OUT", "PROTO")
102 for l in b.listeners:
103 print "%-8s %-8s %s" % (l[0], l[1], l[2])
104
105 print
106
107 print " Zones "
108 print "---------"
109 for z in b.availability_zones:
110 print z
111
112 print
113
114 # Make map of all instance Id's to Name tags
115 import boto
116 from boto.compat.six import iteritems
117 if not options.region:
118 ec2 = boto.connect_ec2()
119 else:
120 ec2 = boto.ec2.connect_to_region(options.region)
121 check_valid_region(ec2, options.region)
122
123 instance_health = b.get_instance_health()
124 instances = [state.instance_id for state in instance_health]
125
126 names = dict((k,'') for k in instances)
127 for i in ec2.get_only_instances():
128 if i.id in instances:
129 names[i.id] = i.tags.get('Name', '')
130
131 name_column_width = max([4] + [len(v) for k,v in iteritems(names)]) + 2
132
133 print "Instances"
134 print "---------"
135 print "%-12s %-15s %-*s %s" % ("ID",
136 "STATE",
137 name_column_width, "NAME",
138 "DESCRIPTION")
139 for state in instance_health:
140 print "%-12s %-15s %-*s %s" % (state.instance_id,
141 state.state,
142 name_column_width, names[state.instance_id],
143 state.description)
144
145 print
146
147
148 def create(elb, name, zones, listeners):
149 """Create an ELB named <name>"""
150 l_list = []
151 for l in listeners:
152 l = l.split(",")
153 if l[2] == 'HTTPS':
154 l_list.append((int(l[0]), int(l[1]), l[2], l[3]))
155 else:
156 l_list.append((int(l[0]), int(l[1]), l[2]))
157
158 b = elb.create_load_balancer(name, zones, l_list)
159 return get(elb, name)
160
161
162 def delete(elb, name):
163 """Delete this ELB"""
164 b = find_elb(elb, name)
165 if b:
166 b.delete()
167 print "Load Balancer %s deleted" % name
168
169
170 def add_instances(elb, name, instances):
171 """Add <instance> to ELB <name>"""
172 b = find_elb(elb, name)
173 if b:
174 b.register_instances(instances)
175 return get(elb, name)
176
177
178 def remove_instances(elb, name, instances):
179 """Remove instance from elb <name>"""
180 b = find_elb(elb, name)
181 if b:
182 b.deregister_instances(instances)
183 return get(elb, name)
184
185
186 def reap_instances(elb, name):
187 """Remove terminated instances from elb <name>"""
188 b = find_elb(elb, name)
189 if b:
190 for state in b.get_instance_health():
191 if (state.state == 'OutOfService' and
192 state.description == 'Instance is in terminated state.'):
193 b.deregister_instances([state.instance_id])
194 return get(elb, name)
195
196
197 def enable_zone(elb, name, zone):
198 """Enable <zone> for elb"""
199 b = find_elb(elb, name)
200 if b:
201 b.enable_zones([zone])
202 return get(elb, name)
203
204
205 def disable_zone(elb, name, zone):
206 """Disable <zone> for elb"""
207 b = find_elb(elb, name)
208 if b:
209 b.disable_zones([zone])
210 return get(elb, name)
211
212
213 def add_listener(elb, name, listeners):
214 """Add listeners to a given load balancer"""
215 l_list = []
216 for l in listeners:
217 l = l.split(",")
218 l_list.append((int(l[0]), int(l[1]), l[2]))
219 b = find_elb(elb, name)
220 if b:
221 b.create_listeners(l_list)
222 return get(elb, name)
223
224
225 def rm_listener(elb, name, ports):
226 """Remove listeners from a given load balancer"""
227 b = find_elb(elb, name)
228 if b:
229 b.delete_listeners(ports)
230 return get(elb, name)
231
232
233 if __name__ == "__main__":
234 try:
235 import readline
236 except ImportError:
237 pass
238 import boto
239 import sys
240 from optparse import OptionParser
241 from boto.mashups.iobject import IObject
242 parser = OptionParser(version=VERSION, usage=usage)
243 parser.add_option("-z", "--zone",
244 help="Operate on zone",
245 action="append", default=[], dest="zones")
246 parser.add_option("-l", "--listener",
247 help="Specify Listener in,out,proto",
248 action="append", default=[], dest="listeners")
249 parser.add_option("-r", "--region",
250 help="Region to connect to",
251 action="store", dest="region")
252
253 (options, args) = parser.parse_args()
254
255 if len(args) < 1:
256 parser.print_help()
257 sys.exit(1)
258
259 if not options.region:
260 elb = boto.connect_elb()
261 else:
262 import boto.ec2.elb
263 elb = boto.ec2.elb.connect_to_region(options.region)
264 check_valid_region(elb, options.region)
265
266 print "%s" % (elb.region.endpoint)
267
268 command = args[0].lower()
269 if command in ("ls", "list"):
270 list(elb)
271 elif command == "get":
272 get(elb, args[1])
273 elif command == "create":
274 if not options.listeners:
275 print "-l option required for command create"
276 sys.exit(1)
277 if not options.zones:
278 print "-z option required for command create"
279 sys.exit(1)
280 create(elb, args[1], options.zones, options.listeners)
281 elif command == "delete":
282 delete(elb, args[1])
283 elif command in ("add", "put"):
284 add_instances(elb, args[1], args[2:])
285 elif command in ("rm", "remove"):
286 remove_instances(elb, args[1], args[2:])
287 elif command == "reap":
288 reap_instances(elb, args[1])
289 elif command in ("en", "enable"):
290 enable_zone(elb, args[1], args[2])
291 elif command == "disable":
292 disable_zone(elb, args[1], args[2])
293 elif command == "addl":
294 if not options.listeners:
295 print "-l option required for command addl"
296 sys.exit(1)
297 add_listener(elb, args[1], options.listeners)
298 elif command == "rml":
299 if not args[2:]:
300 print "port required"
301 sys.exit(2)
302 rm_listener(elb, args[1], args[2:])