Class: PuppetDB::Config

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

Instance Method Summary collapse

Constructor Details

#initialize(overrides = nil, load_files = false) ⇒ Config

Returns a new instance of Config.



5
6
7
8
9
10
# File 'lib/puppetdb/config.rb', line 5

def initialize(overrides = nil, load_files = false)
  @overrides = {}
  overrides.each { |k, v| @overrides[k.to_sym] = v } unless overrides.nil?

  @load_files = load_files
end

Instance Method Details

#[](key) ⇒ Object



101
102
103
# File 'lib/puppetdb/config.rb', line 101

def [](key)
  @config[key]
end

#configObject



76
77
78
# File 'lib/puppetdb/config.rb', line 76

def config
  @config ||= load_config
end

#default_cacertObject



48
49
50
# File 'lib/puppetdb/config.rb', line 48

def default_cacert
  File.join(puppetlabs_root, 'puppet', 'ssl', 'certs', 'ca.pem')
end

#defaultsObject



52
53
54
55
56
57
# File 'lib/puppetdb/config.rb', line 52

def defaults
  {
    cacert: default_cacert,
    token_file: File.join(user_root, 'token')
  }
end

#global_confObject



36
37
38
# File 'lib/puppetdb/config.rb', line 36

def global_conf
  File.join(puppetlabs_root, 'client-tools', 'puppetdb.conf')
end

#load_configObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppetdb/config.rb', line 59

def load_config
  config = defaults
  if @load_files
    if File.exist?(global_conf) && File.readable?(global_conf)
      config = config.merge(load_file(global_conf))
    end

    if @overrides[:config_file]
      config = config.merge(load_file(@overrides[:config_file]))
    elsif File.exist?(user_conf) && File.readable?(user_conf)
      config = config.merge(load_file(user_conf))
    end
  end

  config.merge(@overrides)
end

#load_file(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/puppetdb/config.rb', line 17

def load_file(path)
  loaded_conf = File.open(path) { |f| JSON.parse(f.read, symbolize_names: true)[:puppetdb] }

  # Munge token-file into token_file for better readability in Ruby
  if loaded_conf[:'token-file']
    loaded_conf[:token_file] = loaded_conf.delete(:'token-file')
  end

  loaded_conf
end

#load_tokenObject



80
81
82
83
84
85
86
# File 'lib/puppetdb/config.rb', line 80

def load_token
  if @config.include?(:token)
    @config[:token]
  elsif File.readable?(config[:token_file])
    File.read(config[:token_file]).strip
  end
end

#pemObject



97
98
99
# File 'lib/puppetdb/config.rb', line 97

def pem
  @config.select { |k, _| [:cacert, :cert, :key].include?(k) }
end

#puppetlabs_rootObject



28
29
30
31
32
33
34
# File 'lib/puppetdb/config.rb', line 28

def puppetlabs_root
  if windows?
    'C:\ProgramData\PuppetLabs'
  else
    '/etc/puppetlabs'
  end
end

#server_urlsObject



92
93
94
95
# File 'lib/puppetdb/config.rb', line 92

def server_urls
  return config[:server_urls].split(',') if config[:server_urls].is_a?(String)
  config[:server_urls] || []
end

#tokenObject



88
89
90
# File 'lib/puppetdb/config.rb', line 88

def token
  @token ||= load_token
end

#user_confObject



44
45
46
# File 'lib/puppetdb/config.rb', line 44

def user_conf
  File.join(user_root, 'client-tools', 'puppetdb.conf')
end

#user_rootObject



40
41
42
# File 'lib/puppetdb/config.rb', line 40

def user_root
  File.join(Dir.home, '.puppetlabs')
end

#windows?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/puppetdb/config.rb', line 12

def windows?
  @is_windows ||= (RbConfig::CONFIG['host_os'] =~ %r{mswin|mingw|cygwin})
  @is_windows
end