1
# Copyright (c) 2009 Reza Lotun http://reza.lotun.name/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
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
29
class Instance(object):
30
def __init__(self, connection=None):
31
self.connection = connection
35
return 'Instance:%s' % self.instance_id
37
def startElement(self, name, attrs, connection):
40
def endElement(self, name, value, connection):
41
if name == 'InstanceId':
42
self.instance_id = value
44
setattr(self, name, value)
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):
54
Creates a new AutoScalingGroup with the specified name.
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
62
:param name: Name of autoscaling group.
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
70
:type availability_zone: list
71
:param availability_zone: List of availability zones.
73
:type launch_config: str
74
:param launch_config: Name of launch configuration name.
76
:type load_balancers: list
77
:param load_balancers: List of load balancers.
80
:param minsize: Minimum size of group
83
:param maxsize: Maximum size of group
86
:param cooldown: Amount of time after a Scaling Activity completes
87
before any further scaling activities can start.
90
:return: Updated healthcheck for the instances.
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
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
112
return 'AutoScalingGroup:%s' % self.name
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
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':
141
setattr(self, name, value)
143
def set_capacity(self, capacity):
144
""" Set the desired capacity for the group. """
146
'AutoScalingGroupName' : self.name,
147
'DesiredCapacity' : capacity,
149
req = self.connection.get_object('SetDesiredCapacity', params,
151
self.connection.last_request = req
155
""" Sync local changes with AutoScaling group. """
156
return self.connection._update_group('UpdateAutoScalingGroup', self)
158
def shutdown_instances(self):
159
""" Convenience method which shuts down all instances associated with
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)])
172
# allow triggers to be able to access the autoscale group
174
tr.autoscale_group = weakref.proxy(self)
179
""" Delete this auto-scaling group. """
180
params = {'AutoScalingGroupName' : self.name}
181
return self.connection.get_object('DeleteAutoScalingGroup', params,
184
def get_activities(self, activity_ids=None, max_records=100):
186
Get all activies for this group.
188
return self.connection.get_all_activities(self, activity_ids, max_records)