1
1
# Copyright (c) 2009-2011 Reza Lotun http://reza.lotun.name/
2
2
# Copyright (c) 2011 Jann Kleen
3
# Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
4
# Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved
4
6
# Permission is hereby granted, free of charge, to any person obtaining a
5
7
# copy of this software and associated documentation files (the
32
34
from boto.ec2.regioninfo import RegionInfo
33
35
from boto.ec2.autoscale.request import Request
34
36
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
35
from boto.ec2.autoscale.group import AutoScalingGroup, ProcessType
37
from boto.ec2.autoscale.group import AutoScalingGroup
38
from boto.ec2.autoscale.group import ProcessType
36
39
from boto.ec2.autoscale.activity import Activity
37
from boto.ec2.autoscale.policy import AdjustmentType, MetricCollectionTypes, ScalingPolicy
40
from boto.ec2.autoscale.policy import AdjustmentType
41
from boto.ec2.autoscale.policy import MetricCollectionTypes
42
from boto.ec2.autoscale.policy import ScalingPolicy
43
from boto.ec2.autoscale.policy import TerminationPolicies
38
44
from boto.ec2.autoscale.instance import Instance
39
45
from boto.ec2.autoscale.scheduled import ScheduledUpdateGroupAction
40
46
from boto.ec2.autoscale.tag import Tag
46
52
'sa-east-1': 'autoscaling.sa-east-1.amazonaws.com',
47
53
'eu-west-1': 'autoscaling.eu-west-1.amazonaws.com',
48
54
'ap-northeast-1': 'autoscaling.ap-northeast-1.amazonaws.com',
49
'ap-southeast-1': 'autoscaling.ap-southeast-1.amazonaws.com'}
55
'ap-southeast-1': 'autoscaling.ap-southeast-1.amazonaws.com',
56
'ap-southeast-2': 'autoscaling.ap-southeast-2.amazonaws.com',
79
89
return region.connect(**kw_params)
82
93
class AutoScaleConnection(AWSQueryConnection):
83
94
APIVersion = boto.config.get('Boto', 'autoscale_version', '2011-01-01')
84
95
DefaultRegionEndpoint = boto.config.get('Boto', 'autoscale_endpoint',
85
'autoscaling.amazonaws.com')
86
DefaultRegionName = boto.config.get('Boto', 'autoscale_region_name',
96
'autoscaling.us-east-1.amazonaws.com')
97
DefaultRegionName = boto.config.get('Boto', 'autoscale_region_name',
89
100
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
90
101
is_secure=True, port=None, proxy=None, proxy_port=None,
91
102
proxy_user=None, proxy_pass=None, debug=0,
92
https_connection_factory=None, region=None, path='/'):
103
https_connection_factory=None, region=None, path='/',
104
security_token=None, validate_certs=True):
94
106
Init method to create a new connection to the AutoScaling service.
106
118
is_secure, port, proxy, proxy_port,
107
119
proxy_user, proxy_pass,
108
120
self.region.endpoint, debug,
109
https_connection_factory, path=path)
121
https_connection_factory, path=path,
122
security_token=security_token,
123
validate_certs=validate_certs)
111
125
def _required_auth_capability(self):
114
128
def build_list_params(self, params, items, label):
129
143
['us-east-1b',...]
131
145
# different from EC2 list params
132
for i in xrange(1, len(items)+1):
133
if isinstance(items[i-1], dict):
134
for k, v in items[i-1].iteritems():
146
for i in xrange(1, len(items) + 1):
147
if isinstance(items[i - 1], dict):
148
for k, v in items[i - 1].iteritems():
135
149
if isinstance(v, dict):
136
150
for kk, vv in v.iteritems():
137
151
params['%s.member.%d.%s.%s' % (label, i, k, kk)] = vv
139
153
params['%s.member.%d.%s' % (label, i, k)] = v
140
elif isinstance(items[i-1], basestring):
141
params['%s.member.%d' % (label, i)] = items[i-1]
154
elif isinstance(items[i - 1], basestring):
155
params['%s.member.%d' % (label, i)] = items[i - 1]
143
157
def _update_group(self, op, as_group):
144
158
params = {'AutoScalingGroupName': as_group.name,
160
174
params['DefaultCooldown'] = as_group.default_cooldown
161
175
if as_group.placement_group:
162
176
params['PlacementGroup'] = as_group.placement_group
177
if as_group.termination_policies:
178
self.build_list_params(params, as_group.termination_policies,
179
'TerminationPolicies')
163
180
if op.startswith('Create'):
164
181
# you can only associate load balancers with an autoscale
165
182
# group at creation time
166
183
if as_group.load_balancers:
167
184
self.build_list_params(params, as_group.load_balancers,
168
185
'LoadBalancerNames')
187
for i, tag in enumerate(as_group.tags):
188
tag.build_params(params, i + 1)
169
189
return self.get_object(op, params, Request)
171
191
def create_auto_scaling_group(self, as_group):
213
233
params['InstanceMonitoring.Enabled'] = 'true'
215
235
params['InstanceMonitoring.Enabled'] = 'false'
236
if launch_config.spot_price is not None:
237
params['SpotPrice'] = str(launch_config.spot_price)
238
if launch_config.instance_profile_name is not None:
239
params['IamInstanceProfile'] = launch_config.instance_profile_name
216
240
return self.get_object('CreateLaunchConfiguration', params,
217
241
Request, verb='POST')
333
357
name = autoscale_group
334
358
if isinstance(autoscale_group, AutoScalingGroup):
335
359
name = autoscale_group.name
336
params = {'AutoScalingGroupName' : name}
360
params = {'AutoScalingGroupName': name}
338
362
params['MaxRecords'] = max_records
343
367
return self.get_list('DescribeScalingActivities',
344
368
params, [('member', Activity)])
370
def get_termination_policies(self):
371
"""Gets all valid termination policies.
373
These values can then be used as the termination_policies arg
374
when creating and updating autoscale groups.
376
return self.get_object('DescribeTerminationPolicyTypes',
377
{}, TerminationPolicies)
346
379
def delete_scheduled_action(self, scheduled_action_name,
347
380
autoscale_group=None):
488
521
:param as_group: The auto scaling group to suspend processes on.
490
523
:type scaling_processes: list
491
:param scaling_processes: Processes you want to suspend. If omitted, all
492
processes will be suspended.
524
:param scaling_processes: Processes you want to suspend. If omitted,
525
all processes will be suspended.
494
527
params = {'AutoScalingGroupName': as_group}
495
528
if scaling_processes:
496
self.build_list_params(params, scaling_processes, 'ScalingProcesses')
529
self.build_list_params(params, scaling_processes,
497
531
return self.get_status('SuspendProcesses', params)
499
533
def resume_processes(self, as_group, scaling_processes=None):
510
544
params = {'AutoScalingGroupName': as_group}
512
546
if scaling_processes:
513
self.build_list_params(params, scaling_processes, 'ScalingProcesses')
547
self.build_list_params(params, scaling_processes,
514
549
return self.get_status('ResumeProcesses', params)
516
551
def create_scheduled_group_action(self, as_group, name, time,
614
649
params['HonorCooldown'] = honor_cooldown
615
650
return self.get_status('ExecutePolicy', params)
652
def put_notification_configuration(self, autoscale_group, topic, notification_types):
654
Configures an Auto Scaling group to send notifications when
655
specified events take place.
657
:type as_group: str or
658
:class:`boto.ec2.autoscale.group.AutoScalingGroup` object
659
:param as_group: The Auto Scaling group to put notification
663
:param topic: The Amazon Resource Name (ARN) of the Amazon Simple
664
Notification Service (SNS) topic.
666
:type notification_types: list
667
:param notification_types: The type of events that will trigger
671
name = autoscale_group
672
if isinstance(autoscale_group, AutoScalingGroup):
673
name = autoscale_group.name
675
params = {'AutoScalingGroupName': name,
677
self.build_list_params(params, notification_types, 'NotificationTypes')
678
return self.get_status('PutNotificationConfiguration', params)
617
680
def set_instance_health(self, instance_id, health_status,
618
681
should_respect_grace_period=True):
640
703
params['ShouldRespectGracePeriod'] = 'false'
641
704
return self.get_status('SetInstanceHealth', params)
706
def set_desired_capacity(self, group_name, desired_capacity, honor_cooldown=False):
708
Adjusts the desired size of the AutoScalingGroup by initiating scaling
709
activities. When reducing the size of the group, it is not possible to define
710
which Amazon EC2 instances will be terminated. This applies to any Auto Scaling
711
decisions that might result in terminating instances.
713
:type group_name: string
714
:param group_name: name of the auto scaling group
716
:type desired_capacity: integer
717
:param desired_capacity: new capacity setting for auto scaling group
719
:type honor_cooldown: boolean
720
:param honor_cooldown: by default, overrides any cooldown period
722
params = {'AutoScalingGroupName': group_name,
723
'DesiredCapacity': desired_capacity}
725
params['HonorCooldown'] = json.dumps('True')
727
return self.get_status('SetDesiredCapacity', params)
645
731
def get_all_tags(self, filters=None, max_records=None, next_token=None):
647
733
Lists the Auto Scaling group tags.
649
This action supports pagination by returning a token if there are more
650
pages to retrieve. To get the next page, call this action again with the returned token as the NextToken parameter.
735
This action supports pagination by returning a token if there
736
are more pages to retrieve. To get the next page, call this
737
action again with the returned token as the NextToken
652
740
:type filters: dict
653
741
:param filters: The value of the filter type used to identify
679
767
for i, tag in enumerate(tags):
680
tag.build_params(params, i+1)
768
tag.build_params(params, i + 1)
681
769
return self.get_status('CreateOrUpdateTags', params, verb='POST')
683
771
def delete_tags(self, tags):
691
779
for i, tag in enumerate(tags):
692
tag.build_params(params, i+1)
780
tag.build_params(params, i + 1)
693
781
return self.get_status('DeleteTags', params, verb='POST')