Class: Bolt::PuppetDB::Config

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

Constant Summary collapse

DEFAULT_TOKEN =
Bolt::Util.windows? ? 'nul' : '/dev/null'
DEFAULT_CONFIG =
{ user: '/etc/puppetlabs/puppet/puppetdb.conf',
global: '/etc/puppetlabs/puppet/puppetdb.conf',
win_global: 'C:/ProgramData/PuppetLabs/client-tools/puppetdb.conf' }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Config

Returns a new instance of Config.



41
42
43
44
# File 'lib/bolt/puppetdb/config.rb', line 41

def initialize(settings)
  @settings = settings
  expand_paths
end

Class Method Details

.load_config(filename, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bolt/puppetdb/config.rb', line 22

def self.load_config(filename, options)
  global_path = Bolt::Util.windows? ? DEFAULT_CONFIG[:win_global] : DEFAULT_CONFIG[:global]
  if filename
    if File.exist?(filename)
      config = JSON.parse(File.read(filename))
    else
      raise Bolt::PuppetDBError, "config file #{filename} does not exist"
    end
  elsif File.exist?(DEFAULT_CONFIG[:user])
    config = JSON.parse(File.read(DEFAULT_CONFIG[:user]))
  elsif File.exist?(global_path)
    config = JSON.parse(File.read(global_path))
  else
    config = {}
  end
  config = config.fetch('puppetdb', {})
  new(config.merge(options))
end

Instance Method Details

#cacertObject



87
88
89
90
91
92
93
# File 'lib/bolt/puppetdb/config.rb', line 87

def cacert
  if @settings['cacert'] && validate_file_exists('cacert')
    @settings['cacert']
  else
    raise Bolt::PuppetDBError, "cacert must be specified"
  end
end

#certObject



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

def cert
  validate_cert_and_key
  validate_file_exists('cert')
  @settings['cert']
end

#expand_pathsObject



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

def expand_paths
  %w[cacert cert key token].each do |file|
    @settings[file] = File.expand_path(@settings[file]) if @settings[file]
  end
end

#keyObject



101
102
103
104
105
# File 'lib/bolt/puppetdb/config.rb', line 101

def key
  validate_cert_and_key
  validate_file_exists('key')
  @settings['key']
end

#to_hashObject



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

def to_hash
  @settings.dup
end

#tokenObject



46
47
48
49
50
51
52
53
54
# File 'lib/bolt/puppetdb/config.rb', line 46

def token
  return @token if @token
  if @settings['token']
    @token = File.read(@settings['token'])
  elsif File.exist?(DEFAULT_TOKEN)
    @token = File.read(DEFAULT_TOKEN)
  end
  @token = @token.strip if @token
end

#uriObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bolt/puppetdb/config.rb', line 69

def uri
  return @uri if @uri
  uri = case @settings['server_urls']
        when String
          @settings['server_urls']
        when Array
          @settings['server_urls'].first
        when nil
          raise Bolt::PuppetDBError, "server_urls must be specified"
        else
          raise Bolt::PuppetDBError, "server_urls must be a string or array"
        end

  @uri = URI.parse(uri)
  @uri.port ||= 8081
  @uri
end

#validate_cert_and_keyObject



107
108
109
110
111
112
# File 'lib/bolt/puppetdb/config.rb', line 107

def validate_cert_and_key
  if (@settings['cert'] && !@settings['key']) ||
     (!@settings['cert'] && @settings['key'])
    raise Bolt::PuppetDBError, "cert and key must be specified together"
  end
end

#validate_file_exists(file) ⇒ Object



62
63
64
65
66
67
# File 'lib/bolt/puppetdb/config.rb', line 62

def validate_file_exists(file)
  if @settings[file] && !File.exist?(@settings[file])
    raise Bolt::PuppetDBError, "#{file} file #{@settings[file]} does not exist"
  end
  true
end