Module: AbideDevUtils::Ppt::FacterUtils

Defined in:
lib/abide_dev_utils/ppt/facter_utils.rb

Overview

Methods relating to Facter

Defined Under Namespace

Classes: FactSet, FactSets

Class Method Summary collapse

Class Method Details

.all_versionsObject



241
242
243
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 241

def all_versions
  @all_versions ||= fact_sets.sort.map(&:facter_version)
end

.fact_setsObject

def latest_version

all_versions[-1]

end



231
232
233
234
235
236
237
238
239
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 231

def fact_sets
  @fact_sets ||= FacterDB.facterdb_fact_files.each_with_object([]) do |f, ary|
    facter_version = File.basename(File.dirname(f))
    fact_set = ary.find { |fs| fs.facter_version == facter_version }
    fact_set ||= FactSet.new(facter_version)
    fact_set.load_facts(f)
    ary << fact_set unless ary.include?(fact_set)
  end
end

.latest_versionObject



245
246
247
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 245

def latest_version
  @latest_version ||= all_versions.last
end

.previous_major_version(facter_version = latest_version) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 258

def previous_major_version(facter_version = latest_version)
  majver = facter_version.split('.')[0]
  prev_majver = (majver.to_i - 1).to_s
  prev_ver = all_versions.select { |v| v.start_with?(prev_majver) }.max
  return nil if prev_ver.to_i < 1

  prev_ver
end

.previous_version(facter_version = latest_version) ⇒ Object



267
268
269
270
271
272
273
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 267

def previous_version(facter_version = latest_version)
  reversed = all_versions.reverse
  rev_index = reversed.index(facter_version)
  return nil if rev_index.nil? || rev_index == reversed.length - 1

  reversed[reversed.index(facter_version) + 1]
end

.recurse_versions(version = latest_version, &block) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 275

def recurse_versions(version = latest_version, &block)
  output = yield version
  return output unless output.nil? || output.empty?

  prev_ver = previous_version(version).dup
  return nil if prev_ver.nil?

  recurse_versions(prev_ver, &block)
rescue SystemStackError
  locals = {
    prev_ver_map: @previous_major_version_map,
    current_version: current_version,
  }
  raise "Failed to find output while recursing versions. Locals: #{locals}"
end

.recursive_facts_for_os(os_name, os_release_major = nil, facter_version: latest_version) ⇒ Object



291
292
293
294
295
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 291

def recursive_facts_for_os(os_name, os_release_major = nil, facter_version: latest_version)
  recurse_versions(facter_version) do |ver|
    facts_for_os(os_name, os_release_major, facter_version: ver)
  end
end

.resolve_dot_path(dot_path, facter_version: latest_version, strict: false) ⇒ Object

def facts_for_os(os_name, os_release_major = nil, facter_version: latest_version)

fact_sets..find do |f|
  f['os']['name'] == os_name && f['os']['release']['major'] == os_release_major.to_s
end

end



303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 303

def resolve_dot_path(dot_path, facter_version: latest_version, strict: false)
  path_array = dot_path.delete_prefix('facts.').split('.')
  resolved = if strict
               fact_sets[facter_version].map do |fs|
                 fs.dig(*path_array)
               end
             else
               recurse_versions(facter_version) do |ver|
                 fact_sets[ver].map { |fs| fs.dig(*path_array) }
               end
             end
  resolved&.compact&.uniq
end


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 317

def resolve_related_dot_paths(*dot_paths, facter_version: latest_version, strict: false)
  resolved = []
  if strict
    fact_sets[facter_version].map do |fs|
      resolved << dot_paths.map do |p|
        path_array = p.delete_prefix('facts.').split('.')
        fs.dig(*path_array)
      end
    end
  else
    recurse_versions(facter_version) do |ver|
      fact_sets[ver].map do |fs|
        resolved << dot_paths.map do |p|
          path_array = p.delete_prefix('facts.').split('.')
          fs.dig(*path_array)
        end
      end
    end
  end
  resolved
end

.with_version(version) {|fact_set| ... } ⇒ Object

Yields:

  • (fact_set)


249
250
251
252
253
254
255
256
# File 'lib/abide_dev_utils/ppt/facter_utils.rb', line 249

def with_version(version)
  return unless block_given?

  fact_set = fact_sets.find { |fs| fs.facter_version == version }
  raise "No facts found for version #{version}" unless fact_set

  yield fact_set
end