Module: PuppetDBCLI::Utils

Defined in:
lib/puppetdb_cli/utils.rb

Overview

Utils for PuppetDBCLI

Primarily used for interaction with the PuppetDB::Client

Class Method Summary collapse

Class Method Details

.construct_config_overrides(cli_opts) ⇒ Object


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

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


7
8
9
10
11
# File 'lib/puppetdb_cli/utils.rb', line 7

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


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

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


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

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