Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from puppetlabs/FACT-1981
Browse files Browse the repository at this point in the history
(FACT-1981) Detect facts dynamically based on OS.
  • Loading branch information
sebastian-miclea committed Aug 6, 2019
2 parents 86fc6fb + 6206ae3 commit 2c7eccd
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sudo: false
language: ruby
rvm:
- 2.6
- 2.5
- 2.4
- 2.3
cache:
bundler: true
before_install:
- gem update --system
- gem install bundler
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ GEM
specs:
byebug (11.0.1)
coderay (1.1.2)
coveralls (0.8.23)
json (>= 1.8, < 3)
simplecov (~> 0.16.1)
term-ansicolor (~> 1.3)
thor (>= 0.19.4, < 2.0)
tins (~> 1.6)
diff-lcs (1.3)
docile (1.3.2)
json (2.2.0)
method_source (0.9.2)
pry (0.12.2)
coderay (~> 1.1.0)
Expand All @@ -31,14 +39,23 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.2)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
term-ansicolor (1.7.1)
tins (~> 1.0)
thor (0.20.3)
tins (1.21.1)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
cli.rb-ng!
coveralls (~> 0.8.23)
pry-byebug (~> 3.0)
rake (~> 10.0)
rspec (~> 3.0)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# facter-ng
New version of Puppet Facter written in Ruby

[![Build Status](https://travis-ci.org/puppetlabs/facter-ng.svg?branch=master)](https://travis-ci.org/puppetlabs/facter-ng?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/puppetlabs/facter-ng/badge.svg?branch=master)](https://coveralls.io/github/puppetlabs/facter-ng?branch=master)
1 change: 1 addition & 0 deletions bin/facter
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require_relative '../lib/cli'
require_relative '../lib/base'
require_relative '../lib/fact_loader'
require_relative '../lib/query_parser'
require_relative '../lib/models/loaded_fact'

os = :linux
require_relative "../lib/facts/#{os.to_s}/network_interface"
Expand Down
2 changes: 2 additions & 0 deletions facter-ng.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency "pry-byebug", "~> 3.0"
spec.add_development_dependency 'coveralls', '~> 0.8.23'


spec.add_runtime_dependency 'thor', '~> 0.20.3'
end
24 changes: 2 additions & 22 deletions lib/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def initialize(searched_facts)
matched_facts = []

searched_facts.each do |searched_fact|

matched_facts << Facter::QueryParser.parse(searched_fact, facts)
end

Expand All @@ -19,8 +18,8 @@ def resolve_matched_facts(matched_facts)

matched_facts.each do |matched_fact|
threads << Thread.new do
klass = token_to_class(matched_fact[0])
klass.new(matched_fact[1]).call_the_resolver!
fact_class = matched_fact.fact_class
fact_class.new(matched_fact.filter_tokens).call_the_resolver!
end
end

Expand All @@ -31,28 +30,9 @@ def resolve_matched_facts(matched_facts)

puts results.inspect
end

def token_to_class(str)
Kernel.const_get('Facter::Linux::'+str)
end
end

def self.new(args)
Facter::Base.new(args)
end
end



# class NetworkInterface < Fact

# def initialize(search)

# end

# end


# class Fact

# end
32 changes: 26 additions & 6 deletions lib/fact_loader.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
# frozen_string_literal: true

module Facter
class FactLoader
def self.load(os)
# load facts/#{os}/*.rb
{
'networking.ip' => 'NetworkIP',
'networking.interface' => 'NetworkInterface'
}
# Loads all the facts for the operating system Facter is running on.
# Facts are dynamically loaded from facts/#{operating_system}/*.rb
# Returns a hash containing fact name and fact classes
# e.g.
#
# {
# "networking.interface" => Facter::Linux::NetworkInterface,
# "networking.ip" => Facter::Linux::NetworkIP
# }
def self.load(operating_system)
loaded_facts = {}
os = operating_system.capitalize
os_module_name = Module.const_get("Facter::#{os}")

# select only classes
classes = os_module_name.constants.select { |c| os_module_name.const_get(c).is_a? Class }

classes.each do |class_name|
klass = Class.const_get("Facter::#{os}::" + class_name.to_s)
fact_name = klass::FACT_NAME
loaded_facts.merge!(fact_name => klass)
end

loaded_facts
end
end
end
9 changes: 9 additions & 0 deletions lib/facts/linux/network_interface.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
module Facter
module Linux
class NetworkInterface
FACT_NAME = 'networking.interface'.freeze
@aliases =[]

# def self.fact_name
# @@fact_name
# end

def initialize(*args)
puts 'Dispatching to resolve: ' + args.inspect
# constants = Linux.constants
# puts constants
end

def call_the_resolver!
Expand Down
7 changes: 7 additions & 0 deletions lib/facts/linux/network_ip.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module Facter
module Linux
class NetworkIP
FACT_NAME = 'networking.ip'.freeze
@aliases =[]

# def self.fact_name
# @@fact_name
# end

def initialize(*args)
puts 'Dispatching to resolve: ' + args.inspect
end
Expand Down
9 changes: 9 additions & 0 deletions lib/models/loaded_fact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Facter
class LoadedFact
@fact_name
@fact_class
@fact_attributes

attr_accessor :fact_name, :fact_class, :filter_tokens
end
end
21 changes: 20 additions & 1 deletion lib/query_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
module Facter
class QueryParser
# Searches for facts that could resolve a user query.
# There are 3 types of facts:
# root facts
# e.g. networking
# child facts
# e.g. networking.dhcp
# composite facts
# e.g. networking.interfaces.en0.bindings.address
# Because a root fact will always be resolved by the collection of child facts,
# we can return one or more child facts.
#
# query - is the user input used to search for facts
# fact_list - is a list with all facts for the current operating system
#
# Returns a list of LoadedFact objects that resolve the users query.
def self.parse(query, fact_list)
tokens = query.split('.')
size = tokens.size
Expand All @@ -11,7 +26,11 @@ def self.parse(query, fact_list)
fact_list.each do |fact_name, klass_name|
if fact_name.match?(tokens[elem].join('.'))
filter_tokens = tokens - tokens[elem]
resolvable_fact_list << [klass_name, filter_tokens]

fact = LoadedFact.new
fact.filter_tokens = filter_tokens
fact.fact_class = klass_name
resolvable_fact_list << fact # [klass_name, filter_tokens]
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/facter/fact_loader_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

describe '.load facts for OS' do
it 'load one fact' do
allow_any_instance_of(Module).to receive(:constants).and_return([:NetworkInterface])
fact_hash = Facter::FactLoader.load(:linux)

expect fact_hash.values_at('networking.interface').equal?('Facter::Linux::NetworkInterface')
end

it 'does not load any fact' do
allow_any_instance_of(Module).to receive(:constants).and_return([])
fact_hash = Facter::FactLoader.load(:linux)

expect fact_hash.size.equal?(0)
end
end
7 changes: 6 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# frozen_string_literal: true

require 'coveralls'
Coveralls.wear!

require 'bundler/setup'
require 'cli.rb/ng'
require_relative '../lib/fact_loader'
require_relative '../lib/facts/linux/network_interface'
require_relative 'facter/fact_loader_test'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit 2c7eccd

Please sign in to comment.