Skip to content

Instantly share code, notes, and snippets.

@naftulikay
Last active January 8, 2016 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naftulikay/2aac0358536ccf2fa8b1 to your computer and use it in GitHub Desktop.
Save naftulikay/2aac0358536ccf2fa8b1 to your computer and use it in GitHub Desktop.
CloudFormation YAML Assembler
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import argparse
import glob
import json
import os
import sys
import yaml
def __main__():
parser = argparse.ArgumentParser(
description="Creates a CloudFormation JSON config from a series of YAML files."
)
# optional args
parser.add_argument('-o', '--output', type=argparse.FileType('wb'), default=sys.stdout,
help="The output file to write. Defaults to standard output.")
# required args
parser.add_argument('base_file', type=argparse.FileType('rb'),
help="The base YAML file for the CloudFormation template.")
args = parser.parse_args()
assembler = CloudFormationAssembler(base_file=args.base_file, output_file=args.output)
assembler.assemble()
class CloudFormationAssembler(object):
def __init__(self, base_file, output_file):
self.__result = None
self.__base_file = base_file
self.__base_dir = os.path.dirname(base_file.name)
self.__output_file = output_file
def assemble(self):
# load the base file
self.__result = yaml.safe_load(self.__base_file)
# load all mappings
self.assemble_chunks('mappings', 'Mappings', self.__result)
self.assemble_chunks('parameters', 'Parameters', self.__result)
self.assemble_chunks('resources', 'Resources', self.__result)
# write to output as pretty JSON
json.dump(self.__result, self.__output_file, sort_keys=True, indent=4, separators=(',', ': '))
def assemble_chunks(self, path, dest_key, target):
if os.path.isdir(os.path.join(self.__base_dir, path)):
for chunk in glob.glob(os.path.join(self.__base_dir, path, '*.yml')):
# if the destination key doesn't exist in target, initialize as array
if not dest_key in target.keys() or type(target.get(dest_key)) != dict:
target[dest_key] = {}
with open(chunk, 'rb') as f:
target[dest_key].update(yaml.safe_load(f))
if __name__ == "__main__":
__main__()
.
├── mappings
│   └── mappings.yml
├── parameters
│   └── instance_types.yml
├── resources
│   ├── ec2_instances.yml
│   └── ecs_services.yml
├── c12n-assemble
├── input.yml
└── README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment