Module: ChefProvisioner::Chef

Extended by:
Chef
Included in:
Chef
Defined in:
lib/chef-provisioner/chef.rb

Overview

Simple API to access chef server to manage clients and nodes

Instance Method Summary collapse

Instance Method Details

#configure(**kwargs) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/chef-provisioner/chef.rb', line 11

def configure(**kwargs)
  ChefAPI.configure do |config|
    config.endpoint = kwargs[:endpoint]
    config.client = kwargs[:client]
    config.key = kwargs[:key_path]
  end
end

#create_client(name) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/chef-provisioner/chef.rb', line 25

def create_client(name)
  client = ChefAPI::Resource::Client.create(name: name)
  client.private_key
rescue => e
  puts "Failed to create client #{name}"
  puts e.message
end

#create_node(name, attributes: {}, run_list: []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef-provisioner/chef.rb', line 33

def create_node(name, attributes:{}, run_list:[])
  node = ChefAPI::Resource::Node.create(name: name, run_list: run_list)
  node.automatic = attributes['automatic'] || {}
  node.default = attributes['default'] || {}
  node.normal = attributes['normal'] || {}
  node.override = attributes['override'] || {}
  node.save
rescue => e
  puts "Failed to create node #{name}"
  puts e.message
end

#delete_client(name) ⇒ Object



45
46
47
48
49
50
# File 'lib/chef-provisioner/chef.rb', line 45

def delete_client(name)
  ChefAPI::Resource::Client.destroy(name)
rescue => e
  puts "Failed to delete client #{name}"
  puts e.message
end

#delete_node(name) ⇒ Object



52
53
54
55
56
57
# File 'lib/chef-provisioner/chef.rb', line 52

def delete_node(name)
  ChefAPI::Resource::Node.destroy(name)
rescue => e
  puts "Failed to delete node #{name}"
  puts e.message
end

#init_server(name, attributes: {}, run_list: []) ⇒ Object



19
20
21
22
23
# File 'lib/chef-provisioner/chef.rb', line 19

def init_server(name, attributes:{}, run_list:[])
  key = create_client(name)
  create_node(name, attributes: attributes, run_list: run_list)
  key
end

#nuke(name) ⇒ Object



59
60
61
62
# File 'lib/chef-provisioner/chef.rb', line 59

def nuke(name)
  delete_client(name)
  delete_node(name)
end