Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dliggat committed Jun 12, 2016
1 parent a50d9cd commit c142347
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json/
_output/*.template
Empty file added README.md
Empty file.
Empty file added _output/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion yaml/lambda.yaml → cloudformation/lambda.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
LambdaFunction:
Type: AWS::Lambda::Function
Metadata:
Comment: A Lambda function to monitor running resources in AWS
Comment: A Lambda function to monitor running resources in AWS (updated comment)
DependsOn: [MyLambdaExecutionRole]
Properties:
Code:
Expand Down
Empty file added config/.gitkeep
Empty file.
56 changes: 38 additions & 18 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import boto3
import glob
import jinja2
import json
import os
import yaml
Expand All @@ -12,8 +13,8 @@
coloredlogs.install(level=logging.INFO)

from fabric.api import task
from fabric.contrib.console import confirm
from fabric.utils import abort
from pprint import pprint

# Configure PyYaml to create ordered dicts
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
Expand All @@ -31,34 +32,35 @@ def construct_yaml_ordered_map(loader, node, deep=False):

yaml.add_constructor(_mapping_tag, construct_yaml_ordered_map)

root_dir = os.path.dirname(__file__)
json_dir = os.path.join(root_dir, 'json')
yaml_files = glob.glob(os.path.join(root_dir, 'yaml/*.yaml'))
json_files = glob.glob(os.path.join(root_dir, 'json/*.template'))
ROOT_DIR = os.path.dirname(__file__)
JSON_DIR = os.path.join(ROOT_DIR, '_output')
JSON_EXT = '.template'
YAML_FILES = glob.glob(os.path.join(ROOT_DIR, 'cloudformation/*.yaml.jinja'))
JSON_FILES = glob.glob(os.path.join(JSON_DIR, '*{0}'.format(JSON_EXT)))


@task(default=True)
def render():
if not yaml_files:
if not YAML_FILES:
abort('No YAML files present in directory')

if not os.path.exists(json_dir):
logger.info('Created directory: %s', json_dir)
os.makedirs(json_dir)
if not os.path.exists(JSON_DIR):
logger.info('Created directory: %s', JSON_DIR)
os.makedirs(JSON_DIR)

for yaml_file in yaml_files:
for yaml_file in YAML_FILES:
with open(yaml_file, 'r') as yaml_contents:
data = yaml.load(yaml_contents.read())
json_str = json.dumps(data, indent=2)
json_file = os.path.join(json_dir, os.path.basename(yaml_file).split('.')[0]) + '.template'
json_file = os.path.join(JSON_DIR, os.path.basename(yaml_file).split('.')[0]) + JSON_EXT
with open(json_file, 'w') as json_contents:
json_contents.write(json_str)
logger.info('Converted %s to %s', os.path.basename(yaml_file), os.path.basename(json_file))

@task
def validate():
client = boto3.client('cloudformation')
for json_file in json_files:
for json_file in JSON_FILES:
with open(json_file, 'r') as json_contents:
try:
client.validate_template(TemplateBody=json_contents.read())
Expand All @@ -69,22 +71,40 @@ def validate():

@task
def launch(template_name=None, stack_name=None):

if not template_name:
abort('Must provide template')
if not stack_name:
abort('Must provide stack_name')
client = boto3.client('cloudformation')
with open(os.path.join(json_dir, template_name + '.template')) as json_contents:
response = client.create_stack(StackName=stack_name,
TemplateBody=json_contents.read(),
Capabilities=['CAPABILITY_IAM'])
logger.info(response)

update = False
try:
resp = client.describe_stacks(StackName=stack_name)
message = 'Stack {0} exists, and is in state {1}. Proceed with update?'.format(
stack_name, resp['Stacks'][0]['StackStatus'])
if not confirm(message):
abort('Aborting.')
else:
update = True
except ClientError:
logger.info('No stack named {0}; proceeding with stack creation'.format(stack_name))

with open(os.path.join(JSON_DIR, template_name + JSON_EXT)) as json_contents:
if update:
response = client.update_stack(StackName=stack_name,
TemplateBody=json_contents.read(),
Capabilities=['CAPABILITY_IAM'])
else:
response = client.create_stack(StackName=stack_name,
TemplateBody=json_contents.read(),
Capabilities=['CAPABILITY_IAM'])
logger.info(response)


@task
def clean():
for f in json_files:
for f in JSON_FILES:
os.remove(f)


Expand Down

0 comments on commit c142347

Please sign in to comment.