class Kafo::Configuration

Constants

DEFAULT

Attributes

answer_file[R]
config_file[R]

Public Class Methods

colors_possible?() click to toggle source
# File lib/kafo/configuration.rb, line 10
def self.colors_possible?
  !%xwhich tput 2> /dev/null`.empty? && %xtput colors`.to_i > 0
end
new(file, persist = true) click to toggle source
# File lib/kafo/configuration.rb, line 30
def initialize(file, persist = true)
  @config_file = file
  @persist     = persist
  configure_application
  @logger = KafoConfigure.logger

  @answer_file = app[:answer_file]
  begin
    @data = YAML.load_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

Public Instance Methods

[](key) click to toggle source

if a value is a true we return empty hash because we have no specific options for a particular puppet module

# File lib/kafo/configuration.rb, line 120
def [](key)
  value = @data[key]
  value.is_a?(Hash) ? value : {}
end
add_mapping(module_name, mapping) click to toggle source
# File lib/kafo/configuration.rb, line 94
def add_mapping(module_name, mapping)
  app[:mapping][module_name] = mapping
  save_configuration(app)
end
add_module(name) click to toggle source
# File lib/kafo/configuration.rb, line 86
def add_module(name)
  mod = PuppetModule.new(name).parse
  unless modules.map(&:name).include?(mod.name)
    mod.enable
    @modules << mod
  end
end
app() click to toggle source
# File lib/kafo/configuration.rb, line 60
def app
  @app ||= begin
    begin
      configuration = YAML.load_file(@config_file)
    rescue => e
      configuration = {}
    end

    result            = DEFAULT.merge(configuration || {})
    result[:password] ||= PasswordManager.new.password
    result
  end
end
config_header() click to toggle source
# File lib/kafo/configuration.rb, line 130
def config_header
  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
configure_application() click to toggle source
# File lib/kafo/configuration.rb, line 54
def configure_application
  result = app
  save_configuration(result)
  result
end
get_custom(key) click to toggle source
# File lib/kafo/configuration.rb, line 74
def get_custom(key)
  custom_storage[key.to_sym]
end
module_enabled?(mod) click to toggle source
# File lib/kafo/configuration.rb, line 125
def module_enabled?(mod)
  value = @data[mod.is_a?(String) ? mod : mod.identifier]
  !!value || value.is_a?(Hash)
end
modules() click to toggle source
# File lib/kafo/configuration.rb, line 82
def modules
  @modules ||= @data.keys.map { |mod| PuppetModule.new(mod).parse }
end
params_default_values() click to toggle source
# File lib/kafo/configuration.rb, line 99
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 "Parsing default values from puppet modules..."
    command = PuppetCommand.new("$temp_dir=\"#{temp_dir}\" #{includes} dump_values(#{params})").append('2>&1').command
    @logger.debug %x#{command}`
    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 "Could not get default values, cannot continue"
      KafoConfigure.exit(:defaults_error)
    end
    @logger.info "... finished"
    YAML.load_file(File.join(temp_dir, 'default_values.yaml'))
  end
end
save_configuration(configuration) click to toggle source
# File lib/kafo/configuration.rb, line 47
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) click to toggle source
# File lib/kafo/configuration.rb, line 78
def set_custom(key, value)
  custom_storage[key.to_sym] = value
end
store(data, file = nil) click to toggle source
# File lib/kafo/configuration.rb, line 136
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