Class: Bolt::Config

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

Constant Summary collapse

TRANSPORT_OPTIONS =
%i[password run-as sudo-password extensions
private-key tty tmpdir user connect-timeout disconnect-timeout
cacert token-file service-url interpreters file-protocol smb-port realm].freeze
PUPPETFILE_OPTIONS =
%w[proxy forge].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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bolt/config.rb', line 61

def initialize(boltdir, config_data, overrides = {})
  @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 = { 'puppet_library' => { 'plugin' => 'puppet_agent', 'stop_service' => true } }

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

  @transports = {}

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

  update_from_file(config_data)
  apply_overrides(overrides)

  validate
end

Instance Attribute Details

#boltdirObject

Returns the value of attribute boltdir.



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

def boltdir
  @boltdir
end

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#compile_concurrencyObject

Returns the value of attribute compile_concurrency.



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

def compile_concurrency
  @compile_concurrency
end

#concurrencyObject

Returns the value of attribute concurrency.



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

def concurrency
  @concurrency
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#futureObject

Returns the value of attribute future.



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

def future
  @future
end

#inventoryfileObject

Returns the value of attribute inventoryfile.



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

def inventoryfile
  @inventoryfile
end

#logObject

Returns the value of attribute log.



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

def log
  @log
end

#modulepathObject



249
250
251
# File 'lib/bolt/config.rb', line 249

def modulepath
  @modulepath || @boltdir.modulepath
end

#plugin_hooksObject

Returns the value of attribute plugin_hooks.



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

def plugin_hooks
  @plugin_hooks
end

#pluginsObject

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

#puppetdbObject

Returns the value of attribute puppetdb.



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

def puppetdb
  @puppetdb
end

#puppetfile_configObject

Returns the value of attribute puppetfile_config.



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

def puppetfile_config
  @puppetfile_config
end

#save_rerunObject

Returns the value of attribute save_rerun.



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

def save_rerun
  @save_rerun
end

#traceObject

Returns the value of attribute trace.



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

def trace
  @trace
end

#transportObject

Returns the value of attribute transport.



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

def transport
  @transport
end

#transportsObject

Returns the value of attribute transports.



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

def transports
  @transports
end

Class Method Details

.defaultObject



45
46
47
# File 'lib/bolt/config.rb', line 45

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

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



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

def self.from_boltdir(boltdir, overrides = {})
  data = Bolt::Util.read_config_file(nil, [boltdir.config_file], 'config') || {}
  new(boltdir, data, overrides)
end

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



54
55
56
57
58
59
# File 'lib/bolt/config.rb', line 54

def self.from_file(configfile, overrides = {})
  boltdir = Bolt::Boltdir.new(Pathname.new(configfile).expand_path.dirname)
  data = Bolt::Util.read_config_file(configfile, [], 'config') || {}

  new(boltdir, data, overrides)
end

Instance Method Details

#apply_overrides(options) ⇒ Object



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

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



308
309
310
311
312
# File 'lib/bolt/config.rb', line 308

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



293
294
295
296
297
298
299
300
301
302
# File 'lib/bolt/config.rb', line 293

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



100
101
102
# File 'lib/bolt/config.rb', line 100

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

#default_inventoryfileObject



233
234
235
# File 'lib/bolt/config.rb', line 233

def default_inventoryfile
  [@boltdir.inventory_file]
end

#hiera_configObject



241
242
243
# File 'lib/bolt/config.rb', line 241

def hiera_config
  @hiera_config || @boltdir.hiera_config
end

#matching_paths(paths) ⇒ Object



304
305
306
# File 'lib/bolt/config.rb', line 304

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

#normalize_interpreters(interpreters) ⇒ Object



104
105
106
107
108
# File 'lib/bolt/config.rb', line 104

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

#normalize_log(target) ⇒ Object



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

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

#overwrite_transport_data(transport, transports) ⇒ Object



91
92
93
94
# File 'lib/bolt/config.rb', line 91

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

#puppetfileObject



245
246
247
# File 'lib/bolt/config.rb', line 245

def puppetfile
  @boltdir.puppetfile
end

#rerunfileObject



237
238
239
# File 'lib/bolt/config.rb', line 237

def rerunfile
  @boltdir.rerunfile
end

#transport_confObject



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

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

#transport_data_getObject



96
97
98
# File 'lib/bolt/config.rb', line 96

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

#update_from_inventory(data) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/bolt/config.rb', line 220

def update_from_inventory(data)
  update_from_file(data)

  if data['transport']
    @transport = data['transport']
  end
end

#update_logs(logs) ⇒ Object



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

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

#validateObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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 253

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

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

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