Skip to content

Commit

Permalink
Merge pull request #35 from brettswift/fix/alb_simpleweb_test
Browse files Browse the repository at this point in the history
Fix/alb simpleweb test
  • Loading branch information
brettswift committed Nov 7, 2018
2 parents 86a51bf + 9e4cd2f commit e76b8e9
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 61 deletions.
29 changes: 15 additions & 14 deletions cumulus/steps/ec2/alb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
from troposphere import elasticloadbalancingv2 as alb

CLUSTER_SG_NAME = "%sSG"
ALB_SG_NAME = "%sAlbSG"
ALB_NAME = "%sLoadBalancer"
TARGET_GROUP_DEFAULT = "%sTargetGroup"


class Alb(step.Step):

def __init__(self,
alb_security_group_name,
alb_security_group_ingress_name,
):
self.alb_security_group_name = alb_security_group_name
self.alb_security_group_ingress_name = alb_security_group_ingress_name
step.Step.__init__(self)

def handle(self, chain_context):
print(chain_context.instance_name)
self.create_conditions(chain_context.template)
self.create_security_groups(chain_context.template, chain_context.instance_name)
self.create_default_target_group(chain_context.template, chain_context.instance_name)
Expand All @@ -35,39 +37,34 @@ def create_conditions(self, template):
Not(Equals(Ref("ALBCertType"), "acm")))

def create_security_groups(self, template, instance_name):
alb_sg = ALB_SG_NAME % instance_name

# ALB Security group
template.add_resource(
ec2.SecurityGroup(
alb_sg,
GroupDescription=alb_sg,
self.alb_security_group_name,
GroupDescription=self.alb_security_group_name,
VpcId=Ref("VpcId")
))

template.add_output(
Output("InternalAlbSG", Value=Ref(alb_sg))
Output("InternalAlbSG", Value=Ref(self.alb_security_group_name))
)

# TODO: take a list of Cidr's
# Allow Internet to connect to ALB
template.add_resource(ec2.SecurityGroupIngress(
"LocalNetworkTo%sAlbPort443" % instance_name,
self.alb_security_group_ingress_name,
IpProtocol="tcp", FromPort="443", ToPort="443",
CidrIp="10.0.0.0/0",
GroupId=Ref(alb_sg),
GroupId=Ref(self.alb_security_group_name),
))

def create_load_balancer_alb(self, template, instance_name):
alb_name = ALB_NAME % instance_name
alb_sg = ALB_SG_NAME % instance_name

load_balancer = template.add_resource(alb.LoadBalancer(
alb_name,
# Name=alb_name,
Scheme="internal",
Subnets=Ref("PrivateSubnets"),
SecurityGroups=[Ref(alb_sg)]
SecurityGroups=[Ref(self.alb_security_group_name)]
))

template.add_output(
Expand Down Expand Up @@ -118,7 +115,11 @@ def add_listener(self, template, instance_name):
)

def create_default_target_group(self, template, instance_name):
print(template.__dict__)
"""
:param template:
:param instance_name:
"""
template.add_resource(alb.TargetGroup(
TARGET_GROUP_DEFAULT % instance_name,
Port='80',
Expand Down
6 changes: 3 additions & 3 deletions cumulus/steps/ec2/alb_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
class AlbPort(step.Step):

def __init__(self,
name,
port_to_open,
alb_sg_name):

step.Step.__init__(self)

self.name = name
self.port_to_open = port_to_open
self.alb_sg_name = alb_sg_name

def handle(self, chain_context):
template = chain_context.template

name = '%sElbToASGPort%s' % (chain_context.instance_name, self.port_to_open)

template.add_resource(ec2.SecurityGroupIngress(
name,
self.name,
IpProtocol="tcp",
FromPort=self.port_to_open,
ToPort=self.port_to_open,
Expand Down
4 changes: 3 additions & 1 deletion cumulus/steps/ec2/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Dns(step.Step):

def __init__(self,
namespace,
base_domain,
hosted_zone_id,
dns_name,
Expand All @@ -16,6 +17,7 @@ def __init__(self,
step.Step.__init__(self)

self.base_domain = base_domain
self.namespace = namespace
self.hosted_zone_id = hosted_zone_id
self.dns_name = dns_name

Expand All @@ -37,7 +39,7 @@ def handle(self, chain_context):
EvaluateTargetHealth=False,
),
Name=Join("", [
Ref("namespace"),
self.namespace,
"-",
Ref("env"),
".",
Expand Down
18 changes: 11 additions & 7 deletions cumulus/steps/ec2/launch_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,28 @@ def handle(self, chain_context):

sg_name = "SG%s" % self.asg_name

template.add_resource(ec2.SecurityGroup(
launch_config_security_group = ec2.SecurityGroup(
sg_name,
GroupDescription=sg_name,
**self._get_security_group_parameters()))
**self._get_security_group_parameters()
)

chain_context.metadata[META_SECURITY_GROUP_REF] = Ref(sg_name)

user_data = self.user_data

self._add_instance_profile(chain_context)
instance_profile = self._get_instance_profile(chain_context)

launch_config = autoscaling.LaunchConfiguration(
self.launch_config_name,
UserData=Base64(user_data),
Metadata=self.meta_data,
IamInstanceProfile=Ref('InstanceProfile%s' % chain_context.instance_name),
IamInstanceProfile=Ref(instance_profile),
**self._get_launch_configuration_parameters(chain_context)
)

template.add_resource(instance_profile)
template.add_resource(launch_config_security_group)
template.add_resource(launch_config)

def _get_security_group_parameters(self):
Expand All @@ -79,7 +82,7 @@ def _get_launch_configuration_parameters(self, chain_context):

return parameters

def _add_instance_profile(self, chain_context):
def _get_instance_profile(self, chain_context):

s3readPolicy = iam.Policy(
PolicyName='S3ReadArtifactBucket',
Expand Down Expand Up @@ -117,7 +120,8 @@ def _add_instance_profile(self, chain_context):
Policies=[s3readPolicy]
))

chain_context.template.add_resource(InstanceProfile(
instance_profile = InstanceProfile(
'InstanceProfile%s' % chain_context.instance_name,
Roles=[Ref(cfnrole)]
))
)
return instance_profile
11 changes: 4 additions & 7 deletions cumulus/steps/ec2/target_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@
class TargetGroup(step.Step):

def __init__(self,
name,
port,
vpc_id
):

step.Step.__init__(self)

self.name = name
self.port = port
self.vpc_id = vpc_id

def handle(self, chain_context):

# todo: why is this not allowing a reference?

name = '%sTargetGroup' % chain_context.instance_name

chain_context.metadata[META_TARGET_GROUP_NAME] = name
chain_context.metadata[META_TARGET_GROUP_NAME] = self.name
template = chain_context.template

template.add_resource(alb.TargetGroup(
name,
self.name,
HealthCheckPath="/",
HealthCheckIntervalSeconds="30",
HealthCheckProtocol="HTTP",
Expand Down
7 changes: 4 additions & 3 deletions tests/stacker_test/blueprints/alb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ def create_template(self):
t = self.template
t.add_description("Acceptance Tests for cumulus scaling groups")

instance = self.context.environment['namespace'] + self.context.environment['env']
instance_name = self.context.namespace + "testAlb"

the_chain = chain.Chain()

the_chain.add(alb.Alb())
the_chain.add(alb.Alb(alb_security_group_name="%sAlbSg" % instance_name,
alb_security_group_ingress_name="LocalNetworkTo%sAlbPort443" % instance_name))

chain_context = chaincontext.ChainContext(
template=t,
instance_name=instance
instance_name=instance_name
)

the_chain.run(chain_context)
57 changes: 35 additions & 22 deletions tests/stacker_test/blueprints/website_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
from troposphere import cloudformation, ec2, Ref

from cumulus.chain import chain, chaincontext
from cumulus.components.userdata.linux import LinuxUserData
from cumulus.steps.ec2 import scaling_group, launch_config, block_device_data, ingress_rule, target_group, dns, \
alb_port, listener_rule


class WebsiteSimple(Blueprint):
VARIABLES = {
'namespace': {
'type': CFNString
},
'env': {
'type': CFNString
},
Expand Down Expand Up @@ -81,17 +79,39 @@ def create_template(self):
t = self.template
t.add_description("Acceptance Tests for cumulus scaling groups")

# TODO fix
# instance = self.name + self.context.environment['env']
# TODO: give to builder
the_chain = chain.Chain()

application_port = "8000"

instance_profile_name = "InstanceProfile" + self.name
instance_name = self.context.namespace + "testAlb"

launch_config_name = 'Lc%s' % instance_name
asg_name = 'Asg%s' % instance_name
ec2_role_name = 'Ec2RoleName%s' % instance_name

the_chain.add(launch_config.LaunchConfig(launch_config_name=launch_config_name,
asg_name=asg_name,
ec2_role_name=ec2_role_name,
vpc_id=Ref('VpcId'),
meta_data=self.get_metadata(),
bucket_name=self.context.bucket_name,
user_data=LinuxUserData.user_data_for_cfn_init(
launch_config_name=launch_config_name,
asg_name=asg_name,
configsets='default'
)))

the_chain.add(ingress_rule.IngressRule(
port_to_open="22",
name="TestAlbPort22",
cidr="10.0.0.0/8"
))

the_chain.add(launch_config.LaunchConfig(meta_data=self.get_metadata(),
vpc_id=Ref("VpcId")))
the_chain.add(ingress_rule.IngressRule(
port_to_open=application_port,
name="TestAlbPort8000",
cidr="10.0.0.0/8"
))

the_chain.add(block_device_data.BlockDeviceData(ec2.BlockDeviceMapping(
DeviceName="/dev/xvda",
Expand All @@ -101,29 +121,22 @@ def create_template(self):

the_chain.add(target_group.TargetGroup(
port=application_port,
name='%sTargetGroup' % instance_name,
vpc_id=Ref("VpcId")
))

the_chain.add(scaling_group.ScalingGroup(
))

the_chain.add(ingress_rule.IngressRule(
port_to_open="22",
cidr="10.0.0.0/8"
))

the_chain.add(ingress_rule.IngressRule(
port_to_open=application_port,
cidr="10.0.0.0/8"
))
the_chain.add(scaling_group.ScalingGroup(name=asg_name,
launch_config_name=launch_config_name))

the_chain.add(dns.Dns(
namespace=self.context.namespace,
base_domain=Ref("BaseDomain"),
hosted_zone_id=Ref("AlbCanonicalHostedZoneID"),
dns_name=Ref("AlbDNSName"),
))

the_chain.add(alb_port.AlbPort(
name="AlbPortToOpen8000",
port_to_open=application_port,
alb_sg_name="AlbSg",
))
Expand All @@ -137,7 +150,7 @@ def create_template(self):

chain_context = chaincontext.ChainContext(
template=t,
instance_name=instance_profile_name
instance_name=instance_name
)

the_chain.run(chain_context)
1 change: 1 addition & 0 deletions tests/stacker_test/conf/acceptance.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# http://stacker.readthedocs.io/en/latest/environments.html
namespace: acc
env: ac
# If you want to run these tests yourself, reconfigure the below for your own account
VpcId: vpc-894b89ef
BaseDomain: playpen.dsl.aws.shaw.ca
PrivateSubnets: subnet-7b8cba32,subnet-ed041b8a
Expand Down
6 changes: 3 additions & 3 deletions tests/stacker_test/conf/asgtest.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace: jonsapp
env: dev
BaseDomain: playpen.dsl.aws.shaw.ca
namespace: acc
env: ac
# If you want to run these tests yourself, reconfigure the below for your own account
VpcId: vpc-894b89ef
BaseDomain: playpen.dsl.aws.shaw.ca
PrivateSubnets: subnet-7b8cba32,subnet-ed041b8a
Expand Down
1 change: 0 additions & 1 deletion tests/stacker_test/stacker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ stacks:
variables:
<< : *vpc_variables # yaml parameter expansion syntax
<< : *alb_variables
namespace: ${namespace}
env: ${env}
ImageName: amazonLinux2
SshKeyName: stc-admin-March-2017-PLAYPEN
Expand Down

0 comments on commit e76b8e9

Please sign in to comment.