Module: ChefRunDeck::Chef

Extended by:
Chef
Includes:
ChefAPI::Resource
Included in:
Chef
Defined in:
lib/chef-rundeck/chef.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#admin_api_clientObject



33
34
35
36
37
38
# 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_clientObject

> ChefAPI <= #



26
27
28
29
30
31
# 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) ⇒ Object

> Delete a Node Object



66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object

> Get Node



48
49
50
51
52
# 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

#listObject

> Return Array List of Nodes



55
56
57
# File 'lib/chef-rundeck/chef.rb', line 55

def list
  Node.list
end

#reset!Object



40
41
42
43
44
45
# 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) ⇒ Object

> Return a Node’s Run List



60
61
62
63
# File 'lib/chef-rundeck/chef.rb', line 60

def run_list(node)
  return [] unless Node.exists?(node)
  Node.fetch(node).run_list
end

#search(pattern = '*:*') ⇒ Object

rubocop: disable AbcSize



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef-rundeck/chef.rb', line 165

def search(pattern = '*:*') # rubocop: disable AbcSize
  # => Initialize the Configuration
  transient_settings

  # => Pull in the Pattern
  pattern = Config.rundeck[:pattern]

  # => Execute the Chef Search
  result = PartialSearch.query(:node, search_filter, pattern, start: 0)

  # => Custom-Tailor the Resulting Objects
  result.rows.collect do |node|
    {
      nodename: node['name'],
      hostname: build_hostname(node),
      osArch: node['kernel_machine'],
      osFamily: node['platform'],
      osName: node['platform'],
      osVersion: node['platform_version'],
      description: node['name'],
      roles: node['roles'].sort.join(','),
      recipes: node['recipes'].sort.join(','),
      tags: [node['roles'], node['chef_environment'], node['tags']].flatten.sort.join(','),
      environment: node['chef_environment'],
      editUrl: ::File.join(Config.chef_api_endpoint, 'nodes', node['name']),
      username: remote_username(node)
    }.merge(custom_attributes(node)).reject { |_k, v| v.nil? || String(v).empty? }
  end
end