Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ else
end

gem 'puppet-strings', '>= 1.2.1'
# https://github.com/theforeman/kafo_parsers/pull/49
gem 'kafo_parsers', github: 'ekohl/kafo_parsers', branch: 'bulk-strings'

group :puppet_module do
gem 'metadata-json-lint'
Expand Down
38 changes: 38 additions & 0 deletions lib/kafo/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# encoding: UTF-8
require 'yaml'
require 'tmpdir'
require 'kafo/parser_cache_reader'
require 'kafo_parsers/parsers'
require 'kafo/puppet_module'
require 'kafo/color_scheme'
require 'kafo/data_type_parser'
Expand Down Expand Up @@ -159,6 +161,7 @@ def kafo_modules_dir
end

def add_module(name)
# TOOD: invalidate parser?
mod = PuppetModule.new(name, configuration: self).parse
unless modules.map(&:name).include?(mod.name)
mod.enable
Expand Down Expand Up @@ -343,6 +346,14 @@ def parser_cache
end
end

# Parsed data for a PuppetModule
# @param [Kafo::PuppetModule] mod
# The module to parse. It's retrieved from the cache if available
# @return [Hash] The raw data returned from KafoParsers
def parsed(mod)
parser_cache&.get(mod.identifier, mod.manifest_path) || parser.by_name(mod.class_name)
end

private

def custom_storage
Expand Down Expand Up @@ -399,5 +410,32 @@ def register_data_types
end
end
end

def paths_to_parse
types = module_dirs.map { |module_dir| File.join(module_dir, '*', 'types', '**', '*.pp') }
# TODO: limit to @data.keys after mapping it
manifests = module_dirs.map { |module_dir| File.join(module_dir, '*', 'manifests', '**', '*.pp') }

manifests + types
end

def parser
@parser ||= begin
parser = KafoParsers::Parsers.find_available(:logger => @logger)
if parser
@logger.debug "Using Puppet module parser #{parser}"
parser.new(paths_to_parse)
else
@logger.debug "No available Puppet module parser found"
:none # prevent continually re-checking
end
end

if @parser == :none
raise ParserError.new("No Puppet module parser is installed and no cache of the file #{manifest_path} is available. Please check debug logs and install optional dependencies for the parser.")
end

@parser
end
end
end
40 changes: 7 additions & 33 deletions lib/kafo/puppet_module.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# encoding: UTF-8
require 'kafo/param'
require 'kafo/param_builder'
require 'kafo/parser_cache_reader'
require 'kafo_parsers/parsers'

module Kafo
class PuppetModule
Expand All @@ -11,21 +9,7 @@ class PuppetModule
attr_reader :name, :identifier, :params, :dir_name, :class_name, :manifest_name, :manifest_path,
:groups, :params_path, :params_class_name, :configuration, :raw_data

def self.find_parser
@parser ||= begin
logger = KafoConfigure.logger
parser = KafoParsers::Parsers.find_available(:logger => logger)
if parser
logger.debug "Using Puppet module parser #{parser}"
parser
else
logger.debug "No available Puppet module parser found"
:none # prevent continually re-checking
end
end
end

def initialize(identifier, parser: nil, configuration: KafoConfigure.config)
def initialize(identifier, configuration: KafoConfigure.config)
@identifier = identifier
@configuration = configuration
@name = get_name
Expand All @@ -40,8 +24,6 @@ def initialize(identifier, parser: nil, configuration: KafoConfigure.config)
warn("Manifest #{module_manifest_path} was not found in #{@configuration.module_dirs.join(', ')}")
end
@manifest_path = File.join(module_dir, module_manifest_path)
@parser = parser
@parser_cache = @configuration.parser_cache
@logger = KafoConfigure.logger
@groups = {}
@params_path = get_params_path
Expand All @@ -63,15 +45,7 @@ def enable
end

def parse(builder_klass = ParamBuilder)
@raw_data = @parser_cache.get(identifier, manifest_path) if @parser_cache
if @raw_data.nil?
@parser = self.class.find_parser if @parser.nil?
if @parser.nil? || @parser == :none
raise ParserError.new("No Puppet module parser is installed and no cache of the file #{manifest_path} is available. Please check debug logs and install optional dependencies for the parser.")
else
@raw_data = @parser.parse(manifest_path)
end
end
@raw_data = @configuration.parsed(self)

builder = builder_klass.new(self, @raw_data)

Expand All @@ -81,7 +55,7 @@ def parse(builder_klass = ParamBuilder)

self
rescue ConfigurationException => e
@logger.fatal "Unable to continue because of: #{e.message}"
@logger.fatal "Unable to parse #{manifest_path} because of: #{e.message}"
KafoConfigure.exit(:manifest_error)
end

Expand Down Expand Up @@ -128,24 +102,24 @@ def mapping

# custom module directory name
def get_dir_name
mapping[identifier].nil? ? default_dir_name : (mapping[identifier][:dir_name] || default_dir_name)
mapping.dig(identifier, :dir_name) || default_dir_name
end

# custom manifest filename without .pp extension
def get_manifest_name
mapping[identifier].nil? ? default_manifest_name : (mapping[identifier][:manifest_name] || default_manifest_name)
mapping.dig(identifier, :manifest_name) || default_manifest_name
end

def get_class_name
manifest_name == 'init' ? name : "#{dir_name}::#{manifest_name.gsub('/', '::')}"
end

def get_params_path
mapping[identifier].nil? ? default_params_path : (mapping[identifier][:params_path] || default_params_path)
mapping.dig(identifier, :params_path) || default_params_path
end

def get_params_name
mapping[identifier].nil? ? default_params_name : (mapping[identifier][:params_name] || default_params_name)
mapping.dig(identifier, :params_name) || default_params_name
end

def get_params_class_name
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic_answers.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
puppet: true
testing: true
40 changes: 40 additions & 0 deletions test/kafo/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,46 @@ module Kafo
end
end
end

describe '#parse' do
describe "with parser cache" do
let(:module_name) { 'testing' }
let(:parser_cache_factory) do
ParserCacheFactory.build({:files => {module_name => {:data => {:parameters => [], :groups => []}}}})
end
before do
config.app[:parser_cache_path] = parser_cache_factory.path
end
subject { config.parse(module_name, '/path/to/manifest' }

specify { _(subject.raw_data[:parameters]).must_equal [] }
specify { _(subject.raw_data[:groups]).must_equal [] }
end
end

describe '#parser' do
subject { config.send(:parser) }

describe "with nil parser and no cache" do
before { config.instance_variable_set(:@parser, nil) }

it do
KafoParsers::Parsers.stub(:find_available, nil) do
_(subject).must_raise ParserError
end
end
end

describe "with :none parser cached" do
before { config.instance_variable_set(:@parser, :none) }

specify do
KafoParsers::Parsers.stub(:find_available, 'something') do
_(config.parser).must_raise ParserError
end
end
end
end
end
end
end
6 changes: 4 additions & 2 deletions test/kafo/parser_cache_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ module Kafo

describe "compatibility with writer" do
before do
KafoConfigure.config = Configuration.new(ConfigFileFactory.build('basic', BASIC_CONFIGURATION).path)
KafoConfigure.config = config
config.stub(:parser, parser)
end

let(:config) { Configuration.new(ConfigFileFactory.build('basic', BASIC_CONFIGURATION).path) }
let(:parser) { TestParser.new(BASIC_MANIFEST) }
let(:mod) { PuppetModule.new('puppet', parser: parser) }
let(:mod) { PuppetModule.new('puppet') }
let(:writer) { ParserCacheWriter.write([mod]) }
let(:cache) { ParserCacheFactory.build(writer) }

Expand Down
Loading