Class: Autosign::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/autosign/config.rb

Overview

Class to manage configuration settings. The purpose of this class is to interact with the configuration file, merge defaults and user-provided settings, and present a configuration hash so that validators and other components do not have to re-implement config file handling.

Instance Method Summary collapse

Constructor Details

#initialize(settings_param = {}) ⇒ Autosign::Config

Create a config instance to interact with configuration settings To specify a configuration file, settings_param should include something like: => ‘/usr/local/etc/autosign.conf’

If no defaults are provided, the class checks several common locations for config file path.

Parameters:

  • settings_param (Hash) (defaults to: {})

    config settings that should override defaults and config file settings



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/autosign/config.rb', line 39

def initialize(settings_param = {})
  # set up logging
  @log = Logging.logger[self.class]
  @log.debug "initializing #{self.class.name}"
  # validate parameter
  raise 'settings is not a hash' unless settings_param.is_a?(Hash)

  # look in the following places for a config file
  @config_file_paths = ['/etc/puppetlabs/puppetserver/autosign.conf', '/etc/autosign.conf', '/usr/local/etc/autosign.conf']

  # HOME is unset when puppet runs, so we need to only use it if it's set
  unless ENV['HOME'].nil?
    @config_file_paths << File.join(Dir.home, '.autosign.conf')
  end

  unless settings_param['config_file'].nil?
    @config_file_paths = [settings_param['config_file']]
  end

  @settings = settings_param
  @log.debug 'Using merged settings hash: ' + @settings.to_s
end

Instance Method Details

#settingsHash

Return a merged settings hash of defaults, config file settings and passed in settings (such as from the CLI)

Returns:

  • (Hash)

    deep merged settings hash



66
67
68
69
70
71
72
# File 'lib/autosign/config.rb', line 66

def settings
  @log.debug 'merging settings'
  setting_sources = [default_settings, configfile, @settings]
  merged_settings = setting_sources.inject({}) { |merged, hash| merged.deep_merge!(hash, {:overwrite_arrays => true}) }
  @log.debug 'using merged settings: ' + merged_settings.to_s
  merged_settings
end