Skip to content

Commit

Permalink
Template in the ASG instance lifecycle states.
Browse files Browse the repository at this point in the history
  • Loading branch information
spulec committed Jul 13, 2015
1 parent 4db1412 commit c45968b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
24 changes: 15 additions & 9 deletions moto/autoscaling/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
DEFAULT_COOLDOWN = 300


class InstanceState(object):
def __init__(self, instance, lifecycle_state="InService"):
self.instance = instance
self.lifecycle_state = lifecycle_state


class FakeScalingPolicy(object):
def __init__(self, name, adjustment_type, as_name, scaling_adjustment,
cooldown, autoscaling_backend):
Expand Down Expand Up @@ -125,7 +131,7 @@ def __init__(self, name, availability_zones, desired_capacity, max_size,
self.placement_group = placement_group
self.termination_policies = termination_policies

self.instances = []
self.instance_states = []
self.set_desired_capacity(desired_capacity)

@classmethod
Expand Down Expand Up @@ -179,7 +185,7 @@ def set_desired_capacity(self, new_capacity):
else:
self.desired_capacity = new_capacity

curr_instance_count = len(self.instances)
curr_instance_count = len(self.instance_states)

if self.desired_capacity == curr_instance_count:
return
Expand All @@ -195,14 +201,14 @@ def set_desired_capacity(self, new_capacity):
)
for instance in reservation.instances:
instance.autoscaling_group = self
self.instances.extend(reservation.instances)
self.instance_states.append(InstanceState(instance))
else:
# Need to remove some instances
count_to_remove = curr_instance_count - self.desired_capacity
instances_to_remove = self.instances[:count_to_remove]
instance_ids_to_remove = [instance.id for instance in instances_to_remove]
instances_to_remove = self.instance_states[:count_to_remove]
instance_ids_to_remove = [instance.instance.id for instance in instances_to_remove]
self.autoscaling_backend.ec2_backend.terminate_instances(instance_ids_to_remove)
self.instances = self.instances[count_to_remove:]
self.instance_states = self.instance_states[count_to_remove:]


class AutoScalingBackend(BaseBackend):
Expand Down Expand Up @@ -307,10 +313,10 @@ def delete_autoscaling_group(self, group_name):
self.autoscaling_groups.pop(group_name, None)

def describe_autoscaling_instances(self):
instances = []
instance_states = []
for group in self.autoscaling_groups.values():
instances.extend(group.instances)
return instances
instance_states.extend(group.instance_states)
return instance_states

def set_desired_capacity(self, group_name, desired_capacity):
group = self.autoscaling_groups[group_name]
Expand Down
22 changes: 11 additions & 11 deletions moto/autoscaling/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def set_desired_capacity(self):
return template.render()

def describe_auto_scaling_instances(self):
instances = self.autoscaling_backend.describe_autoscaling_instances()
instance_states = self.autoscaling_backend.describe_autoscaling_instances()
template = self.response_template(DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE)
return template.render(instances=instances)
return template.render(instance_states=instance_states)

def put_scaling_policy(self):
policy = self.autoscaling_backend.create_autoscaling_policy(
Expand Down Expand Up @@ -243,13 +243,13 @@ def execute_policy(self):
<EnabledMetrics/>
<LaunchConfigurationName>{{ group.launch_config_name }}</LaunchConfigurationName>
<Instances>
{% for instance in group.instances %}
{% for instance_state in group.instance_states %}
<member>
<HealthStatus>HEALTHY</HealthStatus>
<AvailabilityZone>us-east-1e</AvailabilityZone>
<InstanceId>{{ instance.id }}</InstanceId>
<LaunchConfigurationName>{{ instance.autoscaling_group.launch_config_name }}</LaunchConfigurationName>
<LifecycleState>InService</LifecycleState>
<InstanceId>{{ instance_state.instance.id }}</InstanceId>
<LaunchConfigurationName>{{ group.launch_config_name }}</LaunchConfigurationName>
<LifecycleState>{{ instance_state.lifecycle_state }}</LifecycleState>
</member>
{% endfor %}
</Instances>
Expand Down Expand Up @@ -315,14 +315,14 @@ def execute_policy(self):
DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE = """<DescribeAutoScalingInstancesResponse xmlns="http://autoscaling.amazonaws.com/doc/2011-01-01/">
<DescribeAutoScalingInstancesResult>
<AutoScalingInstances>
{% for instance in instances %}
{% for instance_state in instance_states %}
<member>
<HealthStatus>HEALTHY</HealthStatus>
<AutoScalingGroupName>{{ instance.autoscaling_group.name }}</AutoScalingGroupName>
<AutoScalingGroupName>{{ instance_state.instance.autoscaling_group.name }}</AutoScalingGroupName>
<AvailabilityZone>us-east-1e</AvailabilityZone>
<InstanceId>{{ instance.id }}</InstanceId>
<LaunchConfigurationName>{{ instance.autoscaling_group.launch_config_name }}</LaunchConfigurationName>
<LifecycleState>InService</LifecycleState>
<InstanceId>{{ instance_state.instance.id }}</InstanceId>
<LaunchConfigurationName>{{ instance_state.instance.autoscaling_group.launch_config_name }}</LaunchConfigurationName>
<LifecycleState>{{ instance_state.lifecycle_state }}</LifecycleState>
</member>
{% endfor %}
</AutoScalingInstances>
Expand Down

0 comments on commit c45968b

Please sign in to comment.