Class: Bolt::PuppetDB::Config
- Inherits:
-
Object
- Object
- Bolt::PuppetDB::Config
- Defined in:
- lib/bolt/puppetdb/config.rb
Constant Summary collapse
- DEFAULT_TOKEN =
File.('~/.puppetlabs/token')
- DEFAULT_CONFIG =
{ user: File.('~/.puppetlabs/client-tools/puppetdb.conf'), global: '/etc/puppetlabs/client-tools/puppetdb.conf' }.freeze
Class Method Summary collapse
-
.default_config ⇒ Hash
Loads default configuration from the puppetdb.conf file on system.
-
.default_windows_config ⇒ String
Returns the path to the puppetdb.conf file on Windows.
Instance Method Summary collapse
- #cacert ⇒ Object
- #cert ⇒ Object
- #connect_timeout ⇒ Object
- #expand_paths(project_path) ⇒ Object
-
#initialize(config:, project: nil, load_defaults: false) ⇒ Config
constructor
A new instance of Config.
- #key ⇒ Object
- #read_timeout ⇒ Object
- #server_urls ⇒ Object
- #to_hash ⇒ Object
- #token ⇒ Object
- #uri ⇒ Object
- #validate_cert_and_key ⇒ Object
- #validate_file_exists(file) ⇒ Object
- #validate_timeout(timeout) ⇒ Object
Constructor Details
#initialize(config:, project: nil, load_defaults: false) ⇒ Config
Returns a new instance of Config.
20 21 22 23 24 25 26 27 28 |
# File 'lib/bolt/puppetdb/config.rb', line 20 def initialize(config:, project: nil, load_defaults: false) @settings = if load_defaults self.class.default_config.merge(config) else config end (project) end |
Class Method Details
.default_config ⇒ Hash
Loads default configuration from the puppetdb.conf file on system. If the file is not present, defaults to an empty hash.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bolt/puppetdb/config.rb', line 43 def self.default_config config = {} global_path = Bolt::Util.windows? ? default_windows_config : DEFAULT_CONFIG[:global] if File.exist?(DEFAULT_CONFIG[:user]) filepath = DEFAULT_CONFIG[:user] elsif File.exist?(global_path) filepath = global_path end begin config = JSON.parse(File.read(filepath)) if filepath rescue StandardError => e Bolt::Logger.logger(self).error("Could not load puppetdb.conf from #{filepath}: #{e.}") end config.fetch('puppetdb', {}) end |
.default_windows_config ⇒ String
Returns the path to the puppetdb.conf file on Windows.
34 35 36 |
# File 'lib/bolt/puppetdb/config.rb', line 34 def self.default_windows_config File.(File.join(ENV['ALLUSERSPROFILE'], 'PuppetLabs/client-tools/puppetdb.conf')) end |
Instance Method Details
#cacert ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/bolt/puppetdb/config.rb', line 132 def cacert if @settings['cacert'] && validate_file_exists('cacert') @settings['cacert'] else raise Bolt::PuppetDBError, "cacert must be specified" end end |
#cert ⇒ Object
140 141 142 143 144 |
# File 'lib/bolt/puppetdb/config.rb', line 140 def cert validate_cert_and_key validate_file_exists('cert') @settings['cert'] end |
#connect_timeout ⇒ Object
159 160 161 162 |
# File 'lib/bolt/puppetdb/config.rb', line 159 def connect_timeout validate_timeout('connect_timeout') @settings['connect_timeout'] end |
#expand_paths(project_path) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/bolt/puppetdb/config.rb', line 82 def (project_path) %w[cacert cert key token].each do |file| next unless @settings[file] @settings[file] = File.(@settings[file], project_path) end end |
#key ⇒ Object
146 147 148 149 150 |
# File 'lib/bolt/puppetdb/config.rb', line 146 def key validate_cert_and_key validate_file_exists('key') @settings['key'] end |
#read_timeout ⇒ Object
164 165 166 167 |
# File 'lib/bolt/puppetdb/config.rb', line 164 def read_timeout validate_timeout('read_timeout') @settings['read_timeout'] end |
#server_urls ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/bolt/puppetdb/config.rb', line 98 def server_urls case @settings['server_urls'] when String [@settings['server_urls']] when Array @settings['server_urls'] when nil raise Bolt::PuppetDBError, "server_urls must be specified" else raise Bolt::PuppetDBError, "server_urls must be a string or array" end end |
#to_hash ⇒ Object
175 176 177 |
# File 'lib/bolt/puppetdb/config.rb', line 175 def to_hash @settings.dup end |
#token ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bolt/puppetdb/config.rb', line 62 def token return @token if @token_computed # Allow nil in config to skip loading a token if @settings.include?('token') if @settings['token'] @token = File.read(@settings['token']) end elsif File.exist?(DEFAULT_TOKEN) @token = File.read(DEFAULT_TOKEN) end # Only use cert based auth in the case token and cert are both configured if @token && cert Bolt::Logger.logger(self).debug("Both cert and token based auth configured, using cert only") @token = nil end @token_computed = true @token = @token.strip if @token end |
#uri ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/bolt/puppetdb/config.rb', line 111 def uri return @uri if @uri require 'addressable/uri' uri = case @settings['server_urls'] when String @settings['server_urls'] when Array @settings['server_urls'].first when nil raise Bolt::PuppetDBError, "server_urls must be specified" else raise Bolt::PuppetDBError, "server_urls must be a string or array" end @uri = Addressable::URI.parse(uri) @uri.port ||= 8081 @uri end |
#validate_cert_and_key ⇒ Object
152 153 154 155 156 157 |
# File 'lib/bolt/puppetdb/config.rb', line 152 def validate_cert_and_key if (@settings['cert'] && !@settings['key']) || (!@settings['cert'] && @settings['key']) raise Bolt::PuppetDBError, "cert and key must be specified together" end end |
#validate_file_exists(file) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/bolt/puppetdb/config.rb', line 90 def validate_file_exists(file) if @settings[file] && !File.exist?(@settings[file]) raise Bolt::PuppetDBError, "#{file} file #{@settings[file]} does not exist" end true end |
#validate_timeout(timeout) ⇒ Object
169 170 171 172 173 |
# File 'lib/bolt/puppetdb/config.rb', line 169 def validate_timeout(timeout) unless @settings[timeout].nil? || (@settings[timeout].is_a?(Integer) && @settings[timeout] > 0) raise Bolt::PuppetDBError, "#{timeout} must be a positive integer, received #{@settings[timeout]}" end end |