cloudformation-ruby-dsl 0.4.3 → 0.4.4

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 (takes optional `--minify true` to minimize the output)
32
+ - `expand`: output the JSON template to the command line (takes optional `--nopretty` 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
@@ -36,19 +36,18 @@ end
36
36
  def cfn_parse_args
37
37
  parameters = {}
38
38
  region = ENV['EC2_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
39
- minify = false
39
+ nopretty = false
40
40
  ARGV.slice_before(/^--/).each do |name, value|
41
- next unless value
42
41
  case name
43
42
  when '--parameters'
44
43
  parameters = Hash[value.split(/;/).map { |pair| pair.split(/=/, 2) }]
45
44
  when '--region'
46
45
  region = value
47
- when '--minify'
48
- minify = value === "true"
46
+ when '--nopretty'
47
+ nopretty = true
49
48
  end
50
49
  end
51
- [parameters, region, minify]
50
+ [parameters, region, nopretty]
52
51
  end
53
52
 
54
53
  def cfn_cmd(template)
@@ -71,21 +70,25 @@ def cfn_cmd(template)
71
70
  # The command line string looks like: --tag "Key=key; Value=value" --tag "Key2=key2; Value2=value"
72
71
  cfn_tags_options = cfn_tags.sort.map { |tag| ["--tag", "Key=%s; Value=%s" % tag.split('=')] }.flatten
73
72
 
73
+ # example: <template.rb> cfn-create-stack my-stack-name --parameters "Env=prod" --region eu-west-1
74
+ # Execute the AWS CLI cfn-cmd command to validate/create/update a CloudFormation stack.
75
+ if action == 'diff' or (action == 'expand' and not template.nopretty)
76
+ template_string = JSON.pretty_generate(template)
77
+ else
78
+ template_string = JSON.generate(template)
79
+ end
80
+
74
81
  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)
82
+ # Write the pretty-printed JSON template to stdout and exit. [--nopretty] option writes output with minimal whitespace
83
+ # example: <template.rb> expand --parameters "Env=prod" --region eu-west-1 --nopretty
84
+ if template.nopretty
85
+ puts template_string
79
86
  else
80
- puts JSON.pretty_generate(template)
87
+ puts template_string
81
88
  end
82
-
83
89
  exit(true)
84
90
  end
85
91
 
86
- # example: <template.rb> cfn-create-stack my-stack-name --parameters "Env=prod" --region eu-west-1
87
- # Execute the AWS CLI cfn-cmd command to validate/create/update a CloudFormation stack.
88
- template_string = JSON.generate(template)
89
92
  temp_file = File.absolute_path("#{$PROGRAM_NAME}.expanded.json")
90
93
  File.write(temp_file, template_string)
91
94
 
@@ -110,7 +113,10 @@ def cfn_cmd(template)
110
113
 
111
114
  # Run CloudFormation commands to describe the existing stack
112
115
  cfn_options_string = cfn_options.map { |arg| "'#{arg}'" }.join(' ')
113
- old_template_string = exec_capture_stdout("cfn-cmd cfn-get-template #{cfn_options_string}")
116
+ old_template_raw = exec_capture_stdout("cfn-cmd cfn-get-template #{cfn_options_string}")
117
+ # ec2 template output is not valid json: TEMPLATE "<json>\n"\n
118
+ old_template_object = JSON.parse(old_template_raw[11..-3])
119
+ old_template_string = JSON.pretty_generate(old_template_object)
114
120
  old_stack_attributes = exec_describe_stack(cfn_options_string)
115
121
  old_tags_string = old_stack_attributes["TAGS"]
116
122
  old_parameters_string = old_stack_attributes["PARAMETERS"]
@@ -127,7 +133,7 @@ def cfn_cmd(template)
127
133
  old_temp_file = File.absolute_path("#{$PROGRAM_NAME}.current.json")
128
134
  new_temp_file = File.absolute_path("#{$PROGRAM_NAME}.expanded.json")
129
135
  File.write(old_temp_file, old_tags_string + old_parameters_string + old_template_string)
130
- File.write(new_temp_file, tags_string + parameters_string + %Q(TEMPLATE "#{template_string}\n"\n))
136
+ File.write(new_temp_file, tags_string + parameters_string + template_string)
131
137
 
132
138
  # Compare templates
133
139
  system(*["diff"] + diff_options + [old_temp_file, new_temp_file])
@@ -261,10 +267,10 @@ end
261
267
 
262
268
  # Core interpreter for the DSL
263
269
  class TemplateDSL < JsonObjectDSL
264
- attr_reader :parameters, :aws_region, :minify
270
+ attr_reader :parameters, :aws_region, :nopretty
265
271
 
266
272
  def initialize()
267
- @parameters, @aws_region, @minify = cfn_parse_args
273
+ @parameters, @aws_region, @nopretty = cfn_parse_args
268
274
  super
269
275
  end
270
276
 
@@ -15,7 +15,7 @@
15
15
  module Cfn
16
16
  module Ruby
17
17
  module Dsl
18
- VERSION = "0.4.3"
18
+ VERSION = "0.4.4"
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.3
4
+ version: 0.4.4
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-06-09 00:00:00.000000000 Z
19
+ date: 2014-07-11 00:00:00.000000000 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: detabulator