| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2009 Chris Moyer http://coredumped.org/ | 2 # Copyright (c) 2009 Chris Moyer http://coredumped.org/ |
| 3 # | 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | 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 | 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- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: | 10 # lowing conditions: |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 return elbs[0] | 65 return elbs[0] |
| 66 | 66 |
| 67 | 67 |
| 68 def list(elb): | 68 def list(elb): |
| 69 """List all ELBs""" | 69 """List all ELBs""" |
| 70 print "%-20s %s" % ("Name", "DNS Name") | 70 print "%-20s %s" % ("Name", "DNS Name") |
| 71 print "-" * 80 | 71 print "-" * 80 |
| 72 for b in elb.get_all_load_balancers(): | 72 for b in elb.get_all_load_balancers(): |
| 73 print "%-20s %s" % (b.name, b.dns_name) | 73 print "%-20s %s" % (b.name, b.dns_name) |
| 74 | 74 |
| 75 def check_valid_region(conn, region): |
| 76 if conn is None: |
| 77 print 'Invalid region (%s)' % region |
| 78 sys.exit(1) |
| 75 | 79 |
| 76 def get(elb, name): | 80 def get(elb, name): |
| 77 """Get details about ELB <name>""" | 81 """Get details about ELB <name>""" |
| 78 | 82 |
| 79 b = find_elb(elb, name) | 83 b = find_elb(elb, name) |
| 80 if b: | 84 if b: |
| 81 print "=" * 80 | 85 print "=" * 80 |
| 82 print "Name: %s" % b.name | 86 print "Name: %s" % b.name |
| 83 print "DNS Name: %s" % b.dns_name | 87 print "DNS Name: %s" % b.dns_name |
| 84 if b.canonical_hosted_zone_name: | 88 if b.canonical_hosted_zone_name: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 print | 105 print |
| 102 | 106 |
| 103 print " Zones " | 107 print " Zones " |
| 104 print "---------" | 108 print "---------" |
| 105 for z in b.availability_zones: | 109 for z in b.availability_zones: |
| 106 print z | 110 print z |
| 107 | 111 |
| 108 print | 112 print |
| 109 | 113 |
| 110 # Make map of all instance Id's to Name tags | 114 # Make map of all instance Id's to Name tags |
| 115 import boto |
| 111 if not options.region: | 116 if not options.region: |
| 112 ec2 = boto.connect_ec2() | 117 ec2 = boto.connect_ec2() |
| 113 else: | 118 else: |
| 114 import boto.ec2.elb | |
| 115 ec2 = boto.ec2.connect_to_region(options.region) | 119 ec2 = boto.ec2.connect_to_region(options.region) |
| 120 check_valid_region(ec2, options.region) |
| 116 | 121 |
| 117 instance_health = b.get_instance_health() | 122 instance_health = b.get_instance_health() |
| 118 instances = [state.instance_id for state in instance_health] | 123 instances = [state.instance_id for state in instance_health] |
| 119 | 124 |
| 120 names = {} | 125 names = dict((k,'') for k in instances) |
| 121 for i in ec2.get_only_instances(instances): | 126 for i in ec2.get_only_instances(): |
| 122 names[i.id] = i.tags.get('Name', '') | 127 if i.id in instances: |
| 128 names[i.id] = i.tags.get('Name', '') |
| 123 | 129 |
| 124 name_column_width = max([4] + [len(v) for k,v in names.iteritems()]) + 2 | 130 name_column_width = max([4] + [len(v) for k,v in names.iteritems()]) + 2 |
| 125 | 131 |
| 126 print "Instances" | 132 print "Instances" |
| 127 print "---------" | 133 print "---------" |
| 128 print "%-12s %-15s %-*s %s" % ("ID", | 134 print "%-12s %-15s %-*s %s" % ("ID", |
| 129 "STATE", | 135 "STATE", |
| 130 name_column_width, "NAME", | 136 name_column_width, "NAME", |
| 131 "DESCRIPTION") | 137 "DESCRIPTION") |
| 132 for state in instance_health: | 138 for state in instance_health: |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 253 |
| 248 if len(args) < 1: | 254 if len(args) < 1: |
| 249 parser.print_help() | 255 parser.print_help() |
| 250 sys.exit(1) | 256 sys.exit(1) |
| 251 | 257 |
| 252 if not options.region: | 258 if not options.region: |
| 253 elb = boto.connect_elb() | 259 elb = boto.connect_elb() |
| 254 else: | 260 else: |
| 255 import boto.ec2.elb | 261 import boto.ec2.elb |
| 256 elb = boto.ec2.elb.connect_to_region(options.region) | 262 elb = boto.ec2.elb.connect_to_region(options.region) |
| 263 check_valid_region(elb, options.region) |
| 257 | 264 |
| 258 print "%s" % (elb.region.endpoint) | 265 print "%s" % (elb.region.endpoint) |
| 259 | 266 |
| 260 command = args[0].lower() | 267 command = args[0].lower() |
| 261 if command in ("ls", "list"): | 268 if command in ("ls", "list"): |
| 262 list(elb) | 269 list(elb) |
| 263 elif command == "get": | 270 elif command == "get": |
| 264 get(elb, args[1]) | 271 get(elb, args[1]) |
| 265 elif command == "create": | 272 elif command == "create": |
| 266 if not options.listeners: | 273 if not options.listeners: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 285 elif command == "addl": | 292 elif command == "addl": |
| 286 if not options.listeners: | 293 if not options.listeners: |
| 287 print "-l option required for command addl" | 294 print "-l option required for command addl" |
| 288 sys.exit(1) | 295 sys.exit(1) |
| 289 add_listener(elb, args[1], options.listeners) | 296 add_listener(elb, args[1], options.listeners) |
| 290 elif command == "rml": | 297 elif command == "rml": |
| 291 if not args[2:]: | 298 if not args[2:]: |
| 292 print "port required" | 299 print "port required" |
| 293 sys.exit(2) | 300 sys.exit(2) |
| 294 rm_listener(elb, args[1], args[2:]) | 301 rm_listener(elb, args[1], args[2:]) |
| OLD | NEW |