Class: Bolt::Config

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/bolt/config.rb,
lib/bolt/config/options.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/remote.rb,
lib/bolt/config/transport/options.rb

Defined Under Namespace

Modules: Options, Transport

Constant Summary collapse

BOLT_CONFIG_NAME =
'bolt.yaml'
BOLT_DEFAULTS_NAME =
'bolt-defaults.yaml'
TRANSPORT_CONFIG =

Transport config classes. Used to load default transport config which gets passed along to the inventory.

{
  'ssh'    => Bolt::Config::Transport::SSH,
  'winrm'  => Bolt::Config::Transport::WinRM,
  'pcp'    => Bolt::Config::Transport::Orch,
  'local'  => Bolt::Config::Transport::Local,
  'docker' => Bolt::Config::Transport::Docker,
  'remote' => Bolt::Config::Transport::Remote
}.freeze
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::BOLT_DEFAULTS_OPTIONS, Options::BOLT_OPTIONS, Options::BOLT_PROJECT_OPTIONS, Options::INVENTORY_OPTIONS, Options::OPTIONS, Options::SUBOPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, config_data, overrides = {}) ⇒ Config

Returns a new instance of Config.



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
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 188

def initialize(project, config_data, overrides = {})
  unless config_data.is_a?(Array)
    config_data = [{ filepath: project.config_file, data: config_data, warnings: [] }]
  end

  @logger       = Logging.logger[self]
  @project      = project
  @warnings     = @project.warnings.dup
  @transports   = {}
  @config_files = []

  default_data = {
    'apply_settings'      => {},
    'color'               => true,
    'compile-concurrency' => Etc.nprocessors,
    'concurrency'         => default_concurrency,
    'format'              => 'human',
    'log'                 => { 'console' => {} },
    'plugin_hooks'        => {},
    'plugins'             => {},
    'puppetdb'            => {},
    'puppetfile'          => {},
    'save-rerun'          => true,
    'transport'           => 'ssh'
  }

  loaded_data = config_data.each_with_object([]) do |data, acc|
    @warnings.concat(data[:warnings]) if data[:warnings].any?

    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

#config_filesObject (readonly)

Returns the value of attribute config_files.



29
30
31
# File 'lib/bolt/config.rb', line 29

def config_files
  @config_files
end

#dataObject (readonly)

Returns the value of attribute data.



29
30
31
# File 'lib/bolt/config.rb', line 29

def data
  @data
end

#modified_concurrencyObject (readonly)

Returns the value of attribute modified_concurrency.



29
30
31
# File 'lib/bolt/config.rb', line 29

def modified_concurrency
  @modified_concurrency
end

#projectObject (readonly)

Returns the value of attribute project.



29
30
31
# File 'lib/bolt/config.rb', line 29

def project
  @project
end

#transportsObject (readonly)

Returns the value of attribute transports.



29
30
31
# File 'lib/bolt/config.rb', line 29

def transports
  @transports
end

#warningsObject (readonly)

Returns the value of attribute warnings.



29
30
31
# File 'lib/bolt/config.rb', line 29

def warnings
  @warnings
end

Class Method Details

.defaultObject



48
49
50
# File 'lib/bolt/config.rb', line 48

def self.default
  new(Bolt::Project.create_project('.'), {})
end

.from_file(configfile, overrides = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bolt/config.rb', line 68

def self.from_file(configfile, overrides = {})
  project = Bolt::Project.create_project(Pathname.new(configfile).expand_path.dirname)

  conf = if project.project_file == project.config_file
           project.data
         else
           Bolt::Util.read_yaml_hash(configfile, 'config')
         end

  data = load_defaults(project).push(
    filepath: project.config_file,
    data: conf,
    warnings: []
  )

  new(project, data, overrides)
end

.from_project(project, overrides = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bolt/config.rb', line 52

def self.from_project(project, overrides = {})
  conf = if project.project_file == project.config_file
           project.data
         else
           Bolt::Util.read_optional_yaml_hash(project.config_file, 'config')
         end

  data = load_defaults(project).push(
    filepath: project.config_file,
    data: conf,
    warnings: []
  )

  new(project, data, overrides)
end

.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 ‘inventoryfile’, and nests all default inventory configuration under an ‘inventory-config’ key.



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
144
145
146
147
148
149
# File 'lib/bolt/config.rb', line 106

def self.load_bolt_defaults_yaml(dir)
  filepath = dir + BOLT_DEFAULTS_NAME
  data     = Bolt::Util.read_yaml_hash(filepath, 'config')
  warnings = []

  # Warn if 'bolt.yaml' detected in same directory.
  if File.exist?(bolt_yaml = dir + BOLT_CONFIG_NAME)
    warnings.push(
      msg: "Detected multiple configuration files: ['#{bolt_yaml}', '#{filepath}']. '#{bolt_yaml}' "\
           "will be ignored."
    )
  end

  # Remove project-specific config such as hiera-config, etc.
  project_config = data.slice(*(BOLT_PROJECT_OPTIONS - BOLT_DEFAULTS_OPTIONS))

  if project_config.any?
    data.reject! { |key, _| project_config.include?(key) }
    warnings.push(
      msg: "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) }
    warnings.push(
      msg: "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 transport-config to top-level so it can be easily merged with
  # config from other sources.
  if data.key?('inventory-config')
    data = data.merge(data.delete('inventory-config'))
  end

  { filepath: filepath, data: data, warnings: warnings }
end

.load_bolt_yaml(dir) ⇒ Object

Loads a ‘bolt.yaml’ file, the legacy configuration file. There’s no special munging needed here since Bolt::Config will just ignore any invalid keys.



153
154
155
156
157
158
159
160
# File 'lib/bolt/config.rb', line 153

def self.load_bolt_yaml(dir)
  filepath = dir + BOLT_CONFIG_NAME
  data     = Bolt::Util.read_yaml_hash(filepath, 'config')
  warnings = [msg: "Configuration file #{filepath} is deprecated and will be removed in a future version "\
                    "of Bolt. Use '#{dir + BOLT_DEFAULTS_NAME}' instead."]

  { filepath: filepath, data: data, warnings: warnings }
end

.load_defaults(project) ⇒ Object



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
# File 'lib/bolt/config.rb', line 162

def self.load_defaults(project)
  confs = []

  # Load system-level config. Prefer a 'bolt-defaults.yaml' file, but fall back to the
  # legacy 'bolt.yaml' file. If the project-level config file is also the system-level
  # config file, don't load it a second time.
  if File.exist?(system_path + BOLT_DEFAULTS_NAME)
    confs << load_bolt_defaults_yaml(system_path)
  elsif File.exist?(system_path + BOLT_CONFIG_NAME) &&
        (system_path + BOLT_CONFIG_NAME) != project.config_file
    confs << load_bolt_yaml(system_path)
  end

  # Load user-level config if there is a homedir. Prefer a 'bolt-defaults.yaml' file, but
  # fall back to the legacy 'bolt.yaml' file.
  if user_path
    if File.exist?(user_path + BOLT_DEFAULTS_NAME)
      confs << load_bolt_defaults_yaml(user_path)
    elsif File.exist?(user_path + BOLT_CONFIG_NAME)
      confs << load_bolt_yaml(user_path)
    end
  end

  confs
end

.system_pathObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/bolt/config.rb', line 86

def self.system_path
  # Lazy-load expensive gem code
  require 'win32/dir' if Bolt::Util.windows?

  if Bolt::Util.windows?
    Pathname.new(File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'bolt', 'etc'))
  else
    Pathname.new(File.join('/etc', 'puppetlabs', 'bolt'))
  end
end

.user_pathObject



97
98
99
100
101
# File 'lib/bolt/config.rb', line 97

def self.user_path
  Pathname.new(File.expand_path(File.join('~', '.puppetlabs', 'etc', 'bolt')))
rescue StandardError
  nil
end

Instance Method Details

#apply_settingsObject



478
479
480
# File 'lib/bolt/config.rb', line 478

def apply_settings
  @data['apply_settings']
end

#check_path_case(type, paths) ⇒ Object

Check if there is a case-insensitive match to the path



487
488
489
490
491
492
493
494
495
496
# File 'lib/bolt/config.rb', line 487

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" }
    @logger.warn msg
  end
end

#colorObject



446
447
448
# File 'lib/bolt/config.rb', line 446

def color
  @data['color']
end

#compile_concurrencyObject



458
459
460
# File 'lib/bolt/config.rb', line 458

def compile_concurrency
  @data['compile-concurrency']
end

#concurrencyObject



422
423
424
# File 'lib/bolt/config.rb', line 422

def concurrency
  @data['concurrency']
end

#deep_cloneObject



292
293
294
# File 'lib/bolt/config.rb', line 292

def deep_clone
  Bolt::Util.deep_clone(self)
end

#default_concurrencyObject



514
515
516
517
518
519
520
# File 'lib/bolt/config.rb', line 514

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

#default_inventoryfileObject



398
399
400
# File 'lib/bolt/config.rb', line 398

def default_inventoryfile
  @project.inventory_file
end

#formatObject



426
427
428
# File 'lib/bolt/config.rb', line 426

def format
  @data['format']
end

#format=(value) ⇒ Object



430
431
432
# File 'lib/bolt/config.rb', line 430

def format=(value)
  @data['format'] = value
end

#hiera_configObject



406
407
408
# File 'lib/bolt/config.rb', line 406

def hiera_config
  @data['hiera-config'] || @project.hiera_config
end

#inventoryfileObject



454
455
456
# File 'lib/bolt/config.rb', line 454

def inventoryfile
  @data['inventoryfile']
end

#logObject



438
439
440
# File 'lib/bolt/config.rb', line 438

def log
  @data['log']
end

#matching_paths(paths) ⇒ Object



498
499
500
# File 'lib/bolt/config.rb', line 498

def matching_paths(paths)
  [*paths].map { |p| Dir.glob([p, casefold(p)]) }.flatten.uniq.reject { |p| [*paths].include?(p) }
end

#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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/bolt/config.rb', line 271

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 'puppetdb', 'plugin_hooks', 'apply_settings', 'log'
        val1.merge(val2)
      # All other values are overwritten
      else
        val2
      end
    end
  end
end

#modulepathObject



414
415
416
# File 'lib/bolt/config.rb', line 414

def modulepath
  @data['modulepath'] || @project.modulepath
end

#modulepath=(value) ⇒ Object



418
419
420
# File 'lib/bolt/config.rb', line 418

def modulepath=(value)
  @data['modulepath'] = value
end

#normalize_overrides(options) ⇒ Object

Transforms CLI options into a config hash that can be merged with default and loaded config.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/bolt/config.rb', line 243

def normalize_overrides(options)
  opts = options.transform_keys(&:to_s)

  # Pull out config options. We need to add 'transport' as it's not part of the
  # OPTIONS hash but is a valid option that can be set with the --transport CLI option
  overrides = opts.slice(*OPTIONS.keys, 'transport')

  # Pull out transport config options
  TRANSPORT_CONFIG.each do |transport, config|
    overrides[transport] = opts.slice(*config.options)
  end

  # Set console log to debug if in debug mode
  if options[:debug]
    overrides['log'] = { 'console' => { 'level' => :debug } }
  end

  if options[:puppetfile_path]
    @puppetfile = options[:puppetfile_path]
  end

  overrides['trace'] = opts['trace'] if opts.key?('trace')

  overrides
end

#plugin_hooksObject



470
471
472
# File 'lib/bolt/config.rb', line 470

def plugin_hooks
  @data['plugin_hooks']
end

#pluginsObject



466
467
468
# File 'lib/bolt/config.rb', line 466

def plugins
  @data['plugins']
end

#puppetdbObject



442
443
444
# File 'lib/bolt/config.rb', line 442

def puppetdb
  @data['puppetdb']
end

#puppetfileObject



410
411
412
# File 'lib/bolt/config.rb', line 410

def puppetfile
  @puppetfile || @project.puppetfile
end

#puppetfile_configObject



462
463
464
# File 'lib/bolt/config.rb', line 462

def puppetfile_config
  @data['puppetfile']
end

#rerunfileObject



402
403
404
# File 'lib/bolt/config.rb', line 402

def rerunfile
  @project.rerunfile
end

#save_rerunObject



450
451
452
# File 'lib/bolt/config.rb', line 450

def save_rerun
  @data['save-rerun']
end

#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.

Returns:

  • (Boolean)


510
511
512
# File 'lib/bolt/config.rb', line 510

def sc_open_max_available?
  !Bolt::Util.windows? && defined?(Etc::SC_OPEN_MAX) && Etc.sysconf(Etc::SC_OPEN_MAX)
end

#traceObject



434
435
436
# File 'lib/bolt/config.rb', line 434

def trace
  @data['trace']
end

#transportObject



482
483
484
# File 'lib/bolt/config.rb', line 482

def transport
  @data['transport']
end

#trusted_externalObject



474
475
476
# File 'lib/bolt/config.rb', line 474

def trusted_external
  @data['trusted-external-command']
end

#validateObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/bolt/config.rb', line 355

def validate
  if @data['future']
    msg = "Configuration option 'future' no longer exposes future behavior."
    @warnings << { option: 'future', msg: msg }
  end

  keys = OPTIONS.keys - %w[plugins plugin_hooks puppetdb]
  keys.each do |key|
    next unless Bolt::Util.references?(@data[key])
    valid_keys = TRANSPORT_CONFIG.keys + %w[plugins plugin_hooks puppetdb]
    raise Bolt::ValidationError,
          "Found unsupported key _plugin in config setting #{key}. Plugins are only available in "\
          "#{valid_keys.join(', ')}."
  end

  unless concurrency.is_a?(Integer) && concurrency > 0
    raise Bolt::ValidationError,
          "Concurrency must be a positive Integer, received #{concurrency.class} #{concurrency}"
  end

  unless compile_concurrency.is_a?(Integer) && compile_concurrency > 0
    raise Bolt::ValidationError,
          "Compile concurrency must be a positive Integer, received #{compile_concurrency.class} "\
          "#{compile_concurrency}"
  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

  if (format == 'rainbow' && Bolt::Util.windows?) || !(%w[human json rainbow].include? format)
    raise Bolt::ValidationError, "Unsupported format: '#{format}'"
  end

  Bolt::Util.validate_file('hiera-config', @data['hiera-config']) if @data['hiera-config']
  Bolt::Util.validate_file('trusted-external-command', trusted_external) if trusted_external

  unless TRANSPORT_CONFIG.include?(transport)
    raise UnknownTransportError, transport
  end
end