Class: CfnCli::CloudFormation

Inherits:
Object
  • Object
show all
Includes:
CfnClient, Loggable
Defined in:
lib/cfncli/cloudformation.rb

Constant Summary collapse

CREATE_ONLY_OPTIONS =

A list of options that only apply for stack creation

%w(on_failure).freeze

Instance Attribute Summary

Attributes included from CfnClient

#retry_backoff, #retry_limit, #stub_responses

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_level, #log_level=, #logger, #logger=

Methods included from CfnClient

#cfn, #cfn_client

Constructor Details

#initializeCloudFormation

Returns a new instance of CloudFormation.



17
18
# File 'lib/cfncli/cloudformation.rb', line 17

def initialize
end

Class Method Details

.parse_json_params(params) ⇒ Object

Converts the ‘standard’ json stack parameters format to the format expected by the API (see blogs.aws.amazon.com/application-management/post/Tx1A23GYVMVFKFD/Passing-Parameters-to-CloudFormation-Stacks-with-the-AWS-CLI-and-Powershell)



109
110
111
112
113
114
115
116
# File 'lib/cfncli/cloudformation.rb', line 109

def self.parse_json_params(params)
  params.map do |param|
    {
      parameter_key: param['ParameterKey'],
      parameter_value: param['ParameterValue']
    }
  end
end

Instance Method Details

#apply_and_list_events(options, config = nil) ⇒ Object

Creates or update the stack and list events



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cfncli/cloudformation.rb', line 45

def apply_and_list_events(options, config = nil)
  # Create/update the stack
  logger.debug "Creating stack #{options['stack_name']}"
  list_nested_events = options['list_nested_events']
  options.delete 'list_nested_events'
  stack = create_or_update_stack(options, config)

  events(stack, config, list_nested_events)
  Waiting.wait(interval: config.interval || default_config.interval, max_attempts: config.retries || default_config.retries) do |waiter|
    waiter.done if stack.finished? && !stack.listing_events?
  end

  stack
end

#create_or_update_stack(options, config = nil) ⇒ Object

Creates a stack if it doesn’t exist otherwise update it



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cfncli/cloudformation.rb', line 28

def create_or_update_stack(options, config = nil)
  opts = process_params(options.dup)

  stack_name = opts['stack_name']
  stack = create_stack_obj(stack_name, config)

  if stack.exists?
    CREATE_ONLY_OPTIONS.each { |key| opts.delete key }
    stack.update(opts)
  else
    stack.create(opts)
  end

  stack
end

#create_stack(options, config = nil) ⇒ Object

Creates a stack and wait for the creation to be finished

Parameters:



23
24
25
# File 'lib/cfncli/cloudformation.rb', line 23

def create_stack(options, config = nil)
  create_or_update_stack(options, config)
end

#default_configObject



118
119
120
# File 'lib/cfncli/cloudformation.rb', line 118

def default_config
  Config::CfnClient.new
end

#delete_and_list_events(options, config = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cfncli/cloudformation.rb', line 82

def delete_and_list_events(options, config = nil)
  # Create/update the stack
  logger.debug "Deleting stack #{options['stack_name']}"
  list_nested_events = options['list_nested_events']
  options.delete 'list_nested_events'
  stack = delete_stack(options, config)

  events(stack.stack_id, config, list_nested_events)

  interval = config.interval if config
  interval = default_config.interval unless interval
  Waiting.wait(interval: interval, max_attempts: config.retries || default_config.retries) do |waiter|
    waiter.done if stack.finished? && !stack.listing_events?
    sleep 1
  end

  stack
end

#delete_stack(options, config = nil) ⇒ Object

Delete a stack



75
76
77
78
79
80
# File 'lib/cfncli/cloudformation.rb', line 75

def delete_stack(options, config = nil)
  stack = create_stack_obj(options['stack_name'], config)
  options['stack_name'] = stack.stack_id
  stack.delete(options, config)
  stack
end

#events(stack_or_name, config = nil, list_nested_events = true, reset_events = true, poller = nil, streamer = nil) ⇒ Object

List stack events



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cfncli/cloudformation.rb', line 61

def events(stack_or_name, config = nil, list_nested_events = true, reset_events = true, poller = nil, streamer = nil)
  stack = stack_or_name
  stack = create_stack_obj(stack_or_name, config) unless stack_or_name.is_a? CfnCli::Stack

  poller ||= EventPoller.new
  streamer ||= EventStreamer.new(stack, config)

  streamer.reset_events if reset_events

  logger.debug "Listing events for stack #{stack.stack_name}"
  stack.list_events(poller, streamer, config, nil, list_nested_events)
end

#stack_successful?(stack_name) ⇒ Boolean

Returns the stack status

Returns:

  • (Boolean)


102
103
104
# File 'lib/cfncli/cloudformation.rb', line 102

def stack_successful?(stack_name)
  Stack.new(stack_name).succeeded?
end