Skip to content
This repository has been archived by the owner on Feb 14, 2018. It is now read-only.

Commit

Permalink
WIP commit for autoscaling groups
Browse files Browse the repository at this point in the history
Can create autoscaling groups, no policies yet
  • Loading branch information
gmr committed Sep 5, 2015
1 parent 87c9399 commit 8295529
Showing 1 changed file with 97 additions and 5 deletions.
102 changes: 97 additions & 5 deletions formulary/builders/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from formulary.builders import base

from formulary.builders import autoscaling
from formulary.resources import cloudformation
from formulary.builders import ec2
from formulary.resources import ec2 as ec2_resources
Expand All @@ -30,6 +31,7 @@ def __init__(self, config, name, amis, local_path, environment_stack,
super(Service, self).__init__(config, name)

self._amis = amis
self._elbs = []
self._instances = []
self._local_path = local_path
self._mappings = config.mappings
Expand All @@ -43,7 +45,8 @@ def __init__(self, config, name, amis, local_path, environment_stack,
self._security_group = self._add_security_group()
self._maybe_add_security_group_ingress()
self._add_instances()
self._maybe_add_elbs()
#if not self._elbs:
# self._maybe_add_elbs()
self._maybe_add_route53_record_sets()
self._add_tag_to_resources('Environment', self._config.environment)
self._add_tag_to_resources('Service', self._name)
Expand All @@ -61,6 +64,48 @@ def _add_autobalanced_instances(self, settings):
if not wait:
wait = self._maybe_add_wait_condition(index, handle, ref_id)

def _add_autoscaling_group(self, config):
LOGGER.debug('Adding autoscaling group')
self._maybe_add_elbs()
name = '{0}-{1}'.format(self._parent,
self._name) if self._parent else self._name
launch_config = self._add_launch_config(config)
availability_zones, subnets = [], []
for subnet in self._environment_stack.subnets:
availability_zones.append(subnet.availability_zone)
subnets.append(subnet.id)

elbs = ['{0}-{1}'.format(self.environment, elb_name)
for elb_name in self._elbs]
settings = config.get('autoscaling', {})
health = settings.get('health_check', {})
tags = []
for key in self._tags:
tags.append({'Key': key,
'Value': self._tags[key],
'PropagateAtLaunch': 'true'})

as_group = autoscaling.AutoScalingGroup(self._config, name,
availability_zones,
str(settings.get('cooldown')),
health.get('grace_period'),
health.get('type'),
None,
{'Ref': 'LaunchConfig'},
elbs or None,
str(settings.get('min')),
str(settings.get('max')),
tags=tags,
vpc_zones=subnets)

as_group.add_parameter('LaunchConfig', {'Type': 'String'})
params = {'LaunchConfig':
{'Fn::GetAtt': [launch_config, 'Outputs.LaunchConfig']}}

template_id, url = as_group.upload(self._name)
self._add_stack(name, url, params)
return utils.camel_case(name)

# Add same-az instances in a method matching auto-balanced ones

def _add_elb(self, name, config):
Expand All @@ -83,7 +128,9 @@ def _add_elb(self, name, config):

template_id, url = builder.upload(self._name)
self._add_stack(name, url, parameters)
self._maybe_add_route53_alias(config, utils.camel_case(name))
ref_name = utils.camel_case(name)
self._maybe_add_route53_alias(config, ref_name)
self._elbs.append(name)

def _add_instance(self, name, subnet, config,
wait_handle=None, dependency=None):
Expand Down Expand Up @@ -149,6 +196,7 @@ def _add_instance(self, name, subnet, config,

def _add_instances(self):
settings = self._config.settings
strategy = settings.get('instance-strategy')
if 'instances' in settings:
LOGGER.debug('Adding instances')
for name, instance_cfg in settings['instances'].items():
Expand All @@ -161,18 +209,62 @@ def _add_instances(self):
self._add_instance(name,
self._get_subnet(cfg['availability_zone']),
cfg)
elif settings.get('instance-strategy') == 'same-az':
elif strategy == 'same-az':
self._maybe_add_availability_zone(settings)
subnet = self._get_subnet(settings['availability_zone'])
for index in range(0, settings.get('instance-count', 1)):
self._add_instance('instance{0}'.format(index),
subnet, settings)
elif settings.get('instance-strategy') == 'az-balanced':
elif strategy == 'az-balanced':
return self._add_autobalanced_instances(settings)
elif 'instance-strategy' in settings:

elif strategy == 'autoscaling':
return self._add_autoscaling_group(self._config.settings)
elif strategy:
raise ValueError('Unknown instance-strategy: '
'{0}'.format(settings['instance-strategy']))

def _add_launch_config(self, config):
LOGGER.debug('Adding launch configuration')
name = 'launch-config-{0}'.format(self._name)
if self._parent:
name = '{0}-{1}'.format(self._parent, name)
stack_name = self._parent or self._environment_stack.name
key_pair = self._config.mappings.get('AWS',
{}).get('KeyName',
{}).get('Value')
lc = autoscaling.LaunchConfiguration(self._config,
name,
self._get_ami_id(),
None,
config.get('instance-type'),
key_pair,
config.get('public-ips', True),
{'Ref': 'SecurityGroupId'},
self._read_user_data(),
config.get('ebs'),
self._get_render_metadata(),
config.get('monitoring'),
stack_name)

lc.add_parameter('SecurityGroupId',
{'Type': 'String',
'Description': 'Security Group Physical ID'})

parameters = {'SecurityGroupId':
{'Fn::GetAtt': [self._security_group,
'Outputs.SecurityGroupId']}}

template_id, url = lc.upload(self._name)
self._add_stack(name, url, parameters)
return utils.camel_case(name)

def _add_scaling_policy(self, settings):
name = 'sp-{0}'.format(self._name)
if self._parent:
name = '{0}-{1}'.format(self._parent, name)
# spolicy = autoscaling.ScalingPolicy(self._config, name,)

def _add_security_group(self):
LOGGER.debug('Adding security group')
name = '{0}-security-group'.format(self._config.service)
Expand Down

0 comments on commit 8295529

Please sign in to comment.