class Kafo::Configuration

Constants

DEFAULT

Attributes

answer_file[R]
config_file[R]

Public Class Methods

new(file, persist = true) click to toggle source
# File lib/kafo/configuration.rb, line 34
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

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 182
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 132
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 124
def add_module(name)
  mod = PuppetModule.new(name, PuppetModule.find_parser, self).parse
  unless modules.map(&:name).include?(mod.name)
    mod.enable
    @modules << mod
  end
end
answers() click to toggle source
# File lib/kafo/configuration.rb, line 267
def answers
  @data
end
app() click to toggle source
# File lib/kafo/configuration.rb, line 69
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() click to toggle source
# File lib/kafo/configuration.rb, line 108
def check_dirs
  [app[:check_dirs] || File.join(root_dir, 'checks')].flatten
end
config_header() click to toggle source
# File lib/kafo/configuration.rb, line 192
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() click to toggle source
# File lib/kafo/configuration.rb, line 63
def configure_application
  result = app
  save_configuration(result)
  result
end
gem_root() click to toggle source
# File lib/kafo/configuration.rb, line 116
def gem_root
  File.join(File.dirname(__FILE__), '../../')
end
get_custom(key) click to toggle source
# File lib/kafo/configuration.rb, line 85
def get_custom(key)
  custom_storage[key.to_sym]
end
kafo_modules_dir() click to toggle source
# File lib/kafo/configuration.rb, line 120
def kafo_modules_dir
  app[:kafo_modules_dir] || (gem_root + '/modules')
end
log_exists?() click to toggle source
# File lib/kafo/configuration.rb, line 263
def log_exists?
  log_files.any? { |f| File.size(f) > 0 }
end
log_file() click to toggle source
# File lib/kafo/configuration.rb, line 251
def log_file
  File.join(app[:log_dir], app[:log_name])
end
log_files() click to toggle source
# File lib/kafo/configuration.rb, line 259
def log_files
  Dir.glob(log_files_pattern)
end
log_files_pattern() click to toggle source
# File lib/kafo/configuration.rb, line 255
def log_files_pattern
  log_file.sub(/(\.log)\Z/) { |suffix| "{.[0-9]*,}#{suffix}" }
end
migrate_configuration(from_config, options={}) click to toggle source
# File lib/kafo/configuration.rb, line 137
def migrate_configuration(from_config, options={})
  keys_to_skip = options.fetch(:skip, [])
  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]
  end
  save_configuration(app)
end
migrations_dir() click to toggle source
# File lib/kafo/configuration.rb, line 284
def migrations_dir
  @config_file.gsub(/\.yaml$/, '.migrations')
end
module(name) click to toggle source
# File lib/kafo/configuration.rb, line 100
def module(name)
  modules.find { |m| m.name == name }
end
module_dirs() click to toggle source
# File lib/kafo/configuration.rb, line 112
def module_dirs
  [app[:module_dirs] || (app[:installer_dir] + '/modules')].flatten.map { |dir| File.expand_path(dir) }
end
module_enabled?(mod) click to toggle source
# File lib/kafo/configuration.rb, line 187
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 93
def modules
  @modules ||= begin
    register_data_types
    @data.keys.map { |mod| PuppetModule.new(mod, PuppetModule.find_parser, self).parse }.sort
  end
end
param(mod_name, param_name) click to toggle source
# File lib/kafo/configuration.rb, line 209
def param(mod_name, param_name)
  mod = self.module(mod_name)
  mod.nil? ? nil : mod.params.find { |p| p.name == param_name }
end
params() click to toggle source
# File lib/kafo/configuration.rb, line 205
def params
  @params ||= modules.map(&:params).flatten
end
params_changed(old_config) click to toggle source
# File lib/kafo/configuration.rb, line 234
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() click to toggle source
# File lib/kafo/configuration.rb, line 149
    def params_default_values
      @params_default_values ||= begin
        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(dump_manifest, [], puppetconf, self).append('2>&1').command
        result = %x#{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_from_output(result.split($/))
      end
    end
params_missing(old_config) click to toggle source
# File lib/kafo/configuration.rb, line 243
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
parser_cache() click to toggle source
# File lib/kafo/configuration.rb, line 288
def parser_cache
  if app[:parser_cache_path]
    @parser_cache ||= Kafo::ParserCacheReader.new_from_file(app[:parser_cache_path])
  end
end
preset_defaults_from_other_config(other_config) click to toggle source
# File lib/kafo/configuration.rb, line 228
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() click to toggle source
# File lib/kafo/configuration.rb, line 214
def preset_defaults_from_puppet
  # set values based on default_values
  params.each do |param|
    param.set_default_from_dump(params_default_values)
  end
end
preset_defaults_from_yaml() click to toggle source
# File lib/kafo/configuration.rb, line 221
def preset_defaults_from_yaml
  # set values based on YAML
  params.each do |param|
    param.set_value_by_config(self)
  end
end
root_dir() click to toggle source
# File lib/kafo/configuration.rb, line 104
def root_dir
  File.expand_path(app[:installer_dir])
end
run_migrations() click to toggle source
# File lib/kafo/configuration.rb, line 271
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
  migrations.migrations.count
end
save_configuration(configuration) click to toggle source
# File lib/kafo/configuration.rb, line 51
def save_configuration(configuration)
  return true unless @persist
  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
set_custom(key, value) click to toggle source
# File lib/kafo/configuration.rb, line 89
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 198
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

Private Instance Methods

custom_storage() click to toggle source
# File lib/kafo/configuration.rb, line 296
def custom_storage
  app[:custom]
end
format(data) click to toggle source
# File lib/kafo/configuration.rb, line 319
def format(data)
  data.gsub('!ruby/sym ', ':')
end
includes() click to toggle source
# File lib/kafo/configuration.rb, line 300
def includes
  modules.map do |mod|
    module_dir = module_dirs.find do |dir|
      params_file = File.join(dir, mod.params_path)
      @logger.debug "checking presence of #{params_file}"
      File.exist?(params_file)
    end
    module_dir ? "include #{mod.dir_name}::#{mod.params_class_name}" : nil
  end.uniq.compact.join(' ')
end
load_yaml_file(filename) click to toggle source
# File lib/kafo/configuration.rb, line 323
def load_yaml_file(filename)
  YAML.load_file(filename)
end
load_yaml_from_output(lines) click to toggle source

Loads YAML from mixed output, finding the “—” and “…” document start/end delimiters

# File lib/kafo/configuration.rb, line 328
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
param_lookups_to_dump() click to toggle source
# File lib/kafo/configuration.rb, line 315
def param_lookups_to_dump
  params.select { |p| p.manifest_default.nil? }.map { |p| %Q{"#{p.identifier}"} }.join(',')
end
params_to_dump() click to toggle source
# File lib/kafo/configuration.rb, line 311
def params_to_dump
  params.select(&:dump_default_needed?).map(&:dump_default).join(',')
end
register_data_types() click to toggle source
# File lib/kafo/configuration.rb, line 339
def register_data_types
  module_dirs.each do |module_dir|
    Dir[File.join(module_dir, '*', 'types', '**', '*.pp')].each do |type_file|
      DataTypeParser.new(File.read(type_file)).register
    end
  end
end