Class: Facter::Util::Resolution

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

Overview

Since:

  • 2.0.0

Instance Attribute Summary collapse

Attributes included from LegacyFacter::Core::Resolvable

#logger, #timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Execution

absolute_path?, execute, execute_command, expand_command, impl, search_paths, 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) ⇒ Facter::Util::Resolution

Create a new resolution mechanism.

Parameters:

  • name (String)

    The name of the resolution.

  • fact (Facter::Fact)

    The fact to which this resolution will be added.

Since:

  • 2.0.0



74
75
76
77
78
79
80
81
# File 'lib/facter/custom_facts/util/resolution.rb', line 74

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.

Since:

  • 2.0.0



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

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 Associated fact with this resolution.

Returns:



63
64
65
# File 'lib/facter/custom_facts/util/resolution.rb', line 63

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.

Since:

  • 2.0.0



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

def fact_type
  @fact_type
end

#fileFacter::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 Associated fact with this resolution.

Returns:

Since:

  • 2.0.0



63
64
65
# File 'lib/facter/custom_facts/util/resolution.rb', line 63

def file
  @file
end

#last_evaluatedFacter::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 Associated fact with this resolution.

Returns:

Since:

  • 2.0.0



63
64
65
# File 'lib/facter/custom_facts/util/resolution.rb', line 63

def last_evaluated
  @last_evaluated
end

#nameString

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

Returns:

  • (String)


56
57
58
# File 'lib/facter/custom_facts/util/resolution.rb', line 56

def name
  @name
end

#value=(value) ⇒ Object (writeonly)

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.

Since:

  • 2.0.0



21
22
23
# File 'lib/facter/custom_facts/util/resolution.rb', line 21

def value=(value)
  @value = value
end

Class Method Details

.exec(command) ⇒ Object

Deprecated.

Use Facter::Core::Execution.execute instead

Since:

  • 2.0.0



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

def exec(command)
  Facter.warnonce('Facter::Util::Resolution.exec is deprecated and will be removed in a future major version. ' \
                  'Use Facter::Core::Execution.execute instead.')
  Facter::Core::Execution.execute(command, on_fail: nil)
end

.which(bin) ⇒ Object

Deprecated.

Use Facter::Core::Execution.which instead

Since:

  • 2.0.0



28
29
30
31
32
# File 'lib/facter/custom_facts/util/resolution.rb', line 28

def which(bin)
  Facter.warnonce('Facter::Util::Resolution.which is deprecated and will be removed in a future major version. ' \
                  'Use Facter::Core::Execution.which instead.')
  Facter::Core::Execution.which(bin)
end

Instance Method Details

#<=>(other) ⇒ bool

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.

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

Returns:

  • (bool)

    Weight comparison result

Since:

  • 2.0.0



168
169
170
171
172
173
174
# File 'lib/facter/custom_facts/util/resolution.rb', line 168

def <=>(other)
  if weight == other.weight
    compare_equal_weights(other)
  else
    weight <=> other.weight
  end
end

#evaluate(&block) ⇒ String

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.

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

Returns:

  • (String)

    Result of the block's evaluation

Since:

  • 2.0.0



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/facter/custom_facts/util/resolution.rb', line 98

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

  instance_eval(&block)

  @last_evaluated = block.source_location.join(':')
end

#options(options) ⇒ nil

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.

Sets options for the aggregate fact

Returns:

  • (nil)

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



116
117
118
119
120
121
122
123
124
# File 'lib/facter/custom_facts/util/resolution.rb', line 116

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

  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_typeSymbol

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 the fact's resolution type

Returns:

  • (Symbol)

    The fact's type

Since:

  • 2.0.0



88
89
90
# File 'lib/facter/custom_facts/util/resolution.rb', line 88

def resolution_type
  :simple
end

#setcode(string) ⇒ Facter::Util::Resolution #setcode(&block) ⇒ Facter::Util::Resolution

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

Overloads:

  • #setcode(string) ⇒ Facter::Util::Resolution

    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) ⇒ Facter::Util::Resolution

    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.

Returns:

Since:

  • 2.0.0



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/facter/custom_facts/util/resolution.rb', line 143

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
  self
end