lib/kafo/configuration.rb in kafo-1.0.6 vs lib/kafo/configuration.rb in kafo-1.0.7

- old
+ new

@@ -22,11 +22,10 @@ :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 => [], @@ -50,13 +49,18 @@ @config_dir = File.dirname(@config_file) end 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))) } + begin + FileUtils.touch @config_file + File.chmod 0600, @config_file + File.open(@config_file, 'w') { |file| file.write(format(YAML.dump(configuration))) } + rescue Errno::EACCES + puts "Insufficient permissions to write to #{@config_file}, can not continue" + KafoConfigure.exit(:insufficient_permissions) + end end def configure_application result = app save_configuration(result) @@ -131,11 +135,11 @@ save_configuration(app) end def migrate_configuration(from_config, options={}) keys_to_skip = options.fetch(:skip, []) - keys = [:log_dir, :log_name, :log_level, :no_prefix, :default_values_dir, + keys = [:log_dir, :log_name, :log_level, :no_prefix, :colors, :color_of_background, :custom, :password, :verbose_log_level] keys += options.fetch(:with, []) keys.each do |key| next if keys_to_skip.include?(key) app[key] = from_config.app[key] @@ -143,19 +147,23 @@ save_configuration(app) end 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 - puppetconf = PuppetConfigurer.new('noop' => true) KafoConfigure.exit_handler.register_cleanup_path puppetconf.config_path + dump_manifest = <<EOS + #{includes} + class { '::kafo_configure::dump_values': + lookups => [#{param_lookups_to_dump}], + variables => [#{params_to_dump}], + } +EOS + @logger.info 'Loading default values from puppet modules...' - command = PuppetCommand.new("$temp_dir=\"#{temp_dir}\" #{includes} dump_values(#{params_to_dump})", [], puppetconf, self).append('2>&1').command + command = PuppetCommand.new(dump_manifest, [], puppetconf, 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" @@ -163,11 +171,12 @@ @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')) + + load_yaml_from_output(result.split($/)) end end # if a value is a true we return empty hash because we have no specific options for a # particular puppet module @@ -302,15 +311,31 @@ def params_to_dump params.select(&:dump_default_needed?).map(&:dump_default).join(',') end + def param_lookups_to_dump + params.select { |p| p.manifest_default.nil? }.map { |p| %{"#{p.identifier}"} }.join(',') + end + def format(data) data.gsub('!ruby/sym ', ':') end def load_yaml_file(filename) YAML.load_file(filename) + end + + # Loads YAML from mixed output, finding the "---" and "..." document start/end delimiters + def load_yaml_from_output(lines) + start = lines.find_index { |l| l.start_with?('---') } + last = lines[start..-1].find_index("...") + if start.nil? || last.nil? + puts "Could not find default values in output" + @logger.error 'Could not find default values in Puppet output, cannot continue' + KafoConfigure.exit(:defaults_error) + end + YAML.load(lines[start,last].join($/)) end def register_data_types module_dirs.each do |module_dir| Dir[File.join(module_dir, '*', 'types', '**', '*.pp')].each do |type_file|