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

Constant Summary

Constants inherited from Sfn::Command

CONFIG_BASE_NAME, VALID_CONFIG_EXTENSIONS

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

Methods included from Sfn::CommandModule::Callbacks

#api_action!, #callbacks_for, #run_callbacks_for

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_load_balancers(stack) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sfn/command/inspect.rb', line 164

def display_load_balancers(stack)
  load_balancers = Smash[
    stack.resources.all.find_all do |resource|
      resource.within?(:load_balancer, :balancers)
    end.map do |lb|
      exp_lb = lb.expand
      lb_pub_addrs = exp_lb.public_addresses.nil? ? nil : exp_lb.public_addresses.map(&:address)
      lb_priv_addrs = exp_lb.private_addresses.nil? ? nil : exp_lb.private_addresses.map(&:address)
      [lb.id, Smash.new(
        :name => exp_lb.name,
        :state => exp_lb.state,
        :public_addresses => lb_pub_addrs,
        :private_addresses => lb_priv_addrs,
        :server_states => exp_lb.server_states,
      ).delete_if { |k, v| v.nil? }]
    end
  ]
  unless load_balancers.empty?
    ui.info "  Load Balancer Instances:"
    ui.puts MultiJson.dump(load_balancers, :pretty => true)
  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
161
162
# 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).compact.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
      if srv
        [srv.id, Smash.new(
          :name => srv.name,
          :addresses => srv.addresses.map(&:address),
        )]
      end
    end.compact
  ]
  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



11
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 11

def execute!
  name_required!
  stack_name = name_args.last
  stack = provider.stack(stack_name)
  ui.info "Stack inspection #{ui.color(stack_name, :bold)}:"
  outputs = api_action!(:api_stack => stack) do
    [:attribute, :nodes, :load_balancers, :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