Class Facter::Resolution
In: lib/facter.rb
Parent: Object

An actual fact resolution mechanism. These are largely just chunks of code, with optional confinements restricting the mechanisms to only working on specific systems. Note that the confinements are always ANDed, so any confinements specified must all be true for the resolution to be suitable.

Methods

confine   exec   have_which   length   new   setcode   setldapname   suitable?   tag   to_s   value  

Attributes

code  [RW] 
fact  [RW] 
interpreter  [RW] 
name  [RW] 

Public Class methods

Execute a chunk of code.

[Source]

     # File lib/facter.rb, line 396
396:         def Resolution.exec(code, interpreter = "/bin/sh")
397:             if interpreter == "/bin/sh"
398:                 binary = code.split(/\s+/).shift
399: 
400:                 if have_which
401:                     path = nil
402:                     if binary !~ /^\//
403:                         path = %x{which #{binary} 2>/dev/null}.chomp
404:                         if path == ""
405:                             # we don't have the binary necessary
406:                             return nil
407:                         end
408:                     else
409:                         path = binary
410:                     end
411: 
412:                     unless FileTest.exists?(path)
413:                         # our binary does not exist
414:                         return nil
415:                     end
416:                 end
417: 
418:                 out = nil
419:                 begin
420:                     out = %x{#{code}}.chomp
421:                 rescue => detail
422:                     $stderr.puts detail
423:                     return nil
424:                 end
425:                 if out == ""
426:                     return nil
427:                 else
428:                     return out
429:                 end
430:             else
431:                 raise ArgumentError,
432:                     "non-sh interpreters are not currently supported"
433:             end
434:         end

[Source]

     # File lib/facter.rb, line 387
387:         def Resolution.have_which
388:             if @have_which.nil?
389:                 %x{which which 2>/dev/null}
390:                 @have_which = ($? == 0)
391:             end
392:             @have_which
393:         end

Create a new resolution mechanism.

[Source]

     # File lib/facter.rb, line 449
449:         def initialize(name)
450:             @name = name
451:             @confines = []
452:             @value = nil
453:         end

Public Instance methods

Add a new confine to the resolution mechanism.

[Source]

     # File lib/facter.rb, line 437
437:         def confine(*args)
438:             if args[0].is_a? Hash
439:                 args[0].each do |fact, values|
440:                     @confines.push Confine.new(fact,*values)
441:                 end
442:             else
443:                 fact = args.shift
444:                 @confines.push Confine.new(fact,*args)
445:             end
446:         end

Return the number of confines.

[Source]

     # File lib/facter.rb, line 456
456:         def length
457:             @confines.length
458:         end

Set our code for returning a value.

[Source]

     # File lib/facter.rb, line 461
461:         def setcode(string = nil, interp = nil, &block)
462:             if string
463:                 @code = string
464:                 @interpreter = interp || "/bin/sh"
465:             else
466:                 unless block_given?
467:                     raise ArgumentError, "You must pass either code or a block"
468:                 end
469:                 @code = block
470:             end
471:         end

Set the name by which this parameter is known in LDAP. The default is just the fact name.

[Source]

     # File lib/facter.rb, line 475
475:         def setldapname(name)
476:             @fact.ldapname = name.to_s
477:         end

Is this resolution mechanism suitable on the system in question?

[Source]

     # File lib/facter.rb, line 480
480:         def suitable?
481:             unless defined? @suitable
482:                 @suitable = true
483:                 if @confines.length == 0
484:                     return true
485:                 end
486:                 @confines.each { |confine|
487:                     unless confine.true?
488:                         @suitable = false
489:                     end
490:                 }
491:             end
492: 
493:             return @suitable
494:         end

Set tags on our parent fact.

[Source]

     # File lib/facter.rb, line 497
497:         def tag(*values)
498:             @fact.tag(*values)
499:         end

[Source]

     # File lib/facter.rb, line 501
501:         def to_s
502:             return self.value()
503:         end

How we get a value for our resolution mechanism.

[Source]

     # File lib/facter.rb, line 506
506:         def value
507:             value = nil
508: 
509:             if @code.is_a?(Proc)
510:                 value = @code.call()
511:             else
512:                 unless defined? @interpreter
513:                     @interpreter = "/bin/sh"
514:                 end
515:                 if @code.nil?
516:                     $stderr.puts "Code for %s is nil" % @name
517:                 else
518:                     value = Resolution.exec(@code,@interpreter)
519:                 end
520:             end
521: 
522:             if value == ""
523:                 value = nil
524:             end
525: 
526:             return value
527:         end

[Validate]