class Puppet::Util::Puppetdb::Config

Attributes

config[R]

@!attribute [r] count

@api private

Public Class Methods

convert_and_validate_urls(uri_strings) click to toggle source

@api private

# 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) click to toggle source

Public class methods

# 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
new(config_hash = {}) click to toggle source

@!group Public instance methods

# File lib/puppet/util/puppetdb/config.rb, line 114
def initialize(config_hash = {})
  @config = config_hash
end

Public Instance Methods

command_broadcast() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 142
def command_broadcast
  config[:command_broadcast]
end
include_unchanged_resources?() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 126
def include_unchanged_resources?
  config[:include_unchanged_resources]
end
min_successful_submissions() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 134
def min_successful_submissions
  config[:min_successful_submissions]
end
server_url_timeout() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 122
def server_url_timeout
  config[:server_url_timeout]
end
server_urls() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 118
def server_urls
  config[:server_urls]
end
soft_write_failure() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 130
def soft_write_failure
  config[:soft_write_failure]
end
sticky_read_failover() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 146
def sticky_read_failover
  config[:sticky_read_failover]
end
submit_only_server_urls() click to toggle source
# File lib/puppet/util/puppetdb/config.rb, line 138
def submit_only_server_urls
  config[:submit_only_server_urls]
end