Module: Sfn::CommandModule::Base::InstanceMethods

Defined in:
lib/sfn/command_module/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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sfn/command_module/base.rb', line 57

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

#allowed_attributesArray<String>

Returns attributes to display.

Returns:

  • (Array<String>)

    attributes to display



89
90
91
# File 'lib/sfn/command_module/base.rb', line 89

def allowed_attributes
  opts.fetch(:attributes, config.fetch(:attributes, default_attributes))
end

#as_title(string) ⇒ String

Format snake cased key to title

Parameters:

  • string (String, Symbol)

Returns:

  • (String)

    String



76
77
78
# File 'lib/sfn/command_module/base.rb', line 76

def as_title(string)
  string.to_s.split('_').map(&:capitalize).join(' ')
end

#attribute_allowed?(attr) ⇒ TrueClass, FalseClass

Check if attribute is allowed for display

Parameters:

  • attr (String)

Returns:

  • (TrueClass, FalseClass)


102
103
104
# File 'lib/sfn/command_module/base.rb', line 102

def attribute_allowed?(attr)
  opts.fetch(:all_attributes, config[:all_attributes], allowed_attributes.include?(attr))
end

#configSmash

Note:

callback requires are also loaded here

Override config method to memoize the result allowing for modifications to the configuration during runtime

Returns:

  • (Smash)


156
157
158
159
160
161
162
163
164
# File 'lib/sfn/command_module/base.rb', line 156

def config
  memoize(:config) do
    result = super
    result.fetch(:callbacks, :require, []).each do |c_loader|
      require c_loader
    end
    result
  end
end

#custom_stack_typesArray<String>

Returns:

  • (Array<String>)


17
18
19
# File 'lib/sfn/command_module/base.rb', line 17

def custom_stack_types
  [config.fetch(:stack_types, [])].flatten.compact
end

#default_attributesArray<String>

Returns default attributes to display.

Returns:

  • (Array<String>)

    default attributes to display



94
95
96
# File 'lib/sfn/command_module/base.rb', line 94

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



133
134
135
136
137
138
139
140
141
142
# File 'lib/sfn/command_module/base.rb', line 133

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

#name_argsArray<String>

Simple compat proxy method

Returns:

  • (Array<String>)


147
148
149
# File 'lib/sfn/command_module/base.rb', line 147

def name_args
  arguments
end

#name_required!NilClass

Force error exception when no name is provided

Returns:

  • (NilClass)

Raises:

  • (ArgumentError)


170
171
172
173
174
175
# File 'lib/sfn/command_module/base.rb', line 170

def name_required!
  if(name_args.empty?)
    ui.error 'Name argument must be provided!'
    raise ArgumentError.new 'Missing required name argument'
  end
end

#poll_stack(name) ⇒ Object

Poll events on stack

Parameters:

  • name (String)

    name of stack



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sfn/command_module/base.rb', line 109

def poll_stack(name)
  provider.connection.stacks.reload
  retry_attempts = 0
  begin
    events = Sfn::Command::Events.new({:poll => true}, [name]).execute!
  rescue => e
    if(retry_attempts < config.fetch(:max_poll_retries, 5).to_i)
      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

Returns:

  • (KnifeCloudformation::Provider)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sfn/command_module/base.rb', line 22

def provider
  begin
    memoize(:provider, :direct) do
      result = Sfn::Provider.new(
        :miasma => config[:credentials],
        :async => false,
        :fetch => false
      )
      result.connection.data[:stack_types] = (
        [
          (result.connection.class.const_get(:RESOURCE_MAPPING).detect do |klass, info|
            info[:api] == :orchestration
          end || []).first
        ] + custom_stack_types
      ).compact.uniq
      retry_config = config.fetch(:retry,
        config.fetch(:retries, {})
      )
      result.connection.data[:retry_ui] = ui
      result.connection.data[:locations] = config.fetch(:locations, {})
      result.connection.data[:retry_type] = retry_config.fetch(:type, :exponential)
      result.connection.data[:retry_interval] = retry_config.fetch(:interval, 5)
      result.connection.data[:retry_max] = retry_config.fetch(:max_attempts, 20)
      result
    end
  rescue
    ui.error 'Failed to create remote API connection. Please validate configuration!'
    raise
  end
end

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

Get stack

Parameters:

  • name (String)

    name of stack

Returns:

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


84
85
86
# File 'lib/sfn/command_module/base.rb', line 84

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

#valid_stack_typesArray<String>

Returns:

  • (Array<String>)


12
13
14
# File 'lib/sfn/command_module/base.rb', line 12

def valid_stack_types
  provider.connection.data[:stack_types]
end