summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrlotun <rlotun@gmail.com>2010-12-22 15:33:40 +0000
committerrlotun <rlotun@gmail.com>2010-12-22 15:33:40 +0000
commit628509c756a47c66a35555322f460792b7c9148e (patch)
treeb053f7f69de5b4dcee33b18633ee4b4dcec5c328
parent7deb3962cfdde13ef9a10b8a16db301d88c9b7a9 (diff)
downloadboto-628509c756a47c66a35555322f460792b7c9148e.tar.gz
More work on implementing autoscale calls.
Added support for getting and setting policies and metric collection types as well as a few bugfixes.
-rw-r--r--boto/ec2/autoscale/__init__.py173
-rw-r--r--boto/ec2/autoscale/group.py17
-rw-r--r--boto/ec2/autoscale/instance.py14
-rw-r--r--boto/ec2/autoscale/launchconfig.py4
-rw-r--r--boto/ec2/autoscale/policy.py59
5 files changed, 248 insertions, 19 deletions
diff --git a/boto/ec2/autoscale/__init__.py b/boto/ec2/autoscale/__init__.py
index 3558d8ac..c4510efb 100644
--- a/boto/ec2/autoscale/__init__.py
+++ b/boto/ec2/autoscale/__init__.py
@@ -31,6 +31,8 @@ from boto.ec2.autoscale.request import Request
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
from boto.ec2.autoscale.group import AutoScalingGroup
from boto.ec2.autoscale.activity import Activity
+from boto.ec2.autoscale.policy import AdjustmentTypes, MetricCollectionTypes, ScalingPolicy
+from boto.ec2.autoscale.instance import Instance
class AutoScaleConnection(AWSQueryConnection):
@@ -115,6 +117,15 @@ class AutoScaleConnection(AWSQueryConnection):
"""
return self._update_group('CreateAutoScalingGroup', as_group)
+ def delete_auto_scaling_group(self, name):
+ """
+ Deletes the specified auto scaling group if the group has no instances
+ and no scaling activities in progress.
+ """
+ params = {'AutoScalingGroupName' : name}
+ return self.connection.get_object('DeleteAutoScalingGroup', params,
+ Request)
+
def create_launch_configuration(self, launch_config):
"""
Creates a new Launch Configuration.
@@ -148,6 +159,18 @@ class AutoScaleConnection(AWSQueryConnection):
return self.get_object('CreateLaunchConfiguration', params,
Request)
+ def delete_launch_configuration(self, launch_config_name):
+ """
+ Deletes the specified LaunchConfiguration.
+
+ The specified launch configuration must not be attached to an Auto
+ Scaling group. Once this call completes, the launch configuration is no
+ longer available for use.
+ """
+ params = {'LaunchConfigurationName' : launch_config_name}
+ return self.connection.get_object('DeleteLaunchConfiguration', params,
+ Request)
+
def get_all_groups(self, names=None, max_records=None, next_token=None):
"""
Returns a full description of each Auto Scaling group in the given
@@ -187,7 +210,7 @@ class AutoScaleConnection(AWSQueryConnection):
return self.get_list('DescribeLaunchConfigurations', params,
[('member', LaunchConfiguration)])
- def get_all_activities(self, autoscale_group, activity_ids=None, max_records=50, next_token=None):
+ def get_all_activities(self, autoscale_group, activity_ids=None, max_records=None, next_token=None):
"""
Get all activities for the given autoscaling group.
@@ -207,8 +230,7 @@ class AutoScaleConnection(AWSQueryConnection):
params['NextToken'] = next_token
if activity_ids:
self.build_list_params(params, activity_ids, 'ActivityIds')
- return self.get_list('DescribeScalingActivities', params,
- [('member', Activity)])
+ return self.get_list('DescribeScalingActivities', params, [('member', Activity)])
def get_all_scheduled_actions(self, autoscale_group=None, scheduled_actions=None,
start_time=None, end_time=None, max_records=None, next_token=None):
@@ -224,6 +246,14 @@ class AutoScaleConnection(AWSQueryConnection):
if next_token:
params['NextToken'] = next_token
+ def delete_scheduled_action(self, scheduled_action_name, autoscale_group=None):
+ params = {
+ 'ScheduledActionName' : scheduled_action_name,
+ }
+ if autoscale_group:
+ params['AutoScalingGroupName'] = autoscale_group
+ return self.get_status('DeleteScheduledAction', params)
+
def terminate_instance(self, instance_id, decrement_capacity=True):
params = {
'InstanceId' : instance_id,
@@ -232,3 +262,140 @@ class AutoScaleConnection(AWSQueryConnection):
return self.get_object('TerminateInstanceInAutoScalingGroup', params,
Activity)
+ def delete_policy(self, policy_name, autoscale_group=None):
+ params = {
+ 'PolicyName' : policy_name,
+ }
+ if autoscale_group:
+ params['AutoScalingGroupName'] = autoscale_group
+ return self.get_status('DeletePolicy', params)
+
+ def get_all_adjustment_types(self):
+ return self.get_list('DescribeAdjustmentTypes', {}, [('member', AdjustmentTypes)])
+
+ def get_all_autoscaling_instances(self, instance_ids=None, max_records=None, next_token=None):
+ """
+ Returns a description of each Auto Scaling instance in the InstanceIds
+ list. If a list is not provided, the service returns the full details
+ of all instances up to a maximum of fifty.
+
+ This action supports pagination by returning a token if there are more
+ pages to retrieve. To get the next page, call this action again with
+ the returned token as the NextToken parameter.
+ """
+ params = {}
+ if instance_ids:
+ self.build_list_params(params, instance_ids, 'InstanceIds')
+ if max_records:
+ params['MaxRecords'] = max_records
+ if next_token:
+ params['NextToken'] = next_token
+ return self.get_object('DescribeAutoscalingInstances', params, Instance)
+
+ def get_all_metric_collection_types(self):
+ """ Returns a list of metrics and a corresponding list of granularities
+ for each metric.
+ """
+ return self.get_object('DescribeMetricCollectionTypes', {}, MetricCollectionTypes)
+
+ def get_all_policies(self, as_group=None, policy_names=None, max_records=None, next_token=None):
+ """
+ Returns descriptions of what each policy does. This action supports
+ pagination. If the response includes a token, there are more records
+ available. To get the additional records, repeat the request with the
+ response token as the NextToken parameter.
+ """
+ params = {}
+ if as_group:
+ params['AutoScalingGroupName'] = as_group
+ if policy_names:
+ self.build_list_params(params, policy_names, 'PolicyNames')
+ if max_records:
+ params['MaxRecords'] = max_records
+ if next_token:
+ params['NextToken'] = next_token
+ return self.get_list('DescribePolicies', params, [('member', ScalingPolicy)])
+
+ def get_all_scaling_process_types(self):
+ # XXX
+ return self.get_object('DescribeScalingProcessTypes', {}, {})
+
+ def get_all_scheduled_actions(self, as_group=None, start_time=None, end_time=None, scheduled_actions=None,
+ max_records=None, next_token=None):
+ params = {}
+ if as_group:
+ params['AutoScalingGroupName'] = as_group
+ if scheduled_actions:
+ self.build_list_params(params, scheduled_actions, 'ScheduledActionNames')
+ if max_records:
+ params['MaxRecords'] = max_records
+ if next_token:
+ params['NextToken'] = next_token
+ #XXX
+ return self.get_object('DescribeScheduledActions', params, {})
+
+ def disable_metrics_collection(self, as_group, metrics=None):
+ """
+ Disables monitoring of group metrics for the Auto Scaling group
+ specified in AutoScalingGroupName. You can specify the list of affected
+ metrics with the Metrics parameter.
+ """
+ params = {
+ 'AutoScalingGroupName' : as_group,
+ }
+ if metrics:
+ self.build_list_params(params, metrics, 'Metrics')
+ return self.get_status('DisableMetricsCollection', params)
+
+ def enable_metrics_collection(self, as_group, granularity, metrics=None):
+ """
+ Enables monitoring of group metrics for the Auto Scaling group
+ specified in AutoScalingGroupName. You can specify the list of enabled
+ metrics with the Metrics parameter.
+
+ Auto scaling metrics collection can be turned on only if the
+ InstanceMonitoring.Enabled flag, in the Auto Scaling group's launch
+ configuration, is set to true.
+
+ :type autoscale_group: string
+ :param autoscale_group: The auto scaling group to get activities on.
+
+ :type granularity: string
+ :param granularity: The granularity to associate with the metrics to
+ collect. Currently, the only legal granularity is "1Minute".
+
+ :type metrics: string list
+ :param metrics: The list of metrics to collect. If no metrics are
+ specified, all metrics are enabled.
+ """
+ params = {
+ 'AutoScalingGroupName' : as_group,
+ 'Granularity' : granularity,
+ }
+ if metrics:
+ self.build_list_params(params, metrics, 'Metrics')
+ return self.get_status('EnableMetricsCollection', params)
+
+ def execute_policy(self, policy_name, as_group=None, honor_cooldown=None):
+ params = {
+ 'PolicyName' : policy_name,
+ }
+ if as_group:
+ params['AutoScalingGroupName'] = as_group
+ if honor_cooldown:
+ params['HonorCooldown'] = honor_cooldown
+ return self.get_status('ExecutePolicy', params)
+
+ def put_scaling_policy(self, policy_name, as_group, adjustment_type, scaling_adjustment, cooldown=None):
+ params = {
+ 'PolicyName' : policy_name,
+ 'AutoScalingGroupName' : as_group,
+ 'AdjustmentType' : adjustment_type,
+ 'ScalingAdjustment' : scaling_adjustment,
+ }
+ if cooldown:
+ params['Cooldown'] = cooldown
+ # XXX
+ return self.get_object('PutScalingPolicy', params, {})
+
+
diff --git a/boto/ec2/autoscale/group.py b/boto/ec2/autoscale/group.py
index 1a4f6b07..29d165c7 100644
--- a/boto/ec2/autoscale/group.py
+++ b/boto/ec2/autoscale/group.py
@@ -25,6 +25,7 @@ from boto.ec2.elb.listelement import ListElement
from boto.resultset import ResultSet
from boto.ec2.autoscale.request import Request
from boto.ec2.autoscale.instance import Instance
+from boto.ec2.autoscale.launchconfig import LaunchConfiguration
class SuspendedProcess(object):
@@ -68,7 +69,7 @@ class AutoScalingGroup(object):
def __init__(self, connection=None, name=None,
launch_config=None,
availability_zones=None,
- load_balancers=None, default_cooldown=0,
+ load_balancers=None, default_cooldown=None,
desired_capacity=None,
min_size=None, max_size=None):
"""
@@ -109,10 +110,10 @@ class AutoScalingGroup(object):
"""
self.name = name
self.connection = connection
- self.min_size = min_size
- self.max_size = max_size
+ self.min_size = int(min_size) if min_size is not None else None
+ self.max_size = int(max_size) if max_size is not None else None
self.created_time = None
- self.default_cooldown = default_cooldown
+ self.default_cooldown = int(default_cooldown) if default_cooldown is not None else None
self.launch_config = launch_config
if self.launch_config:
self.launch_config_name = self.launch_config.name
@@ -211,10 +212,10 @@ class AutoScalingGroup(object):
self.update()
def delete(self):
- """ Delete this auto-scaling group. """
- params = {'AutoScalingGroupName' : self.name}
- return self.connection.get_object('DeleteAutoScalingGroup', params,
- Request)
+ """ Delete this auto-scaling group if no instances attached or no
+ scaling activities in progress.
+ """
+ return self.connection.delete_auto_scaling_group(self.name)
def get_activities(self, activity_ids=None, max_records=50):
"""
diff --git a/boto/ec2/autoscale/instance.py b/boto/ec2/autoscale/instance.py
index 7b5ea8e0..6eb89c2c 100644
--- a/boto/ec2/autoscale/instance.py
+++ b/boto/ec2/autoscale/instance.py
@@ -28,11 +28,16 @@ class Instance(object):
self.launch_config_name = None
self.lifecycle_state = None
self.availability_zone = None
+ self.group_name = None
def __repr__(self):
- return 'Instance<%s>: state:%s, health:%s' % (self.instance_id,
- self.lifecycle_state,
- self.health_status)
+ r = 'Instance<id:%s, state:%s, health:%s' % (self.instance_id,
+ self.lifecycle_state,
+ self.health_status)
+ if self.group_name:
+ r += ' group:%s' % self.group_name
+ r += '>'
+ return r
def startElement(self, name, attrs, connection):
return None
@@ -48,7 +53,8 @@ class Instance(object):
self.lifecycle_state = value
elif name == 'AvailabilityZone':
self.availability_zone = value
+ elif name == 'AutoScalingGroupName':
+ self.group_name = value
else:
setattr(self, name, value)
-
diff --git a/boto/ec2/autoscale/launchconfig.py b/boto/ec2/autoscale/launchconfig.py
index c7f2143d..0c0c1cf3 100644
--- a/boto/ec2/autoscale/launchconfig.py
+++ b/boto/ec2/autoscale/launchconfig.py
@@ -160,7 +160,5 @@ class LaunchConfiguration(object):
def delete(self):
""" Delete this launch configuration. """
- params = {'LaunchConfigurationName' : self.name}
- return self.connection.get_object('DeleteLaunchConfiguration', params,
- Request)
+ return self.connection.delete_launch_configuration(self.name)
diff --git a/boto/ec2/autoscale/policy.py b/boto/ec2/autoscale/policy.py
index 4796bd8a..4095359c 100644
--- a/boto/ec2/autoscale/policy.py
+++ b/boto/ec2/autoscale/policy.py
@@ -46,6 +46,60 @@ class Alarm(object):
setattr(self, name, value)
+class AdjustmentTypes(object):
+ def __init__(self, connection=None):
+ self.connection = connection
+ self.adjustment_types = ListElement([])
+
+ def __repr__(self):
+ return 'AdjustmentTypes:%s' % self.adjustment_types
+
+ def startElement(self, name, attrs, connection):
+ if name == 'AdjustmentTypes':
+ return self.adjustment_types
+
+ def endElement(self, name, value, connection):
+ return
+
+
+class MetricCollectionTypes(object):
+ class BaseType(object):
+ arg = ''
+ def __init__(self, connection):
+ self.connection = connection
+ self.val = None
+ def __repr__(self):
+ return '%s:%s' % (self.arg, self.val)
+ def startElement(self, name, attrs, connection):
+ return
+ def endElement(self, name, value, connection):
+ if name == self.arg:
+ self.val = value
+ class Metric(BaseType):
+ arg = 'Metric'
+ class Granularity(BaseType):
+ arg = 'Granularity'
+
+ def __init__(self, connection=None):
+ self.connection = connection
+ self.metrics = []
+ self.granularities = []
+
+ def __repr__(self):
+ return 'MetricCollectionTypes:<%s, %s>' % (self.metrics, self.granularities)
+
+ def startElement(self, name, attrs, connection):
+ if name == 'Granularities':
+ self.granularities = ResultSet([('member', self.Granularity)])
+ return self.granularities
+ elif name == 'Metrics':
+ self.metrics = ResultSet([('member', self.Metric)])
+ return self.metrics
+
+ def endElement(self, name, value, connection):
+ return
+
+
class ScalingPolicy(object):
def __init__(self, connection=None, name=None):
"""
@@ -70,7 +124,7 @@ class ScalingPolicy(object):
def startElement(self, name, attrs, connection):
if name == 'Alarms':
- self.alrams = ResultSet([('member', Alarm)])
+ self.alarms = ResultSet([('member', Alarm)])
return self.alarms
def endElement(self, name, value, connection):
@@ -87,3 +141,6 @@ class ScalingPolicy(object):
elif name == 'AdjustmentType':
self.adjustment_type = int(value)
+ def delete(self, autoscale_group=None):
+ return self.connection.delete_policy(self.name, autoscale_group)
+