cloudformation-ruby-dsl 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,7 +29,7 @@ To convert existing JSON templates to use the DSL, run
29
29
  You may need to preface this with `bundle exec` if you installed via Bundler.
30
30
 
31
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
32
+ - `expand`: output the JSON template to the command line (takes optional `--minify true` to minimize the output)
33
33
  - `diff`: compare output with existing JSON for a stack
34
34
  - `cfn-validate-template`: run validation against the stack definition
35
35
  - `cfn-create-stack`: create a new stack from the output
@@ -70,11 +70,11 @@ template do
70
70
  }
71
71
 
72
72
  # Generates mappings from external files with various formats.
73
- mapping 'JsonExampleMap', 'maps/json_map.json'
73
+ mapping 'JsonExampleMap', 'maps/map.json'
74
74
 
75
- mapping 'RubyExampleMap', 'maps/ruby_map.rb'
75
+ mapping 'RubyExampleMap', 'maps/map.rb'
76
76
 
77
- mapping 'YamlExampleMap', 'maps/yaml_map.yaml'
77
+ mapping 'YamlExampleMap', 'maps/map.yaml'
78
78
 
79
79
  # Loads JSON mappings dynamically from example directory.
80
80
  Dir.entries('maps/more_maps').each_with_index do |path, index|
@@ -85,7 +85,7 @@ template do
85
85
  # Selects all rows in the table which match the name/value pairs of the predicate object and returns a
86
86
  # set of nested maps, where the key for the map at level n is the key at index n in the specified keys,
87
87
  # except for the last key in the specified keys which is used to determine the value of the leaf-level map.
88
- text = Table.load 'maps/text_table.txt'
88
+ text = Table.load 'maps/table.txt'
89
89
  mapping 'TableExampleMap',
90
90
  text.get_map({ :column0 => 'foo' }, :column1, :column2, :column3)
91
91
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "Mappings": {
3
- "bar": {
3
+ "JsonExampleMap": {
4
4
  "foo": "bar"
5
5
  }
6
6
  }
@@ -0,0 +1,5 @@
1
+ {
2
+ 'Mappings' => {
3
+ 'RubyExampleMap' => { 'baz' => 'blah' }
4
+ }
5
+ }
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  Mappings:
3
- blah:
3
+ YamlExampleMap:
4
4
  foo:
5
5
  bar: yo
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "Mappings": {
3
- "john": {
3
+ "ExampleMap1": {
4
4
  "gregory": "smith"
5
5
  }
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "Mappings": {
3
- "gregory": {
3
+ "ExampleMap2": {
4
4
  "smith": "john"
5
5
  }
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "Mappings": {
3
- "smith": {
3
+ "ExampleMap3": {
4
4
  "john": "gregory"
5
5
  }
6
6
  }
@@ -1,3 +1,5 @@
1
1
  Several people were instrumental in building this project during its incubation stage. Because the process of open sourcing involved squashing the repository to remove confidential information, some of their contributions have been lost from the history that Github displays.
2
2
 
3
+ - [Shawn Smith](https://github.com/shawnsmith): initial implementation
3
4
  - [Nathaniel Eliot](https://github.com/temujin9): converted from raw library to gem, refactored table code, cleaned and prepared code for open sourcing
5
+ - [Jona Fenocchi](http://github.com/jonaf): added support for CloudFormation tags
@@ -35,7 +35,8 @@ end
35
35
  # Parse command-line arguments based on cfn-cmd syntax (cfn-create-stack etc.) and return the parameters and region
36
36
  def cfn_parse_args
37
37
  parameters = {}
38
- region = 'us-east-1'
38
+ region = ENV['EC2_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
39
+ minify = false
39
40
  ARGV.slice_before(/^--/).each do |name, value|
40
41
  next unless value
41
42
  case name
@@ -43,9 +44,11 @@ def cfn_parse_args
43
44
  parameters = Hash[value.split(/;/).map { |pair| pair.split(/=/, 2) }]
44
45
  when '--region'
45
46
  region = value
47
+ when '--minify'
48
+ minify = value === "true"
46
49
  end
47
50
  end
48
- [parameters, region]
51
+ [parameters, region, minify]
49
52
  end
50
53
 
51
54
  def cfn_cmd(template)
@@ -68,23 +71,27 @@ def cfn_cmd(template)
68
71
  # The command line string looks like: --tag "Key=key; Value=value" --tag "Key2=key2; Value2=value"
69
72
  cfn_tags_options = cfn_tags.sort.map { |tag| ["--tag", "Key=%s; Value=%s" % tag.split('=')] }.flatten
70
73
 
71
- template_string = JSON.pretty_generate(template)
74
+ if action == 'expand'
75
+ # Write the pretty-printed JSON template to stdout and exit. [--minify true] option writes minified output
76
+ # example: <template.rb> expand --parameters "Env=prod" --region eu-west-1 --minify true
77
+ if template.minify
78
+ puts JSON.generate(template)
79
+ else
80
+ puts JSON.pretty_generate(template)
81
+ end
82
+
83
+ exit(true)
84
+ end
72
85
 
73
86
  # example: <template.rb> cfn-create-stack my-stack-name --parameters "Env=prod" --region eu-west-1
74
87
  # Execute the AWS CLI cfn-cmd command to validate/create/update a CloudFormation stack.
88
+ template_string = JSON.generate(template)
75
89
  temp_file = File.absolute_path("#{$PROGRAM_NAME}.expanded.json")
76
90
  File.write(temp_file, template_string)
77
91
 
78
92
  cmdline = ['cfn-cmd'] + ARGV + ['--template-file', temp_file] + cfn_tags_options
79
93
 
80
94
  case action
81
- when 'expand'
82
- # Write the pretty-printed JSON template to stdout.
83
- # example: <template.rb> expand --parameters "Env=prod" --region eu-west-1
84
- puts template_string
85
-
86
- exit(true)
87
-
88
95
  when 'diff'
89
96
  # example: <template.rb> diff my-stack-name --parameters "Env=prod" --region eu-west-1
90
97
  # Diff the current template for an existing stack with the expansion of this template.
@@ -190,7 +197,7 @@ def cfn_cmd(template)
190
197
  end
191
198
 
192
199
  def exec_describe_stack cfn_options_string
193
- csv_data = exec_capture_stdout("cfn-cmd cfn-describe-stacks #{cfn_options_string} --headers --show-long")
200
+ csv_data = exec_capture_stdout("cfn-cmd cfn-describe-stacks --stack-name #{cfn_options_string} --headers --show-long")
194
201
  CSV.parse_line(csv_data, :headers => true, :converters => :nil_to_nil)
195
202
  end
196
203
 
@@ -254,10 +261,10 @@ end
254
261
 
255
262
  # Core interpreter for the DSL
256
263
  class TemplateDSL < JsonObjectDSL
257
- attr_reader :parameters, :aws_region
264
+ attr_reader :parameters, :aws_region, :minify
258
265
 
259
266
  def initialize()
260
- @parameters, @aws_region = cfn_parse_args
267
+ @parameters, @aws_region, @minify = cfn_parse_args
261
268
  super
262
269
  end
263
270
 
@@ -285,7 +292,7 @@ class TemplateDSL < JsonObjectDSL
285
292
  # if options is a string and a valid file then the script will process the external file.
286
293
  default(:Mappings, {})[name] = \
287
294
  if options.is_a?(Hash); options
288
- elsif options.is_a?(String); load_from_file(options)['Mappings']
295
+ elsif options.is_a?(String); load_from_file(options)['Mappings'][name]
289
296
  else; raise("Options for mapping #{name} is neither a string or a hash. Error!")
290
297
  end
291
298
  end
@@ -15,7 +15,7 @@
15
15
  module Cfn
16
16
  module Ruby
17
17
  module Dsl
18
- VERSION = "0.4.2"
18
+ VERSION = "0.4.3"
19
19
  end
20
20
  end
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudformation-ruby-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -16,7 +16,7 @@ authors:
16
16
  autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
- date: 2014-04-28 00:00:00.000000000 Z
19
+ date: 2014-06-09 00:00:00.000000000 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: detabulator
@@ -119,13 +119,13 @@ files:
119
119
  - bin/vendor-cfn-bin
120
120
  - cloudformation-ruby-dsl.gemspec
121
121
  - examples/cloudformation-ruby-script.rb
122
- - examples/maps/json_map.json
122
+ - examples/maps/map.json
123
+ - examples/maps/map.rb
124
+ - examples/maps/map.yaml
123
125
  - examples/maps/more_maps/map1.json
124
126
  - examples/maps/more_maps/map2.json
125
127
  - examples/maps/more_maps/map3.json
126
- - examples/maps/ruby_map.rb
127
- - examples/maps/text_table.txt
128
- - examples/maps/yaml_map.yaml
128
+ - examples/maps/table.txt
129
129
  - examples/userdata.sh
130
130
  - initial_contributions.md
131
131
  - lib/cloudformation-ruby-dsl.rb
@@ -1,5 +0,0 @@
1
- {
2
- 'Mappings' => {
3
- 'blah' => { 'baz' => 'blah' }
4
- }
5
- }