Module: PuppetDBCLI::Utils

Defined in:
lib/puppetdb_cli/utils.rb,
lib/puppetdb_cli/utils/default_options.rb

Overview

Utils for PuppetDBCLI

Primarily used for interaction with the PuppetDB::Client

Defined Under Namespace

Modules: DefaultOptions

Class Method Summary collapse

Class Method Details

.construct_config_overrides(cli_opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/puppetdb_cli/utils.rb', line 15

def self.construct_config_overrides(cli_opts)
  {
    config_file: cli_opts[:config],
    server_urls: cli_opts[:urls]&.split(','),
    key: cli_opts[:key],
    cert: cli_opts[:cert],
    cacert: cli_opts[:cacert],
    token_file: cli_opts[:token]
  }.delete_if { |_, v| v.nil? }
end

.log_command_start(name, opts, args) ⇒ Object



9
10
11
12
13
# File 'lib/puppetdb_cli/utils.rb', line 9

def self.log_command_start(name, opts, args)
  PuppetDBCLI.logger.debug "Running the #{name} command"
  PuppetDBCLI.logger.debug "CLI options: #{opts}"
  PuppetDBCLI.logger.debug "CLI arguments: #{args.to_a}"
end

.open_client_connection(cli_opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppetdb_cli/utils.rb', line 26

def self.open_client_connection(cli_opts)
  config_overrides = construct_config_overrides cli_opts
  PuppetDBCLI.logger.debug "Initializing client connection with configuration overrides: #{config_overrides}"

  PuppetDB::Client.new(config_overrides)
rescue URI::InvalidURIError => e
  PuppetDBCLI.logger.fatal "The provided PuppetDB server url was invalid. Failed with message '#{e.message}'"
  exit 1
# This will catch errors like SocketError from HTTParty and RuntimeError from puppetdb-ruby
rescue RuntimeError => e
  PuppetDBCLI.logger.fatal e.message
  exit 1
end

.send_query(client, query) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppetdb_cli/utils.rb', line 40

def self.send_query(client, query)
  PuppetDBCLI.logger.debug "Sending query request '#{query}'"

  client.request('', query, query_mode: :failover)
rescue SocketError => e
  PuppetDBCLI.logger.fatal e.message
  exit 1
rescue PuppetDB::APIError => e
  puts e.response
  PuppetDBCLI.logger.fatal "Last PuppetDB API response code #{e.response&.code}"
  exit 1
end