Class: Facter::Util::Resolution

Inherits:
Object
  • Object
show all
Extended by:
Core::Execution
Includes:
LegacyFacter::Core::Resolvable, LegacyFacter::Core::Suitable
Defined in:
lib/custom_facts/util/resolution.rb

Instance Attribute Summary collapse

Attributes included from LegacyFacter::Core::Resolvable

#logger, #timeout

Instance Method Summary collapse

Methods included from Core::Execution

absolute_path?, exec, execute, expand_command, impl, search_paths, which, with_env

Methods included from LegacyFacter::Core::Suitable

#confine, #has_weight, #suitable?, #weight

Methods included from LegacyFacter::Core::Resolvable

#flush, #limit, #on_flush, #value

Constructor Details

#initialize(name, fact) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new resolution mechanism.

Parameters:

  • name (String)

    The name of the resolution.



51
52
53
54
55
56
57
58
# File 'lib/custom_facts/util/resolution.rb', line 51

def initialize(name, fact)
  @name = name
  @fact = fact
  @confines = []
  @value = nil
  @timeout = 0
  @weight = nil
end

Instance Attribute Details

#codeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/custom_facts/util/resolution.rb', line 16

def code
  @code
end

#factFacter::Util::Fact (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



43
44
45
# File 'lib/custom_facts/util/resolution.rb', line 43

def fact
  @fact
end

#fact_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/custom_facts/util/resolution.rb', line 16

def fact_type
  @fact_type
end

#nameString

The name of this resolution. The resolution name should be unique with respect to the given fact.

Returns:

  • (String)


38
39
40
# File 'lib/custom_facts/util/resolution.rb', line 38

def name
  @name
end

#value=(value) ⇒ Object (writeonly)



17
18
19
# File 'lib/custom_facts/util/resolution.rb', line 17

def value=(value)
  @value = value
end

Instance Method Details

#<=>(other) ⇒ Object

Comparation is done based on weight and fact type. The greatter the weight, the higher the priority. If weights are equal, we consider external facts greater than custom facts



135
136
137
138
139
# File 'lib/custom_facts/util/resolution.rb', line 135

def <=>(other)
  return compare_equal_weights(other) if weight == other.weight
  return 1 if weight > other.weight
  return -1 if weight < other.weight
end

#evaluate(&block) ⇒ void

This method returns an undefined value.

Evaluate the given block in the context of this resolution. If a block has already been evaluated emit a warning to that effect.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/custom_facts/util/resolution.rb', line 68

def evaluate(&block)
  if @last_evaluated
    msg = "Already evaluated #{@name}"
    msg << " at #{@last_evaluated}" if msg.is_a? String
    msg << ', reevaluating anyways'
    LegacyFacter.warn msg
  end

  instance_eval(&block)

  # Ruby 1.9+ provides the source location of procs which can provide useful
  # debugging information if a resolution is being evaluated twice. Since 1.8
  # doesn't support this we opportunistically provide this information.
  @last_evaluated = if block.respond_to? :source_location
                      block.source_location.join(':')
                    else
                      true
                    end
end

#options(options) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
# File 'lib/custom_facts/util/resolution.rb', line 88

def options(options)
  accepted_options = %i[name value timeout weight fact_type]

  accepted_options.each do |option_name|
    instance_variable_set("@#{option_name}", options.delete(option_name)) if options.key?(option_name)
  end

  raise ArgumentError, "Invalid resolution options #{options.keys.inspect}" unless options.keys.empty?
end

#resolution_typeObject



60
61
62
# File 'lib/custom_facts/util/resolution.rb', line 60

def resolution_type
  :simple
end

#setcode(string) ⇒ void #setcode(&block) ⇒ void

This method returns an undefined value.

Sets the code block or external program that will be evaluated to get the value of the fact.

Overloads:

  • #setcode(string) ⇒ void

    Sets an external program to call to get the value of the resolution

    Parameters:

    • string (String)

      the external program to run to get the value

  • #setcode(&block) ⇒ void

    Sets the resolution’s value by evaluating a block at runtime

    Parameters:

    • block (Proc)

      The block to determine the resolution’s value. This block is run when the fact is evaluated. Errors raised from inside the block are rescued and printed to stderr.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/custom_facts/util/resolution.rb', line 115

def setcode(string = nil, &block)
  if string
    @code = proc do
      output = Facter::Core::Execution.execute(string, on_fail: nil)
      if output.nil? || output.empty?
        nil
      else
        output
      end
    end
  elsif block_given?
    @code = block
  else
    raise ArgumentError, 'You must pass either code or a block'
  end
end