diff options
author | David Schmitt <david@dasz.at> | 2010-05-11 18:30:49 +0200 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d (patch) | |
tree | 46348d616d5661ea363d18b8d06666aa449f1957 /lib/puppet | |
parent | b51be2809a81e36fee603693f9d70155c49e94fd (diff) | |
download | puppet-a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d.tar.gz puppet-a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d.tar.xz puppet-a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d.zip |
Start extracting the owner managment for files into providers
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/provider/file/posix.rb | 109 | ||||
-rw-r--r-- | lib/puppet/provider/file/win32.rb | 78 | ||||
-rwxr-xr-x | lib/puppet/type/file/owner.rb | 105 |
3 files changed, 194 insertions, 98 deletions
diff --git a/lib/puppet/provider/file/posix.rb b/lib/puppet/provider/file/posix.rb new file mode 100644 index 000000000..131fd547e --- /dev/null +++ b/lib/puppet/provider/file/posix.rb @@ -0,0 +1,109 @@ +Puppet::Type.type(:file).provide :posix do + desc "Uses POSIX functionality to manage file's users and rights." + + confine :feature => :posix + + include Puppet::Util::POSIX + include Puppet::Util::Warnings + + require 'etc' + + def id2name(id) + return id.to_s if id.is_a?(Symbol) + return nil if id > Puppet[:maximum_uid].to_i + + begin + user = Etc.getpwuid(id) + rescue TypeError + return nil + rescue ArgumentError + return nil + end + + if user.uid == "" + return nil + else + return user.name + end + end + + def insync?(current, should) + return true unless should + + should.each do |value| + if value =~ /^\d+$/ + uid = Integer(value) + elsif value.is_a?(String) + fail "Could not find user %s" % value unless uid = uid(value) + else + uid = value + end + + return true if uid == current + end + + unless Puppet::Util::SUIDManager.uid == 0 + warnonce "Cannot manage ownership unless running as root" + return true + end + + return false + end + + # Determine if the user is valid, and if so, return the UID + def validuser?(value) + begin + number = Integer(value) + return number + rescue ArgumentError + number = nil + end + if number = uid(value) + return number + else + return false + end + end + + def retrieve(resource) + unless stat = resource.stat(false) + return :absent + end + + currentvalue = stat.uid + + # On OS X, files that are owned by -2 get returned as really + # large UIDs instead of negative ones. This isn't a Ruby bug, + # it's an OS X bug, since it shows up in perl, too. + if currentvalue > Puppet[:maximum_uid].to_i + self.warning "Apparently using negative UID (%s) on a platform that does not consistently handle them" % currentvalue + currentvalue = :silly + end + + return currentvalue + end + + def sync(path, links, should) + # Set our method appropriately, depending on links. + if links == :manage + method = :lchown + else + method = :chown + end + + uid = nil + should.each do |user| + break if uid = validuser?(user) + end + + raise Puppet::Error, "Could not find user(s) %s" % @should.join(",") unless uid + + begin + File.send(method, uid, nil, path) + rescue => detail + raise Puppet::Error, "Failed to set owner to '%s': %s" % [uid, detail] + end + + return :file_changed + end +end diff --git a/lib/puppet/provider/file/win32.rb b/lib/puppet/provider/file/win32.rb new file mode 100644 index 000000000..45a36f213 --- /dev/null +++ b/lib/puppet/provider/file/win32.rb @@ -0,0 +1,78 @@ +Puppet::Type.type(:file).provide :win32 do + desc "Uses Win32 functionality to manage file's users and rights." + + confine :feature => :win32 + + include Puppet::Util::Warnings + + require 'sys/admin' + + def id2name(id) + return id.to_s if id.is_a?(Symbol) + return nil if id > Puppet[:maximum_uid].to_i + # should translate ID numbers to usernames + return id + end + + def insync?(current, should) + return true unless should + + should.each do |value| + if value =~ /^\d+$/ + uid = Integer(value) + elsif value.is_a?(String) + fail "Could not find user %s" % value unless uid = uid(value) + else + uid = value + end + + return true if uid == current + end + + unless Puppet::Util::SUIDManager.uid == 0 + warnonce "Cannot manage ownership unless running as root" + return true + end + + return false + end + + # Determine if the user is valid, and if so, return the UID + def validuser?(value) + info "Is '%s' a valid user?" % value + return 0 + begin + number = Integer(value) + return number + rescue ArgumentError + number = nil + end + if number = uid(value) + return number + else + return false + end + end + + def retrieve(resource) + unless stat = resource.stat(false) + return :absent + end + + currentvalue = stat.uid + + # On OS X, files that are owned by -2 get returned as really + # large UIDs instead of negative ones. This isn't a Ruby bug, + # it's an OS X bug, since it shows up in perl, too. + if currentvalue > Puppet[:maximum_uid].to_i + self.warning "Apparently using negative UID (%s) on a platform that does not consistently handle them" % currentvalue + currentvalue = :silly + end + + return currentvalue + end + + def sync(path, links, should) + info("should set '%s'%%owner to '%s'" % [path, should]) + end +end diff --git a/lib/puppet/type/file/owner.rb b/lib/puppet/type/file/owner.rb index 2b530928e..519bb1221 100755 --- a/lib/puppet/type/file/owner.rb +++ b/lib/puppet/type/file/owner.rb @@ -1,73 +1,17 @@ module Puppet Puppet::Type.type(:file).newproperty(:owner) do - include Puppet::Util::POSIX - include Puppet::Util::Warnings - - require 'etc' + desc "To whom the file should belong. Argument can be user name or user ID." @event = :file_changed - def id2name(id) - return id.to_s if id.is_a?(Symbol) - return nil if id > Puppet[:maximum_uid].to_i - - begin - user = Etc.getpwuid(id) - rescue TypeError - return nil - rescue ArgumentError - return nil - end - - if user.uid == "" - return nil - else - return user.name - end - end - def insync?(current) - return true unless should - - @should.each do |value| - if value =~ /^\d+$/ - uid = Integer(value) - elsif value.is_a?(String) - fail "Could not find user %s" % value unless uid = uid(value) - else - uid = value - end - - return true if uid == current - end - - unless Puppet.features.root? - warnonce "Cannot manage ownership unless running as root" - return true - end - - return false - end - - # Determine if the user is valid, and if so, return the UID - def validuser?(value) - begin - number = Integer(value) - return number - rescue ArgumentError - number = nil - end - if number = uid(value) - return number - else - return false - end + provider.insync?(current, @should) end # We want to print names, not numbers def is_to_s(currentvalue) - id2name(currentvalue) || currentvalue + provider.id2name(currentvalue) || currentvalue end def should_to_s(newvalue = @should) @@ -75,7 +19,7 @@ module Puppet when Symbol newvalue.to_s when Integer - id2name(newvalue) || newvalue + provider.id2name(newvalue) || newvalue when String newvalue else @@ -88,7 +32,7 @@ module Puppet if self.should @should = @should.collect do |val| unless val.is_a?(Integer) - if tmp = validuser?(val) + if tmp = provider.validuser?(val) val = tmp else raise "Could not find user %s" % val @@ -98,46 +42,11 @@ module Puppet end end end - - unless stat = @resource.stat(false) - return :absent - end - - currentvalue = stat.uid - - # On OS X, files that are owned by -2 get returned as really - # large UIDs instead of negative ones. This isn't a Ruby bug, - # it's an OS X bug, since it shows up in perl, too. - if currentvalue > Puppet[:maximum_uid].to_i - self.warning "Apparently using negative UID (%s) on a platform that does not consistently handle them" % currentvalue - currentvalue = :silly - end - - return currentvalue + return provider.retrieve(@resource) end def sync - # Set our method appropriately, depending on links. - if resource[:links] == :manage - method = :lchown - else - method = :chown - end - - uid = nil - @should.each do |user| - break if uid = validuser?(user) - end - - raise Puppet::Error, "Could not find user(s) %s" % @should.join(",") unless uid - - begin - File.send(method, uid, nil, @resource[:path]) - rescue => detail - raise Puppet::Error, "Failed to set owner to '%s': %s" % [uid, detail] - end - - return :file_changed + return provider.sync(resource[:path], resource[:links], @should) end end end |