Module: AwsCleaner::Chef

Defined in:
lib/aws-cleaner.rb

Class Method Summary collapse

Class Method Details

.client(config) ⇒ Object

chef connection



24
25
26
27
28
29
30
# File 'lib/aws-cleaner.rb', line 24

def self.client(config)
  ChefAPI::Connection.new(
    endpoint: config[:chef][:url],
    client: config[:chef][:client],
    key: config[:chef][:key]
  )
end

.get_chef_fqdn(instance_id, config) ⇒ Object

call the Chef API to get the FQDN of the instance



42
43
44
45
46
47
48
# File 'lib/aws-cleaner.rb', line 42

def self.get_chef_fqdn(instance_id, config)
  chef = client(config)
  results = chef.search.query(:node, "ec2_instance_id:#{instance_id} OR chef_provisioning_reference_server_id:#{instance_id}")
  return false if results.rows.empty?

  results.rows.first['automatic']['fqdn']
end

.get_chef_node_name(instance_id, config) ⇒ Object

call the Chef API to get the node name of the instance



33
34
35
36
37
38
39
# File 'lib/aws-cleaner.rb', line 33

def self.get_chef_node_name(instance_id, config)
  chef = client(config)
  results = chef.search.query(:node, "ec2_instance_id:#{instance_id} OR chef_provisioning_reference_server_id:#{instance_id}")
  return false if results.rows.empty?

  results.rows.first['name']
end

.remove_from_chef(node_name, chef, config) ⇒ Object

call the Chef API to remove the node



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aws-cleaner.rb', line 51

def self.remove_from_chef(node_name, chef, config)
  client = chef.clients.fetch(node_name)
  client.destroy
  node = chef.nodes.fetch(node_name)
  node.destroy
rescue StandardError => e
  puts "Failed to remove chef node: #{e}"
else
  # puts "Removed #{node_name} from chef"
  AwsCleaner::Notify.notify_chat('Removed ' + node_name + ' from Chef', config)
end