/
fabfile.py
291 lines (228 loc) · 10.7 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import boto3
import datetime
import glob
import jinja2
import json
import os
import pprint
import subprocess
import yaml
from collections import OrderedDict
from botocore.exceptions import ClientError, ValidationError
import logging; logging.basicConfig()
import coloredlogs
logger = logging.getLogger()
coloredlogs.install(level=logging.INFO)
from fabric.api import task
from fabric.operations import local
from fabric.contrib.console import confirm
from fabric.utils import abort
# Configure PyYaml to preserve order in dictionaries (ensures CloudFormation templates render
# in an expected top-level key order).
def construct_ordered_mapping(loader, node, deep=False):
if isinstance(node, yaml.MappingNode):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node, deep))
def construct_yaml_ordered_map(loader, node, deep=False):
data = OrderedDict()
yield data
value = construct_ordered_mapping(loader, node, deep)
data.update(value)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_yaml_ordered_map)
# Various constants and config setup.
ROOT_DIR = os.path.dirname(__file__)
INPUT_DIR = os.path.join(ROOT_DIR, 'cloudformation')
INPUT_EXT = '.yaml.jinja'
INPUT_FILES = glob.glob(os.path.join(INPUT_DIR, '*{0}'.format(INPUT_EXT)))
OUTPUT_DIR = os.path.join(ROOT_DIR, '_output')
OUTPUT_EXT = '.template'
OUTPUT_FILES = glob.glob(os.path.join(OUTPUT_DIR, '*{0}'.format(OUTPUT_EXT)))
# Ensure that .local.yaml config files are loaded last, so that they take precedence in the config dict.
CONFIG_DIR = os.path.join(ROOT_DIR, 'cloudformation_config')
CONFIG_FILES = sorted(glob.glob(os.path.join(CONFIG_DIR, '*{0}'.format('.yaml'))), reverse=True)
LAMBDA_DIR = os.path.join(ROOT_DIR, 'lambda')
BUILDS_SUBDIR = '_builds'
STAGING_SUBDIR = '_staging'
LAMBDA_CONFIG_SUBDIR = 'lambda_config'
def load_config():
"""Load the config from the config directory into a deep dict, keyed by stack type."""
config = {'git': {'hash': subprocess.check_output(['git', 'rev-parse', 'HEAD'])[:-1],
'message': subprocess.check_output([
'git', 'log', '-1', '--pretty=%B']).strip().replace('"', '')
.replace('#', '')
.replace('\n', '')
.replace(':', ' '),
'uncommitted': True if subprocess.call(
'git diff-index --quiet HEAD --'.split(' ')) else False }}
for config_file in CONFIG_FILES:
with open(config_file, 'r') as config_file_contents:
data = yaml.load(config_file_contents.read())
if not data:
continue
basename = os.path.basename(config_file).split('.')[0]
if basename in config:
config[basename].update(data)
else:
config[basename] = data
for (basename, config_item) in config.iteritems():
if basename == 'git':
continue
if not config_item.has_key('parameters'):
config_item['parameters'] = OrderedDict()
# Coalesce the parameters into the format expected by boto3.client('cloudformation').
config_item['parameters'] = [
{'ParameterKey': k,
'ParameterValue': v,
'UsePreviousValue': False} for (k,v) in config_item['parameters'].items()]
logger.info('Loaded config: %s', pprint.pprint(config))
return config
# Fabric tasks.
@task(default=True)
def render():
"""Render yaml.jinja to JSON, via the loaded config."""
if not INPUT_FILES:
abort('No YAML files present in directory')
if not os.path.exists(OUTPUT_DIR):
logger.info('Created directory: %s', OUTPUT_DIR)
os.makedirs(OUTPUT_DIR)
# Load the Jinja environment.
env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True, undefined=jinja2.StrictUndefined)
env.loader = jinja2.FileSystemLoader(INPUT_DIR)
# For each file in the input directory; run through a Jinja render and convert to JSON.
for input_file in INPUT_FILES:
basename = os.path.basename(input_file).split('.')[0]
template = env.get_template('{0}{1}'.format(basename, INPUT_EXT))
config = load_config()
config.update({'__name__': basename})
rendered = template.render(**config)
data = yaml.load(rendered)
output_str = json.dumps(data, indent=2, separators=(',', ': '))
output_file = os.path.join(OUTPUT_DIR, os.path.basename(input_file).split('.')[0]) + OUTPUT_EXT
with open(output_file, 'w') as output_contents:
output_contents.write(output_str)
logger.info('Converted %s to %s', os.path.basename(input_file), os.path.basename(output_file))
@task
def validate():
"""Validates the rendered template against the CloudFormation API."""
client = boto3.client('cloudformation')
for output_file in OUTPUT_FILES:
with open(output_file, 'r') as output_contents:
try:
client.validate_template(TemplateBody=output_contents.read())
except (ClientError, ValidationError) as e:
logger.error('Unable to validate {0}. Exception: {1}'.format(output_file, e))
abort('Template validation error')
@task
def provision(template_name=None, stack_name=None):
"""Creates or updates a CloudFormation stack based on the supplied template type name.
Args:
template_name: (str) The name of the template from the input directory.
stack_name: (str) The stack name to use for CloudFormation.
"""
if not template_name:
abort('Must provide template')
if not stack_name:
abort('Must provide stack_name')
client = boto3.client('cloudformation')
config = load_config()
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(OUTPUT_DIR, template_name + OUTPUT_EXT)) as output_contents:
if update:
response = client.update_stack(StackName=stack_name,
TemplateBody=output_contents.read(),
Parameters=config.get(template_name, {}).get('parameters', []),
Capabilities=['CAPABILITY_IAM'])
else:
response = client.create_stack(StackName=stack_name,
TemplateBody=output_contents.read(),
Parameters=config.get(template_name, {}).get('parameters', []),
Capabilities=['CAPABILITY_IAM'])
logger.info(json.dumps(response, indent=2))
@task
def clean():
""""Deletes all the rendered _output CloudFormation files."""
for f in OUTPUT_FILES:
os.remove(f)
@task
def install_reqs(function_name=None):
"""Installs requirements.txt dependencies for the given Lambda function.
Args:
function_name: (str) The Lambda function within the lambda/ directory to work on.
"""
if not function_name:
abort('Must provide function_name')
requirements_file = os.path.join(LAMBDA_DIR, function_name, 'requirements.txt')
output = local('pip install -U -r {0} 2>&1'.format(requirements_file), capture=True)
logger.info(output)
@task
def invoke(function_name=None):
"""Invokes the given Lambda function.
Args:
function_name: (str) The Lambda function within the lambda/ directory to work on.
"""
if not function_name:
abort('Must provide function_name')
index_file = os.path.join(LAMBDA_DIR, function_name, 'index.py')
output = local('python {0} 2>&1'.format(index_file), capture=True)
logger.info(output)
@task
def build(function_name=None):
"""Creates a deployable package for the given Lambda function in its _builds/ directory.
Args:
function_name: (str) The Lambda function within the lambda/ directory to work on.
"""
if not function_name:
abort('Must provide function_name')
lambda_root = os.path.join(LAMBDA_DIR, function_name)
module_dir = os.path.join(lambda_root, function_name)
lambda_config_dir = os.path.join(lambda_root, LAMBDA_CONFIG_SUBDIR)
staging_dir = os.path.join(lambda_root, STAGING_SUBDIR)
builds_dir = os.path.join(lambda_root, BUILDS_SUBDIR)
build_filename = '{0}-{1}.zip'.format(
datetime.datetime.now().isoformat().replace(':', '.'), function_name)
# Erase previous runs of the build task.
local('rm -rf {0}'.format(staging_dir))
# Set up staging and builds directories.
local('mkdir -p {0}'.format(staging_dir))
local('mkdir -p {0}'.format(builds_dir))
# Install the lambda specific requirements.
local('pip install -r {0}/requirements.txt -t {1}'.format(lambda_root, staging_dir))
# Copy the top level *.py (e.g. index.py) and lambda_config dir into the staging_dir.
local('cp -R {0}/*.py {1}'.format(lambda_root, staging_dir))
local('cp -R {0} {1}'.format(lambda_config_dir, staging_dir))
# Copy the module directory into the staging dir.
local('cp -R {0} {1}'.format(module_dir, staging_dir))
# Zip the whole thing up, and move it to the builds dir.
local('cd {0}; zip -r {1} ./*; mv {1} {2}'.format(staging_dir, build_filename, builds_dir))
@task
def deploy(function_name=None, arn=None):
"""Uploads the latest build of the function package to the Lambda ARN.
Args:
function_name: (str) The Lambda function within the lambda/ directory to work on.
arn: (str) The ARN of the deployed function.
"""
if not function_name:
abort('Must provide function_name')
if not arn:
abort('Must provide arn')
lambda_root = os.path.join(LAMBDA_DIR, function_name)
builds_dir = os.path.join(lambda_root, BUILDS_SUBDIR)
builds = sorted(glob.glob(os.path.join(builds_dir, '*.{0}'.format('zip'))))
if not builds:
abort('No builds exist. Run a `build` command first')
latest_build = builds[-1]
logging.info('Preparing to deploy build: {0}'.format(latest_build))
client = boto3.client('lambda')
with open(latest_build, 'rb') as zip_file:
response = client.update_function_code(FunctionName=arn, ZipFile=zip_file.read())
logger.info(json.dumps(response, indent=2))