Class: Kafo::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/configuration.rb

Constant Summary collapse

DEFAULT =
{
    :log_dir             => '/var/log/kafo',
    :log_name            => 'configuration.log',
    :log_level           => 'info',
    :no_prefix           => false,
    :mapping             => {},
    :answer_file         => './config/answers.yaml',
    :installer_dir       => '.',
    :modules_dir         => './modules',
    :default_values_dir  => '/tmp',
    :colors              => Configuration.colors_possible?,
    :color_of_background => :dark,
    :hook_dirs           => []
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, persist = true) ⇒ Configuration

Returns a new instance of Configuration.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kafo/configuration.rb', line 29

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

Instance Attribute Details

#answer_fileObject (readonly)

Returns the value of attribute answer_file.



8
9
10
# File 'lib/kafo/configuration.rb', line 8

def answer_file
  @answer_file
end

#config_fileObject (readonly)

Returns the value of attribute config_file.



8
9
10
# File 'lib/kafo/configuration.rb', line 8

def config_file
  @config_file
end

Class Method Details

.colors_possible?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/kafo/configuration.rb', line 10

def self.colors_possible?
  !`which tput 2> /dev/null`.empty? && `tput colors`.to_i > 0
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



95
96
97
98
# File 'lib/kafo/configuration.rb', line 95

def [](key)
  value = @data[key]
  value.is_a?(Hash) ? value : {}
end

#appObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kafo/configuration.rb', line 59

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_headerObject



105
106
107
108
109
# File 'lib/kafo/configuration.rb', line 105

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_applicationObject



53
54
55
56
57
# File 'lib/kafo/configuration.rb', line 53

def configure_application
  result = app
  save_configuration(result)
  result
end

#module_enabled?(mod) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/kafo/configuration.rb', line 100

def module_enabled?(mod)
  value = @data[mod.is_a?(String) ? mod : mod.identifier]
  !!value || value.is_a?(Hash)
end

#modulesObject



73
74
75
# File 'lib/kafo/configuration.rb', line 73

def modules
  @modules ||= @data.keys.map { |mod| PuppetModule.new(mod).parse }
end

#params_default_valuesObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kafo/configuration.rb', line 77

def params_default_values
  @params_default_values ||= begin
    @logger.info "Parsing default values from puppet modules..."
    command = PuppetCommand.new("#{includes} dump_values(#{params})").append('2>&1').command
    @logger.debug `#{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(KafoConfigure.config.app[:default_values_dir], 'default_values.yaml'))
  end
end

#save_configuration(configuration) ⇒ Object



46
47
48
49
50
51
# File 'lib/kafo/configuration.rb', line 46

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

#store(data, file = nil) ⇒ Object



111
112
113
114
115
116
# File 'lib/kafo/configuration.rb', line 111

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