Class: Bolt::Config
- Inherits:
-
Object
- Object
- Bolt::Config
- Includes:
- Options
- Defined in:
- lib/bolt/config.rb,
lib/bolt/config/options.rb,
lib/bolt/config/modulepath.rb,
lib/bolt/config/transport/lxd.rb,
lib/bolt/config/transport/ssh.rb,
lib/bolt/config/transport/base.rb,
lib/bolt/config/transport/orch.rb,
lib/bolt/config/transport/local.rb,
lib/bolt/config/transport/winrm.rb,
lib/bolt/config/transport/docker.rb,
lib/bolt/config/transport/podman.rb,
lib/bolt/config/transport/remote.rb,
lib/bolt/config/transport/options.rb more...
Defined Under Namespace
Modules: Options, Transport Classes: Modulepath
Constant Summary collapse
- DEFAULTS_NAME =
'bolt-defaults.yaml'
- DEFAULT_DEFAULT_CONCURRENCY =
The default concurrency value that is used when the ulimit is not low (i.e. < 700)
100
Constants included from Options
Options::CLI_OPTIONS, Options::DEFAULTS_OPTIONS, Options::INVENTORY_OPTIONS, Options::OPTIONS, Options::PLUGIN, Options::PROJECT_OPTIONS, Options::TRANSPORT_CONFIG
Instance Attribute Summary collapse
-
#config_files ⇒ Object
readonly
Returns the value of attribute config_files.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#modified_concurrency ⇒ Object
readonly
Returns the value of attribute modified_concurrency.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#transports ⇒ Object
readonly
Returns the value of attribute transports.
Class Method Summary collapse
- .default ⇒ Object
-
.defaults_schema ⇒ Object
Builds the schema for bolt-defaults.yaml used by the validator.
- .from_project(project, overrides = {}) ⇒ Object
-
.load_bolt_defaults_yaml(dir) ⇒ Object
Loads a ‘bolt-defaults.yaml’ file, which contains default configuration that applies to all projects.
- .load_defaults ⇒ Object
- .system_path ⇒ Object
-
.transport_definitions ⇒ Object
Builds a hash of definitions for transport configuration.
- .user_path ⇒ Object
Instance Method Summary collapse
- #analytics ⇒ Object
- #apply_settings ⇒ Object
-
#check_path_case(type, paths) ⇒ Object
Check if there is a case-insensitive match to the path.
- #color ⇒ Object
- #compile_concurrency ⇒ Object
- #concurrency ⇒ Object
- #deep_clone ⇒ Object
- #default_concurrency ⇒ Object
- #default_inventoryfile ⇒ Object
- #disable_warnings ⇒ Object
- #format ⇒ Object
- #format=(value) ⇒ Object
- #future ⇒ Object
- #hiera_config ⇒ Object
-
#initialize(project, config_data, overrides = {}) ⇒ Config
constructor
A new instance of Config.
- #inventoryfile ⇒ Object
- #log ⇒ Object
- #matching_paths(paths) ⇒ Object
-
#merge_config_layers(*config_data) ⇒ Object
Merge configuration from all sources into a single hash.
- #module_install ⇒ Object
- #modulepath ⇒ Object
- #modulepath=(value) ⇒ Object
-
#normalize_overrides(options) ⇒ Object
Transforms CLI options into a config hash that can be merged with default and loaded config.
- #plugin_cache ⇒ Object
- #plugin_hooks ⇒ Object
- #plugins ⇒ Object
- #policies ⇒ Object
- #puppetdb ⇒ Object
- #puppetfile ⇒ Object
- #rerunfile ⇒ Object
- #save_rerun ⇒ Object
-
#sc_open_max_available? ⇒ Boolean
Etc::SC_OPEN_MAX is meaningless on windows, not defined in PE Jruby and not available on some platforms.
- #spinner ⇒ Object
- #stream ⇒ Object
- #trace ⇒ Object
- #transport ⇒ Object
- #trusted_external ⇒ Object
- #validate ⇒ Object
Constructor Details
permalink #initialize(project, config_data, overrides = {}) ⇒ Config
Returns a new instance of Config.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/bolt/config.rb', line 161 def initialize(project, config_data, overrides = {}) unless config_data.is_a?(Array) config_data = [{ filepath: project.project_file, data: config_data }] end @logger = Bolt::Logger.logger(self) @project = project @transports = {} @config_files = [] default_data = { 'analytics' => true, 'apply-settings' => {}, 'color' => true, 'compile-concurrency' => Etc.nprocessors, 'concurrency' => default_concurrency, 'disable-warnings' => [], 'format' => 'human', 'log' => { 'console' => {} }, 'module-install' => {}, 'plugin-hooks' => {}, 'plugins' => {}, 'puppetdb' => {}, 'save-rerun' => true, 'spinner' => true, 'transport' => 'ssh' } if project.path.directory? default_data['log']['bolt-debug.log'] = { 'level' => 'debug', 'append' => false } end loaded_data = config_data.each_with_object([]) do |data, acc| if data[:data].any? @config_files.push(data[:filepath]) acc.push(data[:data]) end end override_data = normalize_overrides(overrides) # If we need to lower concurrency and concurrency is not configured ld_concurrency = loaded_data.map(&:keys).flatten.include?('concurrency') @modified_concurrency = default_concurrency != DEFAULT_DEFAULT_CONCURRENCY && !ld_concurrency && !override_data.key?('concurrency') @data = merge_config_layers(default_data, *loaded_data, override_data) TRANSPORT_CONFIG.each do |transport, config| @transports[transport] = config.new(@data.delete(transport), @project.path) end finalize_data validate end |
Instance Attribute Details
permalink #config_files ⇒ Object (readonly)
Returns the value of attribute config_files.
23 24 25 |
# File 'lib/bolt/config.rb', line 23 def config_files @config_files end |
permalink #data ⇒ Object (readonly)
Returns the value of attribute data.
23 24 25 |
# File 'lib/bolt/config.rb', line 23 def data @data end |
permalink #modified_concurrency ⇒ Object (readonly)
Returns the value of attribute modified_concurrency.
23 24 25 |
# File 'lib/bolt/config.rb', line 23 def modified_concurrency @modified_concurrency end |
permalink #project ⇒ Object (readonly)
Returns the value of attribute project.
23 24 25 |
# File 'lib/bolt/config.rb', line 23 def project @project end |
permalink #transports ⇒ Object (readonly)
Returns the value of attribute transports.
23 24 25 |
# File 'lib/bolt/config.rb', line 23 def transports @transports end |
Class Method Details
permalink .default ⇒ Object
[View source]
30 31 32 |
# File 'lib/bolt/config.rb', line 30 def self.default new(Bolt::Project.default_project, {}) end |
permalink .defaults_schema ⇒ Object
Builds the schema for bolt-defaults.yaml used by the validator.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bolt/config.rb', line 53 def self.defaults_schema schema = { type: Hash, properties: DEFAULTS_OPTIONS.map { |opt| [opt, _ref: opt] }.to_h, definitions: OPTIONS.merge(transport_definitions) } schema[:definitions]['inventory-config'][:properties] = transport_definitions schema end |
permalink .from_project(project, overrides = {}) ⇒ Object
[View source]
34 35 36 37 38 39 40 41 |
# File 'lib/bolt/config.rb', line 34 def self.from_project(project, overrides = {}) data = load_defaults.push( filepath: project.project_file, data: project.data ) new(project, data, overrides) end |
permalink .load_bolt_defaults_yaml(dir) ⇒ Object
Loads a ‘bolt-defaults.yaml’ file, which contains default configuration that applies to all projects. This file does not allow project-specific configuration such as ‘hiera-config’ and nests all default inventory configuration under an ‘inventory-config’ key.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bolt/config.rb', line 82 def self.load_bolt_defaults_yaml(dir) filepath = dir + DEFAULTS_NAME data = Bolt::Util.read_yaml_hash(filepath, 'config') Bolt::Logger.debug("Loaded configuration from #{filepath}") # Validate the config against the schema. This will raise a single error # with all validation errors. Bolt::Validator.new.tap do |validator| validator.validate(data, defaults_schema, filepath) validator.warnings.each { |warning| Bolt::Logger.warn(warning[:id], warning[:msg]) } validator.deprecations.each { |dep| Bolt::Logger.deprecate(dep[:id], dep[:msg]) } end # Remove project-specific config such as hiera-config, etc. project_config = data.slice(*(PROJECT_OPTIONS - DEFAULTS_OPTIONS)) if project_config.any? data.reject! { |key, _| project_config.include?(key) } Bolt::Logger.warn( "unsupported_project_config", "Unsupported project configuration detected in '#{filepath}': #{project_config.keys}. "\ "Project configuration should be set in 'bolt-project.yaml'." ) end # Remove top-level transport config such as transport, ssh, etc. transport_config = data.slice(*INVENTORY_OPTIONS.keys) if transport_config.any? data.reject! { |key, _| transport_config.include?(key) } Bolt::Logger.warn( "unsupported_inventory_config", "Unsupported inventory configuration detected in '#{filepath}': #{transport_config.keys}. "\ "Transport configuration should be set under the 'inventory-config' option or "\ "in 'inventory.yaml'." ) end # Move data under inventory-config to top-level so it can be easily merged with # config from other sources. Error early if inventory-config is not a hash or # has a plugin reference. if data.key?('inventory-config') unless data['inventory-config'].is_a?(Hash) raise Bolt::ValidationError, "Option 'inventory-config' must be of type Hash, received #{data['inventory-config']} "\ "#{data['inventory-config']} (file: #{filepath})" end if data['inventory-config'].key?('_plugin') raise Bolt::ValidationError, "Found unsupported key '_plugin' for option 'inventory-config'; supported keys are "\ "'#{INVENTORY_OPTIONS.keys.join("', '")}' (file: #{filepath})" end data = data.merge(data.delete('inventory-config')) end { filepath: filepath, data: data } end |
permalink .load_defaults ⇒ Object
[View source]
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/bolt/config.rb', line 145 def self.load_defaults confs = [] # Load system-level config. if File.exist?(system_path + DEFAULTS_NAME) confs << load_bolt_defaults_yaml(system_path) end # Load user-level config if there is a homedir. if user_path && File.exist?(user_path + DEFAULTS_NAME) confs << load_bolt_defaults_yaml(user_path) end confs end |
permalink .system_path ⇒ Object
[View source]
65 66 67 68 69 70 71 |
# File 'lib/bolt/config.rb', line 65 def self.system_path if Bolt::Util.windows? Pathname.new(File.join(ENV['ALLUSERSPROFILE'], 'PuppetLabs', 'bolt', 'etc')) else Pathname.new(File.join('/etc', 'puppetlabs', 'bolt')) end end |
permalink .transport_definitions ⇒ Object
Builds a hash of definitions for transport configuration.
45 46 47 48 49 |
# File 'lib/bolt/config.rb', line 45 def self.transport_definitions INVENTORY_OPTIONS.each_with_object({}) do |(option, definition), acc| acc[option] = TRANSPORT_CONFIG.key?(option) ? definition.merge(TRANSPORT_CONFIG[option].schema) : definition end end |
permalink .user_path ⇒ Object
[View source]
73 74 75 76 77 |
# File 'lib/bolt/config.rb', line 73 def self.user_path Pathname.new(File.(File.join('~', '.puppetlabs', 'etc', 'bolt'))) rescue StandardError nil end |
Instance Method Details
permalink #analytics ⇒ Object
[View source]
466 467 468 |
# File 'lib/bolt/config.rb', line 466 def analytics @data['analytics'] end |
permalink #apply_settings ⇒ Object
[View source]
450 451 452 |
# File 'lib/bolt/config.rb', line 450 def apply_settings @data['apply-settings'] end |
permalink #check_path_case(type, paths) ⇒ Object
Check if there is a case-insensitive match to the path
471 472 473 474 475 476 477 478 479 480 |
# File 'lib/bolt/config.rb', line 471 def check_path_case(type, paths) return if paths.nil? matches = matching_paths(paths) if matches.any? msg = "WARNING: Bolt is case sensitive when specifying a #{type}. Did you mean:\n" matches.each { |path| msg += " #{path}\n" } Bolt::Logger.warn("path_case", msg) end end |
permalink #color ⇒ Object
[View source]
410 411 412 |
# File 'lib/bolt/config.rb', line 410 def color @data['color'] end |
permalink #compile_concurrency ⇒ Object
[View source]
430 431 432 |
# File 'lib/bolt/config.rb', line 430 def compile_concurrency @data['compile-concurrency'] end |
permalink #concurrency ⇒ Object
[View source]
382 383 384 |
# File 'lib/bolt/config.rb', line 382 def concurrency @data['concurrency'] end |
permalink #deep_clone ⇒ Object
[View source]
277 278 279 |
# File 'lib/bolt/config.rb', line 277 def deep_clone Bolt::Util.deep_clone(self) end |
permalink #default_concurrency ⇒ Object
[View source]
498 499 500 501 502 503 504 |
# File 'lib/bolt/config.rb', line 498 def default_concurrency @default_concurrency ||= if !sc_open_max_available? || Etc.sysconf(Etc::SC_OPEN_MAX) >= 300 DEFAULT_DEFAULT_CONCURRENCY else Etc.sysconf(Etc::SC_OPEN_MAX) / 7 end end |
permalink #default_inventoryfile ⇒ Object
[View source]
354 355 356 |
# File 'lib/bolt/config.rb', line 354 def default_inventoryfile @project.inventory_file end |
permalink #disable_warnings ⇒ Object
[View source]
462 463 464 |
# File 'lib/bolt/config.rb', line 462 def disable_warnings Set.new(@project.disable_warnings + @data['disable-warnings']) end |
permalink #format ⇒ Object
[View source]
386 387 388 |
# File 'lib/bolt/config.rb', line 386 def format @data['format'] end |
permalink #format=(value) ⇒ Object
[View source]
390 391 392 |
# File 'lib/bolt/config.rb', line 390 def format=(value) @data['format'] = value end |
permalink #future ⇒ Object
[View source]
394 395 396 |
# File 'lib/bolt/config.rb', line 394 def future @data['future'] end |
permalink #hiera_config ⇒ Object
[View source]
362 363 364 |
# File 'lib/bolt/config.rb', line 362 def hiera_config @data['hiera-config'] || @project.hiera_config end |
permalink #inventoryfile ⇒ Object
[View source]
426 427 428 |
# File 'lib/bolt/config.rb', line 426 def inventoryfile @data['inventoryfile'] end |
permalink #log ⇒ Object
[View source]
402 403 404 |
# File 'lib/bolt/config.rb', line 402 def log @data['log'] end |
permalink #matching_paths(paths) ⇒ Object
[View source]
482 483 484 |
# File 'lib/bolt/config.rb', line 482 def matching_paths(paths) Array(paths).map { |p| Dir.glob([p, casefold(p)]) }.flatten.uniq.reject { |p| Array(paths).include?(p) } end |
permalink #merge_config_layers(*config_data) ⇒ Object
Merge configuration from all sources into a single hash. Precedence from lowest to highest: defaults, system-wide, user-level, project-level, CLI overrides
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/bolt/config.rb', line 251 def merge_config_layers(*config_data) config_data.inject({}) do |acc, config| acc.merge(config) do |key, val1, val2| case key # Plugin config is shallow merged for each plugin when 'plugins' val1.merge(val2) { |_, v1, v2| v1.merge(v2) } # Transports are deep merged when *TRANSPORT_CONFIG.keys Bolt::Util.deep_merge(val1, val2) # Hash values are shallow merged when 'apply-settings', 'log', 'plugin-hooks', 'puppetdb' val1.merge(val2) # Disabled warnings are concatenated when 'disable-warnings' val1.concat(val2) when 'analytics' val1 && val2 # All other values are overwritten else val2 end end end end |
permalink #module_install ⇒ Object
[View source]
458 459 460 |
# File 'lib/bolt/config.rb', line 458 def module_install @project.module_install || @data['module-install'] end |
permalink #modulepath ⇒ Object
[View source]
370 371 372 |
# File 'lib/bolt/config.rb', line 370 def modulepath (@data['modulepath'] || @project.modulepath) + [@project.managed_moduledir.to_s] end |
permalink #modulepath=(value) ⇒ Object
[View source]
374 375 376 |
# File 'lib/bolt/config.rb', line 374 def modulepath=(value) @data['modulepath'] = Array(value) end |
permalink #normalize_overrides(options) ⇒ Object
Transforms CLI options into a config hash that can be merged with default and loaded config.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/bolt/config.rb', line 223 def normalize_overrides() opts = .transform_keys(&:to_s) # Pull out config options. We need to add 'transport' and 'inventoryfile' as they're # not part of the OPTIONS hash but are valid options that can be set with CLI options overrides = opts.slice(*OPTIONS.keys, 'inventoryfile', 'transport') # Pull out transport config options TRANSPORT_CONFIG.each do |transport, config| overrides[transport] = opts.slice(*config.) end overrides['trace'] = opts['trace'] if opts.key?('trace') # Validate the overrides that can have arbitrary values schema = { type: Hash, properties: CLI_OPTIONS.map { |opt| [opt, _ref: opt] }.to_h, definitions: OPTIONS.merge(INVENTORY_OPTIONS) } Bolt::Validator.new.validate(overrides.slice(*CLI_OPTIONS), schema, 'command line') overrides end |
permalink #plugin_cache ⇒ Object
[View source]
378 379 380 |
# File 'lib/bolt/config.rb', line 378 def plugin_cache @project.plugin_cache || @data['plugin-cache'] || {} end |
permalink #plugin_hooks ⇒ Object
[View source]
438 439 440 |
# File 'lib/bolt/config.rb', line 438 def plugin_hooks @data['plugin-hooks'] end |
permalink #plugins ⇒ Object
[View source]
434 435 436 |
# File 'lib/bolt/config.rb', line 434 def plugins @data['plugins'] end |
permalink #policies ⇒ Object
[View source]
442 443 444 |
# File 'lib/bolt/config.rb', line 442 def policies @data['policies'] end |
permalink #puppetdb ⇒ Object
[View source]
406 407 408 |
# File 'lib/bolt/config.rb', line 406 def puppetdb @data['puppetdb'] end |
permalink #puppetfile ⇒ Object
[View source]
366 367 368 |
# File 'lib/bolt/config.rb', line 366 def puppetfile @project.puppetfile end |
permalink #rerunfile ⇒ Object
[View source]
358 359 360 |
# File 'lib/bolt/config.rb', line 358 def rerunfile @project.rerunfile end |
permalink #save_rerun ⇒ Object
[View source]
414 415 416 |
# File 'lib/bolt/config.rb', line 414 def save_rerun @data['save-rerun'] end |
permalink #sc_open_max_available? ⇒ Boolean
Etc::SC_OPEN_MAX is meaningless on windows, not defined in PE Jruby and not available on some platforms. This method holds the logic to decide whether or not to even consider it.
494 495 496 |
# File 'lib/bolt/config.rb', line 494 def sc_open_max_available? !Bolt::Util.windows? && defined?(Etc::SC_OPEN_MAX) && Etc.sysconf(Etc::SC_OPEN_MAX) end |
permalink #spinner ⇒ Object
[View source]
418 419 420 |
# File 'lib/bolt/config.rb', line 418 def spinner @data['spinner'] end |
permalink #stream ⇒ Object
[View source]
422 423 424 |
# File 'lib/bolt/config.rb', line 422 def stream @data['stream'] end |
permalink #trace ⇒ Object
[View source]
398 399 400 |
# File 'lib/bolt/config.rb', line 398 def trace @data['trace'] end |
permalink #transport ⇒ Object
[View source]
454 455 456 |
# File 'lib/bolt/config.rb', line 454 def transport @data['transport'] end |
permalink #trusted_external ⇒ Object
[View source]
446 447 448 |
# File 'lib/bolt/config.rb', line 446 def trusted_external @data['trusted-external-command'] end |
permalink #validate ⇒ Object
[View source]
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/bolt/config.rb', line 333 def validate if @data['modulepath']&.include?(@project.managed_moduledir.to_s) raise Bolt::ValidationError, "Found invalid path in modulepath: #{@project.managed_moduledir}. This path "\ "is automatically appended to the modulepath and cannot be configured." end compile_limit = 2 * Etc.nprocessors unless compile_concurrency < compile_limit raise Bolt::ValidationError, "Compilation is CPU-intensive, set concurrency less than #{compile_limit}" end %w[hiera-config trusted-external-command inventoryfile].each do |opt| Bolt::Util.validate_file(opt, @data[opt]) if @data[opt] end if File.exist?(default_inventoryfile) Bolt::Util.validate_file('inventory file', default_inventoryfile) end end |