Skip to content

Commit e7ca116

Browse files
committed
Merge pull request #72 from mhahn/combine-namespace-environment
Make environment a required parameter.
2 parents a4cab64 + ff2ac20 commit e7ca116

File tree

16 files changed

+108
-52
lines changed

16 files changed

+108
-52
lines changed

README.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ if you'd like to play with something smaller. To launch the stacks, after
4444
installing stacker and loading your AWS API keys in your environment
4545
(AWS\_ACCESS\_KEY\_ID & AWS\_SECRET\_ACCESS\_KEY), call the following::
4646

47-
stacker build -v -p BaseDomain=blahblah.com -r us-east-1 example.com conf/example.yaml
47+
stacker build -v -p BaseDomain=blahblah.com -r us-east-1 conf/stage.env conf/example.yaml
4848

4949
As of now there is no option to tear down the stack in the tool (we plan to
5050
add it), so you'll need to tear the stacks it creates down manually. When doing
@@ -77,12 +77,17 @@ parameter. If it finds it, it will use that automatically.
7777
Environments
7878
============
7979

80-
As well as definining the stack config, you can further customize the stack
81-
config via an environment (ie the ``-e or --environment`` argument).
80+
As well as defining the stack config, you'll need to specify an
81+
environment. The environment should point to a yaml formatted file that
82+
contains a flat dictionary (ie: only ``key: value`` pairs). Those keys
83+
can be used in the stack config as python `string.Template`_ mappings.
8284

83-
The environment should point at a yaml formatted file that contains a flat
84-
dictionary (ie: only key: value pairs). Those keys can be used in the
85-
stack config as python `string.Template`_ mappings.
85+
At a minimum the environment must define the ``namespace`` parameter::
86+
87+
namespace: example.com
88+
89+
This helps preserve the namespace for the stacks between subsequent
90+
builds.
8691

8792
For example, if you wanted to name a stack based on the environment you were
8893
building it in, first you would create an environment file with the

conf/empire/example.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
# Note: The minimum allowed azcount is 2.
99
azcount: 2
1010

11+
# namespace is a unique name that the stacks will be built under. This value
12+
# will be used to prefix the CloudFormation stack names as well as the s3
13+
# bucket that contains revisions of the stacker templates. This is the only
14+
# required environment variable.
15+
namespace:
16+
1117
# An external domain where empire.<domain> CNAME will be created
1218
external_domain:
1319
ssh_key_name:

conf/rds/mysql.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# namespace is a unique name that the stacks will be built under. This value
2+
# will be used to prefix the CloudFormation stack names as well as the s3
3+
# bucket that contains revisions of the stacker templates. This is the only
4+
# required environment variable.
5+
namespace:
6+
17
# VPC settings
28
azcount: 2
39
nat_instance_type: m3.medium

conf/rds/postgres.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# namespace is a unique name that the stacks will be built under. This value
2+
# will be used to prefix the CloudFormation stack names as well as the s3
3+
# bucket that contains revisions of the stacker templates. This is the only
4+
# required environment variable.
5+
namespace:
6+
17
# VPC settings
28
azcount: 2
39
nat_instance_type: m3.medium

conf/stage.env

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# This is just an example of the format for environment files
1+
# This is an example of the format for environment files
22
# You can use a flag dictionary in yaml format. Values can be strings or
3-
# numbers, but cannot be more complex data types (dictionaries, lists, etc)
4-
environment: stage
3+
# numbers, but cannot be more complex data types (dictionaries, lists, etc).j
4+
5+
# namespace is a unique name that the stacks will be built under. This value
6+
# will be used to prefix the CloudFormation stack names as well as the s3
7+
# bucket that contains revisions of the stacker templates. This is the only
8+
# required environment variable.
9+
namespace:

stacker/commands/stacker/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def configure(self, options, **kwargs):
1717
super(Stacker, self).configure(options, **kwargs)
1818
options.provider = aws.Provider(region=options.region)
1919
options.context = Context(
20-
namespace=options.namespace,
2120
environment=options.environment,
2221
parameters=copy.deepcopy(options.parameters),
2322
# We use

stacker/commands/stacker/base.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,17 @@ def add_arguments(self, parser):
146146
parser.add_argument("-r", "--region", default="us-east-1",
147147
help="The AWS region to launch in. Default: "
148148
"%(default)s")
149-
parser.add_argument("-e", "--environment", type=yaml_file_type,
150-
default={},
151-
help="Path to a yaml environment file. The values "
152-
"in the environment file can be used in the "
153-
"stack config as if it were a "
154-
"string.Template type: "
155-
"https://docs.python.org/2/library/"
156-
"string.html#template-strings")
157149
parser.add_argument("-v", "--verbose", action="count", default=0,
158150
help="Increase output verbosity. May be specified "
159151
"up to twice.")
160-
parser.add_argument("namespace",
161-
help="The namespace for the stack collection. "
162-
"This will be used as the prefix to the "
163-
"cloudformation stacks as well as the s3 "
164-
"bucket where templates are stored.")
165-
parser.add_argument("config", type=argparse.FileType(),
166-
help="The config file where stack configuration "
167-
"is located. Must be in yaml format.")
152+
parser.add_argument('environment', type=yaml_file_type,
153+
default={},
154+
help="Path to a yaml environment file. The values in "
155+
"the environment file can be used in the stack "
156+
"config as if it were a string.Template type: "
157+
"https://docs.python.org/2/library/string.html"
158+
"#template-strings. Must define at least a "
159+
"'namespace'.")
160+
parser.add_argument('config', type=argparse.FileType(),
161+
help="The config file where stack configuration is "
162+
"located. Must be in yaml format.")

stacker/context.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .exceptions import MissingEnvironment
12
from .config import parse_config
23
from .stack import Stack
34

@@ -24,18 +25,21 @@ class Context(object):
2425
2526
"""
2627

27-
def __init__(self, namespace, environment=None, stack_names=None,
28+
def __init__(self, environment, stack_names=None,
2829
parameters=None, mappings=None, config=None,
2930
force_stacks=None):
30-
self.namespace = namespace
31-
self.environment = environment or {}
31+
try:
32+
self.namespace = environment['namespace']
33+
except KeyError:
34+
raise MissingEnvironment(['namespace'])
35+
36+
self.environment = environment
3237
self.stack_names = stack_names or []
3338
self.parameters = parameters or {}
3439
self.mappings = mappings or {}
3540
self.config = config or {}
3641
self.force_stacks = force_stacks or []
37-
38-
self._base_fqn = namespace.replace('.', '-').lower()
42+
self._base_fqn = self.namespace.replace('.', '-').lower()
3943

4044
def load_config(self, conf_string):
4145
self.config = parse_config(conf_string, environment=self.environment)

stacker/tests/actions/test_build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_outputs(self, stack_name, *args, **kwargs):
4141
class TestBuildAction(unittest.TestCase):
4242

4343
def setUp(self):
44-
self.context = Context('namespace')
44+
self.context = Context({'namespace': 'namespace'})
4545
self.build_action = build.Action(self.context, provider=TestProvider())
4646

4747
def _get_context(self, **kwargs):
@@ -52,7 +52,7 @@ def _get_context(self, **kwargs):
5252
'else': 'bastion::something'}},
5353
{'name': 'other', 'parameters': {}}
5454
]}
55-
return Context('namespace', config=config, **kwargs)
55+
return Context({'namespace': 'namespace'}, config=config, **kwargs)
5656

5757
def test_resolve_parameters_referencing_non_existant_output(self):
5858
parameters = {

stacker/tests/actions/test_destroy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, name, tags=None, **kwargs):
2525
class TestDestroyAction(unittest.TestCase):
2626

2727
def setUp(self):
28-
self.context = Context('namespace')
28+
self.context = Context({'namespace': 'namespace'})
2929
self.context.config = {
3030
'stacks': [
3131
{'name': 'vpc'},

0 commit comments

Comments
 (0)