Add multi-region support to Autoscaling
This commit is contained in:
parent
82eef28937
commit
7e69c69ae0
4 changed files with 143 additions and 28 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
|
||||
from moto.core import BaseBackend
|
||||
from moto.ec2 import ec2_backend
|
||||
from moto.ec2 import ec2_backends
|
||||
|
||||
# http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/AS_Concepts.html#Cooldown
|
||||
DEFAULT_COOLDOWN = 300
|
||||
|
|
@ -8,7 +8,7 @@ DEFAULT_COOLDOWN = 300
|
|||
|
||||
class FakeScalingPolicy(object):
|
||||
def __init__(self, name, adjustment_type, as_name, scaling_adjustment,
|
||||
cooldown):
|
||||
cooldown, autoscaling_backend):
|
||||
self.name = name
|
||||
self.adjustment_type = adjustment_type
|
||||
self.as_name = as_name
|
||||
|
|
@ -17,14 +17,15 @@ class FakeScalingPolicy(object):
|
|||
self.cooldown = cooldown
|
||||
else:
|
||||
self.cooldown = DEFAULT_COOLDOWN
|
||||
self.autoscaling_backend = autoscaling_backend
|
||||
|
||||
def execute(self):
|
||||
if self.adjustment_type == 'ExactCapacity':
|
||||
autoscaling_backend.set_desired_capacity(self.as_name, self.scaling_adjustment)
|
||||
self.autoscaling_backend.set_desired_capacity(self.as_name, self.scaling_adjustment)
|
||||
elif self.adjustment_type == 'ChangeInCapacity':
|
||||
autoscaling_backend.change_capacity(self.as_name, self.scaling_adjustment)
|
||||
self.autoscaling_backend.change_capacity(self.as_name, self.scaling_adjustment)
|
||||
elif self.adjustment_type == 'PercentChangeInCapacity':
|
||||
autoscaling_backend.change_capacity_percent(self.as_name, self.scaling_adjustment)
|
||||
self.autoscaling_backend.change_capacity_percent(self.as_name, self.scaling_adjustment)
|
||||
|
||||
|
||||
class FakeLaunchConfiguration(object):
|
||||
|
|
@ -104,7 +105,7 @@ class FakeAutoScalingGroup(object):
|
|||
def __init__(self, name, availability_zones, desired_capacity, max_size,
|
||||
min_size, launch_config_name, vpc_zone_identifier,
|
||||
default_cooldown, health_check_period, health_check_type,
|
||||
load_balancers, placement_group, termination_policies):
|
||||
load_balancers, placement_group, termination_policies, autoscaling_backend):
|
||||
self.name = name
|
||||
self.availability_zones = availability_zones
|
||||
self.max_size = max_size
|
||||
|
|
@ -124,6 +125,8 @@ class FakeAutoScalingGroup(object):
|
|||
self.instances = []
|
||||
self.set_desired_capacity(desired_capacity)
|
||||
|
||||
self.autoscaling_backend = autoscaling_backend
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json):
|
||||
properties = cloudformation_json['Properties']
|
||||
|
|
@ -182,7 +185,7 @@ class FakeAutoScalingGroup(object):
|
|||
if self.desired_capacity > curr_instance_count:
|
||||
# Need more instances
|
||||
count_needed = self.desired_capacity - curr_instance_count
|
||||
reservation = ec2_backend.add_instances(
|
||||
reservation = autoscaling_backend.ec2_backend.add_instances(
|
||||
self.launch_config.image_id,
|
||||
count_needed,
|
||||
self.launch_config.user_data,
|
||||
|
|
@ -196,16 +199,22 @@ class FakeAutoScalingGroup(object):
|
|||
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]
|
||||
ec2_backend.terminate_instances(instance_ids_to_remove)
|
||||
autoscaling_backend.ec2_backend.terminate_instances(instance_ids_to_remove)
|
||||
self.instances = self.instances[count_to_remove:]
|
||||
|
||||
|
||||
class AutoScalingBackend(BaseBackend):
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, ec2_backend):
|
||||
self.autoscaling_groups = {}
|
||||
self.launch_configurations = {}
|
||||
self.policies = {}
|
||||
self.ec2_backend = ec2_backend
|
||||
|
||||
def reset(self):
|
||||
ec2_backend = self.ec2_backend
|
||||
self.__dict__ = {}
|
||||
self.__init__(ec2_backend)
|
||||
|
||||
def create_launch_configuration(self, name, image_id, key_name,
|
||||
security_groups, user_data, instance_type,
|
||||
|
|
@ -267,6 +276,7 @@ class AutoScalingBackend(BaseBackend):
|
|||
load_balancers=load_balancers,
|
||||
placement_group=placement_group,
|
||||
termination_policies=termination_policies,
|
||||
autoscaling_backend=self,
|
||||
)
|
||||
self.autoscaling_groups[name] = group
|
||||
return group
|
||||
|
|
@ -328,7 +338,7 @@ class AutoScalingBackend(BaseBackend):
|
|||
def create_autoscaling_policy(self, name, adjustment_type, as_name,
|
||||
scaling_adjustment, cooldown):
|
||||
policy = FakeScalingPolicy(name, adjustment_type, as_name,
|
||||
scaling_adjustment, cooldown)
|
||||
scaling_adjustment, cooldown, self)
|
||||
|
||||
self.policies[name] = policy
|
||||
return policy
|
||||
|
|
@ -343,4 +353,9 @@ class AutoScalingBackend(BaseBackend):
|
|||
policy = self.policies[group_name]
|
||||
policy.execute()
|
||||
|
||||
autoscaling_backend = AutoScalingBackend()
|
||||
|
||||
autoscaling_backends = {}
|
||||
for region, ec2_backend in ec2_backends.items():
|
||||
autoscaling_backends[region] = AutoScalingBackend(ec2_backend)
|
||||
|
||||
autoscaling_backend = autoscaling_backends['us-east-1']
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue