lib/kafo/configuration.rb in kafo-0.0.6 vs lib/kafo/configuration.rb in kafo-0.0.7

- old
+ new

@@ -1,71 +1,74 @@ require 'yaml' require 'kafo/puppet_module' require 'kafo/password_manager' class Configuration - attr_reader :config_file + attr_reader :config_file, :answer_file - def self.application_config_file - File.join(Dir.pwd, 'config/kafo.yaml') - end + DEFAULT = { + :log_dir => '/var/log/kafo', + :log_level => :info, + :no_prefix => false, + :mapping => {}, + :answer_file => '/etc/kafo/kafo.yaml', + :installer_dir => '.', + :default_values_dir => '/tmp' + } - def self.save_configuration(configuration) - File.open(application_config_file, 'w') { |file| file.write(YAML.dump(configuration)) } - end + def initialize(file) + @config_file = file + configure_application + @logger = Logging.logger.root - def self.configure_application + @answer_file = app[:answer_file] begin - configuration = YAML.load_file(application_config_file) - rescue => e - configuration = {} + @data = YAML.load_file(@answer_file) + rescue Errno::ENOENT => e + puts "No answers file at #{@answer_file} found, can not continue" + exit(:no_answer_file) end - default = { - :log_dir => '/var/log/kafo', - :log_level => :info, - :no_prefix => false, - :mapping => {} - } - - result = default.merge(configuration || {}) - result[:password] ||= PasswordManager.new.password - save_configuration(result) + @config_dir = File.dirname(@config_file) + end + def save_configuration(configuration) + File.open(@config_file, 'w') { |file| file.write(YAML.dump(configuration)) } + end + + def configure_application + result = app + save_configuration(result) result end - KAFO = configure_application + def app + @app ||= begin + begin + configuration = YAML.load_file(@config_file) + rescue => e + configuration = {} + end - def initialize(file) - @logger = Logging.logger.root - @logger.info "Loading config file #{file}" - - begin - @data = YAML.load_file(file) - rescue Errno::ENOENT => e - puts "No answers file at #{file} found, can not continue" - exit(23) + result = DEFAULT.merge(configuration || {}) + result[:password] ||= PasswordManager.new.password + result end - - @config_file = file - @config_dir = File.join(Dir.pwd, 'config') end def modules @modules ||= @data.keys.map { |mod| PuppetModule.new(mod).parse } end def params_default_values @params_default_values ||= begin @logger.info "Parsing default values from puppet modules..." - # TODO not dry, kafo_configure.rb does similar thing - modules_path = "modules:#{File.join(gem_root_path, 'modules')}" - @logger.debug `echo '#{includes} dump_values(#{params})' | puppet apply --modulepath #{modules_path} 2>&1` + command = PuppetCommand.new("#{includes} dump_values(#{params})").append('2>&1').command + @logger.debug `#{command}` unless $?.exitstatus == 0 @logger.error "Could not get default values, cannot continue" - exit(25) + exit(:default_error) end @logger.info "... finished" YAML.load_file(File.join(@config_dir, 'default_values.yaml')) end end @@ -81,18 +84,19 @@ value = @data[mod.name] !!value || value.is_a?(Hash) end def config_header - @config_header ||= File.read(File.join(gem_root_path, '/config/config_header.txt')) + files = [ app[:config_header_file], File.join(KafoConfigure.gem_root, '/config/config_header.txt') ].compact + file = files.select { |f| File.exists?(f) }.first + @config_header ||= file.nil? ? '' : File.read(file) end - def gem_root_path - @gem_root_path ||= File.join(File.dirname(__FILE__), '../../') - end - - def store(data) - File.open(config_file, 'w') { |file| file.write(config_header + YAML.dump(data)) } + 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 + YAML.dump(data)) } end private def includes