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
- #pem ⇒ Object
- #puppetlabs_root ⇒ Object
- #server_urls ⇒ Object
- #token ⇒ Object
- #user_conf ⇒ Object
- #user_root ⇒ Object
- #windows? ⇒ Boolean
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 |
#config ⇒ Object
76 77 78 |
# File 'lib/puppetdb/config.rb', line 76 def config @config ||= load_config end |
#default_cacert ⇒ Object
48 49 50 |
# File 'lib/puppetdb/config.rb', line 48 def default_cacert File.join(puppetlabs_root, 'puppet', 'ssl', 'certs', 'ca.pem') end |
#defaults ⇒ Object
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_conf ⇒ Object
36 37 38 |
# File 'lib/puppetdb/config.rb', line 36 def global_conf File.join(puppetlabs_root, 'client-tools', 'puppetdb.conf') end |
#load_config ⇒ Object
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_token ⇒ Object
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 |
#pem ⇒ Object
97 98 99 |
# File 'lib/puppetdb/config.rb', line 97 def pem @config.select { |k, _| [:cacert, :cert, :key].include?(k) } end |
#puppetlabs_root ⇒ Object
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_urls ⇒ Object
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 |
#token ⇒ Object
88 89 90 |
# File 'lib/puppetdb/config.rb', line 88 def token @token ||= load_token end |
#user_conf ⇒ Object
44 45 46 |
# File 'lib/puppetdb/config.rb', line 44 def user_conf File.join(user_root, 'client-tools', 'puppetdb.conf') end |
#user_root ⇒ Object
40 41 42 |
# File 'lib/puppetdb/config.rb', line 40 def user_root File.join(Dir.home, '.puppetlabs') end |
#windows? ⇒ 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 |