module ChefRunDeck::Chef

> This is the Chef module. It interacts with the Chef server

Public Instance Methods

admin_api_client() click to toggle source
# File lib/chef-rundeck/chef.rb, line 33
def admin_api_client
  # => Configure an Administrative Chef API Client
  ChefAPI.endpoint = Config.chef_api_endpoint
  ChefAPI.client = Config.chef_api_admin
  ChefAPI.key = Config.chef_api_admin_key
end
api_client() click to toggle source

> ChefAPI <= #

# File lib/chef-rundeck/chef.rb, line 26
def api_client
  # => Configure a Chef API Client
  ChefAPI.endpoint = Config.chef_api_endpoint
  ChefAPI.client = Config.chef_api_client
  ChefAPI.key = Config.chef_api_client_key
end
delete(node) click to toggle source

> Delete a Node Object

# File lib/chef-rundeck/chef.rb, line 66
def delete(node)
  # => Make sure the Node Exists
  return 'Node not found on Chef Server' unless Node.exists?(node)

  # => Initialize the Admin API Client Settings
  admin_api_client

  # => Delete the Client & Node Object
  Client.delete(node)
  Node.delete(node)
  'Client/Node Deleted from Chef Server'
end
get_node(node, casecomp = false) click to toggle source

> Get Node

# File lib/chef-rundeck/chef.rb, line 48
def get_node(node, casecomp = false)
  node = Node.list.find { |n| n =~ /^#{node}$/i } if casecomp
  return false unless Node.exists?(node)
  Node.fetch(node)
end
list() click to toggle source

> Return Array List of Nodes

# File lib/chef-rundeck/chef.rb, line 55
def list
  Node.list
end
reset!() click to toggle source
# File lib/chef-rundeck/chef.rb, line 40
def reset!
  # => Reset the Chef API Configuration
  ChefAPI.reset!
  # => Clear Transient Configuration
  Config.clear(:rundeck)
end
run_list(node) click to toggle source

> Return a Node's Run List

# File lib/chef-rundeck/chef.rb, line 60
def run_list(node)
  return [] unless Node.exists?(node)
  Node.fetch(node).run_list
end

Private Instance Methods

build_hostname(node) click to toggle source

> Build the Hostname

# File lib/chef-rundeck/chef.rb, line 198
        def build_hostname(node) # rubocop:
  # => anode.bdwyertech.net:22
  [remote_hostname(node), remote_port(node)].compact.join(':')
end
custom_attributes(node) click to toggle source

> Define Extra Attributes for Resource Return

# File lib/chef-rundeck/chef.rb, line 156
        def custom_attributes(node)
  attribs = {}
  Array(Config.rundeck[:extras]).each do |attrib|
    attribs[attrib.to_sym] = node[attrib].inspect
  end
  # => Return the Custom Attributes Hash
  attribs
end
default_search_filter() click to toggle source

> Base Search Filter Definition

# File lib/chef-rundeck/chef.rb, line 110
        def default_search_filter
  {
    name: ['name'],
    kernel_machine: ['kernel', 'machine'],
    kernel_os: ['kernel', 'os'],
    fqdn: ['fqdn'],
    run_list: ['run_list'],
    roles: ['roles'],
    recipes: ['recipes'],
    chef_environment: ['chef_environment'],
    platform: ['platform'],
    platform_version: ['platform_version'],
    tags: ['tags'],
    hostname: ['hostname'],
    rd_hostname: ['rd_hostname'],
    rd_ssh_port: ['rd_ssh_port'],
    rd_winrm_port: ['rd_winrm_port'],
    rd_username: ['rd_username']
  }
end
project() click to toggle source

> Try to Parse Project-Specific Settings

# File lib/chef-rundeck/chef.rb, line 86
        def project
  projectname = Config.query_params['project']
  return {} unless projectname
  settings = Util.parse_json_config(Config.projects_file, false)
  return {} unless settings && settings[projectname]
  settings[projectname]
end
remote_hostname(node) click to toggle source

> Determine the Remote Hostname

# File lib/chef-rundeck/chef.rb, line 206
        def remote_hostname(node)
  node['rd_hostname'] || node['fqdn'] || node['hostname']
end
remote_port(node) click to toggle source

> Determine the Remote Port

# File lib/chef-rundeck/chef.rb, line 213
        def remote_port(node)
  # => WinRM if Windows
  if node['platform'] == 'windows'
    [
      node['rd_winrm_port'],
      Config.query_params['winrm_port'],
      project['winrm_port']
    ].find { |winrm_port| winrm_port }
  else
    # => SSH for Everything Else
    [
      node['rd_ssh_port'],
      Config.query_params['ssh_port'],
      project['ssh_port']
    ].find { |ssh_port| ssh_port }
  end
end
remote_username(node) click to toggle source

> Determine the Remote Username

# File lib/chef-rundeck/chef.rb, line 233
        def remote_username(node)
  [
    node['rd_username'],
    Config.query_params['username'],
    project['username'],
    Config.rd_node_username
  ].find { |username| username }
end
search_filter() click to toggle source

> Construct the Search Filter

# File lib/chef-rundeck/chef.rb, line 148
        def search_filter
  # => Merge the Default Filter with Additions
  default_search_filter.merge(search_filter_additions).reject { |_k, v| v.nil? || String(v).empty? }
end
search_filter_additions() click to toggle source

> Parse Additional Filter Elements

> Default Elements can be removed by passing them in here as null or empty

# File lib/chef-rundeck/chef.rb, line 136
        def search_filter_additions
  attribs = {}
  Array(Config.rundeck[:extras]).each do |attrib|
    attribs[attrib.to_sym] = [attrib]
  end
  # => Return the Custom Filter Additions Hash
  attribs
end
transient_settings() click to toggle source

> Construct Query-Specific Configuration

# File lib/chef-rundeck/chef.rb, line 97
        def transient_settings
  # => Build the Configuration
  cfg = {}
  cfg[:pattern] = Config.query_params['pattern'] || project['pattern'] || '*:*'
  cfg[:extras] = Util.serialize_csv(Config.query_params['extras']) || project['extras']

  # => Make the Settings Available via the Config Object
  Config.add(rundeck: cfg)
end