Class: Pvectl::Commands::Ping

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/ping.rb

Overview

Handler for the ‘pvectl ping` command.

Verifies connectivity to the Proxmox cluster by calling the version endpoint and measuring response time. Provides a quick health check without detailed resource information.

Examples:

Basic usage

pvectl ping
# Output: OK - Connected to pve1.example.com

Wide output with latency

pvectl ping -o wide
# Output: OK - Connected to pve1.example.com | Latency: 45ms

JSON output for scripts

pvectl ping -o json
# Output: {"status":"ok","server":"pve1.example.com","latency_ms":45}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_options) ⇒ Ping

Creates a new Ping command instance.

Parameters:

  • global_options (Hash)

    global CLI options



76
77
78
79
80
81
# File 'lib/pvectl/commands/ping.rb', line 76

def initialize(global_options)
  @global_options = global_options
  @output_format = global_options[:output] || "table"
  @color_flag = global_options[:color]
  @config = nil
end

Class Method Details

.execute(global_options) ⇒ Integer

Executes the ping command.

Parameters:

  • global_options (Hash)

    global CLI options

    • :config [String, nil] path to config file

    • :output [String] output format (table, wide, json, yaml)

    • :color [Boolean, nil] explicit color flag

Returns:

  • (Integer)

    exit code (0 for success, 4 for connection error)



69
70
71
# File 'lib/pvectl/commands/ping.rb', line 69

def self.execute(global_options)
  new(global_options).execute
end

.register(cli) ⇒ void

This method returns an undefined value.

Registers the ping command with the CLI.

Parameters:

  • cli (GLI::App)

    the CLI application object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pvectl/commands/ping.rb', line 28

def self.register(cli)
  cli.desc "Check connectivity to Proxmox cluster"
  cli.long_desc <<~HELP
    Verify connectivity to the Proxmox cluster by calling the version
    API endpoint and measuring response time.

    EXAMPLES
      Basic connectivity check:
        $ pvectl ping

      Wide output with latency:
        $ pvectl ping -o wide

      JSON output for scripting:
        $ pvectl ping -o json

    NOTES
      Uses the configured context (or PROXMOX_HOST environment variable)
      to determine which server to ping.

      Exit code 0 = connected, exit code 4 = connection error.

    SEE ALSO
      pvectl help config          Manage cluster configuration
      pvectl help get nodes       List cluster nodes
  HELP
  cli.command :ping do |c|
    c.action do |global_options, _options, _args|
      exit_code = execute(global_options)
      exit exit_code if exit_code != 0
    end
  end
end

Instance Method Details

#executeInteger

Executes the ping operation.

Returns:

  • (Integer)

    exit code



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pvectl/commands/ping.rb', line 86

def execute
  load_config
  connection = Pvectl::Connection.new(@config)

  result = measure_ping(connection)
  output_result(result, @config.server)

  ExitCodes::SUCCESS
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError => e
  # Re-raise config errors to be handled by CLI error handler
  raise
rescue Timeout::Error
  output_error("Connection timed out", server_url)
  ExitCodes::CONNECTION_ERROR
rescue Errno::ECONNREFUSED
  output_error("Connection refused", server_url)
  ExitCodes::CONNECTION_ERROR
rescue SocketError => e
  output_error(e.message, server_url)
  ExitCodes::CONNECTION_ERROR
rescue StandardError => e
  output_error(e.message, server_url)
  ExitCodes::CONNECTION_ERROR
end