module AwsCleaner::Chef

Public Class Methods

client(config) click to toggle source

chef connection

# 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) click to toggle source

call the Chef API to get the FQDN of the instance

# 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) click to toggle source

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

# 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) click to toggle source

call the Chef API to remove the node

# 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