diff options
author | Luke Kanies <luke@madstop.com> | 2008-08-18 11:47:40 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-08-18 11:47:40 -0500 |
commit | e3971b9751141cd448a8197da024be43581f6dcd (patch) | |
tree | 3ae780db3d9dae0a4df10ec24f928bf3a844e749 /lib/puppet | |
parent | 025edc5c3737f476119df4bab73ebdc68be19430 (diff) | |
parent | 2ec4e298c3274abc8eaad4230bca8d39a48d2e35 (diff) | |
download | puppet-e3971b9751141cd448a8197da024be43581f6dcd.tar.gz puppet-e3971b9751141cd448a8197da024be43581f6dcd.tar.xz puppet-e3971b9751141cd448a8197da024be43581f6dcd.zip |
Merge branch '0.24.x'
Conflicts:
CHANGELOG
test/util/posixtest.rb
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/indirector/facts/facter.rb | 5 | ||||
-rw-r--r-- | lib/puppet/indirector/yaml.rb | 11 | ||||
-rw-r--r-- | lib/puppet/metatype/metaparams.rb | 3 | ||||
-rw-r--r-- | lib/puppet/network/client/master.rb | 7 | ||||
-rw-r--r-- | lib/puppet/parser/lexer.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/resource/param.rb | 12 | ||||
-rw-r--r-- | lib/puppet/parser/scope.rb | 19 | ||||
-rw-r--r-- | lib/puppet/parser/templatewrapper.rb | 56 | ||||
-rw-r--r-- | lib/puppet/provider/confine.rb | 2 | ||||
-rwxr-xr-x | lib/puppet/provider/package/dpkg.rb | 2 | ||||
-rwxr-xr-x | lib/puppet/provider/package/ports.rb | 6 | ||||
-rwxr-xr-x | lib/puppet/provider/service/redhat.rb | 1 | ||||
-rw-r--r-- | lib/puppet/provider/ssh_authorized_key/parsed.rb | 2 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 12 | ||||
-rw-r--r-- | lib/puppet/reports/rrdgraph.rb | 5 | ||||
-rw-r--r-- | lib/puppet/sslcertificates/inventory.rb | 5 | ||||
-rw-r--r-- | lib/puppet/transaction/report.rb | 8 | ||||
-rw-r--r-- | lib/puppet/transportable.rb | 17 | ||||
-rw-r--r-- | lib/puppet/type/yumrepo.rb | 20 | ||||
-rw-r--r-- | lib/puppet/util/ldap/manager.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/log.rb | 36 | ||||
-rwxr-xr-x | lib/puppet/util/posix.rb | 81 |
22 files changed, 228 insertions, 86 deletions
diff --git a/lib/puppet/indirector/facts/facter.rb b/lib/puppet/indirector/facts/facter.rb index 465d90c13..6ed89dac1 100644 --- a/lib/puppet/indirector/facts/facter.rb +++ b/lib/puppet/indirector/facts/facter.rb @@ -12,12 +12,13 @@ class Puppet::Node::Facts::Facter < Puppet::Indirector::Code Dir.entries(dir).find_all { |e| e =~ /\.rb$/ }.each do |file| fqfile = ::File.join(dir, file) begin - Puppet.info "Loading #{type} %s" % ::File.basename(file.sub(".rb",'')) + Puppet.info "Loading %s %s" % + [type, ::File.basename(file.sub(".rb",''))] Timeout::timeout(self.timeout) do load fqfile end rescue => detail - Puppet.warning "Could not load #{type} %s: %s" % [fqfile, detail] + Puppet.warning "Could not load %s %s: %s" % [type, fqfile, detail] end end end diff --git a/lib/puppet/indirector/yaml.rb b/lib/puppet/indirector/yaml.rb index 3573ba560..3f05ce618 100644 --- a/lib/puppet/indirector/yaml.rb +++ b/lib/puppet/indirector/yaml.rb @@ -34,11 +34,20 @@ class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus end end + # Get the yaml directory + def base + (Puppet[:name] == "puppetmasterd") ? Puppet[:yamldir] : Puppet[:clientyamldir] + end + # Return the path to a given node's file. def path(name) - base = (Puppet[:name] == "puppetmasterd") ? Puppet[:yamldir] : Puppet[:clientyamldir] File.join(base, self.class.indirection_name.to_s, name.to_s + ".yaml") end + + # Do a glob on the yaml directory, loading each file found + def search(request) + Dir.glob(File.join(base, self.class.indirection_name.to_s, request.key)).collect { |f| YAML.load_file(f) } + end private diff --git a/lib/puppet/metatype/metaparams.rb b/lib/puppet/metatype/metaparams.rb index bf64d3a93..9ba786ee2 100644 --- a/lib/puppet/metatype/metaparams.rb +++ b/lib/puppet/metatype/metaparams.rb @@ -255,7 +255,8 @@ class Puppet::Type @value.each do |value| unless @resource.catalog.resource(*value) description = self.class.direction == :in ? "dependency" : "dependent" - fail Puppet::Error, "Could not find #{description} %s[%s] for %s" % [value[0].to_s.capitalize, value[1], resource.ref] + fail Puppet::Error, "Could not find %s %s[%s] for %s" % + [description, value[0].to_s.capitalize, value[1], resource.ref] end end end diff --git a/lib/puppet/network/client/master.rb b/lib/puppet/network/client/master.rb index d401cd393..5ca898dab 100644 --- a/lib/puppet/network/client/master.rb +++ b/lib/puppet/network/client/master.rb @@ -337,7 +337,7 @@ class Puppet::Network::Client::Master < Puppet::Network::Client if Puppet[:debug] puts detail.backtrace end - Puppet.err "Could not retrieve #{args[:name]}s: %s" % detail + Puppet.err "Could not retrieve %ss: %s" % [args[:name], detail] end # Now clean up after ourselves @@ -388,12 +388,13 @@ class Puppet::Network::Client::Master < Puppet::Network::Client Dir.entries(dir).find_all { |e| e =~ /\.rb$/ }.each do |file| fqfile = ::File.join(dir, file) begin - Puppet.info "Loading #{type} %s" % ::File.basename(file.sub(".rb",'')) + Puppet.info "Loading %s %s" % + [type, ::File.basename(file.sub(".rb",''))] Timeout::timeout(self.timeout) do load fqfile end rescue => detail - Puppet.warning "Could not load #{type} %s: %s" % [fqfile, detail] + Puppet.warning "Could not load %s %s: %s" % [type, fqfile, detail] end end end diff --git a/lib/puppet/parser/lexer.rb b/lib/puppet/parser/lexer.rb index 51026ea1b..2c5f66e5a 100644 --- a/lib/puppet/parser/lexer.rb +++ b/lib/puppet/parser/lexer.rb @@ -131,7 +131,7 @@ class Puppet::Parser::Lexer TOKENS.add_tokens "Whatever" => :DQTEXT, "Nomatter" => :SQTEXT, "alsonomatter" => :BOOLEAN - TOKENS.add_token :NAME, %r{[a-z][-\w]*} do |lexer, value| + TOKENS.add_token :NAME, %r{[a-z0-9][-\w]*} do |lexer, value| string_token = self # we're looking for keywords here if tmp = KEYWORDS.lookup(value) diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb index 9dd3f26d2..c8dd78a26 100644 --- a/lib/puppet/parser/resource/param.rb +++ b/lib/puppet/parser/resource/param.rb @@ -66,6 +66,14 @@ class Puppet::Parser::Resource::Param def to_s "%s => %s" % [self.name, self.value] end + + def compare(v,db_value) + if (v.is_a?(Puppet::Parser::Resource::Reference)) + return v.to_s == db_value.to_s + else + return v == db_value + end + end def values_to_remove(db_values) values = munge_for_rails(value) @@ -73,7 +81,7 @@ class Puppet::Parser::Resource::Param db_values.collect do |db| db unless (db.line == line_number && values.find { |v| - v == db.value + compare(v,db.value) } ) end.compact end @@ -82,7 +90,7 @@ class Puppet::Parser::Resource::Param values = munge_for_rails(value) line_number = line_to_i() values.collect do |v| - v unless db_values.find { |db| (v == db.value && + v unless db_values.find { |db| (compare(v,db.value) && line_number == db.line) } end.compact end diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index a6e43e7b3..32b127a6b 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -183,6 +183,25 @@ class Puppet::Parser::Scope end end + # Return a hash containing our variables and their values, optionally (and + # by default) including the values defined in our parent. Local values + # shadow parent values. + def to_hash(recursive = true) + if recursive and parent then + target = parent.to_hash(recursive) + end + target ||= Hash.new + @symtable.keys.each { |name| + value = @symtable[name] + if value == :undef then + target.delete(name) + else + target[name] = value + end + } + return target + end + def namespaces @namespaces.dup end diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb index 4790cea30..3b74e62d4 100644 --- a/lib/puppet/parser/templatewrapper.rb +++ b/lib/puppet/parser/templatewrapper.rb @@ -5,50 +5,76 @@ class Puppet::Parser::TemplateWrapper include Puppet::Util Puppet::Util.logmethods(self) - def initialize(scope, file) - @scope = scope - @file = Puppet::Module::find_template(file, @scope.compiler.environment) + def initialize(scope, filename) + @__scope__ = scope + @__file__ = Puppet::Module::find_template(filename, scope.compiler.environment) - unless FileTest.exists?(@file) + unless FileTest.exists?(file) raise Puppet::ParseError, "Could not find template %s" % file end # We'll only ever not have a parser in testing, but, eh. - if @scope.parser - @scope.parser.watch_file(@file) + if scope.parser + scope.parser.watch_file(file) end end + def scope + @__scope__ + end + + def file + @__file__ + end + # Should return true if a variable is defined, false if it is not def has_variable?(name) - if @scope.lookupvar(name.to_s, false) != :undefined + if scope.lookupvar(name.to_s, false) != :undefined true else false end end - # Ruby treats variables like methods, so we can cheat here and - # trap missing vars like they were missing methods. + # Ruby treats variables like methods, so we used to expose variables + # within scope to the ERB code via method_missing. As per RedMine #1427, + # though, this means that conflicts between methods in our inheritance + # tree (Kernel#fork) and variable names (fork => "yes/no") could arise. + # + # Worse, /new/ conflicts could pop up when a new kernel or object method + # was added to Ruby, causing templates to suddenly fail mysteriously when + # Ruby was upgraded. + # + # To ensure that legacy templates using unqualified names work we retain + # the missing_method definition here until we declare the syntax finally + # dead. def method_missing(name, *args) # We have to tell lookupvar to return :undefined to us when # appropriate; otherwise it converts to "". - value = @scope.lookupvar(name.to_s, false) + value = scope.lookupvar(name.to_s, false) if value != :undefined return value else # Just throw an error immediately, instead of searching for # other missingmethod things or whatever. - raise Puppet::ParseError, - "Could not find value for '%s'" % name + raise Puppet::ParseError, "Could not find value for '%s'" % name end end def result + # Expose all the variables in our scope as instance variables of the + # current object, making it possible to access them without conflict + # to the regular methods. + benchmark(:debug, "Bound template variables for #{file}") do + scope.to_hash.each { |name, value| + instance_variable_set("@#{name}", value) + } + end + result = nil - benchmark(:debug, "Interpolated template #{@file}") do - template = ERB.new(File.read(@file), 0, "-") + benchmark(:debug, "Interpolated template #{file}") do + template = ERB.new(File.read(file), 0, "-") result = template.result(binding) end @@ -56,7 +82,7 @@ class Puppet::Parser::TemplateWrapper end def to_s - "template[%s]" % @file + "template[%s]" % file end end diff --git a/lib/puppet/provider/confine.rb b/lib/puppet/provider/confine.rb index 35b80fdcf..e15adcd0e 100644 --- a/lib/puppet/provider/confine.rb +++ b/lib/puppet/provider/confine.rb @@ -25,7 +25,7 @@ class Puppet::Provider::Confine begin require "puppet/provider/confine/%s" % name rescue LoadError => detail - unless detail.to_s.include?("no such file") + unless detail.to_s.include?("No such file") warn "Could not load confine test '%s': %s" % [name, detail] end # Could not find file diff --git a/lib/puppet/provider/package/dpkg.rb b/lib/puppet/provider/package/dpkg.rb index b67cd2b76..ae79f714c 100755 --- a/lib/puppet/provider/package/dpkg.rb +++ b/lib/puppet/provider/package/dpkg.rb @@ -121,7 +121,7 @@ Puppet::Type.type(:package).provide :dpkg, :parent => Puppet::Provider::Package end if hash[:error] != "ok" - raise Puppet::PackageError.new( + raise Puppet::Error.new( "Package %s, version %s is in error state: %s" % [hash[:name], hash[:ensure], hash[:error]] ) diff --git a/lib/puppet/provider/package/ports.rb b/lib/puppet/provider/package/ports.rb index 99e26ef23..96ef3f042 100755 --- a/lib/puppet/provider/package/ports.rb +++ b/lib/puppet/provider/package/ports.rb @@ -33,7 +33,7 @@ Puppet::Type.type(:package).provide :ports, :parent => :freebsd, :source => :fre begin output = portversion(*cmd) rescue Puppet::ExecutionFailure - raise Puppet::PackageError.new(output) + raise Puppet::Error.new(output) end line = output.split("\n").pop @@ -47,7 +47,7 @@ Puppet::Type.type(:package).provide :ports, :parent => :freebsd, :source => :fre info = $3 unless pkgstuff =~ /^(\S+)-([^-\s]+)$/ - raise Puppet::PackageError, + raise Puppet::Error, "Could not match package info '%s'" % pkgstuff end @@ -61,7 +61,7 @@ Puppet::Type.type(:package).provide :ports, :parent => :freebsd, :source => :fre # Else, we need to be updated; we need to pull out the new version unless info =~ /\((\w+) has (.+)\)/ - raise Puppet::PackageError, + raise Puppet::Error, "Could not match version info '%s'" % info end diff --git a/lib/puppet/provider/service/redhat.rb b/lib/puppet/provider/service/redhat.rb index 3fad8bcfe..d26f76ebd 100755 --- a/lib/puppet/provider/service/redhat.rb +++ b/lib/puppet/provider/service/redhat.rb @@ -61,6 +61,7 @@ Puppet::Type.type(:service).provide :redhat, :parent => :init do if @resource[:hasstatus] == :true begin service(@resource[:name], "status") + return :running rescue return :stopped end diff --git a/lib/puppet/provider/ssh_authorized_key/parsed.rb b/lib/puppet/provider/ssh_authorized_key/parsed.rb index 3bd22c06b..602e6dd1b 100644 --- a/lib/puppet/provider/ssh_authorized_key/parsed.rb +++ b/lib/puppet/provider/ssh_authorized_key/parsed.rb @@ -14,7 +14,7 @@ Puppet::Type.type(:ssh_authorized_key).provide(:parsed, :fields => %w{options type key name}, :optional => %w{options}, :rts => /^\s+/, - :match => /^(?:([^ ]+) )?(ssh-dss|ssh-rsa) ([^ ]+)(?: (.+))?$/, + :match => /^(?:(.+) )?(ssh-dss|ssh-rsa) ([^ ]+)(?: (.+))?$/, :post_parse => proc { |record| if record[:options].nil? record[:options] = [:absent] diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index 0053dc28d..255b0e788 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -6,12 +6,12 @@ require 'puppet/util/rails/collection_merger' class Puppet::Rails::Resource < ActiveRecord::Base include Puppet::Util::CollectionMerger - has_many :param_values, :dependent => :destroy - has_many :param_names, :through => :param_values + has_many :param_values, :dependent => :destroy, :class_name => "Puppet::Rails::ParamValue" + has_many :param_names, :through => :param_values, :class_name => "Puppet::Rails::ParamName" + + has_many :resource_tags, :dependent => :destroy, :class_name => "Puppet::Rails::ResourceTag" + has_many :puppet_tags, :through => :resource_tags, :class_name => "Puppet::Rails::PuppetTag" - has_many :resource_tags, :dependent => :destroy - has_many :puppet_tags, :through => :resource_tags - belongs_to :source_file belongs_to :host @@ -82,7 +82,7 @@ class Puppet::Rails::Resource < ActiveRecord::Base end def ref - "%s[%s]" % [self[:restype].capitalize, self[:title]] + "%s[%s]" % [self[:restype].split("::").collect { |s| s.capitalize }.join("::"), self[:title]] end # Convert our object to a resource. Do not retain whether the object diff --git a/lib/puppet/reports/rrdgraph.rb b/lib/puppet/reports/rrdgraph.rb index 03d8a5bdd..5889750dd 100644 --- a/lib/puppet/reports/rrdgraph.rb +++ b/lib/puppet/reports/rrdgraph.rb @@ -90,8 +90,9 @@ Puppet::Reports.register_report(:rrdgraph) do of.puts "<html><head><title>Report graphs for %s</title></head><body>" % host files.each do |file| - of.puts "<a href='#{File.basename(file)}'>%s</a><br/>" % - File.basename(file).sub(".html",'').capitalize + of.puts "<a href='%s'>%s</a><br/>" % + [File.basename(file), + File.basename(file).sub(".html",'').capitalize] end of.puts "</body></html>" end diff --git a/lib/puppet/sslcertificates/inventory.rb b/lib/puppet/sslcertificates/inventory.rb index da959aa3b..295c416cb 100644 --- a/lib/puppet/sslcertificates/inventory.rb +++ b/lib/puppet/sslcertificates/inventory.rb @@ -7,8 +7,9 @@ module Puppet::SSLCertificates # If no inventory exists yet, build an inventory and list all the # certificates that have been signed so far def self.add(cert) - unless FileTest.exists?(Puppet[:cert_inventory]) - inited = false + inited = false + if FileTest.exists?(Puppet[:cert_inventory]) + inited = true end Puppet.settings.write(:cert_inventory, "a") do |f| diff --git a/lib/puppet/transaction/report.rb b/lib/puppet/transaction/report.rb index bd62ebbe6..89da7ed9c 100644 --- a/lib/puppet/transaction/report.rb +++ b/lib/puppet/transaction/report.rb @@ -25,13 +25,7 @@ class Puppet::Transaction::Report hash[key] = [] end - domain = Facter.value("domain") - hostname = Facter.value("hostname") - if !domain || domain.empty? then - @host = hostname - else - @host = [hostname, domain].join(".") - end + @host = Puppet[:certname] end def name diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index 3ad084b3b..d203b5928 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -65,13 +65,16 @@ module Puppet end def to_manifest - "#{self.type.to_s} { \'#{self.name}\':\n%s\n}" % @params.collect { |p, v| - if v.is_a? Array - " #{p} => [\'#{v.join("','")}\']" - else - " #{p} => \'#{v}\'" - end - }.join(",\n") + "%s { '%s':\n%s\n}" % + [self.type.to_s, self.name, + @params.collect { |p, v| + if v.is_a? Array + " #{p} => [\'#{v.join("','")}\']" + else + " #{p} => \'#{v}\'" + end + }.join(",\n") + ] end def to_yaml_properties diff --git a/lib/puppet/type/yumrepo.rb b/lib/puppet/type/yumrepo.rb index cb7c7401b..1e917793b 100644 --- a/lib/puppet/type/yumrepo.rb +++ b/lib/puppet/type/yumrepo.rb @@ -347,7 +347,23 @@ module Puppet newvalue(%r{[1-9][0-9]?}) { } end - - + newproperty(:proxy, :parent => Puppet::IniProperty) do + desc "URL to the proxy server for this repository.\n#{ABSENT_DOC}" + newvalue(:absent) { self.should = :absent } + # Should really check that it's a valid URL + newvalue(/.*/) { } + end + + newproperty(:proxy_username, :parent => Puppet::IniProperty) do + desc "Username for this proxy.\n#{ABSENT_DOC}" + newvalue(:absent) { self.should = :absent } + newvalue(/.*/) { } + end + + newproperty(:proxy_password, :parent => Puppet::IniProperty) do + desc "Password for this proxy.\n#{ABSENT_DOC}" + newvalue(:absent) { self.should = :absent } + newvalue(/.*/) { } + end end end diff --git a/lib/puppet/util/ldap/manager.rb b/lib/puppet/util/ldap/manager.rb index 9761fc753..8d444195e 100644 --- a/lib/puppet/util/ldap/manager.rb +++ b/lib/puppet/util/ldap/manager.rb @@ -80,7 +80,7 @@ class Puppet::Util::Ldap::Manager # Calculate the dn for a given resource. def dn(name) - ["#{rdn.to_s}=%s" % name, base].join(",") + ["%s=%s" % [rdn, name], base].join(",") end # Convert an ldap-style entry hash to a provider-style hash. diff --git a/lib/puppet/util/log.rb b/lib/puppet/util/log.rb index 22f4f3233..8824a8b50 100644 --- a/lib/puppet/util/log.rb +++ b/lib/puppet/util/log.rb @@ -244,23 +244,31 @@ class Puppet::Util::Log newdesttype :console do - PINK = {:console => "[0;31m", :html => "FFA0A0"} - GREEN = {:console => "[0;32m", :html => "00CD00"} - YELLOW = {:console => "[0;33m", :html => "FFFF60"} - SLATE = {:console => "[0;34m", :html => "80A0FF"} - ORANGE = {:console => "[0;35m", :html => "FFA500"} - BLUE = {:console => "[0;36m", :html => "40FFFF"} - RESET = {:console => "[0m", :html => ""} + RED = {:console => "[0;31m", :html => "FFA0A0"} + GREEN = {:console => "[0;32m", :html => "00CD00"} + YELLOW = {:console => "[0;33m", :html => "FFFF60"} + BLUE = {:console => "[0;34m", :html => "80A0FF"} + PURPLE = {:console => "[0;35m", :html => "FFA500"} + CYAN = {:console => "[0;36m", :html => "40FFFF"} + WHITE = {:console => "[0;37m", :html => "FFFFFF"} + HRED = {:console => "[1;31m", :html => "FFA0A0"} + HGREEN = {:console => "[1;32m", :html => "00CD00"} + HYELLOW = {:console => "[1;33m", :html => "FFFF60"} + HBLUE = {:console => "[1;34m", :html => "80A0FF"} + HPURPLE = {:console => "[1;35m", :html => "FFA500"} + HCYAN = {:console => "[1;36m", :html => "40FFFF"} + HWHITE = {:console => "[1;37m", :html => "FFFFFF"} + RESET = {:console => "[0m", :html => ""} @@colormap = { - :debug => SLATE, + :debug => WHITE, :info => GREEN, - :notice => PINK, - :warning => ORANGE, - :err => YELLOW, - :alert => BLUE, - :emerg => RESET, - :crit => RESET + :notice => CYAN, + :warning => YELLOW, + :err => HPURPLE, + :alert => RED, + :emerg => HRED, + :crit => HRED } def colorize(level, str) diff --git a/lib/puppet/util/posix.rb b/lib/puppet/util/posix.rb index 8228734ef..aff797485 100755 --- a/lib/puppet/util/posix.rb +++ b/lib/puppet/util/posix.rb @@ -3,23 +3,22 @@ module Puppet::Util::POSIX # Retrieve a field from a POSIX Etc object. The id can be either an integer # or a name. This only works for users and groups. It's also broken on - # some platforms, unfortunately. - def old_get_posix_field(space, field, id) + # some platforms, unfortunately, which is why we fall back to the other + # method search_posix_field in the gid and uid methods if a sanity check + # fails + def get_posix_field(space, field, id) unless id raise ArgumentError, "Did not get id" end - if id =~ /^\d+$/ - id = Integer(id) - end prefix = "get" + space.to_s if id.is_a?(Integer) if id > Puppet[:maximum_uid].to_i Puppet.err "Tried to get %s field for silly id %s" % [field, id] return nil end - method = (prefix + idfield(space).to_s).intern + method = methodbyid(space) else - method = (prefix + "nam").intern + method = methodbyname(space) end begin @@ -31,13 +30,11 @@ module Puppet::Util::POSIX end # A degenerate method of retrieving name/id mappings. The job of this method is - # to find a specific entry and then return a given field from that entry. - def get_posix_field(type, field, id) + # to retrieve all objects of a certain type, search for a specific entry + # and then return a given field from that entry. + def search_posix_field(type, field, id) idmethod = idfield(type) integer = false - if id =~ /^\d+$/ - id = Integer(id) - end if id.is_a?(Integer) integer = true if id > Puppet[:maximum_uid].to_i @@ -73,14 +70,70 @@ module Puppet::Util::POSIX end end + # Determine what the method is to get users and groups by id + def methodbyid(space) + case Puppet::Util.symbolize(space) + when :gr, :group: return :getgrgid + when :pw, :user, :passwd: return :getpwuid + else + raise ArgumentError.new("Can only handle users and groups") + end + end + + # Determine what the method is to get users and groups by name + def methodbyname(space) + case Puppet::Util.symbolize(space) + when :gr, :group: return :getgrnam + when :pw, :user, :passwd: return :getpwnam + else + raise ArgumentError.new("Can only handle users and groups") + end + end + # Get the GID of a given group, provided either a GID or a name def gid(group) - get_posix_field(:group, :gid, group) + begin + group = Integer(group) + rescue ArgumentError + # pass + end + if group.is_a?(Integer) + name = get_posix_field(:group, :name, group) + gid = get_posix_field(:group, :gid, name) + check_value = gid + else + gid = get_posix_field(:group, :gid, group) + name = get_posix_field(:group, :name, gid) + check_value = name + end + if check_value != group + return search_posix_field(:group, :gid, group) + else + return gid + end end # Get the UID of a given user, whether a UID or name is provided def uid(user) - get_posix_field(:passwd, :uid, user) + begin + user = Integer(user) + rescue ArgumentError + # pass + end + if user.is_a?(Integer) + name = get_posix_field(:passwd, :name, user) + uid = get_posix_field(:passwd, :uid, name) + check_value = uid + else + uid = get_posix_field(:passwd, :uid, user) + name = get_posix_field(:passwd, :name, uid) + check_value = name + end + if check_value != user + return search_posix_field(:passwd, :uid, user) + else + return uid + end end end |