From: Roy Nielsen <[email protected]>
Signed-off-by: James Turnbull <[email protected]> --- lib/puppet/type.rb | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 103 insertions(+), 0 deletions(-) diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index ee87c26..c6e8316 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -25,6 +25,7 @@ class Type include Puppet::Util::Cacher include Puppet::FileCollection::Lookup include Puppet::Util::Tagging + include Puppet::Util::Execution ############################### # Code related to resource type attributes. @@ -1259,6 +1260,45 @@ class Type end end + def self.newcheckme(myname, &block) + @checksme ||= {} + + check = newmetaparam(myname, &block) + @checksme[myname] = check + end + + def self.checks + @checksme.keys + end + + newcheckme(:unless) do + desc "If this parameter is set, then the type will run unless + the command returns 0. For example:: + + exec { \"/bin/echo root >> /usr/lib/cron/cron.allow\": + unless => \"/usr/bin/grep root /usr/lib/cron/cron.allow 2>/dev/null\" + } + + This would add ``root`` to the cron.allow file (on Solaris) unless + ``grep`` determines it's already there. + + Note that this metaparameter requires the full path to the command. + " + + munge do |cmds| + cmds = [cmds] unless cmds.is_a? Array + + cmds.each do |cmd| + @resource.validateexecmd(cmd) + output, status = @resource.runexec(cmd, true) + if status.exitstatus == 0 + self.fail("Unless returned 0 with command: %s" % [cmd]) + end + end + end + end + + class RelationshipMetaparam < Puppet::Parameter class << self attr_accessor :direction, :events, :callback, :subclasses @@ -1726,6 +1766,69 @@ class Type end ############################### + # All of the unless code. + + def checkexec(cmd) + if cmd =~ /^\// + exe = cmd.split(/ /)[0] + unless FileTest.exists?(exe) + raise ArgumentError, "Could not find executable %s" % exe + end + unless FileTest.executable?(exe) + raise ArgumentError, + "%s is not executable" % exe + end + else + raise ArgumentError, + "is somehow not qualified with no search path" + end + end + + def runexec(command, check = false) + output = nil + status = nil + + dir = nil + + checkexec(command) + + dir ||= Dir.pwd + + if check + debug "Executing check '#{command}'" + else + debug "Executing '#{command}'" + end + begin + # Do our chdir + Dir.chdir(dir) do + environment = {} + + withenv environment do + output, status = Puppet::Util::SUIDManager.run_and_capture( + [command] + ) + # The shell returns 127 if the command is missing. + if status.exitstatus == 127 + raise ArgumentError, output + end + end + end + rescue Errno::ENOENT => detail + self.fail detail.to_s + end + + return output, status + end + + def validateexecmd(cmd) + # if we're not fully qualified, require a path + if cmd !~ /^\// + self.fail "'%s' is both unqualifed and specified no search path" % cmd + end + end + + ############################### # All of the scheduling code. # Look up the schedule and set it appropriately. This is done after -- 1.6.0.6 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en -~----------~----~----~----~------~----~------~--~---
