Module: Beaker::DSL::Helpers::TKHelpers

Included in:
BeakerPuppet
Defined in:
lib/beaker-puppet/helpers/tk_helpers.rb

Overview

Convenience methods for modifying and reading TrapperKeeper configs

Instance Method Summary collapse

Instance Method Details

#modify_tk_config(host, config_file_path, options_hash, replace = false) ⇒ Object

Note:

TrapperKeeper config files can be HOCON, JSON, or Ini. We don’t

Modify the given TrapperKeeper config file.

particularly care which of these the file named by ‘config_file_path` on the SUT actually is, just that the contents can be parsed into a map.

Parameters:

  • host (Host)

    A host object

  • options_hash (OptionsHash)

    New hash which will be merged into the given TrapperKeeper config.

  • config_file_path (String)

    Path to the TrapperKeeper config on the given host which is to be modified.

  • replace (Bool) (defaults to: false)

    If set true, instead of updating the existing TrapperKeeper configuration, replace it entirely with the contents of the given hash.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/beaker-puppet/helpers/tk_helpers.rb', line 26

def modify_tk_config(host, config_file_path, options_hash, replace = false)
  return nil if options_hash.empty?

  new_hash = Beaker::Options::OptionsHash.new

  if replace
    new_hash.merge!(options_hash)
  else
    raise "Error: #{config_file_path} does not exist on #{host}" unless host.file_exist?(config_file_path)

    file_string = host.exec(Command.new("cat #{config_file_path}")).stdout

    begin
      tk_conf_hash = read_tk_config_string(file_string)
    rescue RuntimeError
      raise "Error reading trapperkeeper config: #{config_file_path} at host: #{host}"
    end

    new_hash.merge!(tk_conf_hash)
    new_hash.merge!(options_hash)
  end

  file_string = JSON.pretty_generate(new_hash)
  create_remote_file host, config_file_path, file_string
end