Top Level Namespace
Defined Under Namespace
Modules: Cfn Classes: AwsClients, JsonObjectDSL, Namespace, Parameter, 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
-
#apply_parameter_defaults(parameters) ⇒ Object
Apply the default value for any parameter not assigned by the user.
- #aws_account_id ⇒ Object
- #aws_no_value ⇒ Object
- #aws_notification_arns ⇒ Object
- #aws_stack_id ⇒ Object
- #aws_stack_name ⇒ Object
- #base64(value) ⇒ Object
- #cfn(template) ⇒ Object
- #default_region ⇒ Object
- #equal(one, two) ⇒ Object
-
#erb_template(filename, params = {}) ⇒ Object
Combines the provided ERB template with optional parameters.
-
#extract_options(args, opts_no_val, opts_1_val) ⇒ Object
extract options and arguments from a command line string.
-
#file(filename) ⇒ Object
Read the specified file and return its value as a string literal.
- #find_in_map(map, key, name) ⇒ Object
- #fn_and(*condition_list) ⇒ Object
- #fn_if(cond, if_true, if_false) ⇒ Object
- #fn_not(condition) ⇒ Object
- #fn_or(*condition_list) ⇒ Object
- #generate_json(data, pretty = true) ⇒ Object
-
#generate_template(template) ⇒ Object
Formats a template as JSON.
- #get_att(resource, attribute) ⇒ Object
- #get_azs(region = '') ⇒ Object
- #import_value(value) ⇒ Object
-
#interpolate(string, locals = {}) ⇒ Object
Interpolates a string like “NAME={ref(‘Service’)}” and returns a CloudFormation “Fn::Join” operation to collect the results.
- #join(delim, *list) ⇒ Object
- #join_interpolate(delim, string) ⇒ Object
-
#join_list(delim, list) ⇒ Object
Variant of join that matches the native CFN syntax.
-
#no_value ⇒ Object
deprecated, for backward compatibility.
- #not_equal(one, two) ⇒ Object
-
#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.
-
#parse_args ⇒ Object
Parse command-line arguments and return the parameters and region.
-
#raw_template(options = {}, &block) ⇒ Object
Main entry point.
- #ref(name) ⇒ Object
- #select(index, list) ⇒ Object
- #split(delimiter, source_str) ⇒ Object
- #spot_price(spot_price_string, instance_type) ⇒ Object
- #spot_price_for_instance_type(instance_type) ⇒ Object
-
#sub(sub_string, var_map = {}) ⇒ Object
There are two valid forms of Fn::Sub, with a map and without.
-
#template(&block) ⇒ Object
Main entry point.
- #validate_action(action) ⇒ Object
- #warn_deprecated(old, new) ⇒ Object
Instance Method Details
#apply_parameter_defaults(parameters) ⇒ Object
Apply the default value for any parameter not assigned by the user
706 707 708 709 710 711 712 713 714 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 706 def apply_parameter_defaults(parameters) parameters.each do |k, v| if v.empty? parameters[k] = Parameter.new(v.default) $stderr.puts "Using default parameter value " + "'#{k}=#{parameters[k]}'." end end end |
#aws_account_id ⇒ Object
321 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 321 def aws_account_id() ref("AWS::AccountId") end |
#aws_no_value ⇒ Object
325 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 325 def aws_no_value() ref("AWS::NoValue") end |
#aws_notification_arns ⇒ Object
323 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 323 def aws_notification_arns() ref("AWS::NotificationARNs") end |
#aws_stack_id ⇒ Object
327 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 327 def aws_stack_id() ref("AWS::StackId") end |
#aws_stack_name ⇒ Object
329 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 329 def aws_stack_name() ref("AWS::StackName") end |
#base64(value) ⇒ Object
262 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 262 def base64(value) { :'Fn::Base64' => value } end |
#cfn(template) ⇒ Object
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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 163 def cfn(template) aws_clients = AwsClients.new({:region => template.aws_region, :aws_profile => template.aws_profile}) cfn_client = aws_clients.cfn_client s3_client = aws_clients.s3_client action = validate_action( ARGV[0] ) # Find parameters where extension attributes are true then remove them from the # cfn template since we can't pass it to CloudFormation. excised_parameters = template.excise_parameter_attributes!([:Immutable, :UsePreviousValue]) # Tag CloudFormation stacks based on :Tags defined in the template. # Remove them from the template as well, so that the template is valid. = template. # 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. = template.get_tag_attribute(, :Immutable) .each {|k, v| [k] = v[:Value].to_s} template_string = generate_template(template) # Derive stack name from ARGV _, = (ARGV[1..-1], %w(--nopretty), %w(--profile --stack-name --region --parameters --tag --s3-bucket)) # 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 = .shift if [0] && !(/^-/ =~ [0]) resource_name = .shift if [0] && !(/^-/ =~ [0]) else stack_name = template.stack_name end case action when 'help' begin # Give some basic usage. help_string=%q( ## Usage To convert existing JSON templates to use the DSL, run cfntemplate-to-ruby [EXISTING_CFN] > [NEW_NAME.rb] You may need to preface this with `bundle exec` if you installed via Bundler. 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): - `expand`: output the JSON template to the command line (takes optional `--nopretty` to minimize the output) - `diff`: compare an existing stack with your template. Produces following exit codes: ``` 0 - no differences, nothing to update 1 - stack does not exist, template Validation error 2 - there are differences between an existing stack and your template ``` - `validate`: run validation against the stack definition - `create`: create a new stack from the output - `update`: update an existing stack from the output. Produces following exit codes: ``` 0 - update finished successfully 1 - no updates to perform, stack doesn't exist, unable to update immutable parameter or tag, AWS ServiceError exception ``` - `cancel-update`: cancel updating a stack - `delete`: delete a stack (with prompt) - `describe`: get output of an existing stack and output it (takes optional `--nopretty` to minimize output) - `describe-resource`: given two arguments: stack-name and logical-resource-id, get output from a stack concerning the specific resource (takes optional `--nopretty` to minimize output) - `get-template`: get entire template output of an existing stack Command line options similar to cloudformation commands, but parsed by the dsl. --profile --stack-name --region --parameters --tag --s3-bucket Any other parameters are passed directly onto cloudformation. (--disable-rollback for instance) Using the ruby scripts: template.rb create --stack-name my_stack --parameters "BucketName=bucket-s3-static;SnsQueue=mysnsqueue" ) puts help_string exit(true) end 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 template_string 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 !.include? '-U' .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_attributes. old_parameters = Hash[old_attributes.parameters.map { |p| [p.parameter_key, p.parameter_value]}] # Sort the tag strings alphabetically to make them easily comparable = .map { |tag| %Q(TAG "#{tag.key}=#{tag.value}"\n) }.sort.join = .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.map { |key, value| %Q(PARAMETER "#{key}=#{value}"\n) }.join parameters_string = template.parameters.sort.map do |key, value| value = old_parameters[key] if value.empty? && value.use_previous_value && !old_parameters[key].to_s.empty? value = value.default if value.empty? && !value.default.to_s.empty? "PARAMETER \"#{key}=#{value}\"\n" end.join # set default diff options Diffy::Diff..merge!( :diff => "#{.join(' ')}", ) # set default diff output Diffy::Diff.default_format = :color = Diffy::Diff.new(, ).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 !.empty? puts "====== Tags ======" puts 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 .empty? && params_diff.empty? && template_diff.empty? exit(true) else exit(2) end when 'validate' begin validation_payload = {} if template.s3_bucket.nil? then validation_payload = {template_body: template_string} else template_path = "#{Time.now.strftime("%s")}/#{stack_name}.json" # assumption: JSON is the only supported serialization format (YAML not allowed) template_url = "https://s3.amazonaws.com/#{template.s3_bucket}/#{template_path}" begin s3_client.put_object({ bucket: template.s3_bucket, key: template_path, # canned ACL for authorized users to read the bucket (that should be *this* IAM role!) acl: "private", body: template_string, }) rescue Aws::S3::Errors::ServiceError => e $stderr.puts "Failed to upload stack template to S3: #{e}" exit(false) end validation_payload = {template_url: template_url} end valid = cfn_client.validate_template(validation_payload) 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 # Apply any default parameter values apply_parameter_defaults(template.parameters) # default options (not overridable) create_stack_opts = { stack_name: stack_name, parameters: template.parameters.map { |k,v| {parameter_key: k, parameter_value: v}}.to_a, tags: .map { |k,v| {"key" => k.to_s, "value" => v} }.to_a, capabilities: ["CAPABILITY_NAMED_IAM"], } # If the user supplied the --s3-bucket option and # access to the bucket, upload the template body to S3 if template.s3_bucket.nil? then create_stack_opts["template_body"] = template_string else template_path = "#{Time.now.strftime("%s")}/#{stack_name}.json" # assumption: JSON is the only supported serialization format (YAML not allowed) template_url = "https://s3.amazonaws.com/#{template.s3_bucket}/#{template_path}" begin s3_client.put_object({ bucket: template.s3_bucket, key: template_path, # canned ACL for authorized users to read the bucket (that should be *this* IAM role!) acl: "private", body: template_string, }) rescue Aws::S3::Errors::ServiceError => e $stderr.puts "Failed to upload stack template to S3: #{e}" exit(false) end create_stack_opts["template_url"] = template_url end # fill in options from the command line = parse_arg_array_as_hash() create_stack_opts = .merge(create_stack_opts) # remove custom options create_stack_opts.delete(:interactive) # 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., 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 old_parameters = Hash[old_stack.parameters.map { |p| [p.parameter_key, p.parameter_value]}] new_parameters = template.parameters excised_parameters.each do |extension_attribute, parameters| if !parameters.empty? parameters.sort.each do |param| if old_parameters[param] != new_parameters[param] && old_parameters.key?(param) case extension_attribute when :Immutable if !excised_parameters[:UsePreviousValue].include?(param) $stderr.puts "Error: unable to update immutable parameter " + "'#{param}=#{old_parameters[param]}' to '#{param}=#{new_parameters[param]}'." immutables_exist = true end when :UsePreviousValue if !immutables_exist && new_parameters[param].empty? $stderr.puts "Using previous parameter " + "'#{param}=#{old_parameters[param]}'." new_parameters[param] = Parameter.new(old_parameters[param]) new_parameters[param].use_previous_value = true end end end end end end if not .empty? = Hash[old_stack..map { |t| [t.key, t.value]}] = Hash[.map { |k,v| [k, v]}] .sort.each do |tag| if [tag].to_s != [tag].to_s && .key?(tag) $stderr.puts "Error: unable to update immutable tag " + "'#{tag}=#{[tag]}' to '#{tag}=#{[tag]}'." immutables_exist = true end end end # Fail if some parameters or tags were marked as immutable. if immutables_exist exit(false) end # Apply any default parameter values apply_parameter_defaults(template.parameters) # 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_stack..map { |t| [t.key, t.value]}.sort = .map { |k,v| [k, v]}.sort if != puts "\nCloudFormation stack tags that do not match and will be updated:" + "\n" + ( - ).map {|tag| "< #{tag}" }.join("\n") + "\n" + "---" + "\n" + ( - ).map {|tag| "> #{tag}"}.join("\n") end # update the stack begin # default options (not overridable) update_stack_opts = { stack_name: stack_name, parameters: template.parameters.map { |k,v| (v.use_previous_value && old_parameters.include?([k,v])) ? {parameter_key: k, use_previous_value: v.use_previous_value.to_s} : {parameter_key: k, parameter_value: v}}.to_a, tags: .map { |k,v| {"key" => k.to_s, "value" => v.to_s} }.to_a, capabilities: ["CAPABILITY_NAMED_IAM"], } # if the the user supplies a bucket bucket and # access to it, upload the template body if template.s3_bucket.nil? then update_stack_opts["template_body"] = template_string else template_path = "#{Time.now.strftime("%s")}/#{stack_name}.json" # assumption: JSON is the only supported serialization format (YAML not allowed) template_url = "https://s3.amazonaws.com/#{template.s3_bucket}/#{template_path}" s3_client.put_object({ bucket: template.s3_bucket, key: template_path, # canned ACL for authorized users to read the bucket (that should be *this* IAM role!) acl: "private", body: template_string, }) update_stack_opts["template_url"] = template_url end # fill in options from the command line = parse_arg_array_as_hash() update_stack_opts = .merge(update_stack_opts) # remove custom options update_stack_opts.delete(:interactive) # 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_region ⇒ Object
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
291 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 291 def equal(one, two) { :'Fn::Equals' => [one, two] } end |
#erb_template(filename, params = {}) ⇒ Object
Combines the provided ERB template with optional parameters
374 375 376 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 374 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{}
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 674 def (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
338 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 338 def file(filename) File.read(File.absolute_path(filename, File.dirname($PROGRAM_NAME))) end |
#find_in_map(map, key, name) ⇒ Object
264 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 264 def find_in_map(map, key, name) { :'Fn::FindInMap' => [ map, key, name ] } end |
#fn_and(*condition_list) ⇒ Object
303 304 305 306 307 308 309 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 303 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
311 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 311 def fn_if(cond, if_true, if_false) { :'Fn::If' => [cond, if_true, if_false] } end |
#fn_not(condition) ⇒ Object
293 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 293 def fn_not(condition) { :'Fn::Not' => [condition] } end |
#fn_or(*condition_list) ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 295 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) generate_json template, !template.nopretty end |
#get_att(resource, attribute) ⇒ Object
266 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 266 def get_att(resource, attribute) { :'Fn::GetAtt' => [ resource, attribute ] } end |
#get_azs(region = '') ⇒ Object
268 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 268 def get_azs(region = '') { :'Fn::GetAZs' => region } end |
#import_value(value) ⇒ Object
270 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 270 def import_value(value) { :'Fn::ImportValue' => value } 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.
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 344 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
281 282 283 284 285 286 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 281 def join(delim, *list) case list.length when 0 then '' else join_list(delim,list) end end |
#join_interpolate(delim, string) ⇒ Object
357 358 359 360 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 357 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.
289 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 289 def join_list(delim, list) { :'Fn::Join' => [ delim, list ] } end |
#no_value ⇒ Object
deprecated, for backward compatibility
332 333 334 335 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 332 def no_value() warn_deprecated('no_value()', 'aws_no_value()') aws_no_value() end |
#not_equal(one, two) ⇒ Object
313 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 313 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
694 695 696 697 698 699 700 701 702 703 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 694 def parse_arg_array_as_hash() result = {} .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_args ⇒ Object
Parse command-line arguments and return the parameters and region
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 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 87 def parse_args args = { :stack_name => nil, :parameters => {}, :interactive => false, :region => default_region, :profile => nil, :nopretty => false, :s3_bucket => nil, } ARGV.slice_before(/^--/).each do |name, value| case name when '--stack-name' args[:stack_name] = value when '--parameters' args[:parameters] = Hash[value.split(/;/).map { |pair| parts = pair.split(/=/, 2); [ parts[0], Parameter.new(parts[1]) ] }] when '--interactive' args[:interactive] = true when '--region' args[:region] = value when '--profile' args[:profile] = value when '--nopretty' args[:nopretty] = true when '--s3-bucket' args[:s3_bucket] = value end end args end |
#raw_template(options = {}, &block) ⇒ Object
Main entry point
47 48 49 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 47 def raw_template( = {}, &block) TemplateDSL.new(, &block) end |
#ref(name) ⇒ Object
319 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 319 def ref(name) { :Ref => name } end |
#select(index, list) ⇒ Object
315 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 315 def select(index, list) { :'Fn::Select' => [ index, list ] } end |
#split(delimiter, source_str) ⇒ Object
317 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 317 def split(delimiter, source_str) { :'Fn::Split' => [delimiter, source_str] } 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 |
#sub(sub_string, var_map = {}) ⇒ Object
There are two valid forms of Fn::Sub, with a map and without.
273 274 275 276 277 278 279 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 273 def sub(sub_string, var_map = {}) if var_map.empty? return { :'Fn::Sub' => sub_string } else return { :'Fn::Sub' => [sub_string, var_map] } end end |
#template(&block) ⇒ Object
Main entry point
728 729 730 731 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 728 def template(&block) = parse_args raw_template(, &block) end |
#validate_action(action) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 119 def validate_action(action) valid = %w[ help 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
378 379 380 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 378 def warn_deprecated(old, new) $stderr.puts "Warning: '#{old}' has been deprecated. Please update your template to use '#{new}' instead." end |