Advertisement
Guest User

confine.rb

a guest
Jul 5th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.62 KB | None | 0 0
  1. # The class that handles testing whether our providers
  2. # actually work or not.
  3. require 'puppet/util'
  4.  
  5. class Puppet::Provider::Confine
  6.   include Puppet::Util
  7.  
  8.   @tests = {}
  9.  
  10.   class << self
  11.     attr_accessor :name
  12.   end
  13.  
  14.   def self.inherited(klass)
  15.     name = klass.to_s.split("::").pop.downcase.to_sym
  16.     raise "Test #{name} is already defined" if @tests.include?(name)
  17.  
  18.     klass.name = name
  19.  
  20.     @tests[name] = klass
  21.   end
  22.  
  23.   def self.test(name)
  24.     unless @tests[name]
  25.       begin
  26.         require "puppet/provider/confine/#{name}"
  27.       rescue LoadError => detail
  28.         unless detail.to_s =~ /such file/i
  29.           warn "Could not load confine test '#{name}': #{detail}"
  30.         end
  31.         # Could not find file
  32.       end
  33.     end
  34.     @tests[name]
  35.   end
  36.  
  37.   attr_reader :values
  38.  
  39.   # Mark that this confine is used for testing binary existence.
  40.   attr_accessor :for_binary
  41.   def for_binary?
  42.     for_binary
  43.   end
  44.  
  45.   # Used for logging.
  46.   attr_accessor :label
  47.  
  48.   def initialize(values)
  49.     values = [values] unless values.is_a?(Array)
  50.     @values = values
  51.   end
  52.  
  53.   # Provide a hook for the message when there's a failure.
  54.   def message(value)
  55.     ""
  56.   end
  57.  
  58.   # Collect the results of all of them.
  59.   def result
  60.     values.collect { |value| pass?(value) }
  61.   end
  62.  
  63.   # Test whether our confine matches.
  64.   def valid?
  65.     values.each do |value|
  66.       unless pass?(value)
  67.         Puppet.debug(label + ": " + message(value))
  68.         return false
  69.       end
  70.     end
  71.  
  72.     return true
  73.   ensure
  74.     reset
  75.   end
  76.  
  77.   # Provide a hook for subclasses.
  78.   def reset
  79.   end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement