Class: Bolt::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/config.rb

Constant Summary collapse

OPTIONS =
{
  "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"               => "The module path for loading tasks and plan code. This is either an array "\
                                "of directories or a string containing a list of directories separated by the "\
                                "OS-specific PATH separator.",
  "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.**",
  "future"                   => "Whether to use new, breaking changes. This allows testing if Bolt content "\
                                "is compatible with expected future behavior."
}.freeze
DEFAULT_OPTIONS =
{
  "color" => true,
  "concurrency" => 100,
  "compile-concurrency" => "Number of cores",
  "format" => "human",
  "hiera-config" => "Boltdir/hiera.yaml",
  "inventoryfile" => "Boltdir/inventory.yaml",
  "modulepath" => ["Boltdir/modules", "Boltdir/site-modules", "Boltdir/site"],
  "save-rerun" => true,
  "future" => false
}.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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Config.



154
155
156
157
158
159
160
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
# File 'lib/bolt/config.rb', line 154

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

  @logger = Logging.logger[self]

  @boltdir = boltdir
  @concurrency = 100
  @compile_concurrency = Etc.nprocessors
  @transport = 'ssh'
  @format = 'human'
  @puppetdb = {}
  @color = true
  @save_rerun = true
  @puppetfile_config = {}
  @plugins = {}
  @plugin_hooks = {}
  @apply_settings = {}

  # add an entry for the default console logger
  @log = { 'console' => {} }

  @transports = {}

  TRANSPORTS.each do |key, transport|
    @transports[key] = transport.default_options
  end

  @config_files = config_data.map { |config| config[:filepath] }
  config_data = merge_config_data(config_data)
  update_from_file(config_data)

  apply_overrides(overrides)

  validate
end

Instance Attribute Details

#apply_settingsObject

Returns the value of attribute apply_settings.



35
36
37
# File 'lib/bolt/config.rb', line 35

def apply_settings
  @apply_settings
end

#boltdirObject

Returns the value of attribute boltdir.



35
36
37
# File 'lib/bolt/config.rb', line 35

def boltdir
  @boltdir
end

#colorObject

Returns the value of attribute color.



35
36
37
# File 'lib/bolt/config.rb', line 35

def color
  @color
end

#compile_concurrencyObject

Returns the value of attribute compile_concurrency.



35
36
37
# File 'lib/bolt/config.rb', line 35

def compile_concurrency
  @compile_concurrency
end

#concurrencyObject

Returns the value of attribute concurrency.



35
36
37
# File 'lib/bolt/config.rb', line 35

def concurrency
  @concurrency
end

#config_filesObject (readonly)

Returns the value of attribute config_files.



40
41
42
# File 'lib/bolt/config.rb', line 40

def config_files
  @config_files
end

#formatObject

Returns the value of attribute format.



35
36
37
# File 'lib/bolt/config.rb', line 35

def format
  @format
end

#futureObject

Returns the value of attribute future.



35
36
37
# File 'lib/bolt/config.rb', line 35

def future
  @future
end

#inventoryfileObject

Returns the value of attribute inventoryfile.



35
36
37
# File 'lib/bolt/config.rb', line 35

def inventoryfile
  @inventoryfile
end

#logObject

Returns the value of attribute log.



35
36
37
# File 'lib/bolt/config.rb', line 35

def log
  @log
end

#modulepathObject



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

def modulepath
  @modulepath || @boltdir.modulepath
end

#plugin_hooksObject

Returns the value of attribute plugin_hooks.



35
36
37
# File 'lib/bolt/config.rb', line 35

def plugin_hooks
  @plugin_hooks
end

#pluginsObject

Returns the value of attribute plugins.



35
36
37
# File 'lib/bolt/config.rb', line 35

def plugins
  @plugins
end

#puppetdbObject

Returns the value of attribute puppetdb.



35
36
37
# File 'lib/bolt/config.rb', line 35

def puppetdb
  @puppetdb
end

#puppetfile_configObject

Returns the value of attribute puppetfile_config.



35
36
37
# File 'lib/bolt/config.rb', line 35

def puppetfile_config
  @puppetfile_config
end

#save_rerunObject

Returns the value of attribute save_rerun.



35
36
37
# File 'lib/bolt/config.rb', line 35

def save_rerun
  @save_rerun
end

#traceObject

Returns the value of attribute trace.



35
36
37
# File 'lib/bolt/config.rb', line 35

def trace
  @trace
end

#transportObject

Returns the value of attribute transport.



35
36
37
# File 'lib/bolt/config.rb', line 35

def transport
  @transport
end

#transportsObject

Returns the value of attribute transports.



35
36
37
# File 'lib/bolt/config.rb', line 35

def transports
  @transports
end

#trusted_externalObject

Returns the value of attribute trusted_external.



35
36
37
# File 'lib/bolt/config.rb', line 35

def trusted_external
  @trusted_external
end

Class Method Details

.defaultObject



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

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

.from_boltdir(boltdir, overrides = {}) ⇒ Object



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

def self.from_boltdir(boltdir, overrides = {})
  data = {
    filepath: boltdir.config_file,
    data: Bolt::Util.read_optional_yaml_hash(boltdir.config_file, 'config')
  }

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

  new(boltdir, data, overrides)
end

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/bolt/config.rb', line 127

def self.from_file(configfile, overrides = {})
  boltdir = Bolt::Boltdir.new(Pathname.new(configfile).expand_path.dirname)

  data = {
    filepath: boltdir.config_file,
    data: Bolt::Util.read_yaml_hash(configfile, 'config')
  }
  data = load_defaults.push(data).select { |config| config[:data]&.any? }

  new(boltdir, data, overrides)
end

.load_defaultsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/bolt/config.rb', line 139

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

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

  [{ filepath: system_path, data: Bolt::Util.read_optional_yaml_hash(system_path, 'config') },
   { filepath: user_path, data: Bolt::Util.read_optional_yaml_hash(user_path, 'config') }]
end

Instance Method Details

#apply_overrides(options) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/bolt/config.rb', line 309

def apply_overrides(options)
  %i[concurrency transport format trace modulepath inventoryfile color].each do |key|
    send("#{key}=", options[key]) if options.key?(key)
  end

  @save_rerun = options[:'save-rerun'] if options.key?(:'save-rerun')

  if options[:debug]
    @log['console'][:level] = :debug
  end

  @compile_concurrency = options[:'compile-concurrency'] if options[:'compile-concurrency']

  TRANSPORTS.each_key do |transport|
    # Get the options first since transport is modified in the next line
    transport_options = TRANSPORTS[transport]::OPTIONS.keys.map(&:to_sym)
    transport = @transports[transport]
    transport_options.each do |key|
      if options[key]
        transport[key.to_s] = Bolt::Util.walk_keys(options[key], &:to_s)
      end
    end
  end

  if options.key?(:ssl) # this defaults to true so we need to check the presence of the key
    @transports[:winrm]['ssl'] = options[:ssl]
  end

  if options.key?(:'ssl-verify') # this defaults to true so we need to check the presence of the key
    @transports[:winrm]['ssl-verify'] = options[:'ssl-verify']
  end

  if options.key?(:'host-key-check') # this defaults to true so we need to check the presence of the key
    @transports[:ssh]['host-key-check'] = options[:'host-key-check']
  end
end

#casefold(path) ⇒ Object



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

def casefold(path)
  path.chars.map do |l|
    l =~ /[A-Za-z]/ ? "[#{l.upcase}#{l.downcase}]" : l
  end.join
end

#check_path_case(type, paths) ⇒ Object

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



437
438
439
440
441
442
443
444
445
446
# File 'lib/bolt/config.rb', line 437

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

#deep_cloneObject



224
225
226
# File 'lib/bolt/config.rb', line 224

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

#default_inventoryfileObject



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

def default_inventoryfile
  @boltdir.inventory_file
end

#hiera_configObject



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

def hiera_config
  @hiera_config || @boltdir.hiera_config
end

#matching_paths(paths) ⇒ Object



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

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

#merge_config_data(config_data) ⇒ Object

Merge configuration Precedence from highest to lowest is: project, user-level, system-wide



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 194

def merge_config_data(config_data)
  config_data.inject({}) do |acc, config|
    acc.merge(config[:data]) 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 *TRANSPORTS.keys.map(&:to_s)
        Bolt::Util.deep_merge(val1, val2)
      # Hash values are shallow mergeed
      when 'puppetdb', 'plugin_hooks', 'apply_settings', 'log'
        val1.merge(val2)
      # All other values are overwritten
      else
        val2
      end
    end
  end
end

#normalize_interpreters(interpreters) ⇒ Object



228
229
230
231
232
# File 'lib/bolt/config.rb', line 228

def normalize_interpreters(interpreters)
  Bolt::Util.walk_keys(interpreters) do |key|
    key.chars[0] == '.' ? key : '.' + key
  end
end

#normalize_log(target) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/bolt/config.rb', line 234

def normalize_log(target)
  return target if target == 'console'
  target = target[5..-1] if target.start_with?('file:')
  if @future
    'file:' + File.expand_path(target, @boltdir.path)
  else
    'file:' + File.expand_path(target)
  end
end

#overwrite_transport_data(transport, transports) ⇒ Object



215
216
217
218
# File 'lib/bolt/config.rb', line 215

def overwrite_transport_data(transport, transports)
  @transport = transport
  @transports = transports
end

#puppetfileObject



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

def puppetfile
  @boltdir.puppetfile
end

#rerunfileObject



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

def rerunfile
  @boltdir.rerunfile
end

#transport_confObject



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

def transport_conf
  { transport: @transport,
    transports: @transports }
end

#transport_data_getObject



220
221
222
# File 'lib/bolt/config.rb', line 220

def transport_data_get
  { transport: @transport, transports: @transports }
end

#update_from_inventory(data) ⇒ Object



346
347
348
# File 'lib/bolt/config.rb', line 346

def update_from_inventory(data)
  update_transports(data)
end

#update_logs(logs) ⇒ Object



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

def update_logs(logs)
  logs.each_pair do |k, v|
    log_name = normalize_log(k)
    @log[log_name] ||= {}
    log = @log[log_name]

    next unless v.is_a?(Hash)

    if v.key?('level')
      log[:level] = v['level'].to_s
    end

    if v.key?('append')
      log[:append] = v['append']
    end
  end
end

#update_transports(data) ⇒ Object



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 350

def update_transports(data)
  TRANSPORTS.each do |key, impl|
    if data[key.to_s]
      selected = impl.filter_options(data[key.to_s])
      if @future
        to_expand = %w[private-key cacert token-file] & selected.keys
        to_expand.each do |opt|
          selected[opt] = File.expand_path(selected[opt], @boltdir.path) if selected[opt].is_a?(String)
        end
      end

      @transports[key] = Bolt::Util.deep_merge(@transports[key], selected)
    end
    if @transports[key]['interpreters']
      @transports[key]['interpreters'] = normalize_interpreters(@transports[key]['interpreters'])
    end
  end

  @transport = data['transport'] if data.key?('transport')
end

#validateObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/bolt/config.rb', line 396

def validate
  @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 @concurrency.is_a?(Integer) && @concurrency > 0
    raise Bolt::ValidationError, 'Concurrency must be a positive integer'
  end

  unless @compile_concurrency.is_a?(Integer) && @compile_concurrency > 0
    raise Bolt::ValidationError, 'Compile concurrency must be a positive integer'
  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', @hiera_config) if @hiera_config
  Bolt::Util.validate_file('trusted-external-command', @trusted_external) if @trusted_external

  unless @transport.nil? || Bolt::TRANSPORTS.include?(@transport.to_sym)
    raise UnknownTransportError, @transport
  end

  TRANSPORTS.each do |transport, impl|
    impl.validate(@transports[transport])
  end
end