Class: Pvectl::Services::Sendkey

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/services/sendkey.rb

Overview

Orchestrates QEMU monitor key-send operations for a single VM.

Resolves the VM (and its node) via the repository, validates that the target VM is running, and forwards the key sequence verbatim to the Proxmox API. The key string is passed through unmodified — interpretation is delegated to QEMU’s qcode parser.

Examples:

Sending Ctrl+Alt+Delete to a running VM

service = Sendkey.new(vm_repository: vm_repo)
result = service.execute(vmid: 100, key: "ctrl-alt-delete")
result.successful? #=> true

Instance Method Summary collapse

Constructor Details

#initialize(vm_repository:) ⇒ Sendkey

Creates a new Sendkey service.

Parameters:



21
22
23
# File 'lib/pvectl/services/sendkey.rb', line 21

def initialize(vm_repository:)
  @vm_repository = vm_repository
end

Instance Method Details

#execute(vmid:, key:, node: nil) ⇒ Models::VmOperationResult

Sends a QEMU key sequence to a VM.

Looks up the VM (the node kwarg is honored when provided, otherwise the node is taken from the resolved VM). Returns a failed VmOperationResult when the VM cannot be found, is not running, or the underlying API call raises.

Parameters:

  • vmid (Integer, String)

    VM identifier

  • key (String)

    QEMU qcode key sequence (e.g., “ctrl-alt-delete”)

  • node (String, nil) (defaults to: nil)

    optional node override

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pvectl/services/sendkey.rb', line 36

def execute(vmid:, key:, node: nil)
  vmid_int = vmid.to_i

  vm = @vm_repository.get(vmid_int)
  return not_found_result(vmid_int) unless vm

  target_node = node || vm.node
  return not_running_result(vm, target_node, key) unless vm.status == "running"

  @vm_repository.sendkey(vmid_int, target_node, key)

  success_result(vm, target_node, key)
rescue StandardError => e
  failure_result(vm, target_node || vm&.node, key, e.message)
end