Class: Puppet::Util::Puppetdb::Config

Inherits:
Object
  • Object
show all
Includes:
CommandNames
Defined in:
lib/puppet/util/puppetdb/config.rb

Constant Summary

Constants included from CommandNames

Puppet::Util::Puppetdb::CommandNames::CommandDeactivateNode, Puppet::Util::Puppetdb::CommandNames::CommandReplaceCatalog, Puppet::Util::Puppetdb::CommandNames::CommandReplaceFacts, Puppet::Util::Puppetdb::CommandNames::CommandStoreReport

Private instance methods collapse

Public instance methods collapse

Private instance methods collapse

Class Method Summary collapse

Constructor Details

#initialize(config_hash = {}) ⇒ Config

Returns a new instance of Config.



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

def initialize(config_hash = {})
  @config = config_hash
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



154
155
156
# File 'lib/puppet/util/puppetdb/config.rb', line 154

def config
  @config
end

#countObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
# File 'lib/puppet/util/puppetdb/config.rb', line 154

attr_reader :config

Class Method Details

.convert_and_validate_urls(uri_strings) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppet/util/puppetdb/config.rb', line 157

def self.convert_and_validate_urls(uri_strings)
  uri_strings.map do |uri_string|

    begin
      uri = URI(uri_string.strip)
    rescue URI::InvalidURIError => e
      raise URI::InvalidURIError.new, "Error parsing URL '#{uri_string}' in PuppetDB 'server_urls', error message was '#{e.message}'"
    end

    if uri.scheme != 'https'
      raise "PuppetDB 'server_urls' must be https, found '#{uri_string}'"
    end

    if uri.path != '' && uri.path != '/'
      raise "PuppetDB 'server_urls' cannot contain URL paths, found '#{uri_string}'"
    end
    uri.path = ''
    uri
  end
end

.load(config_file = nil) ⇒ Object

Public class methods



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
52
53
54
55
56
57
58
59
60
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/util/puppetdb/config.rb', line 10

def self.load(config_file = nil)
  defaults = {
    :server_urls                 => "https://puppetdb:8081",
    :soft_write_failure          => false,
    :server_url_timeout          => 30,
    :include_unchanged_resources => false,
    :min_successful_submissions => 1,
    :submit_only_server_urls   => "",
    :command_broadcast         => false,
    :sticky_read_failover      => false
  }

  config_file ||= File.join(Puppet[:confdir], "puppetdb.conf")

  if File.exists?(config_file)
    Puppet.debug("Configuring PuppetDB terminuses with config file #{config_file}")
    content = File.read(config_file)
  else
    Puppet.debug("No #{config_file} file found; falling back to default server_urls #{defaults[:server_urls]}")
    content = ''
  end

  result = {}
  section = nil
  content.lines.each_with_index do |line,number|
    # Gotta track the line numbers properly
    number += 1
    case line
    when /^\[(\w+)\s*\]$/
      section = $1
      result[section] ||= {}

    when /^\s*(server|port)\s*=.*$/
      Puppet.warning("Setting '#{line.chomp}' is retired: use 'server_urls' instead. Defaulting to 'server_urls=https://puppetdb:8081'.")
    when /^\s*(\w+)\s*=\s*(\S+|[\S+\s*\,\s*\S]+)\s*$/
      raise "Setting '#{line}' is illegal outside of section in PuppetDB config #{config_file}:#{number}" unless section
      result[section][$1] = $2
    when /^\s*[#;]/
      # Skip comments
    when /^\s*$/
      # Skip blank lines
    else
      raise "Unparseable line '#{line}' in PuppetDB config #{config_file}:#{number}"
    end
  end

  main_section = result['main'] || {}
  # symbolize the keys
  main_section = main_section.inject({}) {|h, (k,v)| h[k.to_sym] = v ; h}

  # merge with defaults but filter out anything except the legal settings
  config_hash = defaults.merge(main_section).reject do |k, v|
    !([:server_urls,
       :ignore_blacklisted_events,
       :include_unchanged_resources,
       :soft_write_failure,
       :server_url_timeout,
       :min_successful_submissions,
       :submit_only_server_urls,
       :command_broadcast,
       :sticky_read_failover].include?(k))
  end

  parsed_urls = config_hash[:server_urls].split(",").map {|s| s.strip}
  config_hash[:server_urls] = convert_and_validate_urls(parsed_urls)

  config_hash[:server_url_timeout] = config_hash[:server_url_timeout].to_i
  config_hash[:include_unchanged_resources] = Puppet::Util::Puppetdb.to_bool(config_hash[:include_unchanged_resources])
  config_hash[:soft_write_failure] = Puppet::Util::Puppetdb.to_bool(config_hash[:soft_write_failure])

  config_hash[:submit_only_server_urls] = convert_and_validate_urls(config_hash[:submit_only_server_urls].split(",").map {|s| s.strip})
  config_hash[:min_successful_submissions] = config_hash[:min_successful_submissions].to_i
  config_hash[:command_broadcast] = Puppet::Util::Puppetdb.to_bool(config_hash[:command_broadcast])
  config_hash[:sticky_read_failover] = Puppet::Util::Puppetdb.to_bool(config_hash[:sticky_read_failover])

  if config_hash[:soft_write_failure] and config_hash[:min_successful_submissions] > 1
    raise "soft_write_failure cannot be enabled when min_successful_submissions is greater than 1"
  end

  overlapping_server_urls = config_hash[:server_urls] & config_hash[:submit_only_server_urls]
  if overlapping_server_urls.length > 0
    overlapping_server_urls_strs = overlapping_server_urls.map { |u| u.to_s }
    raise "Server URLs must be in either server_urls or submit_only_server_urls, not both. "\
      "(#{overlapping_server_urls_strs.to_s} are in both)"
  end

  if config_hash[:min_successful_submissions] > 1 and not config_hash[:command_broadcast]
    raise "command_broadcast must be set to true to use min_successful_submissions"
  end

  if config_hash[:min_successful_submissions] > config_hash[:server_urls].length
    raise "min_successful_submissions (#{config_hash[:min_successful_submissions]}) must be less than "\
      "or equal to the number of server_urls (#{config_hash[:server_urls].length})"
  end

  self.new(config_hash)
rescue => detail
  Puppet.warning "Could not configure PuppetDB terminuses: #{detail}"
  Puppet.warning detail.backtrace if Puppet[:trace]
  raise
end

Instance Method Details

#command_broadcastObject



142
143
144
# File 'lib/puppet/util/puppetdb/config.rb', line 142

def command_broadcast
  config[:command_broadcast]
end

#include_unchanged_resources?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/puppet/util/puppetdb/config.rb', line 126

def include_unchanged_resources?
  config[:include_unchanged_resources]
end

#min_successful_submissionsObject



134
135
136
# File 'lib/puppet/util/puppetdb/config.rb', line 134

def min_successful_submissions
  config[:min_successful_submissions]
end

#server_url_timeoutObject



122
123
124
# File 'lib/puppet/util/puppetdb/config.rb', line 122

def server_url_timeout
  config[:server_url_timeout]
end

#server_urlsObject



118
119
120
# File 'lib/puppet/util/puppetdb/config.rb', line 118

def server_urls
  config[:server_urls]
end

#soft_write_failureObject



130
131
132
# File 'lib/puppet/util/puppetdb/config.rb', line 130

def soft_write_failure
  config[:soft_write_failure]
end

#sticky_read_failoverObject



146
147
148
# File 'lib/puppet/util/puppetdb/config.rb', line 146

def sticky_read_failover
  config[:sticky_read_failover]
end

#submit_only_server_urlsObject



138
139
140
# File 'lib/puppet/util/puppetdb/config.rb', line 138

def submit_only_server_urls
  config[:submit_only_server_urls]
end