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