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
# 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]
  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



99
100
101
102
103
104
# File 'lib/stackup/change_set.rb', line 99

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



117
118
119
120
121
# File 'lib/stackup/change_set.rb', line 117

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



89
90
91
92
93
# File 'lib/stackup/change_set.rb', line 89

def execute
  modify_stack("UPDATE_COMPLETE", "update failed") do
    cf_client.execute_change_set(:stack_name => stack.name, :change_set_name => name)
  end
end

#statusObject



106
107
108
# File 'lib/stackup/change_set.rb', line 106

def status
  describe.status
end