lib/chef/client.rb in chef-0.9.6 vs lib/chef/client.rb in chef-0.9.8.beta.1

- old
+ new

@@ -30,30 +30,29 @@ require 'chef/runner' require 'chef/cookbook/cookbook_collection' require 'chef/cookbook/file_vendor' require 'chef/cookbook/file_system_file_vendor' require 'chef/cookbook/remote_file_vendor' +require 'chef/version' require 'ohai' class Chef class Client + attr_accessor :node + attr_accessor :ohai + attr_accessor :rest + attr_accessor :runner + # TODO: timh/cw: 5-19-2010: json_attribs should be moved to RunContext? - attr_accessor :node, :registration, :json_attribs, :node_name, :ohai, :rest, :runner - attr_reader :node_exists - + attr_reader :json_attribs + # Creates a new Chef::Client. - def initialize() + def initialize(json_attribs=nil) + @json_attribs = json_attribs @node = nil - @registration = nil - @json_attribs = nil - @node_name = nil - @node_exists = true @runner = nil @ohai = Ohai::System.new - Chef::Log.verbose = Chef::Config[:verbose_logging] - Mixlib::Authentication::Log.logger = Ohai::Log.logger = Chef::Log.logger - @ohai_has_run = false end # Do a full run for this Chef::Client. Calls: # # * run_ohai - Collect information about the system @@ -63,22 +62,20 @@ # * converge - Bring this system up to date # # === Returns # true:: Always returns true. def run - self.runner = nil run_context = nil run_ohai - determine_node_name register unless Chef::Config[:solo] build_node begin run_status = Chef::RunStatus.new(node) run_status.start_clock - Chef::Log.info("Starting Chef Run") + Chef::Log.info("Starting Chef Run (Version #{Chef::VERSION})") if Chef::Config[:solo] Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest) } run_context = Chef::RunContext.new(node, Chef::CookbookCollection.new(Chef::CookbookLoader.new)) run_status.run_context = run_context @@ -89,26 +86,24 @@ # downloading (during sync_cookbooks) and lazy (during the run # itself, through FileVendor). After the run is over, clean up the # cache. valid_cache_entries = Hash.new - save_node - # Sync_cookbooks eagerly loads all files except files and templates. # It returns the cookbook_hash -- the return result from # /nodes/#{nodename}/cookbooks -- which we will use for our # run_context. Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::RemoteFileVendor.new(manifest, rest, valid_cache_entries) } cookbook_hash = sync_cookbooks(valid_cache_entries) run_context = Chef::RunContext.new(node, Chef::CookbookCollection.new(cookbook_hash)) run_status.run_context = run_context assert_cookbook_path_not_empty(run_context) - save_node converge(run_context) - save_node + Chef::Log.debug("Saving the current state of node #{node_name}") + @node.save cleanup_file_cache(valid_cache_entries) end run_status.stop_clock @@ -141,56 +136,42 @@ end Chef::Log.error("Exception handlers complete") end def run_ohai - if ohai.keys - ohai.refresh_plugins - else - ohai.all_plugins - end + ohai.all_plugins end - def determine_node_name - unless node_name - if Chef::Config[:node_name] - @node_name = Chef::Config[:node_name] - else - @node_name = ohai[:fqdn] ? ohai[:fqdn] : ohai[:hostname] - Chef::Config[:node_name] = node_name - end + def node_name + name = Chef::Config[:node_name] || ohai[:fqdn] || ohai[:hostname] + Chef::Config[:node_name] = name - raise RuntimeError, "Unable to determine node name from ohai" unless node_name + unless name + msg = "Unable to determine node name: configure node_name or configure the system's hostname and fqdn" + raise Chef::Exceptions::CannotDetermineNodeName, msg end - node_name + + name end # Builds a new node object for this client. Starts with querying for the FQDN of the current # host (unless it is supplied), then merges in the facts from Ohai. # # === Returns # node<Chef::Node>:: Returns the created node object, also stored in @node def build_node Chef::Log.debug("Building node object for #{@node_name}") - - unless Chef::Config[:solo] - begin - @node = rest.get_rest("nodes/#{@node_name}") - rescue Net::HTTPServerException => e - raise unless e.message =~ /^404/ - end + + if Chef::Config[:solo] + @node = Chef::Node.build(node_name) + else + @node = Chef::Node.find_or_create(node_name) end - - unless node - @node_exists = false - @node = Chef::Node.new - @node.name(node_name) - end - @node.prepare_for_run(ohai.data, @json_attribs) - # Need to nil-ify the json attribs so they are not applied on subsequent runs - @json_attribs = nil + @node.process_external_attrs(ohai.data, @json_attribs) + @node.save unless Chef::Config[:solo] + @node.reset_defaults_and_overrides @node end # @@ -304,24 +285,9 @@ Chef::FileCache.find(File.join(%w{cookbooks ** *})).each do |cache_filename| unless valid_cache_entries[cache_filename] Chef::Log.info("Removing #{cache_filename} from the cache; it is no longer on the server.") Chef::FileCache.delete(cache_filename) end - end - end - - # Updates the current node configuration on the server. - # - # === Returns - # Chef::Node - the current node - def save_node - Chef::Log.debug("Saving the current state of node #{node_name}") - if node_exists - @node = @node.save - else - result = rest.post_rest("nodes", node) - @node_exists = true - @node = rest.get_rest(result['uri']) end end # Converges the node. #