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.



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

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

  @load_files = load_files
end

Instance Method Details

#[](key) ⇒ Object



84
85
86
# File 'lib/puppetdb/config.rb', line 84

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

#configObject



59
60
61
# File 'lib/puppetdb/config.rb', line 59

def config
  @config ||= load_config
end

#default_cacertObject



31
32
33
# File 'lib/puppetdb/config.rb', line 31

def default_cacert
  "#{puppetlabs_root}/puppet/ssl/certs/ca.pem"
end

#defaultsObject



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

def defaults
  {
    'cacert' => default_cacert,
    'token-file' => File.join(user_root, 'token')
  }
end

#global_confObject



19
20
21
# File 'lib/puppetdb/config.rb', line 19

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

#load_configObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppetdb/config.rb', line 42

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



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

def load_file(path)
  File.open(path) { |f| JSON.parse(f.read)['puppetdb'] }
end

#load_tokenObject



63
64
65
66
67
68
69
# File 'lib/puppetdb/config.rb', line 63

def load_token
  if @config.include?('token')
    @config['token']
  elsif File.readable?(config['token-file'])
    File.read(config['token-file']).strip
  end
end

#puppetlabs_rootObject



15
16
17
# File 'lib/puppetdb/config.rb', line 15

def puppetlabs_root
  '/etc/puppetlabs'
end

#serverObject



80
81
82
# File 'lib/puppetdb/config.rb', line 80

def server
  server_urls.first || {}
end

#server_urlsObject



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

def server_urls
  return [config['server']] unless config['server'].nil?
  config['server_urls'] || []
end

#tokenObject



71
72
73
# File 'lib/puppetdb/config.rb', line 71

def token
  @token ||= load_token
end

#user_confObject



27
28
29
# File 'lib/puppetdb/config.rb', line 27

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

#user_rootObject



23
24
25
# File 'lib/puppetdb/config.rb', line 23

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