~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/ec2/autoscale/group.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2009 Reza Lotun http://reza.lotun.name/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
 
 
22
import weakref
 
23
 
 
24
from boto.ec2.elb.listelement import ListElement
 
25
from boto.resultset import ResultSet
 
26
from boto.ec2.autoscale.trigger import Trigger
 
27
from boto.ec2.autoscale.request import Request
 
28
 
 
29
class Instance(object):
 
30
    def __init__(self, connection=None):
 
31
        self.connection = connection
 
32
        self.instance_id = ''
 
33
 
 
34
    def __repr__(self):
 
35
        return 'Instance:%s' % self.instance_id
 
36
 
 
37
    def startElement(self, name, attrs, connection):
 
38
        return None
 
39
 
 
40
    def endElement(self, name, value, connection):
 
41
        if name == 'InstanceId':
 
42
            self.instance_id = value
 
43
        else:
 
44
            setattr(self, name, value)
 
45
 
 
46
 
 
47
class AutoScalingGroup(object):
 
48
    def __init__(self, connection=None, group_name=None,
 
49
                 availability_zone=None, launch_config=None,
 
50
                 availability_zones=None,
 
51
                 load_balancers=None, cooldown=0,
 
52
                 min_size=None, max_size=None):
 
53
        """
 
54
        Creates a new AutoScalingGroup with the specified name.
 
55
 
 
56
        You must not have already used up your entire quota of
 
57
        AutoScalingGroups in order for this call to be successful. Once the
 
58
        creation request is completed, the AutoScalingGroup is ready to be
 
59
        used in other calls.
 
60
 
 
61
        :type name: str
 
62
        :param name: Name of autoscaling group.
 
63
 
 
64
        :type availability_zone: str
 
65
        :param availability_zone: An availability zone. DEPRECATED - use the
 
66
                                  availability_zones parameter, which expects
 
67
                                  a list of availability zone
 
68
                                  strings
 
69
 
 
70
        :type availability_zone: list
 
71
        :param availability_zone: List of availability zones.
 
72
 
 
73
        :type launch_config: str
 
74
        :param launch_config: Name of launch configuration name.
 
75
 
 
76
        :type load_balancers: list
 
77
        :param load_balancers: List of load balancers.
 
78
 
 
79
        :type minsize: int
 
80
        :param minsize: Minimum size of group
 
81
 
 
82
        :type maxsize: int
 
83
        :param maxsize: Maximum size of group
 
84
 
 
85
        :type cooldown: int
 
86
        :param cooldown: Amount of time after a Scaling Activity completes
 
87
                         before any further scaling activities can start.
 
88
 
 
89
        :rtype: tuple
 
90
        :return: Updated healthcheck for the instances.
 
91
        """
 
92
        self.name = group_name
 
93
        self.connection = connection
 
94
        self.min_size = min_size
 
95
        self.max_size = max_size
 
96
        self.created_time = None
 
97
        self.cooldown = cooldown
 
98
        self.launch_config = launch_config
 
99
        if self.launch_config:
 
100
            self.launch_config_name = self.launch_config.name
 
101
        else:
 
102
            self.launch_config_name = None
 
103
        self.desired_capacity = None
 
104
        lbs = load_balancers or []
 
105
        self.load_balancers = ListElement(lbs)
 
106
        zones = availability_zones or []
 
107
        self.availability_zone = availability_zone
 
108
        self.availability_zones = ListElement(zones)
 
109
        self.instances = None
 
110
 
 
111
    def __repr__(self):
 
112
        return 'AutoScalingGroup:%s' % self.name
 
113
 
 
114
    def startElement(self, name, attrs, connection):
 
115
        if name == 'Instances':
 
116
            self.instances = ResultSet([('member', Instance)])
 
117
            return self.instances
 
118
        elif name == 'LoadBalancerNames':
 
119
            return self.load_balancers
 
120
        elif name == 'AvailabilityZones':
 
121
            return self.availability_zones
 
122
        else:
 
123
            return
 
124
 
 
125
    def endElement(self, name, value, connection):
 
126
        if name == 'MinSize':
 
127
            self.min_size = value
 
128
        elif name == 'CreatedTime':
 
129
            self.created_time = value
 
130
        elif name == 'Cooldown':
 
131
            self.cooldown = value
 
132
        elif name == 'LaunchConfigurationName':
 
133
            self.launch_config_name = value
 
134
        elif name == 'DesiredCapacity':
 
135
            self.desired_capacity = value
 
136
        elif name == 'MaxSize':
 
137
            self.max_size = value
 
138
        elif name == 'AutoScalingGroupName':
 
139
            self.name = value
 
140
        else:
 
141
            setattr(self, name, value)
 
142
 
 
143
    def set_capacity(self, capacity):
 
144
        """ Set the desired capacity for the group. """
 
145
        params = {
 
146
                  'AutoScalingGroupName' : self.name,
 
147
                  'DesiredCapacity'      : capacity,
 
148
                 }
 
149
        req = self.connection.get_object('SetDesiredCapacity', params,
 
150
                                            Request)
 
151
        self.connection.last_request = req
 
152
        return req
 
153
 
 
154
    def update(self):
 
155
        """ Sync local changes with AutoScaling group. """
 
156
        return self.connection._update_group('UpdateAutoScalingGroup', self)
 
157
 
 
158
    def shutdown_instances(self):
 
159
        """ Convenience method which shuts down all instances associated with
 
160
        this group.
 
161
        """
 
162
        self.min_size = 0
 
163
        self.max_size = 0
 
164
        self.update()
 
165
 
 
166
    def get_all_triggers(self):
 
167
        """ Get all triggers for this auto scaling group. """
 
168
        params = {'AutoScalingGroupName' : self.name}
 
169
        triggers = self.connection.get_list('DescribeTriggers', params,
 
170
                                            [('member', Trigger)])
 
171
 
 
172
        # allow triggers to be able to access the autoscale group
 
173
        for tr in triggers:
 
174
            tr.autoscale_group = weakref.proxy(self)
 
175
 
 
176
        return triggers
 
177
 
 
178
    def delete(self):
 
179
        """ Delete this auto-scaling group. """
 
180
        params = {'AutoScalingGroupName' : self.name}
 
181
        return self.connection.get_object('DeleteAutoScalingGroup', params,
 
182
                                          Request)
 
183
 
 
184
    def get_activities(self, activity_ids=None, max_records=100):
 
185
        """
 
186
        Get all activies for this group.
 
187
        """
 
188
        return self.connection.get_all_activities(self, activity_ids, max_records)
 
189