~ubuntu-branches/ubuntu/raring/python-boto/raring-proposed

« back to all changes in this revision

Viewing changes to bin/elbadmin

  • Committer: Bazaar Package Importer
  • Author(s): Scott Moser
  • Date: 2010-01-05 15:12:37 UTC
  • mfrom: (4.1.5 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100105151237-skgxjkyogir1vl0k
Tags: 1.9b-1ubuntu1
* merge Loic's changes made in 1.8d-1ubuntu2
* Rename Vcs-* to XS-Debian-Vcs-*.
* Run testsuite during build but ignore failures since it currently doesn't
  pass.
* Add ${misc:Depends}.
* Add XB-Python-Version: ${python:Versions}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
"""
 
37
 
 
38
def list(elb):
 
39
    """List all ELBs"""
 
40
    print "%-20s %s" %  ("Name", "DNS Name")
 
41
    print "-"*80
 
42
    for b in elb.get_all_load_balancers():
 
43
        print "%-20s %s" % (b.name, b.dns_name)
 
44
 
 
45
def get(elb, name):
 
46
    """Get details about ELB <name>"""
 
47
    b = elb.get_all_load_balancers(name)
 
48
    if len(b) < 1:
 
49
        print "No load balancer by the name of %s found" % name
 
50
        return
 
51
    b = b[0]
 
52
 
 
53
    print "Name: %s" % b.name
 
54
    print "DNS Name: %s" % b.dns_name
 
55
 
 
56
    print
 
57
 
 
58
    print "Listeners"
 
59
    print "---------"
 
60
    print "%-8s %-8s %s" % ("IN", "OUT", "PROTO")
 
61
    for l in b.listeners:
 
62
        print "%-8s %-8s %s" % (l[0], l[1], l[2])
 
63
 
 
64
    print
 
65
 
 
66
    print "  Zones  "
 
67
    print "---------"
 
68
    for z in b.availability_zones:
 
69
        print z
 
70
 
 
71
    print
 
72
 
 
73
    print "Instances"
 
74
    print "---------"
 
75
    for i in b.instances:
 
76
        print i.id
 
77
 
 
78
    print
 
79
 
 
80
def create(elb, name, zones, listeners):
 
81
    """Create an ELB named <name>"""
 
82
    l_list = []
 
83
    for l in listeners:
 
84
        l = l.split(",")
 
85
        l_list.append((int(l[0]), int(l[1]), l[2]))
 
86
    
 
87
    b = elb.create_load_balancer(name, zones, l_list)
 
88
    return get(elb, name)
 
89
 
 
90
def delete(elb, name):
 
91
    """Delete this ELB"""
 
92
    b = elb.get_all_load_balancers(name)
 
93
    if len(b) < 1:
 
94
        print "No load balancer by the name of %s found" % name
 
95
        return
 
96
    b = b[0]
 
97
    b.delete()
 
98
    print "Load Balancer %s deleted" % name
 
99
 
 
100
def add_instance(elb, name, instance):
 
101
    """Add <instance> to ELB <name>"""
 
102
    b = elb.get_all_load_balancers(name)
 
103
    if len(b) < 1:
 
104
        print "No load balancer by the name of %s found" % name
 
105
        return
 
106
    b = b[0]
 
107
    b.register_instances([instance])
 
108
    return get(elb, name)
 
109
 
 
110
 
 
111
def remove_instance(elb, name, instance):
 
112
    """Remove instance from elb <name>"""
 
113
    b = elb.get_all_load_balancers(name)
 
114
    if len(b) < 1:
 
115
        print "No load balancer by the name of %s found" % name
 
116
        return
 
117
    b = b[0]
 
118
    b.deregister_instances([instance])
 
119
    return get(elb, name)
 
120
 
 
121
def enable_zone(elb, name, zone):
 
122
    """Enable <zone> for elb"""
 
123
    b = elb.get_all_load_balancers(name)
 
124
    if len(b) < 1:
 
125
        print "No load balancer by the name of %s found" % name
 
126
        return
 
127
    b = b[0]
 
128
    b.enable_zones([zone])
 
129
    return get(elb, name)
 
130
 
 
131
def disable_zone(elb, name, zone):
 
132
    """Disable <zone> for elb"""
 
133
    b = elb.get_all_load_balancers(name)
 
134
    if len(b) < 1:
 
135
        print "No load balancer by the name of %s found" % name
 
136
        return
 
137
    b = b[0]
 
138
    b.disable_zones([zone])
 
139
    return get(elb, name)
 
140
 
 
141
 
 
142
 
 
143
if __name__ == "__main__":
 
144
    try:
 
145
        import readline
 
146
    except ImportError:
 
147
        pass
 
148
    import boto
 
149
    import sys
 
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")
 
155
 
 
156
    (options, args) = parser.parse_args()
 
157
 
 
158
    if len(args) < 1:
 
159
        parser.print_help()
 
160
        sys.exit(1)
 
161
 
 
162
    elb = boto.connect_elb()
 
163
    command = args[0].lower()
 
164
    if command in ("ls", "list"):
 
165
        list(elb)
 
166
    elif command == "get":
 
167
        get(elb, args[1])
 
168
    elif command == "create":
 
169
        create(elb, args[1], options.zones, options.listeners)
 
170
    elif command == "delete":
 
171
        delete(elb, args[1])
 
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])