Module: AwsCleaner::Chef

Defined in:
lib/aws-cleaner.rb

Class Method Summary collapse

Class Method Details

.client(config) ⇒ Object

chef connection



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

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



39
40
41
42
43
44
# File 'lib/aws-cleaner.rb', line 39

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



31
32
33
34
35
36
# File 'lib/aws-cleaner.rb', line 31

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aws-cleaner.rb', line 47

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