lib/puppet/util.rb in puppet-0.16.0 vs lib/puppet/util.rb in puppet-0.18.4
- old
+ new
@@ -2,15 +2,25 @@
require 'sync'
require 'puppet/lock'
module Puppet
+ # A command failed to execute.
+ class ExecutionFailure < RuntimeError
+ end
module Util
require 'benchmark'
- # Create a sync point for any threads
- @@sync = Sync.new
+ # Create a hash to store the different sync objects.
+ @@syncresources = {}
+
+ # Return the sync object associated with a given resource.
+ def self.sync(resource)
+ @@syncresources[resource] ||= Sync.new
+ return @@syncresources[resource]
+ end
+
# Execute a block as a given user or group
def self.asuser(user = nil, group = nil)
require 'etc'
uid = nil
@@ -122,28 +132,26 @@
end
end
# Create a shared lock for reading
def self.readlock(file)
- @@sync.synchronize(Sync::SH) do
+ self.sync(file).synchronize(Sync::SH) do
File.open(file) { |f|
f.lock_shared { |lf| yield lf }
}
end
end
- # Create an exclusive lock fro writing, and do the writing in a
+ # Create an exclusive lock for writing, and do the writing in a
# tmp file.
def self.writelock(file, mode = 0600)
tmpfile = file + ".tmp"
- @@sync.synchronize(Sync::EX) do
+ self.sync(file).synchronize(Sync::EX) do
File.open(file, "w", mode) do |rf|
rf.lock_exclusive do |lrf|
- yield lrf
File.open(tmpfile, "w", mode) do |tf|
yield tf
- tf.flush
end
begin
File.rename(tmpfile, file)
rescue => detail
Puppet.err "Could not rename %s to %s: %s" %
@@ -294,11 +302,11 @@
end
def self.symbolize(value)
case value
when String: value = value.intern
- when Symbol: # nothing
+ when Symbol: value
else
raise ArgumentError, "'%s' must be a string or symbol" % value
end
end
@@ -335,25 +343,53 @@
# Only benchmark if our log level is high enough
if Puppet::Log.sendlevel?(level)
result = nil
seconds = Benchmark.realtime {
- result = yield
+ yield
}
object.send(level, msg + (" in %0.2f seconds" % seconds))
- result
+ return seconds
else
yield
end
end
+ # Execute the desired command, and return the status and output.
+ def execute(command, failonfail = true)
+ if respond_to? :debug
+ debug "Executing '%s'" % command
+ else
+ Puppet.debug "Executing '%s'" % command
+ end
+ output = %x{#{command} 2>&1}
+
+ if failonfail
+ unless $? == 0
+ raise ExecutionFailure, output
+ end
+ end
+
+ return output
+ end
+
+ # Create an exclusive lock.
+ def threadlock(resource, type = Sync::EX)
+ Puppet::Util.sync(resource).synchronize(type) do
+ yield
+ end
+ end
+
+ # Because some modules provide their own version of this method.
+ alias util_execute execute
+
module_function :benchmark
def memory
unless defined? @pmap
pmap = %x{which pmap 2>/dev/null}.chomp
- if pmap == ""
+ if $? != 0 or pmap =~ /^no/
@pmap = nil
else
@pmap = pmap
end
end
@@ -361,10 +397,20 @@
return %x{pmap #{Process.pid}| grep total}.chomp.sub(/^\s*total\s+/, '').sub(/K$/, '').to_i
else
0
end
end
+
+ # Just benchmark, with no logging.
+ def thinmark
+ seconds = Benchmark.realtime {
+ yield
+ }
+
+ return seconds
+ end
+
module_function :memory
end
end
-# $Id: util.rb 1113 2006-04-17 16:15:33Z luke $
+# $Id: util.rb 1346 2006-06-30 20:41:41Z luke $