Class: Pvectl::Commands::Console

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

Overview

CLI registration for the ‘pvectl console` command.

Opens an interactive terminal console to a VM or container via WebSocket-based termproxy. Dispatches to ConsoleVm or ConsoleCt based on the resource type argument.

Examples:

Open console to a VM

pvectl console vm 100

Open console to a container

pvectl console ct 200

Open console with explicit credentials

pvectl console vm 100 --user root@pam --password secret

Constant Summary collapse

SUPPORTED_RESOURCES =

Supported resource type arguments.

%w[vm ct container].freeze

Class Method Summary collapse

Class Method Details

.register(cli) ⇒ void

This method returns an undefined value.

Registers the console 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pvectl/commands/console.rb', line 28

def self.register(cli)
  cli.desc "Open interactive terminal console to a VM or container"
  cli.long_desc <<~HELP
    Open an interactive terminal session to a VM or container via
    WebSocket-based termproxy (xtermjs protocol).

    EXAMPLES
      Open console to a VM:
        $ pvectl console vm 100

      Open console to a container:
        $ pvectl console ct 200

      Specify node explicitly:
        $ pvectl console vm 100 --node pve1

      Provide credentials (prompted for password if omitted):
        $ pvectl console vm 100 --user root@pam

    NOTES
      Console requires session authentication (username/password). If your
      config only has an API token, pvectl will prompt for credentials.

      Disconnect with Ctrl+].

      The console uses a WebSocket connection. Network interruptions will
      close the session.

    SEE ALSO
      pvectl help describe        View VM/container configuration
      pvectl help start           Start a stopped VM before connecting
  HELP
  cli.arg_name "RESOURCE_TYPE ID"
  cli.command :console do |c|
    c.desc "Filter by node name"
    c.flag [:node, :n], arg_name: "NODE"

    c.desc "Username for session authentication"
    c.flag [:user], arg_name: "USER"

    c.desc "Password for session authentication"
    c.flag [:password], arg_name: "PASSWORD"

    c.action do |global_options, options, args|
      resource_type = args.shift
      resource_id = args.shift

      unless resource_type && SUPPORTED_RESOURCES.include?(resource_type)
        $stderr.puts "Error: Resource type required (vm, ct)"
        exit Pvectl::ExitCodes::USAGE_ERROR
      end

      exit_code = case resource_type
      when "ct", "container"
        ConsoleCt.execute(resource_id, options, global_options)
      else
        ConsoleVm.execute(resource_id, options, global_options)
      end

      exit exit_code if exit_code != 0
    end
  end
end