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' }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, project_path = nil) ⇒ Config

Returns a new instance of Config.



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

def initialize(settings, project_path = nil)
  @settings = settings
  expand_paths(project_path)
end

Class Method Details

.default_windows_configObject



20
21
22
23
# File 'lib/bolt/puppetdb/config.rb', line 20

def self.default_windows_config
  require 'win32/dir'
  File.expand_path(File.join(Dir::COMMON_APPDATA, 'PuppetLabs/client-tools/puppetdb.conf'))
end

.load_config(options, project_path = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bolt/puppetdb/config.rb', line 25

def self.load_config(options, project_path = nil)
  config = {}
  global_path = Bolt::Util.windows? ? default_windows_config : DEFAULT_CONFIG[:global]

  if File.exist?(DEFAULT_CONFIG[:user])
    filepath = DEFAULT_CONFIG[:user]
  elsif File.exist?(global_path)
    filepath = global_path
  end

  begin
    config = JSON.parse(File.read(filepath)) if filepath
  rescue StandardError => e
    Logging.logger[self].error("Could not load puppetdb.conf from #{filepath}: #{e.message}")
  end

  config = config.fetch('puppetdb', {})
  new(config.merge(options), project_path)
end

Instance Method Details

#cacertObject



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

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

#certObject



116
117
118
119
120
# File 'lib/bolt/puppetdb/config.rb', line 116

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

#expand_paths(project_path) ⇒ Object



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

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

#keyObject



122
123
124
125
126
# File 'lib/bolt/puppetdb/config.rb', line 122

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

#server_urlsObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bolt/puppetdb/config.rb', line 77

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

#to_hashObject



135
136
137
# File 'lib/bolt/puppetdb/config.rb', line 135

def to_hash
  @settings.dup
end

#tokenObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bolt/puppetdb/config.rb', line 50

def token
  return @token if @token
  # Allow nil in config to skip loading a token
  if @settings.include?('token')
    if @settings['token']
      @token = File.read(@settings['token'])
    end
  elsif File.exist?(DEFAULT_TOKEN)
    @token = File.read(DEFAULT_TOKEN)
  end
  @token = @token.strip if @token
end

#uriObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bolt/puppetdb/config.rb', line 90

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



128
129
130
131
132
133
# File 'lib/bolt/puppetdb/config.rb', line 128

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



70
71
72
73
74
75
# File 'lib/bolt/puppetdb/config.rb', line 70

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