Skip to content

Commit

Permalink
Merge pull request #39 from brettswift/feature/consolidate_naming_2
Browse files Browse the repository at this point in the history
add prefix to make project name unique
  • Loading branch information
jsauter committed Nov 20, 2018
2 parents 75139c5 + 371f9dd commit 16f12e1
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 172 deletions.
4 changes: 3 additions & 1 deletion cumulus/chain/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from cumulus.chain import chaincontext # noqa
from cumulus.chain.params import TemplateRequirements # noqa
from cumulus.util.template_query import TemplateQuery # noqa


class Chain:
Expand Down Expand Up @@ -55,6 +54,9 @@ def validate_template(self, chain_context):
"all the required params are: ")
raise AssertionError(message + "\n" + unsatisfied_params)

if chain_context.instance_name:
print(colored('chain_context.instance_name is deprecated', color='red'))

def _execute_all_steps(self, chain_context):
for step in self._steps:
print(colored("RUNNING STEP for class %s " % step.__class__, color='yellow'))
Expand Down
2 changes: 1 addition & 1 deletion cumulus/chain/chaincontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ChainContext:

def __init__(self,
template,
instance_name,
instance_name=None,
auto_param_creation=True
):
"""
Expand Down
8 changes: 6 additions & 2 deletions cumulus/chain/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ class Step:
Define an interface for handling requests.
"""

def __init__(self):
pass
def __init__(self, name='UnNamed'):
"""
:type name: basestring Friendly name of the step to be used in logical naming
"""
self.name = name

def handle(self, chain_context):
# type: (chaincontext.ChainContext) -> None
Expand Down
20 changes: 15 additions & 5 deletions cumulus/components/userdata/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@


class LinuxUserData:
@staticmethod
def user_data_for_cfn_init(launch_config_name, asg_name, configsets):

def __init__(self,
launch_config_name,
asg_name,
config_sets
):
self.launch_config_name = launch_config_name
self.asg_name = asg_name
self.config_sets = config_sets

def user_data_for_cfn_init(self):
"""
:return: A troposphere Join object that contains userdata for use with cfn-init
Expand All @@ -22,8 +31,8 @@ def user_data_for_cfn_init(launch_config_name, asg_name, configsets):
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init ",
" --stack ", Ref("AWS::StackName"),
" --resource ", launch_config_name,
" --configsets %s " % configsets,
" --resource ", self.launch_config_name,
" --configsets %s " % self.config_sets,
" --region ", Ref("AWS::Region"), "\n",
# "# Get exit code of cfn init to use in cfn-signal\n",
# "export init_status=$?", "\n"
Expand All @@ -32,9 +41,10 @@ def user_data_for_cfn_init(launch_config_name, asg_name, configsets):
"# Signal the ASG we are ready\n\n",
"/opt/aws/bin/cfn-signal -e 0",
# "/opt/aws/bin/cfn-signal -e $init_status",
" --resource %s" % asg_name,
" --resource %s" % self.asg_name,
" --stack ", Ref("AWS::StackName"),
" --region ", Ref("AWS::Region"),
"\n"
]))

return default_userdata_asg_signal
18 changes: 13 additions & 5 deletions cumulus/components/userdata/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@

class WindowsUserData:

@staticmethod
def user_data_for_cfn_init(launch_config_name, asg_name, configsets):
def __init__(self,
launch_config_name,
asg_name,
config_sets
):
self.launch_config_name = launch_config_name
self.asg_name = asg_name
self.config_sets = config_sets

def user_data_for_cfn_init(self):
"""
:return: A troposphere Join object that contains userdata for use with cfn-init
:param configsets: The single 'key' value set in the cfn-init Metadata parameter: cloudformation.InitConfigSets
Expand All @@ -20,14 +28,14 @@ def user_data_for_cfn_init(launch_config_name, asg_name, configsets):
"<powershell>\n",
"& ", "$env:ProgramFiles\Amazon\cfn-bootstrap\cfn-init.exe",
" --stack ", Ref("AWS::StackName"),
" --resource ", launch_config_name,
" --configsets %s " % configsets,
" --resource ", self.launch_config_name,
" --configsets %s " % self.config_sets,
" --region ", Ref("AWS::Region"), "\n",
"# Signal the ASG we are ready\n",
"&", "$env:ProgramFiles\Amazon\cfn-signal",
" -e ",
" $LastExitCode",
" --resource %s" % asg_name,
" --resource %s" % self.asg_name,
" --stack ", Ref("AWS::StackName"),
" --region ", Ref("AWS::Region"),
"\n",
Expand Down
8 changes: 6 additions & 2 deletions cumulus/steps/dev_tools/code_build_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class CodeBuildAction(step.Step):

def __init__(self,
prefix,
action_name,
stage_name_to_add,
input_artifact_name,
Expand All @@ -30,13 +31,15 @@ def __init__(self,
role_arn=None,
):
"""
:type prefix: basestring name to make the project unique
:type buildspec: basestring path to buildspec.yml or text containing the buildspec.
:type input_artifact_name: basestring The artifact name in the pipeline. Must contain a buildspec.yml
:type action_name: basestring Displayed on the console
:type environment: troposphere.codebuild.Environment Optional if you need ENV vars or a different build.
:type vpc_config.Vpc_Config: Only required if the codebuild step requires access to the VPC
"""
step.Step.__init__(self)
self.prefix = prefix
self.role_arn = role_arn
self.buildspec = buildspec
self.environment = environment
Expand Down Expand Up @@ -75,6 +78,7 @@ def handle(self, chain_context):
)

project = self.create_project(
prefix=self.prefix,
chain_context=chain_context,
codebuild_role_arn=codebuild_role_arn,
codebuild_environment=self.environment,
Expand Down Expand Up @@ -128,7 +132,7 @@ def get_default_code_build_role(self, chain_context, policy_name, role_name):
)
return codebuild_role

def create_project(self, chain_context, codebuild_role_arn, codebuild_environment, name):
def create_project(self, chain_context, codebuild_role_arn, codebuild_environment, name, prefix):

artifacts = codebuild.Artifacts(Type='CODEPIPELINE')

Expand Down Expand Up @@ -156,7 +160,7 @@ def create_project(self, chain_context, codebuild_role_arn, codebuild_environmen
SecurityGroupIds=[Ref(sg)],
)}

project_name = "Project%s" % name
project_name = "%sProject%s" % (prefix, name)

print("Action %s is using buildspec: " % self.action_name)
print(self.buildspec)
Expand Down
67 changes: 38 additions & 29 deletions cumulus/steps/ec2/alb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@
)
from troposphere import elasticloadbalancingv2 as alb

CLUSTER_SG_NAME = "%sSG"
ALB_NAME = "%sLoadBalancer"
TARGET_GROUP_DEFAULT = "%sTargetGroup"
SG_NAME = "%sSecurityGroup"
ALB_LISTENER = "%sListener"
ALB_NAME = "LoadBalancer"
TARGET_GROUP_DEFAULT = "TargetGroup"


class Alb(step.Step):

def __init__(self,
alb_security_group_name,
alb_security_group_ingress_name,
prefix,
):
self.alb_security_group_name = alb_security_group_name
self.alb_security_group_ingress_name = alb_security_group_ingress_name
step.Step.__init__(self)
"""
:type prefix: basestring prefix to name components uniquely
"""
step.Step.__init__(self, name='Alb')
self.prefix = prefix

def handle(self, chain_context):
sg_name = self.prefix + SG_NAME % self.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)
self.create_load_balancer_alb(chain_context.template, chain_context.instance_name)
self.add_listener(chain_context.template, chain_context.instance_name)
self.create_security_groups(chain_context.template, sg_name)
self.create_default_target_group(chain_context.template)
self.create_load_balancer_alb(chain_context.template, sg_name)
self.add_listener(chain_context.template)

def create_conditions(self, template):
template.add_condition(
Expand All @@ -36,35 +41,40 @@ def create_conditions(self, template):
"UseIAMCert",
Not(Equals(Ref("ALBCertType"), "acm")))

def create_security_groups(self, template, instance_name):
def create_security_groups(self, template, sg_name):

template.add_resource(
ec2.SecurityGroup(
self.alb_security_group_name,
GroupDescription=self.alb_security_group_name,
VpcId=Ref("VpcId")
sg_name,
GroupName=sg_name,
GroupDescription=sg_name,
VpcId=Ref("VpcId"),
Tags=[{'Key': 'Name', 'Value': sg_name}]
))

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

sg_ingress_name = "SecurityGroupIngressTo443"

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

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

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

template.add_output(
Expand All @@ -77,7 +87,7 @@ def create_load_balancer_alb(self, template, instance_name):
Output("DNSName", Value=load_balancer.GetAtt("DNSName"))
)

def add_listener(self, template, instance_name):
def add_listener(self, template):
# Choose proper certificate source ?-> always acm?
acm_cert = Join("", [
"arn:aws:acm:",
Expand All @@ -92,16 +102,15 @@ def add_listener(self, template, instance_name):
":server-certificate/",
Ref("ALBCertName")])
cert_id = If("UseIAMCert", iam_cert, acm_cert)
alb_name = ALB_NAME % instance_name

alb_name = ALB_NAME
with_ssl = alb.Listener(
"Listener",
ALB_LISTENER % self.name,
Port="443",
Protocol="HTTPS",
LoadBalancerArn=Ref(alb_name),
DefaultActions=[alb.Action(
Type="forward",
TargetGroupArn=Ref(TARGET_GROUP_DEFAULT % instance_name)
TargetGroupArn=Ref(TARGET_GROUP_DEFAULT)
)],
Certificates=[alb.Certificate(
CertificateArn=cert_id
Expand All @@ -114,14 +123,14 @@ def add_listener(self, template, instance_name):
Output("IAlbListener", Value=with_ssl.Ref())
)

def create_default_target_group(self, template, instance_name):
def create_default_target_group(self, template):
"""
:param template:
:param instance_name:
"""
template.add_resource(alb.TargetGroup(
TARGET_GROUP_DEFAULT % instance_name,
TARGET_GROUP_DEFAULT,
Port='80',
Protocol="HTTP",
VpcId=Ref("VpcId"),
Expand Down
15 changes: 6 additions & 9 deletions cumulus/steps/ec2/alb_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
from cumulus.chain import step
from cumulus.steps.ec2 import META_SECURITY_GROUP_REF

ALB_PORT_NAME = "AlbPortToOpen%s"


class AlbPort(step.Step):

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

step.Step.__init__(self)
port_to_open):
step.Step.__init__(self, name='')

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

template.add_resource(ec2.SecurityGroupIngress(
self.name,
ALB_PORT_NAME % self.port_to_open,
IpProtocol="tcp",
FromPort=self.port_to_open,
ToPort=self.port_to_open,
SourceSecurityGroupId=Ref(self.alb_sg_name),
SourceSecurityGroupId=Ref("AlbSg"),
GroupId=chain_context.metadata[META_SECURITY_GROUP_REF]
))

0 comments on commit 16f12e1

Please sign in to comment.