cloudformation-ruby-dsl 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +29 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +201 -0
- data/README.md +68 -0
- data/Rakefile +1 -0
- data/bin/cfntemplate-to-ruby +327 -0
- data/cloudformation-ruby-dsl.gemspec +36 -0
- data/examples/cloudformation-ruby-script.rb +195 -0
- data/examples/maps/json_map.json +9 -0
- data/examples/maps/more_maps/map1.json +8 -0
- data/examples/maps/more_maps/map2.json +8 -0
- data/examples/maps/more_maps/map3.json +8 -0
- data/examples/maps/ruby_map.rb +5 -0
- data/examples/maps/text_table.txt +5 -0
- data/examples/maps/yaml_map.yaml +5 -0
- data/examples/userdata.sh +2 -0
- data/initial_contributions.md +3 -0
- data/lib/cloudformation-ruby-dsl.rb +2 -0
- data/lib/cloudformation-ruby-dsl/cfntemplate.rb +406 -0
- data/lib/cloudformation-ruby-dsl/spotprice.rb +50 -0
- data/lib/cloudformation-ruby-dsl/table.rb +93 -0
- data/lib/cloudformation-ruby-dsl/version.rb +21 -0
- metadata +100 -0
data/.gitignore
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
*.swp
|
2
|
+
maestro.json
|
3
|
+
.rakeTasks
|
4
|
+
.DS_Store
|
5
|
+
target
|
6
|
+
*.iml
|
7
|
+
.idea/
|
8
|
+
*.swo
|
9
|
+
/manifests
|
10
|
+
/modules
|
11
|
+
cloudformation/expanded/
|
12
|
+
*.gem
|
13
|
+
*.rbc
|
14
|
+
.bundle
|
15
|
+
.config
|
16
|
+
.yardoc
|
17
|
+
Gemfile.lock
|
18
|
+
InstalledFiles
|
19
|
+
_yardoc
|
20
|
+
coverage
|
21
|
+
doc/
|
22
|
+
lib/bundler/man
|
23
|
+
pkg
|
24
|
+
rdoc
|
25
|
+
spec/reports
|
26
|
+
test/tmp
|
27
|
+
test/version_tmp
|
28
|
+
tmp
|
29
|
+
.rvmrc
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2014 Bazaarvoice
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# cloudformation-ruby-dsl
|
2
|
+
|
3
|
+
A Ruby DSL and helper utilities for building Cloudformation templates dynamically.
|
4
|
+
|
5
|
+
Written by [Bazaarvoice](http://www.bazaarvoice.com): see [the contributors page](https://github.com/bazaarvoice/cloudformation-ruby-dsl/graphs/contributors) and [the initial contributions](https://github.com/bazaarvoice/cloudformation-ruby-dsl/blob/master/initial_contributions.md) for more details.
|
6
|
+
|
7
|
+
## Motivation
|
8
|
+
|
9
|
+
Cloudformation templates often contain repeated stanzas, information which must be loaded from external sources, and other functionality that would be easier handled as code, instead of configuration.
|
10
|
+
|
11
|
+
Consider when a userdata script needs to be added to a Cloudformation template. Traditionally, you would re-write the script by hand in a valid JSON format. Using the DSL, you can specify the file containing the script and generate the correct information at runtime.
|
12
|
+
|
13
|
+
:UserData => base64(interpolate(file('userdata.sh')))
|
14
|
+
|
15
|
+
Additionally, Cloudformation templates are just massive JSON documents, making general readability and reusability an issue. The DSL allows not only a cleaner format (and comments!), but will also allow the same DSL template to be reused as needed.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Run `gem install cloudformation-ruby-dsl` to install system-wide.
|
20
|
+
|
21
|
+
To use in a specific project, add `gem 'cloudformation-ruby-dsl'` to your Gemfile, and then run `bundle`.
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
To convert existing JSON templates to use the DSL, run
|
26
|
+
|
27
|
+
cfntemplate-to-ruby [EXISTING_CFN] > [NEW_NAME.rb]
|
28
|
+
|
29
|
+
You may need to preface this with `bundle exec` if you installed via Bundler.
|
30
|
+
|
31
|
+
Make the resulting file executable (`chmod +x [NEW_NAME.rb]`). It can respond to the following subcommands (which are listed if you run without parameters):
|
32
|
+
- `expand`: output the JSON template to the command line
|
33
|
+
- `diff`: compare output with existing JSON for a stack
|
34
|
+
- `cfn-validate-template`: run validation against the stack definition
|
35
|
+
- `cfn-create-stack`: create a new stack from the output
|
36
|
+
- `cfn-update-stack`: update an existing stack from the output
|
37
|
+
|
38
|
+
Below are the various functions currently available in the DSL. See [the example script](examples/cloudformation-ruby-script.rb) for more usage information.
|
39
|
+
|
40
|
+
### DSL Statements
|
41
|
+
|
42
|
+
Add the named object to the appropriate collection.
|
43
|
+
- `parameter(name, options)` (may be marked :Immutable, which will raise error on a later change)
|
44
|
+
- `mapping(name, options)`
|
45
|
+
- `condition(name, options)`
|
46
|
+
- `resource(name, options)`
|
47
|
+
- `output(name, options)`
|
48
|
+
|
49
|
+
### Cloudformation Function Calls
|
50
|
+
|
51
|
+
Invoke a native Cloudformation function.
|
52
|
+
- `base64(value)`
|
53
|
+
- `find_in_map(map, key, name)`
|
54
|
+
- `get_att(resource, attribute)`
|
55
|
+
- `get_azs(region)`
|
56
|
+
- `join(delim, *list)`
|
57
|
+
- `select(index, list)`
|
58
|
+
- `ref(name)`
|
59
|
+
- `no_value()`
|
60
|
+
|
61
|
+
### Utility Functions
|
62
|
+
|
63
|
+
Additional capabilities for file inclusion, etc.
|
64
|
+
- `tag(tag)`: add tags to the stack, which are inherited by all resources in that stack; can only be used at launch
|
65
|
+
- `file(name)`: return the named file as a string, for further use
|
66
|
+
- `load_from_file(filename)`: load the named file by a given type; currently handles YAML, JSON, and Ruby
|
67
|
+
- `interpolate(string)`: embed CFN references into a string (`{{ref('Service')}}`) for later interpretation by the CFN engine
|
68
|
+
- `Table.load(filename)`: load a table from the listed file, which can then be turned into mappings (via `get_map`)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,327 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright 2013-2014 Bazaarvoice, Inc.
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
unless RUBY_VERSION >= '1.9'
|
18
|
+
# Ruby 1.9 preserves order within Hash objects which avoid scrambling the template output.
|
19
|
+
$stderr.puts "This script requires ruby 1.9+. On OS/X use Homebrew to install ruby 1.9:"
|
20
|
+
$stderr.puts " brew install ruby"
|
21
|
+
exit(2)
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'json'
|
25
|
+
|
26
|
+
def main(argv)
|
27
|
+
unless (argv & %w(-h --help -?)).empty?
|
28
|
+
$stderr.puts <<"EOF"
|
29
|
+
usage: #{$PROGRAM_NAME} [cloudformation-template.json] ...
|
30
|
+
|
31
|
+
Converts the specified CloudFormation JSON template or template fragment to
|
32
|
+
Ruby DSL syntax. Reads from stdin or from the specified json files. Note
|
33
|
+
that the input must be valid JSON.
|
34
|
+
|
35
|
+
Examples:
|
36
|
+
|
37
|
+
# Convert a JSON CloudFormation template to Ruby DSL syntax
|
38
|
+
#{$PROGRAM_NAME} my-template.json > my-template.rb
|
39
|
+
chmod +x my-template.rb
|
40
|
+
|
41
|
+
# Convert the JSON fragment in the clipboard to Ruby DSL syntax
|
42
|
+
pbpaste | #{$PROGRAM_NAME} | less
|
43
|
+
|
44
|
+
EOF
|
45
|
+
exit(2)
|
46
|
+
end
|
47
|
+
if argv.empty?
|
48
|
+
pprint(simplify(JSON.parse($stdin.read)))
|
49
|
+
else
|
50
|
+
argv.each do |filename|
|
51
|
+
pprint(simplify(JSON.parse(File.read(filename))))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
# The user should make the resulting template executable w/chmod +x
|
55
|
+
end
|
56
|
+
|
57
|
+
class FnCall
|
58
|
+
attr_reader :name, :arguments, :multiline
|
59
|
+
|
60
|
+
def initialize(name, arguments, multiline = false)
|
61
|
+
@name = name
|
62
|
+
@arguments = arguments
|
63
|
+
@multiline = multiline
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_s()
|
67
|
+
@name + "(" + @arguments.join(', ') + ")"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def pprint(val)
|
72
|
+
case detect_type(val)
|
73
|
+
when :template
|
74
|
+
pprint_cfn_template(val)
|
75
|
+
when :parameter
|
76
|
+
pprint_cfn_section 'parameter', 'TODO', val
|
77
|
+
when :resource
|
78
|
+
pprint_cfn_resource 'TODO', val
|
79
|
+
when :parameters
|
80
|
+
val.each { |k, v| pprint_cfn_section 'parameter', k, v }
|
81
|
+
when :resources
|
82
|
+
val.each { |k, v| pprint_cfn_resource k, v }
|
83
|
+
else
|
84
|
+
pprint_value(val, '')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Attempt to figure out what fragment of the template we have. This is imprecise and can't
|
89
|
+
# detect Mappings and Outputs sections reliably, so it doesn't attempt to.
|
90
|
+
def detect_type(val)
|
91
|
+
if val.is_a?(Hash) && val['AWSTemplateFormatVersion']
|
92
|
+
:template
|
93
|
+
elsif val.is_a?(Hash) && /^(String|Number)$/ =~ val['Type']
|
94
|
+
:parameter
|
95
|
+
elsif val.is_a?(Hash) && val['Type']
|
96
|
+
:resource
|
97
|
+
elsif val.is_a?(Hash) && val.values.all? { |v| detect_type(v) == :parameter }
|
98
|
+
:parameters
|
99
|
+
elsif val.is_a?(Hash) && val.values.all? { |v| detect_type(v) == :resource }
|
100
|
+
:resources
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def pprint_cfn_template(tpl)
|
105
|
+
puts "#!/usr/bin/env ruby"
|
106
|
+
puts
|
107
|
+
puts "require 'bundler/setup'"
|
108
|
+
puts "require 'cloudformation-ruby-dsl/cfntemplate'"
|
109
|
+
puts "require 'cloudformation-ruby-dsl/spotprice'"
|
110
|
+
puts "require 'cloudformation-ruby-dsl/table'"
|
111
|
+
puts
|
112
|
+
puts "template do"
|
113
|
+
puts
|
114
|
+
tpl.each do |section, v|
|
115
|
+
case section
|
116
|
+
when 'Parameters'
|
117
|
+
v.each { |name, options| pprint_cfn_section 'parameter', name, options }
|
118
|
+
when 'Mappings'
|
119
|
+
v.each { |name, options| pprint_cfn_section 'mapping', name, options }
|
120
|
+
when 'Resources'
|
121
|
+
v.each { |name, options| pprint_cfn_resource name, options }
|
122
|
+
when 'Outputs'
|
123
|
+
v.each { |name, options| pprint_cfn_section 'output', name, options }
|
124
|
+
else
|
125
|
+
print " value #{fmt_key(section)} => "
|
126
|
+
pprint_value v, ' '
|
127
|
+
puts
|
128
|
+
puts
|
129
|
+
end
|
130
|
+
end
|
131
|
+
puts "end.exec!"
|
132
|
+
end
|
133
|
+
|
134
|
+
def pprint_cfn_section(section, name, options)
|
135
|
+
print " #{section} #{fmt_string(name)}"
|
136
|
+
indent = ' ' + (' ' * section.length) + ' '
|
137
|
+
options.each do |k, v|
|
138
|
+
puts ","
|
139
|
+
print indent, fmt_key(k), " => "
|
140
|
+
pprint_value v, indent
|
141
|
+
end
|
142
|
+
puts
|
143
|
+
puts
|
144
|
+
end
|
145
|
+
|
146
|
+
def pprint_cfn_resource(name, options)
|
147
|
+
print " resource #{fmt_string(name)}"
|
148
|
+
indent = ' '
|
149
|
+
options.each do |k, v|
|
150
|
+
unless k == 'Properties'
|
151
|
+
print ", #{fmt_key(k)} => #{fmt(v)}"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
if options.key?('Properties')
|
155
|
+
print ", #{fmt_key('Properties')} => "
|
156
|
+
pprint_value options['Properties'], indent
|
157
|
+
end
|
158
|
+
puts
|
159
|
+
puts
|
160
|
+
end
|
161
|
+
|
162
|
+
def pprint_value(val, indent)
|
163
|
+
# Prefer to print the value on a single line if it's reasonable to do so
|
164
|
+
single_line = is_single_line(val) || is_single_line_hack(val)
|
165
|
+
if single_line && !is_multi_line_hack(val)
|
166
|
+
s = fmt(val)
|
167
|
+
if s.length < 120 || is_single_line_hack(val)
|
168
|
+
print s
|
169
|
+
return
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Print the value across multiple lines
|
174
|
+
if val.is_a?(Hash)
|
175
|
+
puts "{"
|
176
|
+
val.each do |k, v|
|
177
|
+
print "#{indent} #{fmt_key(k)} => "
|
178
|
+
pprint_value v, indent + ' '
|
179
|
+
puts ","
|
180
|
+
end
|
181
|
+
print "#{indent}}"
|
182
|
+
|
183
|
+
elsif val.is_a?(Array)
|
184
|
+
puts "["
|
185
|
+
val.each do |v|
|
186
|
+
print "#{indent} "
|
187
|
+
pprint_value v, indent + ' '
|
188
|
+
puts ","
|
189
|
+
end
|
190
|
+
print "#{indent}]"
|
191
|
+
|
192
|
+
elsif val.is_a?(FnCall) && val.multiline
|
193
|
+
print val.name, "("
|
194
|
+
args = val.arguments
|
195
|
+
sep = ''
|
196
|
+
sub_indent = indent + ' '
|
197
|
+
if val.name == 'join' && args.length > 1
|
198
|
+
pprint_value args[0], indent + ' '
|
199
|
+
args = args[1..-1]
|
200
|
+
sep = ','
|
201
|
+
sub_indent = indent + ' '
|
202
|
+
end
|
203
|
+
unless args.empty?
|
204
|
+
args.each do |v|
|
205
|
+
puts sep
|
206
|
+
print sub_indent
|
207
|
+
pprint_value v, sub_indent
|
208
|
+
sep = ','
|
209
|
+
end
|
210
|
+
if val.name == 'join' && args.length > 1
|
211
|
+
print ","
|
212
|
+
end
|
213
|
+
puts
|
214
|
+
print indent
|
215
|
+
end
|
216
|
+
print ")"
|
217
|
+
|
218
|
+
else
|
219
|
+
print fmt(val)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def is_single_line(val)
|
224
|
+
if val.is_a?(Hash)
|
225
|
+
is_single_line(val.values)
|
226
|
+
elsif val.is_a?(Array)
|
227
|
+
val.empty? ||
|
228
|
+
(val.length == 1 && is_single_line(val[0]) && !val[0].is_a?(Hash)) ||
|
229
|
+
val.all? { |v| v.is_a?(String) }
|
230
|
+
else
|
231
|
+
true
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# Emo-specific hacks to force the desired output formatting
|
236
|
+
def is_single_line_hack(val)
|
237
|
+
is_array_of_strings_hack(val)
|
238
|
+
end
|
239
|
+
|
240
|
+
# Emo-specific hacks to force the desired output formatting
|
241
|
+
def is_multi_line_hack(val)
|
242
|
+
val.is_a?(Hash) && val['email']
|
243
|
+
end
|
244
|
+
|
245
|
+
# Emo-specific hacks to force the desired output formatting
|
246
|
+
def is_array_of_strings_hack(val)
|
247
|
+
val.is_a?(Array) && val.all? { |v| v.is_a?(String) } && val.grep(/\s/).empty? && (
|
248
|
+
val.include?('autoscaling:EC2_INSTANCE_LAUNCH') ||
|
249
|
+
val.include?('m1.small')
|
250
|
+
)
|
251
|
+
end
|
252
|
+
|
253
|
+
def fmt(val)
|
254
|
+
if val == {}
|
255
|
+
'{}'
|
256
|
+
elsif val == []
|
257
|
+
'[]'
|
258
|
+
elsif val.is_a?(Hash)
|
259
|
+
'{ ' + (val.map { |k,v| fmt_key(k) + ' => ' + fmt(v) }).join(', ') + ' }'
|
260
|
+
elsif val.is_a?(Array) && is_array_of_strings_hack(val)
|
261
|
+
'%w(' + val.join(' ') + ')'
|
262
|
+
elsif val.is_a?(Array)
|
263
|
+
'[ ' + (val.map { |v| fmt(v) }).join(', ') + ' ]'
|
264
|
+
elsif val.is_a?(FnCall) && val.arguments.empty?
|
265
|
+
val.name
|
266
|
+
elsif val.is_a?(FnCall)
|
267
|
+
val.name + '(' + (val.arguments.map { |v| fmt(v) }).join(', ') + ')'
|
268
|
+
elsif val.is_a?(String)
|
269
|
+
fmt_string(val)
|
270
|
+
elsif val == nil
|
271
|
+
'null'
|
272
|
+
else
|
273
|
+
val.to_s # number, boolean
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def fmt_key(s)
|
278
|
+
':' + (/^[a-zA-Z_]\w+$/ =~ s ? s : fmt_string(s)) # returns a symbol like :Foo or :'us-east-1'
|
279
|
+
end
|
280
|
+
|
281
|
+
def fmt_string(s)
|
282
|
+
if /[^ -~]/ =~ s
|
283
|
+
s.dump # contains, non-ascii or control char, return double-quoted string
|
284
|
+
else
|
285
|
+
'\'' + s.gsub(/([\\'])/, '\\\\\1') + '\'' # return single-quoted string, escape \ and '
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def simplify(val)
|
290
|
+
if val.is_a?(Hash)
|
291
|
+
val = Hash[val.map { |k,v| [k, simplify(v)] }]
|
292
|
+
if val.length != 1
|
293
|
+
val
|
294
|
+
else
|
295
|
+
k, v = val.entries[0]
|
296
|
+
# CloudFormation functions
|
297
|
+
case
|
298
|
+
when k == 'Fn::Base64'
|
299
|
+
FnCall.new 'base64', [v], true
|
300
|
+
when k == 'Fn::FindInMap'
|
301
|
+
FnCall.new 'find_in_map', v
|
302
|
+
when k == 'Fn::GetAtt'
|
303
|
+
FnCall.new 'get_att', v
|
304
|
+
when k == 'Fn::GetAZs'
|
305
|
+
FnCall.new 'get_azs', v != '' ? [v] : []
|
306
|
+
when k == 'Fn::Join'
|
307
|
+
FnCall.new 'join', [v[0]] + v[1], true
|
308
|
+
when k == 'Fn::Select'
|
309
|
+
FnCall.new 'select', v
|
310
|
+
when k == 'Ref' && v == 'AWS::Region'
|
311
|
+
FnCall.new 'aws_region', []
|
312
|
+
when k == 'Ref' && v == 'AWS::StackName'
|
313
|
+
FnCall.new 'aws_stack_name', []
|
314
|
+
when k == 'Ref'
|
315
|
+
FnCall.new 'ref', [v]
|
316
|
+
else
|
317
|
+
val
|
318
|
+
end
|
319
|
+
end
|
320
|
+
elsif val.is_a?(Array)
|
321
|
+
val.map { |v| simplify(v) }
|
322
|
+
else
|
323
|
+
val
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
main(ARGV)
|