Class: Chef::Knife::GhostHosts

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/ghost_hosts.rb

Instance Method Summary collapse

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chef/knife/ghost_hosts.rb', line 12

def run
  all_nodes = []
  q = Chef::Search::Query.new
  query = @name_args[0] || "*:*"
  q.search(:node, query) do |node|
    all_nodes << node
  end
  all_nodes.each do |node|
    if node.has_key?("ec2")
      fqdn = node['ec2']['public_hostname']
      ipaddress = node['ec2']['public_ipv4']
    else
      fqdn = node['fqdn']
      ipaddress = node['ipaddress']
    end

    host = Ghost::Host.new(*[fqdn, ipaddress].compact)
    begin
      Ghost.store.add(host)
      ui.msg "[Adding] #{host.name} -> #{host.ip}"
    rescue Ghost::Host::NotResolvable
      ui.fatal "Unable to resolve IP address for target host #{ip.inspect}."
    end
  end
end