Class: Pvectl::Commands::ConsoleVm
- Inherits:
-
Object
- Object
- Pvectl::Commands::ConsoleVm
- Defined in:
- lib/pvectl/commands/console_vm.rb,
sig/pvectl/commands/console_vm.rbs
Overview
Handler for the pvectl console vm command.
Opens an interactive terminal console session to a QEMU virtual machine via WebSocket-based termproxy. Requires a running VM and interactive terminal (TTY).
Class Method Summary collapse
-
.execute(vmid, options, global_options) ⇒ Integer
Executes the console VM command.
Instance Method Summary collapse
-
#execute ⇒ Integer
Executes the console flow.
-
#initialize(vmid, options, global_options) ⇒ ConsoleVm
constructor
Initializes a console VM command.
-
#load_config ⇒ void
Loads configuration from file or environment.
-
#not_found(message) ⇒ Integer
Outputs a not-found error and returns the exit code.
-
#prompt_password ⇒ String?
Prompts the user for a password (hidden input).
-
#prompt_username(default = nil) ⇒ String?
Prompts the user for a username.
-
#resolve_credentials ⇒ Array<String, String>, Array<nil, nil>
Resolves authentication credentials for the console session.
-
#resource_path ⇒ String
Returns the API resource path for a QEMU VM.
-
#usage_error(message) ⇒ Integer
Outputs a usage error and returns the exit code.
Constructor Details
#initialize(vmid, options, global_options) ⇒ ConsoleVm
Initializes a console VM command.
35 36 37 38 39 |
# File 'lib/pvectl/commands/console_vm.rb', line 35 def initialize(vmid, , ) @vmid = vmid = = end |
Class Method Details
.execute(vmid, options, global_options) ⇒ Integer
Executes the console VM command.
26 27 28 |
# File 'lib/pvectl/commands/console_vm.rb', line 26 def self.execute(vmid, , ) new(vmid, , ).execute end |
Instance Method Details
#execute ⇒ Integer
Executes the console flow.
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 |
# File 'lib/pvectl/commands/console_vm.rb', line 44 def execute return usage_error("VMID is required") unless @vmid return usage_error("Console requires an interactive terminal (TTY)") unless $stdin.tty? load_config connection = Pvectl::Connection.new(@config) repo = Pvectl::Repositories::Vm.new(connection) resource = repo.get(@vmid.to_i) return not_found("VM #{@vmid} not found") unless resource username, password = resolve_credentials return ExitCodes::GENERAL_ERROR if username.nil? || password.nil? $stderr.puts "Connecting to VM #{resource.vmid} (#{resource.name || 'unnamed'}) on node #{resource.node}..." Pvectl::Services::Console.new.run( resource: resource, resource_path: resource_path, server: @config.server, username: username, password: password, verify_ssl: @config.verify_ssl ) ExitCodes::SUCCESS rescue Services::Console::ResourceNotRunningError => e $stderr.puts "Error: #{e.message}" ExitCodes::GENERAL_ERROR rescue Services::Console::AuthenticationError => e $stderr.puts "Error: #{e.message}" ExitCodes::PERMISSION_DENIED rescue Pvectl::Config::ConfigNotFoundError, Pvectl::Config::InvalidConfigError, Pvectl::Config::ContextNotFoundError, Pvectl::Config::ClusterNotFoundError, Pvectl::Config::UserNotFoundError, Pvectl::Config::MissingCredentialsError raise # re-raise for CLI handler rescue Errno::ECONNREFUSED, SocketError, Timeout::Error => e $stderr.puts "Error: Cannot connect to console: #{e.message}" ExitCodes::CONNECTION_ERROR rescue StandardError => e $stderr.puts "Error: #{e.message}" ExitCodes::GENERAL_ERROR end |
#load_config ⇒ void
This method returns an undefined value.
Loads configuration from file or environment.
162 163 164 165 166 |
# File 'lib/pvectl/commands/console_vm.rb', line 162 def load_config service = Pvectl::Config::Service.new service.load(config: [:config]) @config = service.current_config end |
#not_found(message) ⇒ Integer
Outputs a not-found error and returns the exit code.
181 182 183 184 |
# File 'lib/pvectl/commands/console_vm.rb', line 181 def not_found() $stderr.puts "Error: #{message}" ExitCodes::NOT_FOUND end |
#prompt_password ⇒ String?
Prompts the user for a password (hidden input).
150 151 152 153 154 155 156 157 |
# File 'lib/pvectl/commands/console_vm.rb', line 150 def prompt_password $stderr.print "Password: " password = $stdin.noecho(&:gets)&.strip $stderr.puts # newline after hidden input return nil if password.nil? || password.empty? password end |
#prompt_username(default = nil) ⇒ String?
Prompts the user for a username.
138 139 140 141 142 143 144 145 |
# File 'lib/pvectl/commands/console_vm.rb', line 138 def prompt_username(default = nil) prompt = default ? "Username [#{default}]: " : "Username: " $stderr.print prompt input = $stdin.gets&.strip return nil if input.nil? input.empty? ? default : input end |
#resolve_credentials ⇒ Array<String, String>, Array<nil, nil>
Resolves authentication credentials for the console session.
Priority: CLI flags > config file > interactive prompt. Extracts default username from token_id when available.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pvectl/commands/console_vm.rb', line 106 def resolve_credentials username = [:user] password = [:password] if username.nil? && password.nil? && @config.username && @config.password username = @config.username password = @config.password end if username.nil? && @config.token_id # Extract default username from token_id (e.g., "root@pam!pvectl" -> "root@pam") default_username = @config.token_id.split("!").first end # When credentials come from config (username+password pair), use them directly. # Otherwise, prompt interactively — always show both prompts so the user knows # which username will be used and can change it. if password.nil? username = prompt_username(username || default_username) return [nil, nil] if username.nil? password = prompt_password end return [nil, nil] if password.nil? [username, password] end |
#resource_path ⇒ String
Returns the API resource path for a QEMU VM.
96 97 98 |
# File 'lib/pvectl/commands/console_vm.rb', line 96 def resource_path "qemu/#{@vmid}" end |
#usage_error(message) ⇒ Integer
Outputs a usage error and returns the exit code.
172 173 174 175 |
# File 'lib/pvectl/commands/console_vm.rb', line 172 def usage_error() $stderr.puts "Error: #{message}" ExitCodes::USAGE_ERROR end |