diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-04-27 22:44:28 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-04-27 22:44:28 -0700 |
commit | bab9b3459fe41849970e50cb9b96ea62370fbad0 (patch) | |
tree | c685354e2798bf08905dca308f9c58a52b94e33a /lib/chef | |
parent | 39c0a0994cb5af59e4fbb9fefbc487f6bc21d2fb (diff) | |
download | chef-bab9b3459fe41849970e50cb9b96ea62370fbad0.tar.gz |
chef compiles
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/compile.rb | 8 | ||||
-rw-r--r-- | lib/chef/log/formatter.rb | 15 | ||||
-rw-r--r-- | lib/chef/mixin/params_validate.rb | 57 | ||||
-rw-r--r-- | lib/chef/node.rb | 26 | ||||
-rw-r--r-- | lib/chef/platform.rb | 172 | ||||
-rw-r--r-- | lib/chef/provider.rb | 5 | ||||
-rw-r--r-- | lib/chef/provider/file.rb | 80 | ||||
-rw-r--r-- | lib/chef/resource.rb | 30 | ||||
-rw-r--r-- | lib/chef/resource/directory.rb | 89 | ||||
-rw-r--r-- | lib/chef/resource/file.rb | 137 | ||||
-rw-r--r-- | lib/chef/runner.rb | 93 |
11 files changed, 608 insertions, 104 deletions
diff --git a/lib/chef/compile.rb b/lib/chef/compile.rb index 5b1f9d756e..e974852426 100644 --- a/lib/chef/compile.rb +++ b/lib/chef/compile.rb @@ -25,12 +25,12 @@ class Chef class Compile - attr_accessor :node, :cookbook_loader, :resource_collection, :definitions + attr_accessor :node, :cookbook_loader, :collection, :definitions def initialize() @node = nil @cookbook_loader = Chef::CookbookLoader.new - @resource_collection = Chef::ResourceCollection.new + @collection = Chef::ResourceCollection.new @definitions = Hash.new end @@ -50,10 +50,10 @@ class Chef rmatch = recipe.match(/(.+?)::(.+)/) if rmatch cookbook = @cookbook_loader[rmatch[1]] - cookbook.load_recipe(rmatch[2], @node, @resource_collection, @definitions, @cookbook_loader) + cookbook.load_recipe(rmatch[2], @node, @collection, @definitions, @cookbook_loader) else cookbook = @cookbook_loader[recipe] - cookbook.load_recipe("default", @node, @resource_collection, @definitions, @cookbook_loader) + cookbook.load_recipe("default", @node, @collection, @definitions, @cookbook_loader) end end end diff --git a/lib/chef/log/formatter.rb b/lib/chef/log/formatter.rb index d177060d97..1c7044bdb1 100644 --- a/lib/chef/log/formatter.rb +++ b/lib/chef/log/formatter.rb @@ -28,9 +28,20 @@ require 'time' class Chef class Log class Formatter < Logger::Formatter - # Prints a log message as '[time] severity: message' + @@show_time = true + + def self.show_time=(show=false) + @@show_time = show + end + + # Prints a log message as '[time] severity: message' if Chef::Log::Formatter.show_time == true. + # Otherwise, doesn't print the time. def call(severity, time, progname, msg) - sprintf("[%s] %s: %s\n", time.rfc2822(), severity, msg2str(msg)) + if @@show_time + sprintf("[%s] %s: %s\n", time.rfc2822(), severity, msg2str(msg)) + else + sprintf("%s: %s\n", severity, msg2str(msg)) + end end # Converts some argument to a Logger.severity() call to a string. Regular strings pass through like diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb index 4c6b5f6612..29c0b6d002 100644 --- a/lib/chef/mixin/params_validate.rb +++ b/lib/chef/mixin/params_validate.rb @@ -2,7 +2,7 @@ # Chef::Mixin::ParamsValidate # # Because I can't deal with not having named params. Strongly based on Dave Rolsky's excellent -# Params::Validate module for Perl. Please don't blame him, though, if you hate this. :) +# Params::Validate module for Perl. Please don't blame him, though, this is full of bugs. :) # # Author:: Adam Jacob (<adam@hjksolutions.com>) # Copyright:: Copyright (c) 2008 HJK Solutions, LLC @@ -42,12 +42,15 @@ class Chef # :callbacks:: Takes a hash of Procs, which should return true if the argument is valid. # The key will be inserted into the error message if the Proc does not return true: # "Option #{key}'s value #{value} #{message}!" - # :kind_of:: Ensure that the value is a kind_of?(Whatever) - # :respond_to:: Esnure that the value has a given method. Takes one method name or an array of + # :kind_of:: Ensure that the value is a kind_of?(Whatever). If passed an array, it will ensure + # that the value is one of those types. + # :respond_to:: Ensure that the value has a given method. Takes one method name or an array of # method names. # :required:: Raise an exception if this parameter is missing. Valid values are true or false, # by default, options are not required. # :regex:: Match the value of the paramater against a regular expression. + # :equal_to:: Match the value of the paramater with ==. An array means it can be equal to any + # of the values. def validate(opts, map) #-- # validate works by taking the keys in the validation map, assuming it's a hash, and @@ -80,8 +83,19 @@ class Chef opts end + def set_or_return(instance, new_value, opts, map) + if new_value != nil + validate(opts, map) + instance = new_value + else + instance + end + end + private + + # Return the value of a parameter, or nil if it doesn't exist. def _pv_opts_lookup(opts, key) if opts.has_key?(key.to_s) @@ -104,12 +118,33 @@ class Chef end end + def _pv_equal_to(opts, key, to_be) + value = _pv_opts_lookup(opts, key) + if value != nil + passes = false + [ to_be ].flatten.each do |tb| + if value == tb + passes = true + end + end + unless passes + raise ArgumentError, "Option #{key} must be equal to one of: #{to_be.join(", ")}! You passed #{value.inspect}." + end + end + end + # Raise an exception if the parameter is not a kind_of?(to_be) def _pv_kind_of(opts, key, to_be) value = _pv_opts_lookup(opts, key) if value != nil - unless value.kind_of?(to_be) - raise ArgumentError, "Option #{key} must be a kind of #{to_be}! You passed #{to_be.inspect}." + passes = false + [ to_be ].flatten.each do |tb| + if value.kind_of?(tb) + passes = true + end + end + unless passes + raise ArgumentError, "Option #{key} must be a kind of #{to_be}! You passed #{value.inspect}." end end end @@ -137,11 +172,17 @@ class Chef # Check a parameter against a regular expression. def _pv_regex(opts, key, regex) value = _pv_opts_lookup(opts, key) - if value != nil - if regex.match(value) == nil - raise ArgumentError, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}" + passes = false + [ regex ].flatten.each do |r| + if value != nil + if r.match(value.to_s) + passes = true + end end end + unless passes + raise ArgumentError, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}" + end end # Check a parameter against a hash of proc's. diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 7cb42effd0..c5be016ec8 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -21,6 +21,7 @@ # require File.join(File.dirname(__FILE__), "mixin", "check_helper") +require File.join(File.dirname(__FILE__), "mixin", "params_validate") require File.join(File.dirname(__FILE__), "mixin", "from_file") require 'rubygems' @@ -33,6 +34,7 @@ class Chef include Chef::Mixin::CheckHelper include Chef::Mixin::FromFile + include Chef::Mixin::ParamsValidate # Create a new Chef::Node object. def initialize() @@ -79,13 +81,18 @@ class Chef # Set the name of this Node, or return the current name. def name(arg=nil) - set_if_args(@name, arg) do |a| - case a - when String - @name = a - else - raise ArgumentError, "The nodes name must be a string" - end + if arg != nil + validate( + { :name => arg }, + { + :name => { + :kind_of => String + } + } + ) + @name = arg + else + @name end end @@ -168,5 +175,10 @@ class Chef result_object.to_json end + # As a string + def to_s + "node[#{@name}]" + end + end end
\ No newline at end of file diff --git a/lib/chef/platform.rb b/lib/chef/platform.rb new file mode 100644 index 0000000000..bebdcfa297 --- /dev/null +++ b/lib/chef/platform.rb @@ -0,0 +1,172 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +Dir[File.join(File.dirname(__FILE__), 'provider/**/*.rb')].sort.each { |lib| require lib } +require File.join(File.dirname(__FILE__), 'mixin', 'params_validate') + +class Chef + class Platform + + @platforms = { + :mac_os_x => {}, + :ubuntu => {}, + :centos => {}, + :redhat => {}, + :gentoo => {}, + :solaris => {}, + :default => { + :file => Chef::Provider::File + } + } + + class << self + attr_accessor :platforms + + include Chef::Mixin::ParamsValidate + + def find(name, version) + provider_map = @platforms[:default].clone + + name_sym = name + if name.kind_of?(String) + name.downcase! + name.gsub!(/\s/, "_") + name_sym = name.to_sym + end + + if @platforms.has_key?(name_sym) + if @platforms[name_sym].has_key?(version) + Chef::Log.debug("Platform #{name.to_s} version #{version} found") + if @platforms[name_sym].has_key?(:default) + provider_map.merge!(@platforms[name_sym][:default]) + end + provider_map.merge!(@platforms[name_sym][version]) + elsif @platforms[name_sym].has_key?(:default) + provider_map.merge!(@platforms[name_sym][:default]) + end + else + Chef::Log.debug("Platform #{name} not found, using all defaults. (Unsupported platform?)") + end + provider_map + end + + def find_provider(platform, version, resource_type) + pmap = Chef::Platform.find(platform, version) + rtkey = resource_type + if resource_type.kind_of?(Chef::Resource) + rtkey = resource_type.resource_name + end + if pmap.has_key?(rtkey) + pmap[rtkey] + else + raise( + ArgumentError, + "Cannot find a provider for #{resource_type} on #{platform} version #{version}" + ) + end + end + + def find_provider_for_node(node, resource_type) + platform = nil + version = nil + if node.attribute?("lsbdistid") + platform = node[:lsbdistid] + elsif node.attribute?("macosx_productname") + platform = node[:macosx_productname] + elsif node.attribute?("operatingsystem") + platform = node[:operatingsystem] + end + raise ArgumentError, "Cannot find a platform for #{node}" unless platform + + if node.attribute?("lsbdistrelease") + version = node[:lsbdistrelease] + elsif node.attribute?("macosx_productversion") + version = node[:macosx_productversion] + elsif node.attribute?("operatingsystemversion") + version = node[:operatingsystemversion] + end + raise ArgumentError, "Cannot find a version for #{node}" unless version + + provider = find_provider(platform, version, resource_type) + end + + def set(args) + validate( + args, + { + :platform => { + :kind_of => Symbol, + :required => false, + }, + :version => { + :kind_of => String, + :required => false, + }, + :resource => { + :kind_of => Symbol, + }, + :provider => { + :kind_of => [ String, Symbol, Class ], + } + } + ) + if args.has_key?(:platform) + if args.has_key?(:version) + if @platforms.has_key?(args[:platform]) + if @platforms[args[:platform]].has_key?(args[:version]) + @platforms[args[:platform]][args[:version]][args[:resource].to_sym] = args[:provider] + else + @platforms[args[:platform]][args[:version]] = { + args[:resource].to_sym => args[:provider] + } + end + else + @platforms[args[:platform]] = { + args[:version] => { + args[:resource].to_sym => args[:provider] + } + } + end + else + if @platforms.has_key?(args[:platform]) + @platforms[args[:platform]][:default][args[:resource].to_sym] = args[:provider] + else + @platforms[args[:platform]] = { + :default => { + args[:resource].to_sym => args[:provider] + } + } + end + end + else + if @platforms.has_key?(:default) + @platforms[:default][args[:resource].to_sym] = args[:provider] + else + @platforms[:default] = { + args[:resource].to_sym => args[:provider] + } + end + end + end + + end + + end +end
\ No newline at end of file diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index d296b63227..018f687069 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -29,5 +29,10 @@ class Chef @current_resource = nil end + def action_nothing + Chef::Log.debug("Doing nothing for #{self.to_s}") + true + end + end end
\ No newline at end of file diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb index c0805674ec..7bb729cd87 100644 --- a/lib/chef/provider/file.rb +++ b/lib/chef/provider/file.rb @@ -47,58 +47,100 @@ class Chef # Compare the ownership of a file. Returns true if they are the same, false if they are not. def compare_owner - case @new_resource.owner - when /^\d+$/, Integer - @set_user_id = @new_resource.owner.to_i - @set_user_id == @current_resource.owner - else - # This raises an ArugmentError if you can't find the user - user_info = Etc.getpwnam(@new_resource.owner) - @set_user_id = user_info.uid - @set_user_id == @current_resource.owner + if @new_resource.owner != nil + case @new_resource.owner + when /^\d+$/, Integer + @set_user_id = @new_resource.owner.to_i + @set_user_id == @current_resource.owner + else + # This raises an ArugmentError if you can't find the user + user_info = Etc.getpwnam(@new_resource.owner) + @set_user_id = user_info.uid + @set_user_id == @current_resource.owner + end end end # Set the ownership on the file, assuming it is not set correctly already. def set_owner unless compare_owner + Chef::Log.info("Setting owner to #{@set_user_id} for #{@new_resource}") ::File.chown(@set_user_id, nil, @new_resource.path) + @new_resource.updated = true end end # Compares the group of a file. Returns true if they are the same, false if they are not. def compare_group - case @new_resource.group - when /^\d+$/, Integer - @set_group_id = @new_resource.group.to_i - @set_group_id == @current_resource.group - else - group_info = Etc.getgrnam(@new_resource.group) - @set_group_id = group_info.gid - @set_group_id == @current_resource.group + if @new_resource.group != nil + case @new_resource.group + when /^\d+$/, Integer + @set_group_id = @new_resource.group.to_i + @set_group_id == @current_resource.group + else + group_info = Etc.getgrnam(@new_resource.group) + @set_group_id = group_info.gid + @set_group_id == @current_resource.group + end end end def set_group unless compare_group + Chef::Log.info("Setting group to #{@set_group_id} for #{@new_resource}") ::File.chown(nil, @set_group_id, @new_resource.path) + @new_resource.updated = true + end + end + + def compare_mode + if @new_resource.mode != nil + case @new_resource.mode + when /^\d+$/, Integer + real_mode = sprintf("%o" % (@new_resource.mode & 007777)) + real_mode.to_i == @current_resource.mode.to_i + end + end + end + + def set_mode + unless compare_mode && @new_resource.mode != nil + Chef::Log.info("Setting mode to #{sprintf("%o" % (@new_resource.mode & 007777)) + } for #{@new_resource}") + ::File.chmod(@new_resource.mode.to_i, @new_resource.path) + @new_resource.updated = true end end def action_create unless ::File.exists?(@new_resource.path) + Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}") ::File.open(@new_resource.path, "w+") { |f| } + @new_resource.updated = true end - set_owner - set_group + set_owner if @new_resource.owner != nil + set_group if @new_resource.group != nil + set_mode if @new_resource.mode != nil end def action_delete if ::File.exists?(@new_resource.path) && ::File.writable?(@new_resource.path) + Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.path}") ::File.delete(@new_resource.path) + @new_resource.updated = true + else + raise "Cannot delete #{@new_resource} at #{@new_resource_path}!" end end + def action_touch + action_create + time = Time.now + Chef::Log.info("Updating #{@new_resource} with new atime/mtime of #{time}") + ::File.utime(time, time, @new_resource.path) + @new_resource.updated = true + end + end end end
\ No newline at end of file diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index fab6279a0e..5a5263b321 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -18,16 +18,18 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # +require File.join(File.dirname(__FILE__), "mixin", "params_validate") require File.join(File.dirname(__FILE__), "mixin", "check_helper") require 'yaml' class Chef class Resource - + include Chef::Mixin::CheckHelper + include Chef::Mixin::ParamsValidate - attr_accessor :tag, :actions, :params - attr_reader :name, :noop, :resource_name, :collection, :notifies, :subscribes + attr_accessor :tag, :actions, :params, :provider, :updated, :allowed_actions + attr_reader :resource_name, :collection def initialize(name, collection=nil) @name = name @@ -41,6 +43,28 @@ class Chef @before = nil @actions = Hash.new @params = Hash.new + @provider = nil + @allowed_actions = [ :nothing ] + @action = :nothing + @updated = false + end + + def action(arg=nil) + if arg != nil + arg = arg.to_sym + end + set_or_return( + @action, + arg, + { + :action => arg, + }, + { + :action => { + :equal_to => @allowed_actions, + } + } + ) end def name(name=nil) diff --git a/lib/chef/resource/directory.rb b/lib/chef/resource/directory.rb new file mode 100644 index 0000000000..b03d98fe9b --- /dev/null +++ b/lib/chef/resource/directory.rb @@ -0,0 +1,89 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +class Chef + class Resource + class Directory < Chef::Resource::File + + def initialize(name, collection=nil) + @resource_name = :file + super(name, collection) + @path = name + @action = "create" + end + + def action(arg=nil) + set_if_args(@action, arg) do + case arg + when "create", "delete", "touch", "nothing" + @action = arg + else + raise ArgumentError, "action must be create, delete, or touch!" + end + end + end + + def group(arg=nil) + set_if_args(@group, arg) do + case arg + when /^([a-z]|[A-Z]|[0-9]|_|-)+$/, Integer + @group = arg + else + raise ArgumentError, "group must match /^([a-z]|[A-Z]|[0-9]|_|-)$/, Integer!" + end + end + end + + def mode(arg=nil) + set_if_args(@mode, arg) do + case "#{arg.to_s}" + when /^\d{3,4}$/ + @mode = arg + else + raise ArgumentError, "mode must be a valid unix file mode - 3 or 4 digets!" + end + end + end + + def owner(arg=nil) + set_if_args(@owner, arg) do + case arg + when /^([a-z]|[A-Z]|[0-9]|_|-)+$/, Integer + @owner = arg + else + raise ArgumentError, "group must match /^([a-z]|[A-Z]|[0-9]|_|-)$/, Integer!" + end + end + end + + def path(arg=nil) + set_if_args(@path, arg) do + case arg + when String + @path = arg + else + raise ArgumentError, "path must be a string!" + end + end + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb index 59c51ba8d3..3ea75c106b 100644 --- a/lib/chef/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -18,6 +18,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # + class Chef class Resource class File < Chef::Resource @@ -28,83 +29,97 @@ class Chef @path = name @backup = true @action = "create" + @allowed_actions.push(:create, :delete, :touch) end def backup(arg=nil) - set_if_args(@backup, arg) do - case arg - when true, false, Integer - @backup = arg - else - raise ArgumentError, "backup must be true, false, or a number!" - end - end + set_or_return( + @backup, + arg, + { + :backup => arg, + }, + { + :backup => { + :kind_of => [ Integer, TrueClass, FalseClass ], + } + } + ) end - + def checksum(arg=nil) - set_if_args(@checksum, arg) do - case arg - when /^[a-zA-Z0-9]{32}$/ # md5sum - @checksum = arg - else - raise ArgumentError, "checksum must be an md5sum!" - end - end + set_or_return( + @checksum, + arg, + { + :checksum => arg, + }, + { + :checksum => { + :regex => /^[a-zA-Z0-9]{32}$/, + }, + } + ) end - - def action(arg=nil) - set_if_args(@action, arg) do - case arg - when "create", "delete" - @action = arg - else - raise ArgumentError, "action must be create or delete!" - end - end - end - + def group(arg=nil) - set_if_args(@group, arg) do - case arg - when /^([a-z]|[A-Z]|[0-9]|_|-)+$/, Integer - @group = arg - else - raise ArgumentError, "group must match /^([a-z]|[A-Z]|[0-9]|_|-)$/, Integer!" - end - end + set_or_return( + @group, + arg, + { + :group => arg + }, + { + :group => { + :regex => [ /^([a-z]|[A-Z]|[0-9]|_|-)+$/, /^\d+$/ ] + } + } + ) end def mode(arg=nil) - set_if_args(@mode, arg) do - case "#{arg.to_s}" - when /^\d{3,4}$/ - @mode = arg - else - raise ArgumentError, "mode must be a valid unix file mode - 3 or 4 digets!" - end - end + set_or_return( + @mode, + arg, + { + :mode => arg + }, + { + :mode => { + :regex => /^\d{3,4}$/ + } + } + ) end def owner(arg=nil) - set_if_args(@owner, arg) do - case arg - when /^([a-z]|[A-Z]|[0-9]|_|-)+$/, Integer - @owner = arg - else - raise ArgumentError, "group must match /^([a-z]|[A-Z]|[0-9]|_|-)$/, Integer!" - end - end + set_or_return( + @owner, + arg, + { + :owner => arg, + }, + { + :owner => { + :regex => [ /^([a-z]|[A-Z]|[0-9]|_|-)+$/, /^\d+$/ ] + } + } + ) end def path(arg=nil) - set_if_args(@path, arg) do - case arg - when String - @path = arg - else - raise ArgumentError, "path must be a string!" - end - end + set_or_return( + @path, + arg, + { + :path => arg, + }, + { + :path => { + :kind_of => String, + }, + } + ) end end diff --git a/lib/chef/runner.rb b/lib/chef/runner.rb new file mode 100644 index 0000000000..60f3082e88 --- /dev/null +++ b/lib/chef/runner.rb @@ -0,0 +1,93 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.join(File.dirname(__FILE__), "mixin", "params_validate") + +class Chef + class Runner + + include Chef::Mixin::ParamsValidate + + def initialize(node, collection) + validate( + { + :node => node, + :collection => collection, + }, + { + :node => { + :kind_of => Chef::Node, + }, + :collection => { + :kind_of => Chef::ResourceCollection, + }, + } + ) + @node = node + @collection = collection + end + + def build_provider(resource) + provider_klass = resource.provider + if provider_klass == nil + provider_klass = Chef::Platform.find_provider_for_node(@node, resource) + end + Chef::Log.debug("#{resource} using #{provider_klass.to_s}") + provider = provider_klass.new(@node, resource) + end + + def converge + start_time = Time.now + Chef::Log.info("Starting Chef Run") + delayed_actions = Array.new + + @collection.each do |resource| + Chef::Log.debug("Processing #{resource}") + provider = build_provider(resource) + provider.load_current_resource + provider.send("action_#{resource.action}") + if resource.updated + resource.actions.each_key do |action| + if resource.actions[action].has_key?(:immediate) + resource.actions[action][:immediate].each do |r| + Chef::Log.info("#{resource} sending action #{action} to #{r} (immediate)") + build_provider(r).send("action_#{action}") + end + end + if resource.actions[action].has_key?(:delayed) + resource.actions[action][:delayed].each do |r| + delayed_actions << lambda { + Chef::Log.info("#{resource} sending action #{action} to #{r} (delayed)") + build_provider(r).send("action_#{action}") + } + end + end + end + end + end + + # Run all our :delayed actions + delayed_actions.each { |da| da.call } + end_time = Time.now + Chef::Log.info("Chef Run complete in #{end_time - start_time} seconds") + true + end + end +end
\ No newline at end of file |