diff --git a/lib/kafo/configuration.rb b/lib/kafo/configuration.rb index d873b7bb..0052aae3 100644 --- a/lib/kafo/configuration.rb +++ b/lib/kafo/configuration.rb @@ -12,37 +12,42 @@ class Configuration attr_reader :config_file, :answer_file, :scenario_id DEFAULT = { - ScenarioOption::NAME => '', - ScenarioOption::DESCRIPTION => '', - ScenarioOption::ENABLED => true, - ScenarioOption::LOG_DIR => '/var/log/kafo', - ScenarioOption::LOG_OWNER => nil, - ScenarioOption::LOG_GROUP => nil, - ScenarioOption::STORE_DIR => '', - ScenarioOption::LOG_NAME => 'configuration.log', - ScenarioOption::LOG_LEVEL => 'notice', - ScenarioOption::NO_PREFIX => false, - ScenarioOption::MAPPING => {}, - ScenarioOption::ANSWER_FILE => './config/answers.yaml', - ScenarioOption::INSTALLER_DIR => '.', - ScenarioOption::MODULE_DIRS => ['./modules'], - ScenarioOption::COLORS => Kafo::ColorScheme.colors_possible?, - ScenarioOption::COLOR_OF_BACKGROUND => :dark, - ScenarioOption::HOOK_DIRS => [], - ScenarioOption::CHECK_DIRS => nil, - ScenarioOption::CUSTOM => {}, - ScenarioOption::FACTS => {}, - ScenarioOption::LOW_PRIORITY_MODULES => [], - ScenarioOption::VERBOSE => false, - ScenarioOption::VERBOSE_LOG_LEVEL => 'notice', - ScenarioOption::SKIP_PUPPET_VERSION_CHECK => false, - ScenarioOption::PARSER_CACHE_PATH => nil, - ScenarioOption::IGNORE_UNDOCUMENTED => nil, - ScenarioOption::ORDER => nil, - ScenarioOption::HIERA_CONFIG => nil, - ScenarioOption::KAFO_MODULES_DIR => nil, - ScenarioOption::CONFIG_HEADER_FILE => nil, - ScenarioOption::DONT_SAVE_ANSWERS => nil, + ScenarioOption::NAME => '', + ScenarioOption::DESCRIPTION => '', + ScenarioOption::ENABLED => true, + ScenarioOption::LOG_DIR => '/var/log/kafo', + ScenarioOption::LOG_OWNER => nil, + ScenarioOption::LOG_GROUP => nil, + ScenarioOption::STORE_DIR => '', + ScenarioOption::LOG_NAME => 'configuration.log', + ScenarioOption::LOG_LEVEL => 'notice', + ScenarioOption::NO_PREFIX => false, + ScenarioOption::MAPPING => {}, + ScenarioOption::ANSWER_FILE => './config/answers.yaml', + ScenarioOption::INSTALLER_DIR => '.', + ScenarioOption::MODULE_DIRS => ['./modules'], + ScenarioOption::COLORS => Kafo::ColorScheme.colors_possible?, + ScenarioOption::COLOR_OF_BACKGROUND => :dark, + ScenarioOption::HOOK_DIRS => [], + ScenarioOption::CHECK_DIRS => nil, + ScenarioOption::CUSTOM => {}, + ScenarioOption::FACTS => {}, + ScenarioOption::LOW_PRIORITY_MODULES => [], + ScenarioOption::VERBOSE => false, + ScenarioOption::VERBOSE_LOG_LEVEL => 'notice', + ScenarioOption::SKIP_PUPPET_VERSION_CHECK => false, + ScenarioOption::PARSER_CACHE_PATH => nil, + ScenarioOption::IGNORE_UNDOCUMENTED => nil, + ScenarioOption::ORDER => nil, + ScenarioOption::HIERA_CONFIG => nil, + ScenarioOption::KAFO_MODULES_DIR => nil, + ScenarioOption::CONFIG_HEADER_FILE => nil, + ScenarioOption::DONT_SAVE_ANSWERS => nil, + ScenarioOption::PARAMS_FILES => { + ScenarioOption::DEFAULT_PARAMS_FILE => nil, + ScenarioOption::USER_PARAMS_FILE => nil, + ScenarioOption::SCENARIO_DEFAULT_PARAMS_FILE => nil, + }, } def self.get_scenario_id(filename) @@ -320,6 +325,42 @@ def answers @data end + def params_files + app[ScenarioOption::PARAMS_FILES] + end + + def default_params_file + params_files[ScenarioOption::DEFAULT_PARAMS_FILE] + end + + def scenario_params_file + params_files[ScenarioOption::SCENARIO_DEFAULT_PARAMS_FILE] + end + + def user_params_file + params_files[ScenarioOption::USER_PARAMS_FILE] + end + + def scenario_params + return unless scenario_params_file + + begin + load_yaml_file(scenario_params_file) + rescue Errno::ENOENT + {} + end + end + + def user_params + return unless user_params_file + + begin + load_yaml_file(user_params_file) + rescue Errno::ENOENT + {} + end + end + def run_migrations migrations = Kafo::Migrations.new(migrations_dir) @app, @data = migrations.run(app, answers) diff --git a/lib/kafo/kafo_configure.rb b/lib/kafo/kafo_configure.rb index 41389c75..fbe58dad 100644 --- a/lib/kafo/kafo_configure.rb +++ b/lib/kafo/kafo_configure.rb @@ -155,9 +155,45 @@ def initialize(*args) self.class.hooking.execute(:init) set_parameters # here the params gets parsed and we need app config populated + migrate_answers_to_user_params_file set_options end + def migrate_answers_to_user_params_file + return unless config.app[:answer_file] + return if config.user_params_file.nil? + return if File.exist?(config.user_params_file) + + enabled_modules = config.modules.select(&:enabled?) + default_params = Hash[enabled_modules.map { |mod| [mod.identifier, mod.default_params_hash] }] + default_params = default_params.reject { |key, value| !value.is_a?(Hash) } + + user_params = Hash[config.modules.map { |mod| [mod.identifier, {}] }] + + config.answers.each do |puppet_class, values| + if values.is_a?(Hash) + values.each do |param, value| + user_params[puppet_class][param] = value unless default_params[puppet_class][param] == value + end + else + user_params[puppet_class] = values unless default_params[puppet_class] == values + end + end + + config.scenario_params.each do |puppet_class, values| + if values.is_a?(Hash) + values.each do |param, value| + user_params[puppet_class].delete(param) if user_params[puppet_class][param] == value + end + else + user_params.delete(puppet_class) if user_params[puppet_class] == values + end + end + + config.store(user_params, config.user_params_file) + config.app[:answer_file].delete + end + def config self.class.config end @@ -210,6 +246,8 @@ def execute self.class.hooking.execute(:pre_commit) unless dont_save_answers? || noop? config.configure_application + store_default_params + store_user_params store_params self.class.scenario_manager.link_last_scenario(self.class.config_file) if self.class.scenario_manager.configured? end @@ -253,8 +291,8 @@ def modules config.modules.sort end - def module(name) - modules.detect { |m| m.name == name } + def module(identifier) + modules.detect { |m| m.identifier == identifier } end def param(mod, name) @@ -457,6 +495,7 @@ def parse_cli_arguments self.class.exit(:missing_argument) end param.value = cli_value unless cli_value.nil? + param.user_value = cli_value unless cli_value.nil? end end @@ -466,7 +505,33 @@ def argument_missing?(value) def store_params(file = nil) data = Hash[config.modules.map { |mod| [mod.identifier, mod.enabled? ? mod.params_hash : false] }] - config.store(data, file) + config.store(data, config.answer_file) + end + + def store_default_params + return unless config.default_params_file + + enabled_modules = config.modules.select(&:enabled?) + data = Hash[enabled_modules.map { |mod| [mod.identifier, mod.default_params_hash] }] + data = data.reject { |mod, values| values.empty? } + config.store(data, config.default_params_file) + end + + def store_user_params + return unless config.user_params_file + user_params = {} + + config.scenario_params.each do |mod_name, params| + mod = self.module(mod_name) + + next if mod.user_params_hash == false && params == false + next if mod.user_params_hash.empty? && params != false + next if mod.user_params_hash == params + + user_params[mod.identifier] = mod.user_params_hash + end + + config.store(user_params, config.user_params_file) end def validate_all(logging = true) diff --git a/lib/kafo/param.rb b/lib/kafo/param.rb index 32d1a5f5..2ee526ff 100644 --- a/lib/kafo/param.rb +++ b/lib/kafo/param.rb @@ -7,7 +7,7 @@ class Param UNSET_VALUES = ['UNSET', :undef] attr_reader :name, :module, :type, :manifest_default - attr_accessor :doc, :value_set, :condition + attr_accessor :doc, :value_set, :condition, :user_value attr_writer :groups def initialize(builder, name, type) diff --git a/lib/kafo/puppet_module.rb b/lib/kafo/puppet_module.rb index c51d15b1..a588b5ab 100644 --- a/lib/kafo/puppet_module.rb +++ b/lib/kafo/puppet_module.rb @@ -97,6 +97,16 @@ def params_hash Hash[params.map { |param| [param.name, param.value] }] end + def default_params_hash + Hash[params.map { |param| [param.name, param.default] }] + end + + def user_params_hash + return false unless enabled? + user_params = params.reject { |param| param.user_value.nil? } + Hash[user_params.map { |param| [param.name, param.user_value] }] + end + def <=>(other) @configuration.app[:low_priority_modules].each do |module_name| return 1 if self.name.include?(module_name) && !other.name.include?(module_name) diff --git a/lib/kafo/scenario_option.rb b/lib/kafo/scenario_option.rb index b399d683..515f7c9d 100644 --- a/lib/kafo/scenario_option.rb +++ b/lib/kafo/scenario_option.rb @@ -12,6 +12,18 @@ class ScenarioOption # Path to answer file, if the file does not exist a $pwd/config/answers.yaml is used as a fallback ANSWER_FILE = :answer_file + # Hash containing different params files and their paths + PARAMS_FILES = :params_files + + # Path to file that will store the default params + DEFAULT_PARAMS_FILE = :default_params_file + + # Path to file that will store the user input params from the CLI + USER_PARAMS_FILE = :user_params_file + + # Path to file that will store the user input params from the CLI + SCENARIO_DEFAULT_PARAMS_FILE = :scenario_default_params_file + # Enable colors? If you don't touch this, we'll autodetect terminal capabilities COLORS = :colors # Color scheme, we support :bright and :dark (first is better for white background, dark for black background)