1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
# A module to collect utility functions.
require 'sync'
require 'puppet/lock'
module Puppet
# A command failed to execute.
class ExecutionFailure < Puppet::Error
end
module Util
require 'benchmark'
require 'puppet/util/posix'
extend Puppet::Util::POSIX
# 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
# Change the process to a different user
def self.chuser
if Facter["operatingsystem"].value == "Darwin"
$stderr.puts "Ruby on darwin is broken; puppetmaster will not set its UID to 'puppet' and must run as root"
return
end
if group = Puppet[:group]
group = self.gid(group)
unless group
raise Puppet::Error, "No such group %s" % Puppet[:group]
end
unless Puppet::SUIDManager.gid == group
begin
Puppet::SUIDManager.egid = group
Puppet::SUIDManager.gid = group
rescue => detail
Puppet.warning "could not change to group %s: %s" %
[group.inspect, detail]
$stderr.puts "could not change to group %s" % group.inspect
# Don't exit on failed group changes, since it's
# not fatal
#exit(74)
end
end
end
if user = Puppet[:user]
user = self.uid(user)
unless user
raise Puppet::Error, "No such user %s" % Puppet[:user]
end
unless Puppet::SUIDManager.uid == user
begin
Puppet::SUIDManager.uid = user
Puppet::SUIDManager.euid = user
rescue
$stderr.puts "could not change to user %s" % user
exit(74)
end
end
end
end
# Create a shared lock for reading
def self.readlock(file)
self.sync(file).synchronize(Sync::SH) do
File.open(file) { |f|
f.lock_shared { |lf| yield lf }
}
end
end
# Create an exclusive lock for writing, and do the writing in a
# tmp file.
def self.writelock(file, mode = 0600)
tmpfile = file + ".tmp"
unless FileTest.directory?(File.dirname(tmpfile))
raise Puppet::DevError, "Cannot create %s; directory %s does not exist" %
[file, File.dirname(file)]
end
self.sync(file).synchronize(Sync::EX) do
File.open(file, "w", mode) do |rf|
rf.lock_exclusive do |lrf|
File.open(tmpfile, "w", mode) do |tf|
yield tf
end
begin
File.rename(tmpfile, file)
rescue => detail
Puppet.err "Could not rename %s to %s: %s" %
[file, tmpfile, detail]
end
end
end
end
end
# Create instance methods for each of the log levels. This allows
# the messages to be a little richer. Most classes will be calling this
# method.
def self.logmethods(klass, useself = true)
Puppet::Log.eachlevel { |level|
klass.send(:define_method, level, proc { |args|
if args.is_a?(Array)
args = args.join(" ")
end
if useself
Puppet::Log.create(
:level => level,
:source => self,
:message => args
)
else
Puppet::Log.create(
:level => level,
:message => args
)
end
})
}
end
# Proxy a bunch of methods to another object.
def self.classproxy(klass, objmethod, *methods)
classobj = class << klass; self; end
methods.each do |method|
classobj.send(:define_method, method) do |*args|
obj = self.send(objmethod)
obj.send(method, *args)
end
end
end
# Proxy a bunch of methods to another object.
def self.proxy(klass, objmethod, *methods)
methods.each do |method|
klass.send(:define_method, method) do |*args|
obj = self.send(objmethod)
obj.send(method, *args)
end
end
end
# XXX this should all be done using puppet objects, not using
# normal mkdir
def self.recmkdir(dir,mode = 0755)
if FileTest.exist?(dir)
return false
else
tmp = dir.sub(/^\//,'')
path = [File::SEPARATOR]
tmp.split(File::SEPARATOR).each { |dir|
path.push dir
if ! FileTest.exist?(File.join(path))
Dir.mkdir(File.join(path), mode)
elsif FileTest.directory?(File.join(path))
next
else FileTest.exist?(File.join(path))
raise "Cannot create %s: basedir %s is a file" %
[dir, File.join(path)]
end
}
return true
end
end
# Execute a given chunk of code with a new umask.
def self.withumask(mask)
cur = File.umask(mask)
begin
yield
ensure
File.umask(cur)
end
end
def benchmark(*args)
msg = args.pop
level = args.pop
object = nil
if args.empty?
object = Puppet
else
object = args.pop
end
unless level
raise Puppet::DevError, "Failed to provide level to :benchmark"
end
unless object.respond_to? level
raise Puppet::DevError, "Benchmarked object does not respond to %s" % level
end
# Only benchmark if our log level is high enough
if level != :none and Puppet::Log.sendlevel?(level)
result = nil
seconds = Benchmark.realtime {
yield
}
object.send(level, msg + (" in %0.2f seconds" % seconds))
return seconds
else
yield
end
end
def binary(bin)
if bin =~ /^\//
if FileTest.exists? bin
return true
else
return nil
end
else
ENV['PATH'].split(":").each do |dir|
if FileTest.exists? File.join(dir, bin)
return File.join(dir, bin)
end
end
return nil
end
end
module_function :binary
# Execute the provided command in a pipe, yielding the pipe object.
def execpipe(command, failonfail = true)
if respond_to? :debug
debug "Executing '%s'" % command
else
Puppet.debug "Executing '%s'" % command
end
output = open("| #{command} 2>&1") do |pipe|
yield pipe
end
if failonfail
unless $? == 0
raise ExecutionFailure, output
end
end
return output
end
def execfail(command, exception)
begin
output = execute(command)
return output
rescue ExecutionFailure
raise exception, output
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
command += " 2>&1" unless command =~ />/
output = %x{#{command}}
if failonfail
unless $? == 0
raise ExecutionFailure, "Could not execute '%s': %s" % [command, output]
end
end
return output
end
module_function :execute
# 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 $? != 0 or pmap =~ /^no/
@pmap = nil
else
@pmap = pmap
end
end
if @pmap
return %x{pmap #{Process.pid}| grep total}.chomp.sub(/^\s*total\s+/, '').sub(/K$/, '').to_i
else
0
end
end
def symbolize(value)
if value.respond_to? :intern
value.intern
else
value
end
end
def symbolizehash(hash)
newhash = {}
hash.each do |name, val|
if name.is_a? String
newhash[name.intern] = val
else
newhash[name] = val
end
end
end
def symbolizehash!(hash)
hash.each do |name, val|
if name.is_a? String
hash[name.intern] = val
hash.delete(name)
end
end
return hash
end
module_function :symbolize, :symbolizehash, :symbolizehash!
# Just benchmark, with no logging.
def thinmark
seconds = Benchmark.realtime {
yield
}
return seconds
end
module_function :memory
end
end
require 'puppet/util/errors'
require 'puppet/util/methodhelper'
require 'puppet/util/metaid'
require 'puppet/util/classgen'
require 'puppet/util/docs'
require 'puppet/util/execution'
require 'puppet/util/logging'
require 'puppet/util/package'
require 'puppet/util/warnings'
# $Id: util.rb 1863 2006-11-13 02:07:07Z luke $
|