Class: Bolt::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/config.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

Defined Under Namespace

Modules: Transport

Constant Summary collapse

TRANSPORT_CONFIG =
{
  '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
TRANSPORT_OPTION =
{ 'transport' => 'The default transport to use when the '\
'transport for a target is not specified in the URL.' }.freeze
DEFAULT_TRANSPORT_OPTION =
{ 'transport' => 'ssh' }.freeze
CONFIG_IN_INVENTORY =
TRANSPORT_CONFIG.merge(TRANSPORT_OPTION)
OPTIONS =

NOTE: All configuration options should have a corresponding schema property

in schemas/bolt-config.schema.json
{
  "apply_settings"           => "A map of Puppet settings to use when applying Puppet code",
  "color"                    => "Whether to use colored output when printing messages to the console.",
  "compile-concurrency"      => "The maximum number of simultaneous manifest block compiles.",
  "concurrency"              => "The number of threads to use when executing on remote targets.",
  "format"                   => "The format to use when printing results. Options are `human` and `json`.",
  "hiera-config"             => "The path to your Hiera config.",
  "inventoryfile"            => "The path to a structured data inventory file used to refer to groups of "\
                                "targets on the command line and from plans.",
  "log"                      => "The configuration of the logfile output. Configuration can be set for "\
                                "`console` and the path to a log file, such as `~/.puppetlabs/bolt/debug.log`.",
  "modulepath"               => "An array of directories that Bolt loads content (e.g. plans and tasks) from.",
  "plugin_hooks"             => "Which plugins a specific hook should use.",
  "plugins"                  => "A map of plugins and their configuration data.",
  "puppetdb"                 => "A map containing options for configuring the Bolt PuppetDB client.",
  "puppetfile"               => "A map containing options for the `bolt puppetfile install` command.",
  "save-rerun"               => "Whether to update `.rerun.json` in the Bolt project directory. If "\
                                "your target names include passwords, set this value to `false` to avoid "\
                                "writing passwords to disk.",
  "transport"                => "The default transport to use when the transport for a target is not specified "\
                                "in the URL or inventory.",
  "trusted-external-command" => "The path to an executable on the Bolt controller that can produce "\
                                "external trusted facts. **External trusted facts are experimental in both "\
                                "Puppet and Bolt and this API may change or be removed.**"
}.freeze
DEFAULT_OPTIONS =
{
  "color" => true,
  "compile-concurrency" => "Number of cores",
  "concurrency" => "100 or one-third of the ulimit, whichever is lower",
  "format" => "human",
  "hiera-config" => "Boltdir/hiera.yaml",
  "inventoryfile" => "Boltdir/inventory.yaml",
  "modulepath" => ["Boltdir/modules", "Boltdir/site-modules", "Boltdir/site"],
  "save-rerun" => true
}.freeze
PUPPETFILE_OPTIONS =
{
  "forge" => "A subsection that can have its own `proxy` setting to set an HTTP proxy for Forge operations "\
             "only, and a `baseurl` setting to specify a different Forge host.",
  "proxy" => "The HTTP proxy to use for Git and Forge operations."
}.freeze
LOG_OPTIONS =
{
  "append" => "Add output to an existing log file. Available only for logs output to a "\
              "filepath.",
  "level"  => "The type of information in the log. Either `debug`, `info`, `notice`, "\
              "`warn`, or `error`."
}.freeze
DEFAULT_LOG_OPTIONS =
{
  "append" => true,
  "level"  => "`warn` for console, `notice` for file"
}.freeze
APPLY_SETTINGS =
{
  "show_diff" => "Whether to log and report a contextual diff when files are being replaced. "\
                 "See [Puppet documentation](https://puppet.com/docs/puppet/latest/configuration.html#showdiff) "\
                 "for details"
}.freeze
DEFAULT_APPLY_SETTINGS =
{
  "show_diff" => false
}.freeze
DEFAULT_DEFAULT_CONCURRENCY =
100

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.



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

def initialize(project, config_data, overrides = {})
  unless config_data.is_a?(Array)
    config_data = [{ filepath: project.config_file, data: config_data }]
  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.map do |config|
    @config_files.push(config[:filepath])
    config[:data]
  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.



26
27
28
# File 'lib/bolt/config.rb', line 26

def config_files
  @config_files
end

#dataObject (readonly)

Returns the value of attribute data.



26
27
28
# File 'lib/bolt/config.rb', line 26

def data
  @data
end

#modified_concurrencyObject (readonly)

Returns the value of attribute modified_concurrency.



26
27
28
# File 'lib/bolt/config.rb', line 26

def modified_concurrency
  @modified_concurrency
end

#projectObject (readonly)

Returns the value of attribute project.



26
27
28
# File 'lib/bolt/config.rb', line 26

def project
  @project
end

#transportsObject (readonly)

Returns the value of attribute transports.



26
27
28
# File 'lib/bolt/config.rb', line 26

def transports
  @transports
end

#warningsObject (readonly)

Returns the value of attribute warnings.



26
27
28
# File 'lib/bolt/config.rb', line 26

def warnings
  @warnings
end

Class Method Details

.defaultObject



113
114
115
# File 'lib/bolt/config.rb', line 113

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

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



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bolt/config.rb', line 131

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 = { filepath: project.config_file, data: conf }
  data = load_defaults(project).push(data).select { |config| config[:data]&.any? }

  new(project, data, overrides)
end

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



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bolt/config.rb', line 117

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 = { filepath: project.config_file, data: conf }

  data = load_defaults(project).push(data).select { |config| config[:data]&.any? }

  new(project, data, overrides)
end

.load_defaults(project) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/bolt/config.rb', line 144

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

  # Don't load /etc/puppetlabs/bolt/bolt.yaml twice
  confs = if project.path == Bolt::Project.system_path
            []
          else
            system_path = Pathname.new(File.join(Bolt::Project.system_path, 'bolt.yaml'))
            [{ filepath: system_path, data: Bolt::Util.read_optional_yaml_hash(system_path, 'config') }]
          end

  user_path = begin
                Pathname.new(File.expand_path(File.join('~', '.puppetlabs', 'etc', 'bolt', 'bolt.yaml')))
              rescue ArgumentError
                nil
              end

  confs << { filepath: user_path, data: Bolt::Util.read_optional_yaml_hash(user_path, 'config') } if user_path
  confs
end

Instance Method Details

#apply_settingsObject



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

def apply_settings
  @data['apply_settings']
end

#check_path_case(type, paths) ⇒ Object

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



460
461
462
463
464
465
466
467
468
469
# File 'lib/bolt/config.rb', line 460

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



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

def color
  @data['color']
end

#compile_concurrencyObject



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

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

#concurrencyObject



395
396
397
# File 'lib/bolt/config.rb', line 395

def concurrency
  @data['concurrency']
end

#deep_cloneObject



265
266
267
# File 'lib/bolt/config.rb', line 265

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

#default_concurrencyObject



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

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



371
372
373
# File 'lib/bolt/config.rb', line 371

def default_inventoryfile
  @project.inventory_file
end

#formatObject



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

def format
  @data['format']
end

#format=(value) ⇒ Object



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

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

#hiera_configObject



379
380
381
# File 'lib/bolt/config.rb', line 379

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

#inventoryfileObject



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

def inventoryfile
  @data['inventoryfile']
end

#logObject



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

def log
  @data['log']
end

#matching_paths(paths) ⇒ Object



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

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



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

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



387
388
389
# File 'lib/bolt/config.rb', line 387

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

#modulepath=(value) ⇒ Object



391
392
393
# File 'lib/bolt/config.rb', line 391

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.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/bolt/config.rb', line 217

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

  # Pull out config options
  overrides = opts.slice(*OPTIONS.keys)

  # Pull out transport config options
  TRANSPORT_CONFIG.each do |transport, config|
    overrides[transport] = opts.slice(*config.options.keys)
  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



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

def plugin_hooks
  @data['plugin_hooks']
end

#pluginsObject



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

def plugins
  @data['plugins']
end

#puppetdbObject



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

def puppetdb
  @data['puppetdb']
end

#puppetfileObject



383
384
385
# File 'lib/bolt/config.rb', line 383

def puppetfile
  @puppetfile || @project.puppetfile
end

#puppetfile_configObject



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

def puppetfile_config
  @data['puppetfile']
end

#rerunfileObject



375
376
377
# File 'lib/bolt/config.rb', line 375

def rerunfile
  @project.rerunfile
end

#save_rerunObject



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

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)


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

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

#traceObject



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

def trace
  @data['trace']
end

#transportObject



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

def transport
  @data['transport']
end

#trusted_externalObject



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

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

#validateObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/bolt/config.rb', line 328

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

  unless %w[human json].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