Class: Bolt::Config
- Inherits:
-
Struct
- Object
- Struct
- Bolt::Config
- Defined in:
- lib/bolt/config.rb
Constant Summary collapse
- DEFAULTS =
{ concurrency: 100, 'compile-concurrency': Concurrent.processor_count, transport: 'ssh', format: 'human', modulepath: [], puppetdb: {}, color: true }.freeze
- TRANSPORT_OPTIONS =
%i[password run-as sudo-password extensions private-key tty tmpdir user connect-timeout cacert token-file service-url].freeze
- TRANSPORT_DEFAULTS =
{ 'connect-timeout' => 10, 'tty' => false }.freeze
- TRANSPORT_SPECIFIC_DEFAULTS =
{ ssh: { 'host-key-check' => true }, winrm: { 'ssl' => true, 'ssl-verify' => true }, pcp: { 'task-environment' => 'production', 'local-validation' => false }, local: {} }.freeze
- BOLTDIR_NAME =
'Boltdir'
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#concurrency ⇒ Object
Returns the value of attribute concurrency.
-
#format ⇒ Object
Returns the value of attribute format.
-
#inventoryfile ⇒ Object
Returns the value of attribute inventoryfile.
-
#log ⇒ Object
Returns the value of attribute log.
-
#modulepath ⇒ Object
Returns the value of attribute modulepath.
-
#puppetdb ⇒ Object
Returns the value of attribute puppetdb.
-
#trace ⇒ Object
Returns the value of attribute trace.
-
#transport ⇒ Object
Returns the value of attribute transport.
-
#transports ⇒ Object
Returns the value of attribute transports.
Instance Method Summary collapse
- #boltdir ⇒ Object
- #deep_clone ⇒ Object
- #default_boltdir ⇒ Object
- #default_config ⇒ Object
- #default_hiera ⇒ Object
- #default_inventory ⇒ Object
- #default_modulepath ⇒ Object
- #find_boltdir(dir) ⇒ Object
-
#initialize(**kwargs) ⇒ Config
constructor
A new instance of Config.
-
#legacy_conf ⇒ Object
TODO: This is deprecated in 0.21.0 and can be removed in release 0.22.0.
- #load_file(path) ⇒ Object
- #normalize_log(target) ⇒ Object
- #pwd ⇒ Object
- #transport_conf ⇒ Object
-
#update(options) ⇒ Object
The order in which config is processed is important.
- #update_from_cli(options) ⇒ Object
-
#update_from_defaults ⇒ Object
Defaults that do not vary based on boltdir should not be included here.
- #update_from_inventory(data) ⇒ Object
- #validate ⇒ Object
- #validate_hiera_conf(path) ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ Config
Returns a new instance of Config.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bolt/config.rb', line 78 def initialize(**kwargs) super() @logger = Logging.logger[self] @pwd = kwargs.delete(:pwd) DEFAULTS.merge(kwargs).each { |k, v| self[k] = v } # add an entry for the default console logger self[:log] ||= {} self[:log]['console'] ||= {} self[:transports] ||= {} TRANSPORTS.each_key do |transport| self[:transports][transport] ||= {} TRANSPORT_DEFAULTS.each do |k, v| unless self[:transports][transport][k] self[:transports][transport][k] = v end end TRANSPORT_SPECIFIC_DEFAULTS[transport].each do |k, v| unless self[:transports][transport].key? k self[:transports][transport][k] = v end end end end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def color @color end |
#concurrency ⇒ Object
Returns the value of attribute concurrency
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def concurrency @concurrency end |
#format ⇒ Object
Returns the value of attribute format
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def format @format end |
#inventoryfile ⇒ Object
Returns the value of attribute inventoryfile
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def inventoryfile @inventoryfile end |
#log ⇒ Object
Returns the value of attribute log
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def log @log end |
#modulepath ⇒ Object
Returns the value of attribute modulepath
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def modulepath @modulepath end |
#puppetdb ⇒ Object
Returns the value of attribute puppetdb
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def puppetdb @puppetdb end |
#trace ⇒ Object
Returns the value of attribute trace
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def trace @trace end |
#transport ⇒ Object
Returns the value of attribute transport
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def transport @transport end |
#transports ⇒ Object
Returns the value of attribute transports
27 28 29 |
# File 'lib/bolt/config.rb', line 27 def transports @transports end |
Instance Method Details
#boltdir ⇒ Object
168 169 170 |
# File 'lib/bolt/config.rb', line 168 def boltdir @boltdir ||= find_boltdir(pwd) || default_boltdir end |
#deep_clone ⇒ Object
107 108 109 |
# File 'lib/bolt/config.rb', line 107 def deep_clone Bolt::Util.deep_clone(self) end |
#default_boltdir ⇒ Object
172 173 174 |
# File 'lib/bolt/config.rb', line 172 def default_boltdir File.(File.join('~', '.puppetlabs', 'bolt')) end |
#default_config ⇒ Object
195 196 197 198 |
# File 'lib/bolt/config.rb', line 195 def default_config path = File.join(boltdir, 'bolt.yaml') File.exist?(path) ? path : legacy_conf end |
#default_hiera ⇒ Object
204 205 206 |
# File 'lib/bolt/config.rb', line 204 def default_hiera File.join(boltdir, 'hiera.yaml') end |
#default_inventory ⇒ Object
200 201 202 |
# File 'lib/bolt/config.rb', line 200 def default_inventory File.join(boltdir, 'inventory.yaml') end |
#default_modulepath ⇒ Object
176 177 178 |
# File 'lib/bolt/config.rb', line 176 def default_modulepath [File.join(boltdir, "modules")] end |
#find_boltdir(dir) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bolt/config.rb', line 153 def find_boltdir(dir) path = dir boltdir = nil while boltdir.nil? && path && path != File.dirname(path) maybe_boltdir = File.join(path, BOLTDIR_NAME) boltdir = maybe_boltdir if File.directory?(maybe_boltdir) path = File.dirname(path) end boltdir end |
#legacy_conf ⇒ Object
TODO: This is deprecated in 0.21.0 and can be removed in release 0.22.0.
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/bolt/config.rb', line 181 def legacy_conf return @legacy_conf if defined?(@legacy_conf) root_path = File.(File.join('~', '.puppetlabs')) legacy_paths = [File.join(root_path, 'bolt.yaml'), File.join(root_path, 'bolt.yml')] @legacy_conf = legacy_paths.find { |path| File.exist?(path) } @legacy_conf ||= legacy_paths[0] if @legacy_conf correct_path = File.join(default_boltdir, 'bolt.yaml') msg = "Found configfile at deprecated location #{@legacy_conf}. Global config should be in #{correct_path}" @logger.warn(msg) end @legacy_conf end |
#load_file(path) ⇒ Object
257 258 259 260 261 |
# File 'lib/bolt/config.rb', line 257 def load_file(path) data = Bolt::Util.read_config_file(path, [default_config], 'config') update_from_file(data) if data validate_hiera_conf(data ? data['hiera-config'] : nil) end |
#normalize_log(target) ⇒ Object
111 112 113 114 115 |
# File 'lib/bolt/config.rb', line 111 def normalize_log(target) return target if target == 'console' target = target[5..-1] if target.start_with?('file:') 'file:' + File.(target) end |
#pwd ⇒ Object
164 165 166 |
# File 'lib/bolt/config.rb', line 164 def pwd @pwd ||= Dir.pwd end |
#transport_conf ⇒ Object
271 272 273 274 |
# File 'lib/bolt/config.rb', line 271 def transport_conf { transport: self[:transport], transports: self[:transports] } end |
#update(options) ⇒ Object
The order in which config is processed is important
251 252 253 254 255 |
# File 'lib/bolt/config.rb', line 251 def update() update_from_defaults load_file([:configfile]) update_from_cli() end |
#update_from_cli(options) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/bolt/config.rb', line 208 def update_from_cli() %i[concurrency compile-concurrency transport format trace modulepath inventoryfile color].each do |key| self[key] = [key] if .key?(key) end if [:debug] self[:log]['console'][:level] = :debug elsif [:verbose] self[:log]['console'][:level] = :info end TRANSPORTS.each_key do |transport| transport = self[:transports][transport] TRANSPORT_OPTIONS.each do |key| if [key] transport[key.to_s] = Bolt::Util.walk_keys([key], &:to_s) end end end if .key?(:ssl) # this defaults to true so we need to check the presence of the key self[:transports][:winrm]['ssl'] = [:ssl] end if .key?(:'ssl-verify') # this defaults to true so we need to check the presence of the key self[:transports][:winrm]['ssl-verify'] = [:'ssl-verify'] end if .key?(:'host-key-check') # this defaults to true so we need to check the presence of the key self[:transports][:ssh]['host-key-check'] = [:'host-key-check'] end end |
#update_from_defaults ⇒ Object
Defaults that do not vary based on boltdir should not be included here.
Defaults which are treated differently from specified values like ‘inventoryfile’ cannot be included here or they will not be handled correctly.
245 246 247 248 |
# File 'lib/bolt/config.rb', line 245 def update_from_defaults self[:modulepath] = default_modulepath self[:'hiera-config'] = default_hiera end |
#update_from_inventory(data) ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/bolt/config.rb', line 263 def update_from_inventory(data) update_from_file(data) if data['transport'] self[:transport] = data['transport'] end end |
#validate ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/bolt/config.rb', line 280 def validate self[:log].each_pair do |name, params| if params.key?(:level) && !Bolt::Logger.valid_level?(params[:level]) raise Bolt::ValidationError, "level of log #{name} must be one of: #{Bolt::Logger.levels.join(', ')}; received #{params[:level]}" end if params.key?(:append) && params[:append] != true && params[:append] != false raise Bolt::ValidationError, "append flag of log #{name} must be a Boolean, received #{params[:append]}" end end unless self[:concurrency].is_a?(Integer) && self[:concurrency] > 0 raise Bolt::ValidationError, 'Concurrency must be a positive integer' end unless self[:'compile-concurrency'].is_a?(Integer) && self[:'compile-concurrency'] > 0 raise Bolt::ValidationError, 'Compile concurrency must be a positive integer' end compile_limit = 2 * Concurrent.processor_count unless self[:'compile-concurrency'] < compile_limit raise Bolt::ValidationError, "Compilation is CPU-intensive, set concurrency less than #{compile_limit}" end unless %w[human json].include? self[:format] raise Bolt::ValidationError, "Unsupported format: '#{self[:format]}'" end unless self[:transport].nil? || Bolt::TRANSPORTS.include?(self[:transport].to_sym) raise UnknownTransportError, self[:transport] end TRANSPORTS.each do |transport, impl| impl.validate(self[:transports][transport]) end end |
#validate_hiera_conf(path) ⇒ Object
276 277 278 |
# File 'lib/bolt/config.rb', line 276 def validate_hiera_conf(path) Bolt::Util.read_config_file(path, [default_hiera], 'hiera-config') end |