Class: Sfn::Command::Inspect

Inherits:
Sfn::Command
  • Object
show all
Includes:
Sfn::CommandModule::Base, Utils::Ssher
Defined in:
lib/sfn/command/inspect.rb

Overview

Inspect command

Instance Method Summary collapse

Methods included from Utils::Ssher

#remote_file_contents

Methods included from Sfn::CommandModule::Base

included

Methods inherited from Sfn::Command

#config, #initialize

Constructor Details

This class inherits a constructor from Sfn::Command

Instance Method Details

#display_attribute(stack) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sfn/command/inspect.rb', line 94

def display_attribute(stack)
  [config[:attribute]].flatten.compact.each do |stack_attribute|
    attr = stack_attribute.split('.').inject(stack) do |memo, key|
      args = key.scan(/\(([^)]*)\)/).flatten.first.to_s
      if(args)
        args = args.split(',').map{|a| a.to_i.to_s == a ? a.to_i : a}
        key = key.split('(').first
      end
      if(memo.public_methods.include?(key.to_sym))
        if(args.size == 1 && args.first.to_s.start_with?('&'))
          memo.send(key, &args.first.slice(2, args.first.size).to_sym)
        else
          memo.send(*[key, args].flatten.compact)
        end
      else
        raise NoMethodError.new "Invalid attribute requested! (#{memo.class}##{key})"
      end
    end
    ui.info "  Attribute Lookup -> #{config[:attribute]}:"
    ui.puts MultiJson.dump(
      MultiJson.load(
        MultiJson.dump(attr)
      ),
      :pretty => true
    )
  end
end

#display_instance_failure(stack) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
81
# File 'lib/sfn/command/inspect.rb', line 35

def display_instance_failure(stack)
  instances = stack.resources.all.find_all do |resource|
    resource.state.to_s.end_with?('failed')
  end.map do |resource|
    # If compute instance, simply expand
    if(resource.within?(:compute, :servers))
      resource.instance
    # If a waitcondition, check for instance ID
    elsif(resource.type.to_s.downcase.end_with?('waitcondition'))
      if(resource.status_reason.to_s.include?('uniqueId'))
        srv_id = resource.status_reason.split(' ').last.strip
        provider.connection.api_for(:compute).servers.get(srv_id)
      end
    end
  end.compact
  if(instances.empty?)
    ui.error 'Failed to locate any failed instances'
  else
    log_path = config[:failure_log_path]
    if(log_path.to_s.empty?)
      log_path = '/var/log/chef/client.log'
    end
    opts = ssh_key ? {:keys => [ssh_key]} : {}
    instances.each do |instance|
      ui.info "  -> Log inspect for #{instance.id}:"
      address = instance.addresses_public.map do |address|
        if(address.version == 4)
          address.address
        end
      end
      if(address)
        ssh_attempt_users.each do |user|
          begin
            ui.info remote_file_contents(address.first, user, log_path, opts)
            break
          rescue Net::SSH::AuthenticationFailed
            ui.warn "Authentication failed for user #{user} on instance #{address}"
          rescue => e
            ui.error "Failed to retrieve log: #{e}"
            _debug e
            break
          end
        end
      end
    end
  end
end

#display_nodes(stack) ⇒ Object



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
155
156
157
158
159
160
# File 'lib/sfn/command/inspect.rb', line 122

def display_nodes(stack)
  asg_nodes = Smash[
    stack.resources.all.find_all do |resource|
      resource.within?(:auto_scale, :groups)
    end.map do |group_resource|
      asg = group_resource.expand
      [
        asg.name,
        Smash[
          asg.servers.map(&:expand).map{|s|
            [s.id, Smash.new(
                :name => s.name,
                :addresses => s.addresses.map(&:address)
            )]
          }
        ]
      ]
    end
  ]
  compute_nodes = Smash[
    stack.resources.all.find_all do |resource|
      resource.within?(:compute, :servers)
    end.map do |srv|
      srv = srv.instance
      [srv.id, Smash.new(
          :name => srv.name,
          :addresses => srv.addresses.map(&:address)
      )]
    end
  ]
  unless(asg_nodes.empty?)
    ui.info '  AutoScale Group Instances:'
    ui.puts MultiJson.dump(asg_nodes, :pretty => true)
  end
  unless(compute_nodes.empty?)
    ui.info '  Compute Instances:'
    ui.puts MultiJson.dump(compute_nodes, :pretty => true)
  end
end

#execute!Object

Run the stack inspection action



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sfn/command/inspect.rb', line 12

def execute!
  stack_name = name_args.last
  stack = provider.connection.stacks.get(stack_name)
  ui.info "Stack inspection #{ui.color(stack_name, :bold)}:"
  outputs = api_action!(:api_stack => stack) do
    [:attribute, :nodes, :instance_failure].map do |key|
      if(config.has_key?(key))
        send("display_#{key}", stack)
        key
      end
    end.compact
  end
  if(outputs.empty?)
    ui.info '  Stack dump:'
    ui.puts MultiJson.dump(
      MultiJson.load(
        stack.reload.to_json
      ),
      :pretty => true
    )
  end
end

#ssh_attempt_usersArray<String>

Users to attempt SSH connection

Returns:

  • (Array<String>)

    usernames for ssh connect attempt



86
87
88
# File 'lib/sfn/command/inspect.rb', line 86

def ssh_attempt_users
  [config[:ssh_user], config[:ssh_attempt_users], ENV['USER']].flatten.compact.uniq
end

#ssh_keyObject



90
91
92
# File 'lib/sfn/command/inspect.rb', line 90

def ssh_key
  config[:identity_file]
end