Class: Kafo::Configuration

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

Constant Summary collapse

DEFAULT =
{
    :name                 => '',
    :description          => '',
    :enabled              => true,
    :log_dir              => '/var/log/kafo',
    :store_dir            => '',
    :log_name             => 'configuration.log',
    :log_level            => 'info',
    :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 => [],
    :verbose_log_level    => 'info'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, persist = true) ⇒ Configuration

Returns a new instance of Configuration.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kafo/configuration.rb', line 35

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

Instance Attribute Details

#answer_fileObject (readonly)

Returns the value of attribute answer_file.



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

def answer_file
  @answer_file
end

#config_fileObject (readonly)

Returns the value of attribute config_file.



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

def config_file
  @config_file
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



169
170
171
172
# File 'lib/kafo/configuration.rb', line 169

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

#add_mapping(module_name, mapping) ⇒ Object



128
129
130
131
# File 'lib/kafo/configuration.rb', line 128

def add_mapping(module_name, mapping)
  app[:mapping][module_name] = mapping
  save_configuration(app)
end

#add_module(name) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/kafo/configuration.rb', line 120

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

#answersObject



254
255
256
# File 'lib/kafo/configuration.rb', line 254

def answers
  @data
end

#appObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kafo/configuration.rb', line 65

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_dirsObject



104
105
106
# File 'lib/kafo/configuration.rb', line 104

def check_dirs
  [app[:check_dirs] || File.join(root_dir, 'checks')].flatten
end

#config_headerObject



179
180
181
182
183
# File 'lib/kafo/configuration.rb', line 179

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_applicationObject



59
60
61
62
63
# File 'lib/kafo/configuration.rb', line 59

def configure_application
  result = app
  save_configuration(result)
  result
end

#gem_rootObject



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

def gem_root
  File.join(File.dirname(__FILE__), '../../')
end

#get_custom(key) ⇒ Object



81
82
83
# File 'lib/kafo/configuration.rb', line 81

def get_custom(key)
  custom_storage[key.to_sym]
end

#kafo_modules_dirObject



116
117
118
# File 'lib/kafo/configuration.rb', line 116

def kafo_modules_dir
  app[:kafo_modules_dir] || (gem_root + '/modules')
end

#log_exists?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/kafo/configuration.rb', line 250

def log_exists?
  log_files.any? { |f| File.size(f) > 0 }
end

#log_fileObject



238
239
240
# File 'lib/kafo/configuration.rb', line 238

def log_file
  File.join(app[:log_dir], app[:log_name])
end

#log_filesObject



246
247
248
# File 'lib/kafo/configuration.rb', line 246

def log_files
  Dir.glob(log_files_pattern)
end

#log_files_patternObject



242
243
244
# File 'lib/kafo/configuration.rb', line 242

def log_files_pattern
  log_file.sub(/(\.log)\Z/i) { |suffix| "{.[0-9]*,}#{suffix}" }
end

#migrate_configuration(from_config, options = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/kafo/configuration.rb', line 133

def migrate_configuration(from_config, options={})
  keys_to_skip = options.fetch(:skip, [])
  keys = [:log_dir, :log_name, :log_level, :no_prefix, :default_values_dir,
    :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_dirObject



271
272
273
# File 'lib/kafo/configuration.rb', line 271

def migrations_dir
  @config_file.gsub(/\.yaml$/, '.migrations')
end

#module(name) ⇒ Object



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

def module(name)
  modules.find { |m| m.name == name }
end

#module_dirsObject



108
109
110
# File 'lib/kafo/configuration.rb', line 108

def module_dirs
  [app[:module_dirs] || (app[:installer_dir] + '/modules')].flatten.map { |dir| File.expand_path(dir) }
end

#module_enabled?(mod) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
# File 'lib/kafo/configuration.rb', line 174

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

#modulesObject



89
90
91
92
93
94
# File 'lib/kafo/configuration.rb', line 89

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) ⇒ Object



196
197
198
199
# File 'lib/kafo/configuration.rb', line 196

def param(mod_name, param_name)
  mod = self.module(mod_name)
  mod.nil? ? nil : mod.params.find { |p| p.name == param_name }
end

#paramsObject



192
193
194
# File 'lib/kafo/configuration.rb', line 192

def params
  @params ||= modules.map(&:params).flatten
end

#params_changed(old_config) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/kafo/configuration.rb', line 221

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_valuesObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/kafo/configuration.rb', line 145

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 'Loading default values from puppet modules...'
    command = PuppetCommand.new("$temp_dir=\"#{temp_dir}\" #{includes} dump_values(#{params_to_dump})", ['--noop', '--reports='], 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"
      @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_file(File.join(temp_dir, 'default_values.yaml'))
  end
end

#params_missing(old_config) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/kafo/configuration.rb', line 230

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_cacheObject



275
276
277
278
279
# File 'lib/kafo/configuration.rb', line 275

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) ⇒ Object



215
216
217
218
219
# File 'lib/kafo/configuration.rb', line 215

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_puppetObject



201
202
203
204
205
206
# File 'lib/kafo/configuration.rb', line 201

def preset_defaults_from_puppet
  # set values based on default_values
  params.each do |param|
    param.set_default(params_default_values)
  end
end

#preset_defaults_from_yamlObject



208
209
210
211
212
213
# File 'lib/kafo/configuration.rb', line 208

def preset_defaults_from_yaml
  # set values based on YAML
  params.each do |param|
    param.set_value_by_config(self)
  end
end

#root_dirObject



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

def root_dir
  File.expand_path(app[:installer_dir])
end

#run_migrationsObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/kafo/configuration.rb', line 258

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) ⇒ Object



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

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) ⇒ Object



85
86
87
# File 'lib/kafo/configuration.rb', line 85

def set_custom(key, value)
  custom_storage[key.to_sym] = value
end

#store(data, file = nil) ⇒ Object



185
186
187
188
189
190
# File 'lib/kafo/configuration.rb', line 185

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