Top Level Namespace

Defined Under Namespace

Modules: Cfn Classes: AwsCfn, JsonObjectDSL, Namespace, Struct, Table, TemplateDSL

Constant Summary collapse

SPOT_PRICES_BY_INSTANCE_TYPE =

This list of prices was sourced from the on-demand prices current as of 12/3/2012. We expect the actual price we pay per instance to be roughly 1/10 the prices below.

{
    "m1.small"    => 0.065,
    "m1.medium"   => 0.130,
    "m1.large"    => 0.260,
    "m1.xlarge"   => 0.520,
    "m3.xlarge"   => 0.580,
    "m3.2xlarge"  => 1.160,
    "t1.micro"    => 0.020,
    "m2.xlarge"   => 0.450,
    "m2.2xlarge"  => 0.900,
    "m2.4xlarge"  => 1.800,
    "c1.medium"   => 0.165,
    "c1.xlarge"   => 0.660,
    "cc1.4xlarge" => 1.300,
    "cc2.8xlarge" => 2.400,
    "cg1.4xlarge" => 2.100,
    "hi1.4xlarge" => 3.100,
    "hs1.8xlarge" => 4.600,
    "cr1.8xlarge" => 4.000,
}

Instance Method Summary collapse

Instance Method Details

#aws_account_idObject



211
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 211

def () ref("AWS::AccountId") end

#aws_no_valueObject



215
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 215

def aws_no_value() ref("AWS::NoValue") end

#aws_notification_arnsObject



213
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 213

def aws_notification_arns() ref("AWS::NotificationARNs") end

#aws_stack_idObject



217
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 217

def aws_stack_id() ref("AWS::StackId") end

#aws_stack_nameObject



219
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 219

def aws_stack_name() ref("AWS::StackName") end

#base64(value) ⇒ Object



165
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 165

def base64(value) { :'Fn::Base64' => value } end

#cfn(template) ⇒ Object



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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 138

def cfn(template)
  aws_cfn = AwsCfn.new({:region => template.aws_region, :aws_profile => template.aws_profile})
  cfn_client = aws_cfn.cfn_client

  action = validate_action( ARGV[0] )

  # Find parameters where extension attribute :Immutable is true then remove it from the
  # cfn template since we can't pass it to CloudFormation.
  immutable_parameters = template.excise_parameter_attribute!(:Immutable)

  # Tag CloudFormation stacks based on :Tags defined in the template.
  # Remove them from the template as well, so that the template is valid.
  cfn_tags = template.excise_tags!

  # Find tags where extension attribute `:Immutable` is true then remove it from the
  # tag's properties hash since it can't be passed to CloudFormation.
  immutable_tags = template.get_tag_attribute(cfn_tags, :Immutable)

  cfn_tags.each {|k, v| cfn_tags[k] = v[:Value].to_s}

  if action == 'diff' or (action == 'expand' and not template.nopretty)
    template_string = JSON.pretty_generate(template)
  else
    template_string = JSON.generate(template)
  end

  # Derive stack name from ARGV
  _, options = extract_options(ARGV[1..-1], %w(--nopretty), %w(--profile --stack-name --region --parameters --tag))
  # If the first argument is not an option and stack_name is undefined, assume it's the stack name
  # The second argument, if present, is the resource name used by the describe-resource command
  if template.stack_name.nil?
    stack_name = options.shift if options[0] && !(/^-/ =~ options[0])
    resource_name = options.shift if options[0] && !(/^-/ =~ options[0])
  else
    stack_name = template.stack_name
  end

  case action
  when 'expand'
    # Write the pretty-printed JSON template to stdout and exit.  [--nopretty] option writes output with minimal whitespace
    # example: <template.rb> expand --parameters "Env=prod" --region eu-west-1 --nopretty
    if template.nopretty
      puts template_string
    else
      puts template_string
    end
    exit(true)

  when 'diff'
    # example: <template.rb> diff my-stack-name --parameters "Env=prod" --region eu-west-1
    # Diff the current template for an existing stack with the expansion of this template.

    # `diff` operation exit codes are:
      # 0 - no differences are found. Outputs nothing to make it easy to use the output of the diff call from within other scripts.
      # 1 - produced by any ValidationError exception (e.g. "Stack with id does not exist")
      # 2 - there are changes to update (tags, params, template)
    # If you want output of the entire file, simply use this option with a large number, i.e., -U 10000
    # In fact, this is what Diffy does by default; we just don't want that, and we can't support passing arbitrary options to diff
    # because Diffy's "context" configuration is mutually exclusive with the configuration to pass arbitrary options to diff
    if !options.include? '-U'
      options.push('-U', '0')
    end

    # Ensure a stack name was provided
    if stack_name.empty?
      $stderr.puts "Error: a stack name is required"
      exit(false)
    end

    # describe the existing stack
    begin
      old_template_body = cfn_client.get_template({stack_name: stack_name}).template_body
    rescue Aws::CloudFormation::Errors::ValidationError => e
      $stderr.puts "Error: #{e}"
      exit(false)
    end

    # parse the string into a Hash, then convert back into a string; this is the only way Ruby JSON lets us pretty print a JSON string
    old_template   = JSON.pretty_generate(JSON.parse(old_template_body))
    # there is only ever one stack, since stack names are unique
    old_attributes = cfn_client.describe_stacks({stack_name: stack_name}).stacks[0]
    old_tags       = old_attributes.tags
    old_parameters = old_attributes.parameters

    # Sort the tag strings alphabetically to make them easily comparable
    old_tags_string = old_tags.map { |tag| %Q(TAG "#{tag.key}=#{tag.value}"\n) }.sort.join
    tags_string     = cfn_tags.map { |k, v| %Q(TAG "#{k.to_s}=#{v}"\n) }.sort.join

    # Sort the parameter strings alphabetically to make them easily comparable
    old_parameters_string = old_parameters.sort! {|pCurrent, pNext| pCurrent.parameter_key <=> pNext.parameter_key }.map { |param| %Q(PARAMETER "#{param.parameter_key}=#{param.parameter_value}"\n) }.join
    parameters_string     = template.parameters.sort.map { |key, value| "PARAMETER \"#{key}=#{value}\"\n" }.join

    # set default diff options
    Diffy::Diff.default_options.merge!(
      :diff    => "#{options.join(' ')}",
    )
    # set default diff output
    Diffy::Diff.default_format = :color

    tags_diff     = Diffy::Diff.new(old_tags_string, tags_string).to_s.strip!
    params_diff   = Diffy::Diff.new(old_parameters_string, parameters_string).to_s.strip!
    template_diff = Diffy::Diff.new(old_template, template_string).to_s.strip!

    if !tags_diff.empty?
      puts "====== Tags ======"
      puts tags_diff
      puts "=================="
      puts
    end

    if !params_diff.empty?
      puts "====== Parameters ======"
      puts params_diff
      puts "========================"
      puts
    end

    if !template_diff.empty?
      puts "====== Template ======"
      puts template_diff
      puts "======================"
      puts
    end

    if tags_diff.empty? && params_diff.empty? && template_diff.empty?
      exit(true)
    else
      exit(2)
    end

  when 'validate'
    begin
      valid = cfn_client.validate_template({template_body: template_string})
      if valid.successful?
        puts "Validation successful"
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ValidationError => e
      $stderr.puts "Validation error: #{e}"
      exit(false)
    end

  when 'create'
    begin

      # default options (not overridable)
      create_stack_opts = {
          stack_name: stack_name,
          template_body: template_string,
          parameters: template.parameters.map { |k,v| {parameter_key: k, parameter_value: v}}.to_a,
          tags: cfn_tags.map { |k,v| {"key" => k.to_s, "value" => v} }.to_a,
          capabilities: ["CAPABILITY_IAM"],
      }

      # fill in options from the command line
      extra_options = parse_arg_array_as_hash(options)
      create_stack_opts = extra_options.merge(create_stack_opts)

      # create stack
      create_result = cfn_client.create_stack(create_stack_opts)
      if create_result.successful?
        puts create_result.stack_id
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ServiceError => e
      $stderr.puts "Failed to create stack: #{e}"
      exit(false)
    end

  when 'cancel-update'
    begin
      cancel_update_result = cfn_client.cancel_update_stack({stack_name: stack_name})
      if cancel_update_result.successful?
        $stderr.puts "Canceled updating stack #{stack_name}."
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ServiceError => e
      $stderr.puts "Failed to cancel updating stack: #{e}"
      exit(false)
    end

  when 'delete'
    begin
      if HighLine.agree("Really delete #{stack_name} in #{cfn_client.config.region}? [Y/n]")
        delete_result = cfn_client.delete_stack({stack_name: stack_name})
        if delete_result.successful?
          $stderr.puts "Deleted stack #{stack_name}."
          exit(true)
        end
      else
        $stderr.puts "Canceled deleting stack #{stack_name}."
        exit(true)
      end
      rescue Aws::CloudFormation::Errors::ServiceError => e
        $stderr.puts "Failed to delete stack: #{e}"
        exit(false)
    end

  when 'describe'
    begin
      describe_stack = cfn_client.describe_stacks({stack_name: stack_name})
      describe_stack_resources = cfn_client.describe_stack_resources({stack_name: stack_name})
      if describe_stack.successful? and describe_stack_resources.successful?
        stacks = {}
        stack_resources = {}
        describe_stack_resources.stack_resources.each { |stack_resource|
          if stack_resources[stack_resource.stack_name].nil?
            stack_resources[stack_resource.stack_name] = []
          end
          stack_resources[stack_resource.stack_name].push({
            logical_resource_id: stack_resource.logical_resource_id,
            physical_resource_id: stack_resource.physical_resource_id,
            resource_type: stack_resource.resource_type,
            timestamp: stack_resource.timestamp,
            resource_status: stack_resource.resource_status,
            resource_status_reason: stack_resource.resource_status_reason,
            description: stack_resource.description,
          })
        }
        describe_stack.stacks.each { |stack| stacks[stack.stack_name] = stack.to_map.merge!({resources: stack_resources[stack.stack_name]}) }
        unless template.nopretty
          puts JSON.pretty_generate(stacks)
        else
          puts JSON.generate(stacks)
        end
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ServiceError => e
      $stderr.puts "Failed describe stack #{stack_name}: #{e}"
      exit(false)
    end

  when 'describe-resource'
    begin
      describe_stack_resource = cfn_client.describe_stack_resource({
        stack_name: stack_name,
        logical_resource_id: resource_name,
      })
      if describe_stack_resource.successful?
        unless template.nopretty
          puts JSON.pretty_generate(describe_stack_resource.stack_resource_detail)
        else
          puts JSON.generate(describe_stack_resource.stack_resource_detail)
        end
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ServiceError => e
      $stderr.puts "Failed get stack resource details: #{e}"
      exit(false)
    end

  when 'get-template'
    begin
      get_template_result = cfn_client.get_template({stack_name: stack_name})
      template_body = JSON.parse(get_template_result.template_body)
      if get_template_result.successful?
        unless template.nopretty
          puts JSON.pretty_generate(template_body)
        else
          puts JSON.generate(template_body)
        end
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ServiceError => e
      $stderr.puts "Failed get stack template: #{e}"
      exit(false)
    end

  when 'update'

    # Run CloudFormation command to describe the existing stack
    old_stack = cfn_client.describe_stacks({stack_name: stack_name}).stacks

    # this might happen if, for example, stack_name is an empty string and the Cfn client returns ALL stacks
    if old_stack.length > 1
      $stderr.puts "Error: found too many stacks with this name. There should only be one."
      exit(false)
    else
      # grab the first (and only) result
      old_stack = old_stack[0]
    end

    # If updating a stack and some parameters or tags are marked as immutable, set the variable to true.
    immutables_exist = nil 

    if not immutable_parameters.empty?
      old_parameters = Hash[old_stack.parameters.map { |p| [p.parameter_key, p.parameter_value]}]
      new_parameters = template.parameters
      immutable_parameters.sort.each do |param|
        if old_parameters[param].to_s != new_parameters[param].to_s && old_parameters.key?(param)
          $stderr.puts "Error: unable to update immutable parameter " +
                           "'#{param}=#{old_parameters[param]}' to '#{param}=#{new_parameters[param]}'."
          immutables_exist = true
        end
      end
    end

    if not immutable_tags.empty?
      old_cfn_tags = Hash[old_stack.tags.map { |t| [t.key, t.value]}]
      cfn_tags_ary = Hash[cfn_tags.map { |k,v| [k, v]}]
      immutable_tags.sort.each do |tag|
        if old_cfn_tags[tag].to_s != cfn_tags_ary[tag].to_s && old_cfn_tags.key?(tag)
          $stderr.puts "Error: unable to update immutable tag " +
                           "'#{tag}=#{old_cfn_tags[tag]}' to '#{tag}=#{cfn_tags_ary[tag]}'."
          immutables_exist = true
        end
      end
    end

    # Fail if some parameters or tags were marked as immutable.
    if immutables_exist
      exit(false)
    end

    # Compare the sorted arrays of parameters for an exact match and print difference.
    old_parameters = old_stack.parameters.map { |p| [p.parameter_key, p.parameter_value]}.sort
    new_parameters = template.parameters.sort
    if new_parameters != old_parameters
      puts "\nCloudFormation stack parameters that do not match and will be updated:" +
               "\n" + (old_parameters - new_parameters).map {|param| "< #{param}" }.join("\n") +
               "\n" + "---" +
               "\n" + (new_parameters - old_parameters).map {|param| "> #{param}"}.join("\n")
    end

    # Compare the sorted arrays of tags for an exact match and print difference.
    old_cfn_tags = old_stack.tags.map { |t| [t.key, t.value]}.sort
    cfn_tags_ary = cfn_tags.map { |k,v| [k, v]}.sort
    if cfn_tags_ary != old_cfn_tags
      puts "\nCloudFormation stack tags that do not match and will be updated:" +
               "\n" + (old_cfn_tags - cfn_tags_ary).map {|tag| "< #{tag}" }.join("\n") +
               "\n" + "---" +
               "\n" + (cfn_tags_ary - old_cfn_tags).map {|tag| "> #{tag}"}.join("\n")
    end

    # update the stack
    begin

      # default options (not overridable)
      update_stack_opts = {
          stack_name: stack_name,
          template_body: template_string,
          parameters: template.parameters.map { |k,v| {parameter_key: k, parameter_value: v}}.to_a,
          tags: cfn_tags.map { |k,v| {"key" => k.to_s, "value" => v.to_s} }.to_a,
          capabilities: ["CAPABILITY_IAM"],
      }

      # fill in options from the command line
      extra_options = parse_arg_array_as_hash(options)
      update_stack_opts = extra_options.merge(update_stack_opts)

      # update the stack
      update_result = cfn_client.update_stack(update_stack_opts)
      if update_result.successful?
        puts update_result.stack_id
        exit(true)
      end
    rescue Aws::CloudFormation::Errors::ServiceError => e
      $stderr.puts "Failed to update stack: #{e}"
      exit(false)
    end

  end
end

#default_regionObject



51
52
53
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 51

def default_region
  ENV['EC2_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
end

#equal(one, two) ⇒ Object



183
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 183

def equal(one, two) { :'Fn::Equals' => [one, two] } end

#erb_template(filename, params = {}) ⇒ Object

Combines the provided ERB template with optional parameters



264
265
266
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 264

def erb_template(filename, params = {})
  ERB.new(file(filename), nil, '-').result(Namespace.new(params).get_binding)
end

#extract_options(args, opts_no_val, opts_1_val) ⇒ Object

extract options and arguments from a command line string

Example:

desired, unknown = extract_options(“arg1 –option withvalue –optionwithoutvalue”, %w(–option), %w())

puts desired => Array“–option”, “withvalue” puts unknown => Array{}



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 521

def extract_options(args, opts_no_val, opts_1_val)
  args = args.clone
  opts = []
  rest = []
  while (arg = args.shift) != nil
    if opts_no_val.include?(arg)
      opts.push(arg)
    elsif opts_1_val.include?(arg)
      opts.push(arg)
      opts.push(arg) if (arg = args.shift) != nil
    else
      rest.push(arg)
    end
  end
  [opts, rest]
end

#file(filename) ⇒ Object

Read the specified file and return its value as a string literal



228
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 228

def file(filename) File.read(File.absolute_path(filename, File.dirname($PROGRAM_NAME))) end

#find_in_map(map, key, name) ⇒ Object



167
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 167

def find_in_map(map, key, name) { :'Fn::FindInMap' => [ map, key, name ] } end

#fn_and(*condition_list) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 195

def fn_and(*condition_list)
  case condition_list.length
    when 0..1 then raise "fn_and needs at least 2 items."
    when 2..10 then  { :'Fn::And' => condition_list }
    else raise "fn_and needs a list of 2-10 items that evaluate to true/false."
  end
end

#fn_if(cond, if_true, if_false) ⇒ Object



203
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 203

def fn_if(cond, if_true, if_false) { :'Fn::If' => [cond, if_true, if_false] } end

#fn_not(condition) ⇒ Object



185
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 185

def fn_not(condition) { :'Fn::Not' => [condition] } end

#fn_or(*condition_list) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 187

def fn_or(*condition_list)
  case condition_list.length
    when 0..1 then raise "fn_or needs at least 2 items."
    when 2..10 then  { :'Fn::Or' => condition_list }
    else raise "fn_or needs a list of 2-10 items that evaluate to true/false."
  end
end

#generate_json(data, pretty = true) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 10

def generate_json(data, pretty = true)
  # Raw formatting
  return JSON.generate(data) unless pretty

  # Pretty formatting
  JSON.pretty_generate(data)
end

#generate_template(template) ⇒ Object

Formats a template as JSON



6
7
8
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 6

def generate_template(template)
  format_json template, !template.nopretty
end

#get_att(resource, attribute) ⇒ Object



169
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 169

def get_att(resource, attribute) { :'Fn::GetAtt' => [ resource, attribute ] } end

#get_azs(region = '') ⇒ Object



171
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 171

def get_azs(region = '') { :'Fn::GetAZs' => region } end

#interpolate(string, locals = {}) ⇒ Object

Interpolates a string like “NAME={ref(‘Service’)}” and returns a CloudFormation “Fn::Join” operation to collect the results. Anything between and } is interpreted as a Ruby expression and eval’d. This is especially useful with Ruby “here” documents. Local variables may also be exposed to the string via the ‘locals` hash.



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 234

def interpolate(string, locals={})
  list = []
  while string.length > 0
    head, match, string = string.partition(/\{\{.*?\}\}/)
    list << head if head.length > 0
    list << eval(match[2..-3], nil, 'interpolated string') if match.length > 0
  end

  # Split out strings in an array by newline, for visibility
  list = list.flat_map {|value| value.is_a?(String) ? value.lines.to_a : value }
  join('', *list)
end

#join(delim, *list) ⇒ Object



173
174
175
176
177
178
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 173

def join(delim, *list)
  case list.length
    when 0 then ''
    else join_list(delim,list)
  end
end

#join_interpolate(delim, string) ⇒ Object



247
248
249
250
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 247

def join_interpolate(delim, string)
  $stderr.puts "join_interpolate(delim,string) has been deprecated; use interpolate(string) instead"
  interpolate(string)
end

#join_list(delim, list) ⇒ Object

Variant of join that matches the native CFN syntax.



181
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 181

def join_list(delim, list) { :'Fn::Join' => [ delim, list ] } end

#no_valueObject

deprecated, for backward compatibility



222
223
224
225
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 222

def no_value()
    warn_deprecated('no_value()', 'aws_no_value()')
    aws_no_value()
end

#not_equal(one, two) ⇒ Object



205
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 205

def not_equal(one, two) fn_not(equal(one,two)) end

#parse_arg_array_as_hash(options) ⇒ Object

convert an array of option strings to a hash example input: [“–option”, “value”, “–optionwithnovalue”] example output: => “value”, :optionwithnovalue: true



541
542
543
544
545
546
547
548
549
550
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 541

def parse_arg_array_as_hash(options)
  result = {}
  options.slice_before(/\A--[a-zA-Z_-]\S/).each { |o|
      key = ((o[0].sub '--', '').gsub '-', '_').downcase.to_sym
      value = if o.length > 1 then o.drop(1) else true end
      value = value[0] if value.is_a?(Array) and value.length == 1
      result[key] = value
  }
  result
end

#parse_argsObject

Parse command-line arguments and return the parameters and region



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 72

def parse_args
  stack_name = nil
  parameters = {}
  region     = default_region
  profile    = nil
  nopretty   = false
  ARGV.slice_before(/^--/).each do |name, value|
    case name
    when '--stack-name'
      stack_name = value
    when '--parameters'
      parameters = Hash[value.split(/;/).map { |pair| pair.split(/=/, 2) }]  #/# fix for syntax highlighting
    when '--region'
      region = value
    when '--profile'
      profile = value
    when '--nopretty'
      nopretty = true
    end
  end
  [stack_name, parameters, region, profile, nopretty]
end

#raw_template(parameters = {}, stack_name = nil, aws_region = default_region, aws_profile = nil, nopretty = false, &block) ⇒ Object

Main entry point



47
48
49
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 47

def raw_template(parameters = {}, stack_name = nil, aws_region = default_region, aws_profile = nil, nopretty = false, &block)
  TemplateDSL.new(parameters, stack_name, aws_region, aws_profile, nopretty, &block)
end

#ref(name) ⇒ Object



209
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 209

def ref(name) { :Ref => name } end

#select(index, list) ⇒ Object



207
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 207

def select(index, list) { :'Fn::Select' => [ index, list ] } end

#spot_price(spot_price_string, instance_type) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/cloudformation-ruby-dsl/spotprice.rb', line 38

def spot_price(spot_price_string, instance_type)
  case spot_price_string
    when 'false', '' then aws_no_value()
    when 'true' then spot_price_for_instance_type(instance_type)
    else spot_price_string
  end
end

#spot_price_for_instance_type(instance_type) ⇒ Object



46
47
48
49
50
# File 'lib/cloudformation-ruby-dsl/spotprice.rb', line 46

def spot_price_for_instance_type(instance_type)
  # Add 10% to ensure that we have a small buffer against current spot prices increasing
  # to the on-demand prices, which theoretically could happen often.
  SPOT_PRICES_BY_INSTANCE_TYPE[instance_type] * 1.10
end

#template(&block) ⇒ Object

Main entry point



561
562
563
564
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 561

def template(&block)
  stack_name, parameters, aws_region, aws_profile, nopretty = parse_args
  raw_template(parameters, stack_name, aws_region, aws_profile, nopretty, &block)
end

#validate_action(action) ⇒ Object



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
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 95

def validate_action(action)
  valid = %w[
    expand
    diff
    validate
    create
    update
    cancel-update
    delete
    describe
    describe-resource
    get-template
  ]
  removed = %w[
    cfn-list-stack-resources
    cfn-list-stacks
  ]
  deprecated = {
    "cfn-validate-template"        => "validate",
    "cfn-create-stack"             => "create",
    "cfn-update-stack"             => "update",
    "cfn-cancel-update-stack"      => "cancel-update",
    "cfn-delete-stack"             => "delete",
    "cfn-describe-stack-events"    => "describe",
    "cfn-describe-stack-resources" => "describe",
    "cfn-describe-stack-resource"  => "describe-resource",
    "cfn-get-template"             => "get-template"
  }
  if deprecated.keys.include? action
    replacement = deprecated[action]
    $stderr.puts "WARNING: '#{action}' is deprecated and will be removed in a future version. Please use '#{replacement}' instead."
    action = replacement
  end
  unless valid.include? action
    if removed.include? action
      $stderr.puts "ERROR: native command #{action} is no longer supported by cloudformation-ruby-dsl."
    end
    $stderr.puts "usage: #{$PROGRAM_NAME} <#{valid.join('|')}>"
    exit(2)
  end
  action
end

#warn_deprecated(old, new) ⇒ Object



268
269
270
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 268

def warn_deprecated(old, new)
    $stderr.puts "Warning: '#{old}' has been deprecated.  Please update your template to use '#{new}' instead."
end