Skip to content

Instantly share code, notes, and snippets.

@amosshapira
Created March 15, 2016 10:27
Show Gist options
  • Save amosshapira/f28c964f4103c7db2207 to your computer and use it in GitHub Desktop.
Save amosshapira/f28c964f4103c7db2207 to your computer and use it in GitHub Desktop.
import os.path
import sys
import yaml
import json
import importlib
import getopt
from troposphere import Template
sys.path.append(os.path.dirname(os.path.realpath(__file__ + "/..")))
def config_stack(stack_name):
return {
'stack': stack_name
}
def merge(a, b, path=None, update=True):
"http://stackoverflow.com/questions/7204805/python-dictionaries-of-dictionaries-merge"
"merges b into a"
if path is None: path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
elif isinstance(a[key], list) and isinstance(b[key], list):
for idx, val in enumerate(b[key]):
a[key][idx] = merge(a[key][idx], b[key][idx], path + [str(key), str(idx)], update=update)
elif update:
a[key] = b[key]
else:
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
else:
a[key] = b[key]
return a
def read_yaml_file(filename):
with open(filename, 'r') as f:
return yaml.load(f)
def config(stack_name):
stack = read_yaml_file("stacks/%s.yaml" % stack_name)
stack_config = merge(config_stack(stack_name), stack)
config_files = [
"configuration/templates/%s/config.yaml" % stack['template_name'],
"configuration/regions/%s/config.yaml" % stack['region'],
"configuration/stacks/%s/config.yaml" % stack_name,
"configuration/stacks/%s/secrets.yaml" % stack_name
]
for config_file in config_files:
if os.path.exists(config_file):
stack_config = merge(stack_config, read_yaml_file(config_file))
return stack_config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment