Class: EasyType::FlexibleFact

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_type/flexible_fact.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basic_name) ⇒ FlexibleFact

Returns a new instance of FlexibleFact.



44
45
46
47
# File 'lib/easy_type/flexible_fact.rb', line 44

def initialize(basic_name)
  @basic_name = basic_name
  @parts = []
end

Class Method Details

.config_dataObject



32
33
34
# File 'lib/easy_type/flexible_fact.rb', line 32

def self.config_data
  @config_data ||= Hocon::ConfigFactory.parse_file(config_file).resolve
end

.config_dirObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/easy_type/flexible_fact.rb', line 16

def self.config_dir
  kernel = Facter.value(:kernel)
  case kernel
  when 'Linux'
    '/etc/puppetlabs/facter'
  when 'windows'
    'C:/ProgramData/PuppetLabs/facter/etc'      
  else
    fail "'#{kernel}' not (yet) supported."
  end
end

.config_fileObject



28
29
30
# File 'lib/easy_type/flexible_fact.rb', line 28

def self.config_file
  "#{config_dir}/facter.conf"
end

.define(basic_name, &definition) ⇒ Object



10
11
12
13
14
# File 'lib/easy_type/flexible_fact.rb', line 10

def self.define(basic_name, &definition)
  obj = new(basic_name)
  obj.instance_eval(&definition)
  obj.apply()
end

Instance Method Details

#applyObject



109
110
111
112
113
114
115
# File 'lib/easy_type/flexible_fact.rb', line 109

def apply()
  if force_dot_enabled?
    force_dot_implementation
  else
    chuncked_implementation
  end
end

#chuncked_implementationObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/easy_type/flexible_fact.rb', line 81

def chuncked_implementation
  #
  # Ensure correct variable are available as local variables in Facter closure scopes
  #
  parts = @parts
  components = @components
  Facter.add(@basic_name.to_sym, :type => :aggregate) do
    parts.each do | part|
      name = part.keys.first
      implementation = part[name]
      chunk(name.to_sym) do
        components.reduce({name.to_sym => {}}) do |data, dir|
          data[name.to_sym][dir] = implementation.call(dir)
          data
        end
      end
    end
  end
end

#components(components) ⇒ Object



101
102
103
# File 'lib/easy_type/flexible_fact.rb', line 101

def components(components)
  @components = components
end

#force_dot_enabled?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/easy_type/flexible_fact.rb', line 53

def force_dot_enabled?
  return false if no_caching_in_facter_yet?

  global_config.dig('force-dot-resolution')
end

#force_dot_implementationObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/easy_type/flexible_fact.rb', line 59

def force_dot_implementation
  @parts.each do | part|
    #
    # Ensure correct variable are available as local variables in Facter closure scopes
    #
    name = part.keys.first
    implementation = part[name]
    components = @components
    #
    # Now define the facts in the correct way
    #
    Facter.add("#{@basic_name}.#{name}") do
      setcode do
        components.each_with_object({}) do |dir, data|
          data[dir] = implementation.call(dir)
          data
        end
      end
    end
  end
end

#global_configObject



36
37
38
39
40
41
42
# File 'lib/easy_type/flexible_fact.rb', line 36

def global_config
  if self.class.config_data.has_path?('global')
    self.class.config_data.get_value('global').unwrapped
  else
    {}
  end
end

#no_caching_in_facter_yet?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/easy_type/flexible_fact.rb', line 49

def no_caching_in_facter_yet?
  Gem::Version.new(Facter.version) < Gem::Version.new('4.1.0')
end

#part(name, &implementation) ⇒ Object



105
106
107
# File 'lib/easy_type/flexible_fact.rb', line 105

def part(name, &implementation)
  @parts << {name => implementation}
end