From ff6562f9fa1ddf7e816256764f1bcc491693df9f Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 30 Jun 2006 14:42:46 +0000 Subject: Fixing #133. Added a "notify" and a "before" metaparam; notify is the opposite of subscribe, and before is the opposite of require. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1344 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/type.rb | 107 ++++++++++++++++++++++++++++++++++++++++++++++------- test/types/type.rb | 48 ++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 14 deletions(-) diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 752a2ac37..cfd742ee7 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -1923,12 +1923,20 @@ class Type < Puppet::Element def builddepends # Handle the requires if self[:require] - self.handledepends(self[:require], :NONE, nil) + self.handledepends(self[:require], :NONE, nil, true) end # And the subscriptions if self[:subscribe] - self.handledepends(self[:subscribe], :ALL_EVENTS, :refresh) + self.handledepends(self[:subscribe], :ALL_EVENTS, :refresh, true) + end + + if self[:notify] + self.handledepends(self[:notify], :ALL_EVENTS, :refresh, false) + end + + if self[:before] + self.handledepends(self[:before], :NONE, nil, false) end end @@ -1946,7 +1954,7 @@ class Type < Puppet::Element } end - def handledepends(requires, event, method) + def handledepends(requires, event, method, up) # Requires are specified in the form of [type, name], so they're always # an array. But we want them to be an array of arrays. unless requires[0].is_a?(Array) @@ -1968,22 +1976,26 @@ class Type < Puppet::Element end self.debug("subscribes to %s" % [object]) - #unless @dependencies.include?(object) - # @dependencies << object - #end - - # pure requires don't call methods - #next if method.nil? + # Are we requiring them, or vice versa? + source = target = nil + if up + source = object + target = self + else + source = self + target = object + end # ok, both sides of the connection store some information # we store the method to call when a given subscription is # triggered, but the source object decides whether subargs = { :event => event, - :source => object, - :target => self + :source => source, + :target => target } - if method and self.respond_to?(method) + + if method and target.respond_to?(method) subargs[:callback] = method end Puppet::Event::Subscription.new(subargs) @@ -2209,8 +2221,6 @@ class Type < Puppet::Element requires = values + requires end requires - #p @parent[:require] - #@parent.handledepends(requires, :NONE, nil) end end @@ -2344,6 +2354,75 @@ class Type < Puppet::Element end end end + + newmetaparam(:notify) do + desc %{This parameter is the opposite of **subscribe** -- it sends events + to the specified object: + + file { "/etc/sshd_config": + source => "....", + notify => service[sshd] + } + + service { sshd: + ensure => running + } + + This will restart the sshd service if the sshd config file changes.} + + + # Take whatever dependencies currently exist and add these. + munge do |notifies| + # We need to be two arrays deep... + unless notifies.is_a?(Array) + notifies = [notifies] + end + unless notifies[0].is_a?(Array) + notifies = [notifies] + end + if values = @parent[:notify] + notifies = values + notifies + end + notifies + end + + end + + newmetaparam(:before) do + desc %{This parameter is the opposite of **require** -- it guarantees + that the specified object is applied later than the specifying + object: + + file { "/var/nagios/configuration": + source => "...", + recurse => true, + before => exec["nagios-rebuid"] + } + + exec { "nagios-rebuild": + command => "/usr/bin/make", + cwd => "/var/nagios/configuration" + } + + This will make sure all of the files are up to date before the + make command is run.} + + # Take whatever dependencies currently exist and add these. + munge do |notifies| + # We need to be two arrays deep... + unless notifies.is_a?(Array) + notifies = [notifies] + end + unless notifies[0].is_a?(Array) + notifies = [notifies] + end + if values = @parent[:notify] + notifies = values + notifies + end + notifies + end + + end end # Puppet::Type end diff --git a/test/types/type.rb b/test/types/type.rb index 0c21c034c..a47050057 100644 --- a/test/types/type.rb +++ b/test/types/type.rb @@ -415,6 +415,54 @@ end assert_equal(parammethod, Puppet::Type.method(:newparam), "newparam method got replaced by newtype") end + + def test_notify_metaparam + file = Puppet::Type.newfile( + :path => tempfile(), + :notify => ["exec", "notifytest"], + :ensure => :file + ) + + path = tempfile() + exec = Puppet::Type.newexec( + :name => "notifytest", + :command => "/bin/touch #{path}", + :refreshonly => true + ) + + assert_apply(file, exec) + + assert(exec.requires?(file), + "Notify did not correctly set up the requirement chain.") + + assert(FileTest.exists?(path), + "Exec path did not get created.") + end + + def test_before_metaparam + file = Puppet::Type.newfile( + :path => tempfile(), + :before => ["exec", "beforetest"], + :content => "yaytest" + ) + + path = tempfile() + exec = Puppet::Type.newexec( + :name => "beforetest", + :command => "/bin/cp #{file[:path]} #{path}" + ) + + assert_apply(file, exec) + + assert(exec.requires?(file), + "Before did not correctly set up the requirement chain.") + + assert(FileTest.exists?(path), + "Exec path did not get created.") + + assert_equal("yaytest", File.read(path), + "Exec did not correctly copy file.") + end end # $Id$ -- cgit