Class: OctocatalogDiff::PuppetDB

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/puppetdb.rb

Overview

A standard way to connect to PuppetDB from the various scripts in this repository.

Constant Summary collapse

DEFAULT_HTTPS_PORT =
8081
DEFAULT_HTTP_PORT =
8080

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PuppetDB

Constructor - will construct connection parameters from a variety of sources, including arguments and environment variables. Supported environment variables:

PUPPETDB_URL
PUPPETDB_HOST [+ PUPPETDB_PORT] [+ PUPPETDB_SSL]

Order of precedence:

1. :puppetdb_url argument (String or Array<String>)
2. :puppetdb_host argument [+ :puppetdb_port] [+ :puppetdb_ssl]
3. ENV['PUPPETDB_URL']
4. ENV['PUPPETDB_HOST'] [+ ENV['PUPPETDB_PORT']], [+ ENV['PUPPETDB_SSL']]

When it finds one of these, it stops and does not process any others.

When :puppetdb_url is an array, all given URLs are tried, in random order, until a connection succeeds. If a connection succeeds, any errors from previously failed connections are suppressed.

Supported arguments:

Parameters:

  • :puppetdb_url (String or Array<String>)

    PuppetDB URL(s) to try in random order

  • :puppetdb_host (String)

    PuppetDB hostname, when constructing a URL

  • :puppetdb_port (Integer)

    Port number, defaults to 8080 (non-SSL) or 8081 (SSL)

  • :puppetdb_ssl (Boolean)

    defaults to true, because you should use SSL

  • :puppetdb_ssl_ca (String)

    Path to file containing CA certificate

  • :puppetdb_ssl_verify (Boolean)

    Override the CA verification setting guessed from parameters

  • :puppetdb_ssl_client_pem (String)

    PEM-encoded client key and certificate

  • :puppetdb_ssl_client_p12 (String)

    pkcs12-encoded client key and certificate

  • :puppetdb_ssl_client_password (String)

    Path to file containing password for SSL client key (any format)

  • :puppetdb_ssl_client_auth (Boolean)

    Override the client-auth that is guessed from parameters

  • :puppetdb_token (String)

    PE RBAC token to authenticate to PuppetDB API

  • :timeout (Integer)

    Connection timeout for PuppetDB (default=10)


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
# File 'lib/octocatalog-diff/puppetdb.rb', line 47

def initialize(options = {})
  @connections =
    if options.key?(:puppetdb_url)
      urls = options[:puppetdb_url].is_a?(Array) ? options[:puppetdb_url] : [options[:puppetdb_url]]
      urls.map { |url| parse_url(url) }
    elsif options.key?(:puppetdb_host)
      is_ssl = options.fetch(:puppetdb_ssl, true)
      default_port = is_ssl ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT
      port = options.fetch(:puppetdb_port, default_port).to_i
      [{ ssl: is_ssl, host: options[:puppetdb_host], port: port }]
    elsif ENV['PUPPETDB_URL'] && !ENV['PUPPETDB_URL'].empty?
      [parse_url(ENV['PUPPETDB_URL'])]
    elsif ENV['PUPPETDB_HOST'] && !ENV['PUPPETDB_HOST'].empty?
      # Because environment variables are strings...
      # This will get the env var and see if it equals 'true'; the result
      # of this == comparison is the true/false boolean we need.
      is_ssl = ENV.fetch('PUPPETDB_SSL', 'true') == 'true'
      default_port = is_ssl ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT
      port = ENV.fetch('PUPPETDB_PORT', default_port).to_i
      [{ ssl: is_ssl, host: ENV['PUPPETDB_HOST'], port: port }]
    else
      []
    end
  @timeout = options.fetch(:timeout, 10)
  @options = options
end

Instance Attribute Details

#connectionsObject (readonly)

Allow connections to be read (used in tests for now)


15
16
17
# File 'lib/octocatalog-diff/puppetdb.rb', line 15

def connections
  @connections
end

Instance Method Details

#get(path) ⇒ Object

Wrapper around the httparty call in the private _get method. Returns the parsed result of getting the provided URL and returns a friendlier error message if there are network connection problems to PuppetDB.

Parameters:

  • path (String)

    Path portion of the URL

Returns:

  • (Object)

    Parsed reply from PuppetDB as an object


80
81
82
83
84
# File 'lib/octocatalog-diff/puppetdb.rb', line 80

def get(path)
  _get(path)
rescue Net::OpenTimeout, Errno::ECONNREFUSED => exc
  raise OctocatalogDiff::Errors::PuppetDBConnectionError, "#{exc.class} connecting to PuppetDB (need VPN on?): #{exc.message}"
end