summaryrefslogtreecommitdiff
path: root/lib/chef/client.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/client.rb')
-rw-r--r--lib/chef/client.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index 0ae7ef70e6..681260eee2 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -28,6 +28,7 @@ class Chef
attr_accessor :node, :registration, :safe_name
+ # Creates a new Chef::Client.
def initialize()
@node = nil
@safe_name = nil
@@ -35,14 +36,27 @@ class Chef
@rest = Chef::REST.new(Chef::Config[:registration_url])
end
+ # Do a full run for this Chef::Client. Calls:
+ #
+ # * build_node
+ # * register
+ # * authenticate
+ # * do_attribute_files
+ # * save_node
+ # * converge
+ #
+ # In that order.
def run
build_node
register
authenticate
+ do_attribute_files
save_node
converge
end
+ # Builds a new node object for this client. Starts with querying for the FQDN of the current
+ # host, then merges in the facts from Facter.
def build_node(node_name=nil)
node_name ||= Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
@safe_name = node_name.gsub(/\./, '_')
@@ -63,6 +77,10 @@ class Chef
@node
end
+ # If this node has been registered before, this method will fetch the current registration
+ # data.
+ #
+ # If it has not, we register it by calling create_registration.
def register
@registration = nil
begin
@@ -81,12 +99,15 @@ class Chef
end
end
+ # Generates a random secret, stores it in the Chef::Filestore with the "registration" key,
+ # and posts our nodes registration information to the server.
def create_registration
@secret = random_password(40)
Chef::FileStore.store("registration", @safe_name, { "secret" => @secret })
@rest.post_rest("registrations", { :id => @safe_name, :password => @secret })
end
+ # Authenticates the node via OpenID.
def authenticate
response = @rest.post_rest('openid/consumer/start', {
"openid_identifier" => "#{Chef::Config[:openid_url]}/openid/server/node/#{@safe_name}",
@@ -98,10 +119,22 @@ class Chef
)
end
+ # Gets all the attribute files included in all the cookbooks available on the server,
+ # and executes them.
+ def do_attribute_files
+ af_list = @rest.get_rest('cookbooks/_attribute_files')
+ af_list.each do |af|
+ @node.instance_eval(af["contents"], "#{af['cookbook']}/#{af['name']}", 1)
+ end
+ end
+
+ # Updates the current node configuration on the server.
def save_node
@rest.put_rest("nodes/#{@safe_name}", @node)
end
+ # Compiles the full list of recipes for the server, and passes it to an instance of
+ # Chef::Runner.converge.
def converge
results = @rest.get_rest("nodes/#{@safe_name}/compile")
results["collection"].resources.each do |r|
@@ -112,6 +145,7 @@ class Chef
end
protected
+ # Generates a random password of "len" length.
def random_password(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""