Class: Chef::Knife::CloudformationInspect

Inherits:
Chef::Knife
  • Object
show all
Includes:
KnifeCloudformation::Knife::Base, KnifeCloudformation::Utils::Ssher
Defined in:
lib/chef/knife/cloudformation_inspect.rb

Overview

Cloudformation inspect command

Instance Method Summary collapse

Methods included from KnifeCloudformation::Utils::Ssher

#remote_file_contents

Methods included from KnifeCloudformation::Knife::Base

included

Instance Method Details

#_runObject

Run the stack inspection action



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/knife/cloudformation_inspect.rb', line 54

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

#display_attribute(stack) ⇒ Object



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/chef/knife/cloudformation_inspect.rb', line 138

def display_attribute(stack)
  attr = config[: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.info MultiJson.dump(
    MultiJson.load(
      MultiJson.dump(attr)
    ),
    :pretty => true
  )
end

#display_instance_failure(stack) ⇒ Object



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
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/knife/cloudformation_inspect.rb', line 75

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 = Chef::Config[:knife][:cloudformation][: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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/chef/knife/cloudformation_inspect.rb', line 164

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.info MultiJson.dump(asg_nodes, :pretty => true)
  end
  unless(compute_nodes.empty?)
    ui.info '  Compute Instances:'
    ui.info MultiJson.dump(compute_nodes, :pretty => true)
  end
end

#ssh_attempt_usersArray<String>

Users to attempt SSH connection



126
127
128
129
130
131
# File 'lib/chef/knife/cloudformation_inspect.rb', line 126

def ssh_attempt_users
  base_user = Chef::Config[:knife][:cloudformation][:ssh_user] ||
    Chef::Config[:knife][:ssh_user] ||
    ENV['USER']
  [base_user, Chef::Config[:knife][:cloudformation][:ssh_attempt_users]].flatten.compact
end

#ssh_keyObject



133
134
135
136
# File 'lib/chef/knife/cloudformation_inspect.rb', line 133

def ssh_key
  Chef::Config[:knife][:cloudformation][:identity_file] ||
    Chef::Config[:knife][:identity_file]
end