This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
132 lines (116 loc) · 4.05 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
#!/usr/bin/env python
"""
This is the StackFormation build script based on Fabric (http://fabfile.org/).
"""
import boto
import boto.cloudformation
import botocross as bc
from fabric.api import local, task
import logging
import nose
import os
import stackformation as sfn
log = logging.getLogger('fabric')
bc.configure_logging(log, 'INFO')
@task(default=True)
def test(args=None):
"""
Run all tests.
Specify string argument ``args`` for additional args to ``nosetests``.
"""
test_unit(args)
test_integration(args)
@task
def test_integration(args=None):
"""
Run all integration tests.
Specify string argument ``args`` for additional args to ``nosetests``.
"""
default_args = "--nocapture --verbose --nologcapture tests/integration"
default_args += (" " + args) if args else ""
nose.core.run(argv=[''] + default_args.split())
@task
def test_unit(args=None):
"""
Run all unit tests.
Specify string argument ``args`` for additional args to ``nosetests``.
"""
default_args = "--nocapture --verbose --nologcapture tests/unit"
default_args += (" " + args) if args else ""
nose.core.run(argv=[''] + default_args.split())
@task
def create_buckets(bucket_base_name):
"""
Create required S3 buckets.
"""
sfn.create_buckets(bucket_base_name)
@task
def validate_buckets(bucket_base_name):
"""
Validate that required S3 buckets exist.
"""
sfn.validate_buckets(bucket_base_name)
@task
def list():
"""
List templates.
"""
sfn.list_templates()
@task
def upload(bucket_base_name):
"""
Upload templates to existing S3 buckets.
"""
test()
sfn.upload_templates(bucket_base_name)
@task
def validate_body():
"""
Validate templates via body upload.
"""
try:
cfn = boto.connect_cloudformation()
for dirpath, dirnames, filenames in os.walk(sfn.TEMPLATES_DIR):
# upload only *.template files
filenames = [ filename for filename in filenames if filename.endswith('.template') ]
print "Validating ", len(filenames), "templates from ", dirpath, " ..."
for filename in filenames:
try:
with open(os.path.join(dirpath, filename), 'r') as template:
response = cfn.validate_template(template_body=template.read())
except:
pass
except boto.exception.BotoServerError, e:
log.error(e.error_message)
@task
def validate_url(bucket_base_name):
"""
Validate templates via URLs of S3 objects.
"""
regions = boto.cloudformation.regions()
for region in regions:
bucket_name = bucket_base_name + '-' + region.name
try:
cfn = boto.connect_cloudformation(region=region)
for dirpath, dirnames, filenames in os.walk(sfn.TEMPLATES_DIR):
# upload only *.template files
filenames = [ filename for filename in filenames if filename.endswith('.template') ]
print "Validating ", len(filenames), "templates in ", bucket_name, " ..."
for filename in filenames:
keyname = os.path.relpath(os.path.join(dirpath, filename)).replace('\\', '/')
template_url = "http://s3"
# handle S3 legacy issue regarding region 'US Standard', see e.g.
# https://forums.aws.amazon.com/message.jspa?messageID=185820
if region.name != 'us-east-1':
template_url += ('-' + region.name)
template_url += ('.amazonaws.com/' + bucket_name + '/' + keyname)
log.info("... validating {0} ...".format(keyname))
template = cfn.validate_template(template_url=template_url)
except boto.exception.BotoServerError, e:
log.error(e.error_message)
@task
def download_aws_samples():
"""
Download the AWS CloudFormation sample templates.
"""
local('python ' + os.path.abspath(os.path.join('.', 'scripts', 'download-aws-cloudformation-samples.py')))