Class: Stackup::ChangeSet

Inherits:
Object
  • Object
show all
Includes:
ErrorHandling
Defined in:
lib/stackup/change_set.rb

Overview

An abstraction of a CloudFormation change-set.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorHandling

#handling_cf_errors

Constructor Details

#initialize(name, stack) ⇒ ChangeSet

Returns a new instance of ChangeSet.



9
10
11
12
# File 'lib/stackup/change_set.rb', line 9

def initialize(name, stack)
  @name = name
  @stack = stack
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/stackup/change_set.rb', line 14

def name
  @name
end

#stackObject (readonly)

Returns the value of attribute stack.



15
16
17
# File 'lib/stackup/change_set.rb', line 15

def stack
  @stack
end

Instance Method Details

#create(options = {}) ⇒ String

Create the change-set.

Refer Aws::CloudFormation::Client#create_change_set

(see http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudFormation/Client.html#create_change_set-instance_method)

Parameters:

  • (defaults to: {})

    change-set options

Options Hash (options):

  • :capabilities (Array<String>) — default: CAPABILITY_NAMED_IAM

    list of capabilities required for stack template

  • :description (String)

    change-set description

  • :notification_arns (String)

    ARNs for the Amazon SNS topics associated with this stack

  • :parameters (Hash, Array<Hash>)

    stack parameters, either as a Hash, or an Array of Aws::CloudFormation::Types::Parameter structures

  • :tags (Hash, Array<Hash>)

    stack tags, either as a Hash, or an Array of Aws::CloudFormation::Types::Tag structures

  • :resource_types (Array<String>)

    resource types that you have permissions to work with

  • :template (Hash)

    stack template, as Ruby data

  • :template_body (String)

    stack template, as JSON or YAML

  • :template_url (String)

    location of stack template

  • :use_previous_template (boolean)

    if true, reuse the existing template

  • :force (boolean)

    if true, delete any existing change-set of the same name

Returns:

  • change-set id

Raises:

  • if the stack doesn’t exist



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/stackup/change_set.rb', line 53

def create(options = {})
  options = options.dup
  options[:stack_name] = stack.name
  options[:change_set_name] = name
  options[:change_set_type] = stack.exists? ? "UPDATE" : "CREATE"
  force = options.delete(:force)
  options[:template_body] = MultiJson.dump(options.delete(:template)) if options[:template]
  # optionally override template_body with the original template to preserve formatting (& comments in YAML)
  template_orig = options.delete(:template_orig)
  options[:template_body] = template_orig if options.delete(:preserve)
  options[:parameters] = Parameters.new(options[:parameters]).to_a if options[:parameters]
  options[:tags] = normalize_tags(options[:tags]) if options[:tags]
  options[:capabilities] ||= ["CAPABILITY_NAMED_IAM"]
  delete if force
  handling_cf_errors do
    cf_client.create_change_set(options)
    loop do
      current = describe
      logger.debug("change_set_status=#{current.status}")
      case current.status
      when /COMPLETE/
        return current.status
      when "FAILED"
        logger.error(current.status_reason)
        raise StackUpdateError, "change-set creation failed" if status == "FAILED"
      end
      sleep(wait_poll_interval)
    end
    status
  end
end

#deleteObject

Delete a change-set.

Raises:

  • if the stack doesn’t exist



102
103
104
105
106
107
# File 'lib/stackup/change_set.rb', line 102

def delete
  handling_cf_errors do
    cf_client.delete_change_set(:stack_name => stack.name, :change_set_name => name)
  end
  nil
end

#describeObject

Describe the change-set.

Refer Aws::CloudFormation::Client#describe_change_set

(http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudFormation/Client.html#describe_change_set-instance_method)

Returns:

  • change-set state, as data



120
121
122
123
124
# File 'lib/stackup/change_set.rb', line 120

def describe
  handling_cf_errors do
    cf_client.describe_change_set(:stack_name => stack.name, :change_set_name => name)
  end
end

#executeString

Execute the change-set.

Returns:

  • resulting stack status

Raises:

  • if the change-set doesn’t exist

  • if the stack doesn’t exist

  • if operation fails



92
93
94
95
96
# File 'lib/stackup/change_set.rb', line 92

def execute
  modify_stack(/(CREATE|UPDATE)_COMPLETE/, "update failed") do
    cf_client.execute_change_set(:stack_name => stack.name, :change_set_name => name)
  end
end

#statusObject



109
110
111
# File 'lib/stackup/change_set.rb', line 109

def status
  describe.status
end