This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f9e38a
commit d9c2a04
Showing
19 changed files
with
485 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
class FactGroups | ||
attr_reader :groups, :block_list | ||
|
||
@groups_ttls = [] | ||
|
||
STRING_TO_SECONDS = { 'seconds' => 1, 'minutes' => 60, 'hours' => 3600, 'days' => 3600 * 24 }.freeze | ||
|
||
def initialize(group_list_path = nil) | ||
@groups_file_path = group_list_path || File.join(ROOT_DIR, 'fact_groups.conf') | ||
@groups ||= File.readable?(@groups_file_path) ? Hocon.load(@groups_file_path) : {} | ||
load_groups | ||
end | ||
|
||
# Breakes down blocked groups in blocked facts | ||
def blocked_facts | ||
fact_list = [] | ||
|
||
@block_list.each do |group_name| | ||
facts_for_block = @groups[group_name] | ||
|
||
fact_list += facts_for_block || [group_name] | ||
end | ||
|
||
fact_list | ||
end | ||
|
||
# Get the group name a fact is part of | ||
def get_fact_group(fact_name) | ||
@groups.detect { |k, v| break k if Array(v).find { |f| fact_name =~ /^#{f}.*/ } } | ||
end | ||
|
||
# Get config ttls for a given group | ||
def get_group_ttls(group_name) | ||
return unless (ttls = @groups_ttls.find { |g| g[group_name] }) | ||
|
||
ttls_to_seconds(ttls[group_name]) | ||
end | ||
|
||
private | ||
|
||
def load_groups | ||
config = ConfigReader.init(Options[:config]) | ||
@block_list = config.block_list || {} | ||
@groups_ttls = ConfigReader.init(Options[:config]).ttls || {} | ||
end | ||
|
||
def ttls_to_seconds(ttls) | ||
duration, unit = ttls.split(' ', 2) | ||
duration.to_i * STRING_TO_SECONDS[unit] | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
class CacheManager | ||
def initialize | ||
@groups = {} | ||
@log = Log.new(self) | ||
@fact_groups = Facter::FactGroups.new | ||
end | ||
|
||
def cache_dir | ||
LegacyFacter::Util::Config.facts_cache_dir | ||
end | ||
|
||
def resolve_facts(searched_facts) | ||
return searched_facts, [] if !File.directory?(cache_dir) || !Options[:cache] | ||
|
||
facts = [] | ||
searched_facts.each do |fact| | ||
res = resolve_fact(fact) | ||
facts << res if res | ||
end | ||
facts.each do |fact| | ||
searched_facts.delete_if { |f| f.name == fact.name } | ||
end | ||
[searched_facts, facts] | ||
end | ||
|
||
def cache_facts(resolved_facts) | ||
return unless Options[:cache] | ||
|
||
resolved_facts.each do |fact| | ||
cache_fact(fact) | ||
end | ||
|
||
write_cache unless @groups.empty? | ||
end | ||
|
||
private | ||
|
||
def resolve_fact(searched_fact) | ||
group_name = @fact_groups.get_fact_group(searched_fact.name) | ||
return unless group_name | ||
|
||
return unless group_cached?(group_name) | ||
|
||
return if check_ttls(group_name).zero? | ||
|
||
data = read_group_json(group_name) | ||
return unless data | ||
|
||
@log.debug("loading cached values for #{group_name} facts") | ||
create_fact(searched_fact, data[searched_fact.name]) | ||
end | ||
|
||
def create_fact(searched_fact, value) | ||
resolved_fact = Facter::ResolvedFact.new(searched_fact.name, value, searched_fact.type) | ||
resolved_fact.user_query = searched_fact.user_query | ||
resolved_fact.filter_tokens = searched_fact.filter_tokens | ||
resolved_fact | ||
end | ||
|
||
def cache_fact(fact) | ||
group_name = @fact_groups.get_fact_group(fact.name) | ||
return if !group_name || fact.value.nil? | ||
|
||
return unless group_cached?(group_name) | ||
|
||
@groups[group_name] ||= {} | ||
@groups[group_name][fact.name] = fact.value | ||
end | ||
|
||
def write_cache | ||
unless File.directory?(cache_dir) | ||
require 'fileutils' | ||
FileUtils.mkdir_p(cache_dir) | ||
end | ||
|
||
@groups.each do |group_name, data| | ||
next if check_ttls(group_name).zero? | ||
|
||
@log.debug("caching values for #{group_name} facts") | ||
cache_file_name = File.join(cache_dir, group_name) | ||
File.write(cache_file_name, JSON.pretty_generate(data)) | ||
end | ||
end | ||
|
||
def read_group_json(group_name) | ||
return @groups[group_name] if @groups.key?(group_name) | ||
|
||
cache_file_name = File.join(cache_dir, group_name) | ||
data = nil | ||
if File.readable?(cache_file_name) | ||
file = Util::FileHelper.safe_read(cache_file_name) | ||
begin | ||
data = JSON.parse(file) | ||
rescue JSON::ParserError | ||
delete_cache(group_name) | ||
end | ||
end | ||
@groups[group_name] = data | ||
end | ||
|
||
def group_cached?(group_name) | ||
cached = @fact_groups.get_group_ttls(group_name) ? true : false | ||
delete_cache(group_name) unless cached | ||
cached | ||
end | ||
|
||
def check_ttls(group_name) | ||
ttls = @fact_groups.get_group_ttls(group_name) | ||
return 0 unless ttls | ||
|
||
cache_file_name = File.join(cache_dir, group_name) | ||
return ttls unless File.readable?(cache_file_name) | ||
|
||
file_time = File.mtime(cache_file_name) | ||
expire_date = file_time + ttls | ||
if expire_date < Time.now | ||
File.delete(cache_file_name) | ||
return ttls | ||
end | ||
expire_date.to_i | ||
end | ||
|
||
def delete_cache(group_name) | ||
cache_file_name = File.join(cache_dir, group_name) | ||
File.delete(cache_file_name) if File.readable?(cache_file_name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.