Class: PuppetDB::Config
- Inherits:
-
Object
- Object
- PuppetDB::Config
- Defined in:
- lib/puppetdb/config.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #config ⇒ Object
- #default_cacert ⇒ Object
- #defaults ⇒ Object
- #global_conf ⇒ Object
-
#initialize(overrides = nil, load_files = false) ⇒ Config
constructor
A new instance of Config.
- #load_config ⇒ Object
- #load_file(path) ⇒ Object
- #load_token ⇒ Object
- #puppetlabs_root ⇒ Object
- #server ⇒ Object
- #server_urls ⇒ Object
- #token ⇒ Object
- #user_conf ⇒ Object
- #user_root ⇒ Object
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 |
#config ⇒ Object
59 60 61 |
# File 'lib/puppetdb/config.rb', line 59 def config @config ||= load_config end |
#default_cacert ⇒ Object
31 32 33 |
# File 'lib/puppetdb/config.rb', line 31 def default_cacert "#{puppetlabs_root}/puppet/ssl/certs/ca.pem" end |
#defaults ⇒ Object
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_conf ⇒ Object
19 20 21 |
# File 'lib/puppetdb/config.rb', line 19 def global_conf File.join(puppetlabs_root, 'client-tools', 'puppetdb.conf') end |
#load_config ⇒ Object
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_token ⇒ Object
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_root ⇒ Object
15 16 17 |
# File 'lib/puppetdb/config.rb', line 15 def puppetlabs_root '/etc/puppetlabs' end |
#server ⇒ Object
80 81 82 |
# File 'lib/puppetdb/config.rb', line 80 def server server_urls.first || {} end |
#server_urls ⇒ Object
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 |
#token ⇒ Object
71 72 73 |
# File 'lib/puppetdb/config.rb', line 71 def token @token ||= load_token end |
#user_conf ⇒ Object
27 28 29 |
# File 'lib/puppetdb/config.rb', line 27 def user_conf File.join(user_root, 'client-tools', 'puppetdb.conf') end |
#user_root ⇒ Object
23 24 25 |
# File 'lib/puppetdb/config.rb', line 23 def user_root File.join(Dir.home, '.puppetlabs') end |