2
# Copyright (c) 2009 Chris Moyer http://coredumped.org/
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-
12
# The above copyright notice and this permission notice shall be included
13
# in all copies or substantial portions of the Software.
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
23
# Elastic Load Balancer Tool
26
usage = """%prog [options] [command]
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
32
add <name> <instance> Add <instance> in ELB <name>
33
remove|rm <name> <instance> Remove <instance> from ELB <name>
34
enable|en <name> <zone> Enable Zone <zone> for ELB <name>
35
disable <name> <zone> Disable Zone <zone> for ELB <name>
40
print "%-20s %s" % ("Name", "DNS Name")
42
for b in elb.get_all_load_balancers():
43
print "%-20s %s" % (b.name, b.dns_name)
46
"""Get details about ELB <name>"""
47
b = elb.get_all_load_balancers(name)
49
print "No load balancer by the name of %s found" % name
53
print "Name: %s" % b.name
54
print "DNS Name: %s" % b.dns_name
60
print "%-8s %-8s %s" % ("IN", "OUT", "PROTO")
62
print "%-8s %-8s %s" % (l[0], l[1], l[2])
68
for z in b.availability_zones:
80
def create(elb, name, zones, listeners):
81
"""Create an ELB named <name>"""
85
l_list.append((int(l[0]), int(l[1]), l[2]))
87
b = elb.create_load_balancer(name, zones, l_list)
90
def delete(elb, name):
92
b = elb.get_all_load_balancers(name)
94
print "No load balancer by the name of %s found" % name
98
print "Load Balancer %s deleted" % name
100
def add_instance(elb, name, instance):
101
"""Add <instance> to ELB <name>"""
102
b = elb.get_all_load_balancers(name)
104
print "No load balancer by the name of %s found" % name
107
b.register_instances([instance])
108
return get(elb, name)
111
def remove_instance(elb, name, instance):
112
"""Remove instance from elb <name>"""
113
b = elb.get_all_load_balancers(name)
115
print "No load balancer by the name of %s found" % name
118
b.deregister_instances([instance])
119
return get(elb, name)
121
def enable_zone(elb, name, zone):
122
"""Enable <zone> for elb"""
123
b = elb.get_all_load_balancers(name)
125
print "No load balancer by the name of %s found" % name
128
b.enable_zones([zone])
129
return get(elb, name)
131
def disable_zone(elb, name, zone):
132
"""Disable <zone> for elb"""
133
b = elb.get_all_load_balancers(name)
135
print "No load balancer by the name of %s found" % name
138
b.disable_zones([zone])
139
return get(elb, name)
143
if __name__ == "__main__":
150
from optparse import OptionParser
151
from boto.mashups.iobject import IObject
152
parser = OptionParser(version=VERSION, usage=usage)
153
parser.add_option("-z", "--zone", help="Operate on zone", action="append", default=[], dest="zones")
154
parser.add_option("-l", "--listener", help="Specify Listener in,out,proto", action="append", default=[], dest="listeners")
156
(options, args) = parser.parse_args()
162
elb = boto.connect_elb()
163
command = args[0].lower()
164
if command in ("ls", "list"):
166
elif command == "get":
168
elif command == "create":
169
create(elb, args[1], options.zones, options.listeners)
170
elif command == "delete":
172
elif command in ("add", "put"):
173
add_instance(elb, args[1], args[2])
174
elif command in ("rm", "remove"):
175
remove_instance(elb, args[1], args[2])
176
elif command in ("en", "enable"):
177
enable_zone(elb, args[1], args[2])
178
elif command == "disable":
179
disable_zone(elb, args[1], args[2])