class Qurd::Action::Chef

Clean up chef client and node data @example Chef configuration

chef_configuration: "/etc/chef/some_knife_config.rb"

Public Class Methods

configure(_action) click to toggle source

Add a setter and getter for {#Qurd::Message} chef_node chef_client and configure Chef using {#Qurd::Configuration} chef_configuration or the default /etc/chef/knife.rb. If {#Qurd::Configuration} log_file is defined, Chef will log to it. @param [String] _action the name of the action being configured

# File lib/qurd/action/chef.rb, line 17
def self.configure(_action)
  return if @configure_done
  configure_chef
  Qurd::Message.add_accessor(:chef_node)
  Qurd::Message.add_accessor(:chef_client)
  @configure_done = true
end
configure_chef() click to toggle source

Parse Chef's configuration file, as defined in qurd.yml, set Chef's log_level, and Chef's log_location

# File lib/qurd/action/chef.rb, line 27
def self.configure_chef
  config = File.expand_path(qurd_configuration.chef_configuration ||
                            '/etc/chef/knife.rb')
  qurd_logger.debug("Configuring Chef for using #{config}")
  ::Chef::Config.from_file(config)
  ::Chef::Config[:log_level] = qurd_configuration.log_level
  if qurd_configuration.log_file_io
    qurd_logger.debug('Setting chef log file to ' \
                      "'#{qurd_configuration.log_file_io.path}'")
    ::Chef::Config[:log_location] = qurd_configuration.log_file_io
  end
end

Public Instance Methods

run_before() click to toggle source

Find the node, using the instance_id of the message

# File lib/qurd/action/chef.rb, line 41
def run_before
  find_chef_node
  find_chef_client
end
terminate() click to toggle source

Delete the node, if the message did not fail other processing steps and dry_run is not true @see {#Qurd::Message}

# File lib/qurd/action/chef.rb, line 49
def terminate
  if failed?
    qurd_logger.warn('Not deleting, message failed to process')
  elsif qurd_configuration.dry_run
    check_dry_run
  else
    qurd_logger.debug('Deleting')
    chef_node.destroy unless chef_node.nil?
    chef_client.destroy unless chef_client.nil?
  end
end
test() click to toggle source

Respond to test actions

# File lib/qurd/action/chef.rb, line 62
def test
  qurd_logger.info('Test')
end

Private Instance Methods

check_dry_run() click to toggle source

Print log messages, based on object state

# File lib/qurd/action/chef.rb, line 119
def check_dry_run
  if !find_chef_node
    qurd_logger.debug('Dry run; missing node')
  elsif !find_chef_client
    qurd_logger.debug('Dry run; missing client')
  else
    qurd_logger.debug('Dry run; would delete')
  end
end
chef_search_client(name) click to toggle source

Search for a Chef client, based on the name passed, likely FQDN @param [String] name the client's name @return [Chef::ApiClient|nil]

# File lib/qurd/action/chef.rb, line 113
def chef_search_client(name)
  res = chef_search.search(:client, "name:#{name}")
  res.last == 1 ? res[0][0] : nil
end
chef_search_node(key, value) click to toggle source

Search for a Chef node, based on the instance_id @param [String] key the chef key to search @param [String] value the value of the key to search @return [Chef::Node|nil]

# File lib/qurd/action/chef.rb, line 105
def chef_search_node(key, value)
  res = chef_search.search(:node, "#{key}:#{value}")
  res.last == 1 ? res[0][0] : nil
end
find_chef_client() click to toggle source

Set the message chef_client and context chef_client_name @see chef_search_client

# File lib/qurd/action/chef.rb, line 84
def find_chef_client
  client = chef_search_client(chef_node.name)
  self.chef_client = client
  message.context[:chef_client_name] = client.name
  qurd_logger.debug('Chef client found')
rescue NoMethodError
  qurd_logger.warn('Chef client not found')
  self.chef_client = nil
  message.context[:chef_client_name] = nil
end
find_chef_node() click to toggle source

Set the message chef_node and context chef_name @see chef_search_node

# File lib/qurd/action/chef.rb, line 70
def find_chef_node
  node = chef_search_node("instance_id", instance_id)
  node ||= chef_search_node("name", instance_name)
  self.chef_node = node
  message.context[:chef_name] = node.name
  qurd_logger.debug('Chef node found')
rescue NoMethodError
  qurd_logger.warn('Chef node not found')
  self.chef_node = nil
  message.context[:chef_name] = nil
end