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
cacert token-file service-url].freeze
TRANSPORT_DEFAULTS =
{
  'connect-timeout' => 10,
  'tty' => false
}.freeze
TRANSPORT_SPECIFIC_DEFAULTS =
{
  ssh: {
    'host-key-check' => true
  },
  winrm: {
    'ssl' => true,
    'ssl-verify' => true
  },
  pcp: {
    'task-environment' => 'production',
    'local-validation' => false
  },
  local: {}
}.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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bolt/config.rb', line 84

def initialize(boltdir, config_data, overrides = {})
  @logger = Logging.logger[self]

  @boltdir = boltdir
  @concurrency = 100
  @compile_concurrency = Concurrent.processor_count
  @transport = 'ssh'
  @format = 'human'
  @puppetdb = {}
  @color = true

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

  @transports = {}
  TRANSPORTS.each_key do |transport|
    @transports[transport] = TRANSPORT_DEFAULTS.merge(TRANSPORT_SPECIFIC_DEFAULTS[transport])
  end

  update_from_file(config_data)
  apply_overrides(overrides)

  validate
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#compile_concurrencyObject

Returns the value of attribute compile_concurrency.



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

def compile_concurrency
  @compile_concurrency
end

#concurrencyObject

Returns the value of attribute concurrency.



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

def concurrency
  @concurrency
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#inventoryfileObject

Returns the value of attribute inventoryfile.



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

def inventoryfile
  @inventoryfile
end

#logObject

Returns the value of attribute log.



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

def log
  @log
end

#modulepathObject



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

def modulepath
  @modulepath || @boltdir.modulepath
end

#puppetdbObject

Returns the value of attribute puppetdb.



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

def puppetdb
  @puppetdb
end

#traceObject

Returns the value of attribute trace.



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

def trace
  @trace
end

#transportObject

Returns the value of attribute transport.



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

def transport
  @transport
end

#transportsObject

Returns the value of attribute transports.



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

def transports
  @transports
end

Class Method Details

.defaultObject



58
59
60
# File 'lib/bolt/config.rb', line 58

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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bolt/config.rb', line 62

def self.from_boltdir(boltdir, overrides = {})
  # *Optionally* load the boltdir config file, and fall back to the legacy
  # config if that isn't found. Because logging is built in to the
  # legacy_conf method, we don't want to look that up unless we need it.
  configs = if boltdir.config_file.exist?
              [boltdir.config_file]
            else
              [legacy_conf]
            end

  data = Bolt::Util.read_config_file(nil, configs, 'config') || {}

  new(boltdir, data, overrides)
end

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



77
78
79
80
81
82
# File 'lib/bolt/config.rb', line 77

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

.legacy_confObject

TODO: This is deprecated in 0.21.0 and can be removed in release 0.22.0.



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bolt/config.rb', line 163

def self.legacy_conf
  root_path = File.expand_path(File.join('~', '.puppetlabs'))
  legacy_paths = [File.join(root_path, 'bolt.yaml'), File.join(root_path, 'bolt.yml')]
  legacy_conf = legacy_paths.find { |path| File.exist?(path) }
  found_legacy_conf = !!legacy_conf
  legacy_conf ||= legacy_paths[0]
  if found_legacy_conf
    correct_path = Bolt::Boltdir.default_boltdir.config_file
    msg = "Found configfile at deprecated location #{legacy_conf}. Global config should be in #{correct_path}"
    Logging.logger[self].warn(msg)
  end
  legacy_conf
end

Instance Method Details

#apply_overrides(options) ⇒ Object



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

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

  if options[:debug]
    @log['console'][:level] = :debug
  elsif options[:verbose]
    @log['console'][:level] = :info
  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

#deep_cloneObject



109
110
111
# File 'lib/bolt/config.rb', line 109

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

#default_inventoryfileObject



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

def default_inventoryfile
  [@boltdir.inventory_file]
end

#hiera_configObject



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

def hiera_config
  @hiera_config || @boltdir.hiera_config
end

#normalize_log(target) ⇒ Object



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

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

#puppetfileObject



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

def puppetfile
  @boltdir.puppetfile
end

#transport_confObject



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

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

#update_from_inventory(data) ⇒ Object



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

def update_from_inventory(data)
  update_from_file(data)

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

#update_logs(logs) ⇒ Object



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

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



241
242
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
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/bolt/config.rb', line 241

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 * Concurrent.processor_count
  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

  if @hiera_config && !(File.file?(@hiera_config) && File.readable?(@hiera_config))
    raise Bolt::FileError, "Could not read hiera-config file #{@hiera_config}", @hiera_config
  end

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

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