Skip to content

Commit 59320bc

Browse files
committed
Updates to fix changes to resulting templates
There are still a few differences from the templates as generated before, but I think they're all reasonable: * More consistently adding our common tags everywhere (fixed bugs in add_common_tags) * Some parameters are being added to templates in a different order than before * One place had previously mislabeled the desired instance count as the maximum instance count, and that is now fixed.
1 parent 8b8f728 commit 59320bc

File tree

4 files changed

+118
-86
lines changed

4 files changed

+118
-86
lines changed

stack/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from troposphere import AWS_REGION, Equals, If, Not, Ref
22

3-
from . import USE_DOKKU, USE_EB, USE_ECS
3+
from . import USE_DOKKU, USE_EB, USE_ECS, USE_EC2, USE_GOVCLOUD
44
from .template import template
55
from .utils import ParameterWithDefaults as Parameter
66

@@ -23,7 +23,7 @@
2323
label="Admin IP Address",
2424
))
2525

26-
if any([USE_DOKKU, USE_EB, USE_ECS]):
26+
if any([USE_DOKKU, USE_EB, USE_ECS, USE_EC2, USE_GOVCLOUD]):
2727
secret_key = Ref(template.add_parameter(
2828
Parameter(
2929
"SecretKey",

stack/containers.py

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,53 @@
44
from awacs import ecr
55
from troposphere import Ref, iam
66

7-
from stack import USE_ECS, USE_EKS
7+
from stack import USE_ECS, USE_EKS, USE_DOKKU, USE_EB
88
from stack.template import template
99
from stack.utils import ParameterWithDefaults as Parameter
1010

1111
if not USE_EKS:
1212
from stack.assets import assets_management_policy
1313
from stack.logs import logging_policy
1414

15-
desired_container_instances = Ref(
16-
template.add_parameter(
17-
Parameter(
18-
"DesiredScale",
19-
Description="Desired container instances count",
20-
Type="Number",
21-
Default="3" if USE_ECS else "2",
22-
),
23-
group="Application Server",
24-
label="Desired Instance Count",
15+
if not USE_DOKKU and not USE_EB:
16+
desired_container_instances = Ref(
17+
template.add_parameter(
18+
Parameter(
19+
"DesiredScale",
20+
Description="Desired container instances count",
21+
Type="Number",
22+
Default="3" if USE_ECS else "2",
23+
),
24+
group="Application Server",
25+
label="Desired Instance Count",
26+
)
2527
)
26-
)
27-
max_container_instances = Ref(
28-
template.add_parameter(
29-
Parameter(
30-
"MaxScale",
31-
Description="Maximum container instances count",
32-
Type="Number",
33-
Default="3" if USE_ECS else "4",
34-
),
35-
group="Application Server",
36-
label="Maximum Instance Count",
28+
max_container_instances = Ref(
29+
template.add_parameter(
30+
Parameter(
31+
"MaxScale",
32+
Description="Maximum container instances count",
33+
Type="Number",
34+
Default="3" if USE_ECS else "4",
35+
),
36+
group="Application Server",
37+
label="Maximum Instance Count",
38+
)
3739
)
38-
)
3940

40-
container_volume_size = Ref(
41-
template.add_parameter(
42-
Parameter(
43-
"ContainerVolumeSize",
44-
Description="Size of instance EBS root volume (in GB)",
45-
Type="Number",
46-
Default="20" if USE_EKS else "8",
47-
),
48-
group="Application Server",
49-
label="Root Volume Size",
50-
)
51-
)
41+
if not USE_ECS:
42+
container_volume_size = Ref(
43+
template.add_parameter(
44+
Parameter(
45+
"ContainerVolumeSize",
46+
Description="Size of instance EBS root volume (in GB)",
47+
Type="Number",
48+
Default="20" if USE_EKS else "8",
49+
),
50+
group="Application Server",
51+
label="Root Volume Size",
52+
)
53+
)
5254

5355
if USE_EKS:
5456
container_policies = []
@@ -89,39 +91,40 @@
8991
]
9092
)
9193

92-
container_instance_role = iam.Role(
93-
"ContainerInstanceRole",
94-
template=template,
95-
AssumeRolePolicyDocument=dict(
96-
Statement=[
94+
if not USE_EB:
95+
container_instance_role = iam.Role(
96+
"ContainerInstanceRole",
97+
template=template,
98+
AssumeRolePolicyDocument=dict(
99+
Statement=[
100+
dict(
101+
Effect="Allow",
102+
Principal=dict(Service=["ec2.amazonaws.com"]),
103+
Action=["sts:AssumeRole"],
104+
)
105+
]
106+
),
107+
Path="/",
108+
Policies=container_policies,
109+
**(
97110
dict(
98-
Effect="Allow",
99-
Principal=dict(Service=["ec2.amazonaws.com"]),
100-
Action=["sts:AssumeRole"],
111+
ManagedPolicyArns=[
112+
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
113+
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
114+
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
115+
]
101116
)
102-
]
103-
),
104-
Path="/",
105-
Policies=container_policies,
106-
**(
107-
dict(
108-
ManagedPolicyArns=[
109-
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
110-
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
111-
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
112-
]
113-
)
114-
if USE_EKS
115-
else {}
116-
),
117-
)
117+
if USE_EKS
118+
else {}
119+
),
120+
)
118121

119-
container_instance_profile = iam.InstanceProfile(
120-
"ContainerInstanceProfile",
121-
template=template,
122-
Path="/",
123-
Roles=[Ref(container_instance_role)],
124-
)
122+
container_instance_profile = iam.InstanceProfile(
123+
"ContainerInstanceProfile",
124+
template=template,
125+
Path="/",
126+
Roles=[Ref(container_instance_role)],
127+
)
125128

126129
container_instance_type = Ref(
127130
template.add_parameter(

stack/ecs_cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
template=template,
107107
)
108108

109-
container_instance_configuration_name = "ECSContainerLaunchConfiguration"
109+
container_instance_configuration_name = "ContainerLaunchConfiguration"
110110

111111
autoscaling_group_name = "AutoScalingGroup"
112112

stack/tags.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,62 @@
1-
from troposphere import AWS_STACK_NAME, Ref, Tags
1+
from troposphere import AWS_STACK_NAME, Ref, Tags, autoscaling
22

33
from .template import template
44

5-
common_tags = Tags({'aws-web-stacks:stack-name': Ref(AWS_STACK_NAME)})
5+
common_tags = Tags({"aws-web-stacks:stack-name": Ref(AWS_STACK_NAME)})
6+
7+
8+
def tags_types_of_resource(resource):
9+
"""
10+
return an iterable of the acceptable types for
11+
the Tags property on this resource.
12+
"""
13+
tags_type = resource.props["Tags"][0]
14+
if isinstance(tags_type, tuple):
15+
return tags_type
16+
return [tags_type]
17+
18+
def tags_type_of_resource(resource):
19+
"""
20+
Return the type that this resource expects its Tags
21+
property to be. E.g. list, Tags, autoscaling.Tags.
22+
If there are multiple possibilities, returns the first.
23+
"""
24+
return tags_types_of_resource(resource)[0]
25+
26+
def add_empty_tags(resource):
27+
# Put an empty tags prop on this resource, of the right type.
28+
tags_type = tags_type_of_resource(resource)
29+
resource.Tags = tags_type()
630

731

832
def add_common_tags(template):
933
for resource in template.resources.values():
10-
if 'Tags' not in resource.propnames:
34+
if "Tags" not in resource.propnames:
1135
continue
36+
37+
# WARNING: adding two Tags() objects together modifies and returns
38+
# the second object, giving it the concatenation of the
39+
# tags from the first and second objects, in that order.
40+
1241
if not hasattr(resource, "Tags"):
13-
# We need to create an empty tags prop for this resource.
14-
# Not all resources use the same type for their tags, sigh.
15-
# At least we can figure it out from the information that
16-
# troposphere puts on the class.
17-
tags_type = resource.props["Tags"][0]
18-
if isinstance(tags_type, tuple):
19-
tags_type = tags_type[0]
20-
resource.Tags = tags_type()
21-
22-
if isinstance(resource.Tags, Tags):
23-
tags = Tags() + common_tags
24-
tags += resource.Tags
25-
resource.Tags = tags
26-
if isinstance(resource.Tags, dict):
42+
# resource.Tags = common_tags
43+
# return
44+
add_empty_tags(resource)
45+
46+
if isinstance(resource.Tags, (Tags, autoscaling.Tags)):
47+
resource.Tags = common_tags + resource.Tags
48+
49+
elif isinstance(resource.Tags, dict):
2750
tags = common_tags.to_dict() # actually returns a list. Sigh.
2851
tags = dict(tags) # convert to a dict.
29-
tags.update(resource.Tags) # override with any tags from this resource.
52+
tags.update(**resource.Tags) # override with any tags from this resource.
3053
resource.Tags = tags # and set the result on the resource again.
3154

55+
elif isinstance(resource.Tags, list):
56+
tags = tags_type_of_resource(resource)()
57+
tags.tags = resource.Tags
58+
resource.Tags = common_tags + tags
59+
else:
60+
raise TypeError("Unknown type %s for Tags on %s" % (type(resource.Tags), resource))
3261

3362
add_common_tags(template)

0 commit comments

Comments
 (0)