Class: OctocatalogDiff::PuppetDB
- Inherits:
-
Object
- Object
- OctocatalogDiff::PuppetDB
- 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
-
#connections ⇒ Object
readonly
Allow connections to be read (used in tests for now).
Instance Method Summary collapse
-
#get(path) ⇒ Object
Wrapper around the httparty call in the private _get method.
-
#initialize(options = {}) ⇒ PuppetDB
constructor
Constructor - will construct connection parameters from a variety of sources, including arguments and environment variables.
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:
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( = {}) @connections = if .key?(:puppetdb_url) urls = [:puppetdb_url].is_a?(Array) ? [:puppetdb_url] : [[:puppetdb_url]] urls.map { |url| parse_url(url) } elsif .key?(:puppetdb_host) is_ssl = .fetch(:puppetdb_ssl, true) default_port = is_ssl ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT port = .fetch(:puppetdb_port, default_port).to_i [{ ssl: is_ssl, host: [: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 = .fetch(:timeout, 10) @options = end |
Instance Attribute Details
#connections ⇒ Object (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.
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.}" end |