Module: Cfer

Defined in:
lib/cfer.rb,
lib/cfer/cli.rb,
lib/cfer/block.rb,
lib/cfer/config.rb,
lib/cfer/console.rb,
lib/cfer/version.rb,
lib/cfer/cfn/cfer_credentials_provider.rb

Overview

Contains the core Cfer logic

Defined Under Namespace

Modules: Cfn, Cli, Core, Util Classes: Block, BlockHash, Config

Constant Summary collapse

DEBUG =
true
LOGGER =

The Cfer logger

Logger.new(STDERR)
VERSION =
"0.6.0"

Class Method Summary collapse

Class Method Details

.converge!(stack_name, options = {}) ⇒ Object

Creates or updates a CloudFormation stack

Parameters:

  • stack_name (String)

    The name of the stack to update

  • options (Hash) (defaults to: {})

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cfer.rb', line 60

def converge!(stack_name, options = {})
  config(options)
  options[:on_failure].upcase! if options[:on_failure]
  tmpl = options[:template] || "#{stack_name}.rb"
  cfn = options[:aws_options] || {}

  cfn_stack = options[:cfer_client] || Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name))
  raise Cfer::Util::CferError, "No such template file: #{tmpl}" unless File.exist?(tmpl) || options[:cfer_stack]
  stack =
    options[:cfer_stack] ||
      Cfer::stack_from_file(tmpl,
        options.merge(
          client: cfn_stack,
          parameters: generate_final_parameters(options)
        )
      )

  begin
    operation = stack.converge!(options)
    if options[:follow] && !options[:change]
      begin
        tail! stack_name, options.merge(cfer_client: cfn_stack)
      rescue Interrupt
        puts "Caught interrupt. What would you like to do?"
        case HighLine.new($stdin, $stderr).choose('Continue', 'Quit', 'Rollback')
        when 'Continue'
          retry
        when 'Rollback'
          rollback_opts = {
            stack_name: stack_name
          }

          rollback_opts[:role_arn] = options[:role_arn] if options[:role_arn]

          case operation
          when :created
            cfn_stack.delete_stack rollback_opts
          when :updated
            cfn_stack.cancel_update_stack rollback_opts
          end
          retry
        end
      end
    end
    # This is allowed to fail, particularly if we decided to roll back
    describe! stack_name, options rescue nil
  rescue Aws::CloudFormation::Errors::ValidationError => e
    Cfer::LOGGER.info "CFN validation error: #{e.message}"
  end
  stack
end

.delete!(stack_name, options = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cfer.rb', line 190

def delete!(stack_name, options = {})
  config(options)
  cfn = options[:aws_options] || {}
  cfn_stack = options[:cfer_client] || cfn_stack = Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name))

  delete_opts = {
    stack_name: stack_name
  }
  delete_opts[:role_arn] = options[:role_arn] if options[:role_arn]
  cfn_stack.delete_stack(delete_opts)

  if options[:follow]
    tail! stack_name, options.merge(cfer_client: cfn_stack)
  end
rescue Aws::CloudFormation::Errors::ValidationError => e
  if e.message =~ /Stack .* does not exist/
    raise Cfer::Util::StackDoesNotExistError, e.message
  else
    raise e
  end
end

.describe!(stack_name, options = {}) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cfer.rb', line 112

def describe!(stack_name, options = {})
  config(options)
  cfn = options[:aws_options] || {}
  cfn_stack = options[:cfer_client] || Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name))
   = cfn_stack.
  cfn_stack = cfn_stack.fetch_stack

  cfer_version = .fetch("Cfer", {}).fetch("Version", nil)
  if cfer_version
    cfer_version_str = [cfer_version["major"], cfer_version["minor"], cfer_version["patch"]].join '.'
    cfer_version_str << '-' << cfer_version["pre"] unless cfer_version["pre"].nil?
    cfer_version_str << '+' << cfer_version["build"] unless cfer_version["build"].nil?
  end

  Cfer::LOGGER.debug "Describe stack: #{cfn_stack}"
  Cfer::LOGGER.debug "Describe metadata: #{}"

  case options[:output_format]
  when 'json'
    puts render_json(cfn_stack, options)
  when 'table', nil
    puts "Status: #{cfn_stack[:stack_status]}"
    puts "Description: #{cfn_stack[:description]}" if cfn_stack[:description]
    puts "Created with Cfer version: #{Semantic::Version.new(cfer_version_str)} (current: #{Cfer::SEMANTIC_VERSION.to_s})" if cfer_version
    puts ""
    def tablify(list, type)
      list ||= []
      list.map { |param|
        {
          :Type => type.to_s.titleize,
          :Key => param[:"#{type}_key"],
          :Value => param[:"#{type}_value"]
        }
      }
    end
    parameters = tablify(cfn_stack[:parameters] || [], 'parameter')
    outputs = tablify(cfn_stack[:outputs] || [], 'output')
    tp parameters + outputs, :Type, :Key, {:Value => {:width => 80}}
  else
    raise Cfer::Util::CferError, "Invalid output format #{options[:output_format]}."
  end
  cfn_stack
end

.estimate!(tmpl, options = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/cfer.rb', line 180

def estimate!(tmpl, options = {})
  config(options)
  cfn = options[:aws_options] || {}

  cfn_stack = options[:cfer_client] || Cfer::Cfn::Client.new(cfn)
  stack = options[:cfer_stack] || Cfer::stack_from_file(tmpl,
    options.merge(client: cfn_stack, parameters: generate_final_parameters(options)))
  puts cfn_stack.estimate(stack)
end

.generate!(tmpl, options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/cfer.rb', line 169

def generate!(tmpl, options = {})
  config(options)
  cfn = options[:aws_options] || {}

  cfn_stack = options[:cfer_client] || Cfer::Cfn::Client.new(cfn)
  raise Cfer::Util::CferError, "No such template file: #{tmpl}" unless File.exist?(tmpl) || options[:cfer_stack]
  stack = options[:cfer_stack] || Cfer::stack_from_file(tmpl,
    options.merge(client: cfn_stack, parameters: generate_final_parameters(options))).to_h
  puts render_json(stack, options)
end

.stack_from_block(options = {}, &block) ⇒ Cfer::Core::Stack

Builds a Cfer::Core::Stack from a Ruby block

Parameters:

  • options (Hash) (defaults to: {})

    The stack options

  • block

    The block containing the Cfn DSL

Options Hash (options):

  • :parameters (Hash)

    The CloudFormation stack parameters

Returns:



218
219
220
221
222
223
224
# File 'lib/cfer.rb', line 218

def stack_from_block(options = {}, &block)
  s = Cfer::Core::Stack.new(options)
  templatize_errors('block') do
    s.build_from_block(&block)
  end
  s
end

.stack_from_file(file, options = {}) ⇒ Cfer::Core::Stack

Builds a Cfer::Core::Stack from a ruby script

Parameters:

  • file (String)

    The file containing the Cfn DSL or plain JSON

  • options (Hash) (defaults to: {})

    (see #stack_from_block)

Returns:



231
232
233
234
235
236
237
238
239
# File 'lib/cfer.rb', line 231

def stack_from_file(file, options = {})
  return stack_from_stdin(options) if file == '-'

  s = Cfer::Core::Stack.new(options)
  templatize_errors(file) do
    s.build_from_file file
  end
  s
end

.stack_from_stdin(options = {}) ⇒ Cfer::Core::Stack

Builds a Cfer::Core::Stack from stdin

Parameters:

  • options (Hash) (defaults to: {})

    (see #stack_from_block)

Returns:



245
246
247
248
249
250
251
# File 'lib/cfer.rb', line 245

def stack_from_stdin(options = {})
  s = Cfer::Core::Stack.new(options)
  templatize_errors('STDIN') do
    s.build_from_string STDIN.read, 'STDIN'
  end
  s
end

.tail!(stack_name, options = {}, &block) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cfer.rb', line 156

def tail!(stack_name, options = {}, &block)
  config(options)
  cfn = options[:aws_options] || {}
  cfn_client = options[:cfer_client] || Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name))
  if block
    cfn_client.tail(options, &block)
  else
    cfn_client.tail(options) do |event|
      Cfer::LOGGER.info "%s %-30s %-40s %-20s %s" % [event.timestamp, color_map(event.resource_status), event.resource_type, event.logical_resource_id, event.resource_status_reason]
    end
  end
end