| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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.1" | |
| 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 | |
| 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> | |
| 36 addl <name> Add listeners (specified by -l) to the ELB
<name> | |
| 37 rml <name> <port> Remove Listener(s) specified by the port o
n the ELB | |
| 38 """ | |
| 39 | |
| 40 def list(elb): | |
| 41 """List all ELBs""" | |
| 42 print "%-20s %s" % ("Name", "DNS Name") | |
| 43 print "-"*80 | |
| 44 for b in elb.get_all_load_balancers(): | |
| 45 print "%-20s %s" % (b.name, b.dns_name) | |
| 46 | |
| 47 def get(elb, name): | |
| 48 """Get details about ELB <name>""" | |
| 49 elbs = elb.get_all_load_balancers(name) | |
| 50 if len(elbs) < 1: | |
| 51 print "No load balancer by the name of %s found" % name | |
| 52 return | |
| 53 for b in elbs: | |
| 54 if name in b.name: | |
| 55 print "="*80 | |
| 56 print "Name: %s" % b.name | |
| 57 print "DNS Name: %s" % b.dns_name | |
| 58 | |
| 59 print | |
| 60 | |
| 61 print "Listeners" | |
| 62 print "---------" | |
| 63 print "%-8s %-8s %s" % ("IN", "OUT", "PROTO") | |
| 64 for l in b.listeners: | |
| 65 print "%-8s %-8s %s" % (l[0], l[1], l[2]) | |
| 66 | |
| 67 print | |
| 68 | |
| 69 print " Zones " | |
| 70 print "---------" | |
| 71 for z in b.availability_zones: | |
| 72 print z | |
| 73 | |
| 74 print | |
| 75 | |
| 76 print "Instances" | |
| 77 print "---------" | |
| 78 for i in b.instances: | |
| 79 print i.id | |
| 80 | |
| 81 print | |
| 82 | |
| 83 def create(elb, name, zones, listeners): | |
| 84 """Create an ELB named <name>""" | |
| 85 l_list = [] | |
| 86 for l in listeners: | |
| 87 l = l.split(",") | |
| 88 if l[2]=='HTTPS': | |
| 89 l_list.append((int(l[0]), int(l[1]), l[2], l[3])) | |
| 90 else : l_list.append((int(l[0]), int(l[1]), l[2])) | |
| 91 | |
| 92 b = elb.create_load_balancer(name, zones, l_list) | |
| 93 return get(elb, name) | |
| 94 | |
| 95 def delete(elb, name): | |
| 96 """Delete this ELB""" | |
| 97 b = elb.get_all_load_balancers(name) | |
| 98 if len(b) < 1: | |
| 99 print "No load balancer by the name of %s found" % name | |
| 100 return | |
| 101 for i in b: | |
| 102 if name in i.name: | |
| 103 i.delete() | |
| 104 print "Load Balancer %s deleted" % name | |
| 105 | |
| 106 def add_instance(elb, name, instance): | |
| 107 """Add <instance> to ELB <name>""" | |
| 108 b = elb.get_all_load_balancers(name) | |
| 109 if len(b) < 1: | |
| 110 print "No load balancer by the name of %s found" % name | |
| 111 return | |
| 112 for i in b: | |
| 113 if name in i.name: | |
| 114 i.register_instances([instance]) | |
| 115 return get(elb, name) | |
| 116 | |
| 117 | |
| 118 def remove_instance(elb, name, instance): | |
| 119 """Remove instance from elb <name>""" | |
| 120 b = elb.get_all_load_balancers(name) | |
| 121 if len(b) < 1: | |
| 122 print "No load balancer by the name of %s found" % name | |
| 123 return | |
| 124 for i in b: | |
| 125 if name in i.name: | |
| 126 i.deregister_instances([instance]) | |
| 127 return get(elb, name) | |
| 128 | |
| 129 def enable_zone(elb, name, zone): | |
| 130 """Enable <zone> for elb""" | |
| 131 b = elb.get_all_load_balancers(name) | |
| 132 if len(b) < 1: | |
| 133 print "No load balancer by the name of %s found" % name | |
| 134 return | |
| 135 b = b[0] | |
| 136 b.enable_zones([zone]) | |
| 137 return get(elb, name) | |
| 138 | |
| 139 def disable_zone(elb, name, zone): | |
| 140 """Disable <zone> for elb""" | |
| 141 b = elb.get_all_load_balancers(name) | |
| 142 if len(b) < 1: | |
| 143 print "No load balancer by the name of %s found" % name | |
| 144 return | |
| 145 b = b[0] | |
| 146 b.disable_zones([zone]) | |
| 147 return get(elb, name) | |
| 148 | |
| 149 def add_listener(elb, name, listeners): | |
| 150 """Add listeners to a given load balancer""" | |
| 151 l_list = [] | |
| 152 for l in listeners: | |
| 153 l = l.split(",") | |
| 154 l_list.append((int(l[0]), int(l[1]), l[2])) | |
| 155 b = elb.get_all_load_balancers(name) | |
| 156 if len(b) < 1: | |
| 157 print "No load balancer by the name of %s found" % name | |
| 158 return | |
| 159 b = b[0] | |
| 160 b.create_listeners(l_list) | |
| 161 return get(elb, name) | |
| 162 | |
| 163 def rm_listener(elb, name, ports): | |
| 164 """Remove listeners from a given load balancer""" | |
| 165 b = elb.get_all_load_balancers(name) | |
| 166 if len(b) < 1: | |
| 167 print "No load balancer by the name of %s found" % name | |
| 168 return | |
| 169 b = b[0] | |
| 170 b.delete_listeners(ports) | |
| 171 return get(elb, name) | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 if __name__ == "__main__": | |
| 177 try: | |
| 178 import readline | |
| 179 except ImportError: | |
| 180 pass | |
| 181 import boto | |
| 182 import sys | |
| 183 from optparse import OptionParser | |
| 184 from boto.mashups.iobject import IObject | |
| 185 parser = OptionParser(version=VERSION, usage=usage) | |
| 186 parser.add_option("-z", "--zone", help="Operate on zone", action="append", d
efault=[], dest="zones") | |
| 187 parser.add_option("-l", "--listener", help="Specify Listener in,out,proto",
action="append", default=[], dest="listeners") | |
| 188 | |
| 189 (options, args) = parser.parse_args() | |
| 190 | |
| 191 if len(args) < 1: | |
| 192 parser.print_help() | |
| 193 sys.exit(1) | |
| 194 | |
| 195 elb = boto.connect_elb() | |
| 196 | |
| 197 print "%s" % (elb.region.endpoint) | |
| 198 | |
| 199 command = args[0].lower() | |
| 200 if command in ("ls", "list"): | |
| 201 list(elb) | |
| 202 elif command == "get": | |
| 203 get(elb, args[1]) | |
| 204 elif command == "create": | |
| 205 create(elb, args[1], options.zones, options.listeners) | |
| 206 elif command == "delete": | |
| 207 delete(elb, args[1]) | |
| 208 elif command in ("add", "put"): | |
| 209 add_instance(elb, args[1], args[2]) | |
| 210 elif command in ("rm", "remove"): | |
| 211 remove_instance(elb, args[1], args[2]) | |
| 212 elif command in ("en", "enable"): | |
| 213 enable_zone(elb, args[1], args[2]) | |
| 214 elif command == "disable": | |
| 215 disable_zone(elb, args[1], args[2]) | |
| 216 elif command == "addl": | |
| 217 add_listener(elb, args[1], options.listeners) | |
| 218 elif command == "rml": | |
| 219 rm_listener(elb, args[1], args[2:]) | |
| OLD | NEW |