Module: KnifeCloudformation::Knife::Base::InstanceMethods

Defined in:
lib/knife-cloudformation/knife/base.rb

Overview

Instance methods for cloudformation command classes

Instance Method Summary collapse

Instance Method Details

#_debug(e, *args) ⇒ Object

Write exception information if debug is enabled

Parameters:

  • e (Exception)
  • args (String)

    extra strings to output



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/knife-cloudformation/knife/base.rb', line 21

def _debug(e, *args)
  if(ENV['DEBUG'])
    ui.fatal "Exception information: #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}\n"
    if(e.is_a?(Miasma::Error::ApiError))
      ui.fatal "Response body: #{e.response.body.to_s.inspect}"
    end
    args.each do |string|
      ui.fatal string
    end
  end
end

#allowed_attributesArray<String>

Returns attributes to display.

Returns:

  • (Array<String>)

    attributes to display



42
43
44
# File 'lib/knife-cloudformation/knife/base.rb', line 42

def allowed_attributes
  Chef::Config[:knife][:cloudformation][:attributes] || default_attributes
end

#attribute_allowed?(attr) ⇒ TrueClass, FalseClass

Check if attribute is allowed for display

Parameters:

  • attr (String)

Returns:

  • (TrueClass, FalseClass)


55
56
57
# File 'lib/knife-cloudformation/knife/base.rb', line 55

def attribute_allowed?(attr)
  config[:all_attributes] || allowed_attributes.include?(attr)
end

#configure_chefObject

Disable chef configuration. Let the dep loader do that for us so it doesn’t squash config values set via options



102
103
104
# File 'lib/knife-cloudformation/knife/base.rb', line 102

def configure_chef
  true
end

#default_attributesArray<String>

Returns default attributes to display.

Returns:

  • (Array<String>)

    default attributes to display



47
48
49
# File 'lib/knife-cloudformation/knife/base.rb', line 47

def default_attributes
  %w(timestamp stack_name id)
end

#get_things(stack = nil, message = nil) { ... } ⇒ Object

Wrapper for information retrieval. Provides consistent error message for failures

Parameters:

  • stack (String) (defaults to: nil)

    stack name

  • message (String) (defaults to: nil)

    failure message

Yields:

  • block to wrap error handling

Returns:

  • (Object)

    result of yield



89
90
91
92
93
94
95
96
97
98
# File 'lib/knife-cloudformation/knife/base.rb', line 89

def get_things(stack=nil, message=nil)
  begin
    yield
  rescue => e
    ui.fatal "#{message || 'Failed to retrieve information'}#{" for requested stack: #{stack}" if stack}"
    ui.fatal "Reason: #{e}"
    _debug(e)
    exit 1
  end
end

#poll_stack(name) ⇒ Object

Poll events on stack

Parameters:

  • name (String)

    name of stack



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/knife-cloudformation/knife/base.rb', line 62

def poll_stack(name)
  retry_attempts = 0
  begin
    provider.connection.stacks.reload
    knife_events = Chef::Knife::CloudformationEvents.new
    knife_events.name_args.push(name)
    Chef::Config[:knife][:cloudformation][:poll] = true
    knife_events.run
  rescue => e
    if(retry_attempts < Chef::Config[:knife][:cloudformation].fetch(:max_poll_retries, 5))
      retry_attempts += 1
      warn "Unexpected error encountered (#{e.class}: #{e}) Retrying [retry count: #{retry_attempts}]"
      sleep(1)
      retry
    else
      raise
    end
  end
end

#providerKnifeCloudformation::Provider



13
14
15
# File 'lib/knife-cloudformation/knife/base.rb', line 13

def provider
  self.class.provider
end

#runObject

Wrapper to allow consistent exception handling



107
108
109
110
111
112
113
114
115
# File 'lib/knife-cloudformation/knife/base.rb', line 107

def run
  begin
    _run
  rescue => e
    ui.fatal "Unexpected Error: #{e.message}"
    _debug(e)
    exit 1
  end
end

#stack(name) ⇒ Miasma::Models::Orchestration::Stack

Get stack

Parameters:

  • name (String)

    name of stack

Returns:

  • (Miasma::Models::Orchestration::Stack)


37
38
39
# File 'lib/knife-cloudformation/knife/base.rb', line 37

def stack(name)
  provider.stacks.get(name)
end