Class: Kafo::Configuration
- Inherits:
-
Object
- Object
- Kafo::Configuration
- Defined in:
- lib/kafo/configuration.rb
Constant Summary collapse
- DEFAULT =
{ :name => '', :description => '', :log_dir => '/var/log/kafo', :log_name => 'configuration.log', :log_level => 'info', :no_prefix => false, :mapping => {}, :answer_file => './config/answers.yaml', :installer_dir => '.', :module_dirs => ['./modules'], :default_values_dir => '/tmp', :colors => Kafo::ColorScheme.colors_possible?, :color_of_background => :dark, :hook_dirs => [], :custom => {}, :low_priority_modules => [], :verbose_log_level => 'info' }
Instance Attribute Summary collapse
-
#answer_file ⇒ Object
readonly
Returns the value of attribute answer_file.
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
Instance Method Summary collapse
-
#[](key) ⇒ Object
if a value is a true we return empty hash because we have no specific options for a particular puppet module.
- #add_mapping(module_name, mapping) ⇒ Object
- #add_module(name) ⇒ Object
- #answers ⇒ Object
- #app ⇒ Object
- #check_dirs ⇒ Object
- #config_header ⇒ Object
- #configure_application ⇒ Object
- #gem_root ⇒ Object
- #get_custom(key) ⇒ Object
-
#initialize(file, persist = true) ⇒ Configuration
constructor
A new instance of Configuration.
- #kafo_modules_dir ⇒ Object
- #log_exists? ⇒ Boolean
- #log_file ⇒ Object
- #migrate_configuration(from_config, options = {}) ⇒ Object
- #migrations_dir ⇒ Object
- #module_dirs ⇒ Object
- #module_enabled?(mod) ⇒ Boolean
- #modules ⇒ Object
- #param(mod, name) ⇒ Object
- #params ⇒ Object
- #params_changed(old_config) ⇒ Object
- #params_default_values ⇒ Object
- #params_missing(old_config) ⇒ Object
- #preset_defaults_from_other_config(other_config) ⇒ Object
- #preset_defaults_from_puppet ⇒ Object
- #preset_defaults_from_yaml ⇒ Object
- #root_dir ⇒ Object
- #run_migrations ⇒ Object
- #save_configuration(configuration) ⇒ Object
- #set_custom(key, value) ⇒ Object
- #store(data, file = nil) ⇒ Object
- #temp_config_file ⇒ Object
Constructor Details
#initialize(file, persist = true) ⇒ Configuration
Returns a new instance of Configuration.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kafo/configuration.rb', line 32 def initialize(file, persist = true) @config_file = file @persist = persist configure_application @logger = KafoConfigure.logger @answer_file = app[:answer_file] begin @data = load_yaml_file(@answer_file) rescue Errno::ENOENT => e puts "No answer file at #{@answer_file} found, can not continue" KafoConfigure.exit(:no_answer_file) end @config_dir = File.dirname(@config_file) end |
Instance Attribute Details
#answer_file ⇒ Object (readonly)
Returns the value of attribute answer_file.
10 11 12 |
# File 'lib/kafo/configuration.rb', line 10 def answer_file @answer_file end |
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
10 11 12 |
# File 'lib/kafo/configuration.rb', line 10 def config_file @config_file end |
Instance Method Details
#[](key) ⇒ Object
if a value is a true we return empty hash because we have no specific options for a particular puppet module
159 160 161 162 |
# File 'lib/kafo/configuration.rb', line 159 def [](key) value = @data[key] value.is_a?(Hash) ? value : {} end |
#add_mapping(module_name, mapping) ⇒ Object
118 119 120 121 |
# File 'lib/kafo/configuration.rb', line 118 def add_mapping(module_name, mapping) app[:mapping][module_name] = mapping save_configuration(app) end |
#add_module(name) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/kafo/configuration.rb', line 110 def add_module(name) mod = PuppetModule.new(name, KafoParsers::PuppetModuleParser, self).parse unless modules.map(&:name).include?(mod.name) mod.enable @modules << mod end end |
#answers ⇒ Object
239 240 241 |
# File 'lib/kafo/configuration.rb', line 239 def answers @data end |
#app ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kafo/configuration.rb', line 62 def app @app ||= begin begin configuration = load_yaml_file(@config_file) rescue => e configuration = {} end result = DEFAULT.merge(configuration || {}) result[:password] ||= PasswordManager.new.password result[:module_dirs] = result[:modules_dir] || result[:module_dirs] result.delete(:modules_dir) result end end |
#check_dirs ⇒ Object
94 95 96 |
# File 'lib/kafo/configuration.rb', line 94 def check_dirs [app[:check_dirs] || File.join(root_dir, 'checks')].flatten end |
#config_header ⇒ Object
169 170 171 172 173 |
# File 'lib/kafo/configuration.rb', line 169 def config_header files = [app[:config_header_file], File.join(gem_root, '/config/config_header.txt')].compact file = files.select { |f| File.exists?(f) }.first @config_header ||= file.nil? ? '' : File.read(file) end |
#configure_application ⇒ Object
56 57 58 59 60 |
# File 'lib/kafo/configuration.rb', line 56 def configure_application result = app save_configuration(result) result end |
#gem_root ⇒ Object
102 103 104 |
# File 'lib/kafo/configuration.rb', line 102 def gem_root File.join(File.dirname(__FILE__), '../../') end |
#get_custom(key) ⇒ Object
78 79 80 |
# File 'lib/kafo/configuration.rb', line 78 def get_custom(key) custom_storage[key.to_sym] end |
#kafo_modules_dir ⇒ Object
106 107 108 |
# File 'lib/kafo/configuration.rb', line 106 def kafo_modules_dir app[:kafo_modules_dir] || (gem_root + '/modules') end |
#log_exists? ⇒ Boolean
235 236 237 |
# File 'lib/kafo/configuration.rb', line 235 def log_exists? File.exists?(log_file) && File.size(log_file) > 0 end |
#log_file ⇒ Object
231 232 233 |
# File 'lib/kafo/configuration.rb', line 231 def log_file File.join(app[:log_dir], app[:log_name]) end |
#migrate_configuration(from_config, options = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/kafo/configuration.rb', line 123 def migrate_configuration(from_config, ={}) keys_to_skip = .fetch(:skip, []) keys = [:log_dir, :log_name, :log_level, :no_prefix, :default_values_dir, :colors, :color_of_background, :custom, :password, :verbose_log_level] keys += .fetch(:with, []) keys.each do |key| next if keys_to_skip.include?(key) app[key] = from_config.app[key] end save_configuration(app) end |
#migrations_dir ⇒ Object
255 256 257 |
# File 'lib/kafo/configuration.rb', line 255 def migrations_dir @config_file.gsub(/\.yaml$/, '.migrations') end |
#module_dirs ⇒ Object
98 99 100 |
# File 'lib/kafo/configuration.rb', line 98 def module_dirs [app[:module_dirs] || (app[:installer_dir] + '/modules')].flatten.map { |dir| File.(dir) } end |
#module_enabled?(mod) ⇒ Boolean
164 165 166 167 |
# File 'lib/kafo/configuration.rb', line 164 def module_enabled?(mod) value = @data[mod.is_a?(String) ? mod : mod.identifier] !!value || value.is_a?(Hash) end |
#modules ⇒ Object
86 87 88 |
# File 'lib/kafo/configuration.rb', line 86 def modules @modules ||= @data.keys.map { |mod| PuppetModule.new(mod, KafoParsers::PuppetModuleParser, self).parse }.sort end |
#param(mod, name) ⇒ Object
186 187 188 |
# File 'lib/kafo/configuration.rb', line 186 def param(mod, name) params.detect { |p| p.name == name && p.module.name == mod } end |
#params ⇒ Object
182 183 184 |
# File 'lib/kafo/configuration.rb', line 182 def params @params ||= modules.map(&:params).flatten end |
#params_changed(old_config) ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/kafo/configuration.rb', line 210 def params_changed(old_config) # finds params that had different value in the old config params.select do |par| next unless par.module.enabled? old_param = old_config.param(par.module.class_name, par.name) old_param && old_param.value != par.value end end |
#params_default_values ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/kafo/configuration.rb', line 135 def params_default_values @params_default_values ||= begin @logger.debug "Creating tmp dir within #{app[:default_values_dir]}..." temp_dir = Dir.mktmpdir(nil, app[:default_values_dir]) KafoConfigure.exit_handler.register_cleanup_path temp_dir @logger.info 'Loading default values from puppet modules...' command = PuppetCommand.new("$temp_dir=\"#{temp_dir}\" #{includes} dump_values(#{params_to_dump})", ['--noop'], self).append('2>&1').command result = `#{command}` @logger.debug result unless $?.exitstatus == 0 log = app[:log_dir] + '/' + app[:log_name] puts "Could not get default values, check log file at #{log} for more information" @logger.error command @logger.error result @logger.error 'Could not get default values, cannot continue' KafoConfigure.exit(:defaults_error) end @logger.info "... finished" load_yaml_file(File.join(temp_dir, 'default_values.yaml')) end end |
#params_missing(old_config) ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/kafo/configuration.rb', line 219 def params_missing(old_config) # finds params that are present but will be missing in the new config old_config.params.select do |par| next if !par.module.enabled? || !module_enabled?(par.module.name) param(par.module.class_name, par.name).nil? end end |
#preset_defaults_from_other_config(other_config) ⇒ Object
204 205 206 207 208 |
# File 'lib/kafo/configuration.rb', line 204 def preset_defaults_from_other_config(other_config) params_changed(other_config).each do |par| param(par.module.class_name, par.name).value = other_config.param(par.module.class_name, par.name).value end end |
#preset_defaults_from_puppet ⇒ Object
190 191 192 193 194 195 |
# File 'lib/kafo/configuration.rb', line 190 def preset_defaults_from_puppet # set values based on default_values params.each do |param| param.set_default(params_default_values) end end |
#preset_defaults_from_yaml ⇒ Object
197 198 199 200 201 202 |
# File 'lib/kafo/configuration.rb', line 197 def preset_defaults_from_yaml # set values based on YAML params.each do |param| param.set_value_by_config(self) end end |
#root_dir ⇒ Object
90 91 92 |
# File 'lib/kafo/configuration.rb', line 90 def root_dir File.(app[:installer_dir]) end |
#run_migrations ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/kafo/configuration.rb', line 243 def run_migrations migrations = Kafo::Migrations.new(migrations_dir) @app, @data = migrations.run(app, answers) if migrations.migrations.count > 0 @modules = nil # force the lazy loaded modules to reload next time they are used save_configuration(app) store(answers) migrations.store_applied @logger.info("#{migrations.migrations.count} migration/s were applied. Updated configuration was saved.") end end |
#save_configuration(configuration) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/kafo/configuration.rb', line 49 def save_configuration(configuration) return true unless @persist FileUtils.touch @config_file File.chmod 0600, @config_file File.open(@config_file, 'w') { |file| file.write(format(YAML.dump(configuration))) } end |
#set_custom(key, value) ⇒ Object
82 83 84 |
# File 'lib/kafo/configuration.rb', line 82 def set_custom(key, value) custom_storage[key.to_sym] = value end |
#store(data, file = nil) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/kafo/configuration.rb', line 175 def store(data, file = nil) filename = file || answer_file FileUtils.touch filename File.chmod 0600, filename File.open(filename, 'w') { |file| file.write(config_header + format(YAML.dump(data))) } end |
#temp_config_file ⇒ Object
227 228 229 |
# File 'lib/kafo/configuration.rb', line 227 def temp_config_file @temp_config_file ||= "/tmp/kafo_answers_#{rand(1_000_000)}.yaml" end |