qemu-server/PVE/QemuServer/Monitor.pm
Stefan Reiter 0a13e08ec2 refactor: create QemuServer::Monitor for high-level QMP access
QMP and monitor helpers are moved from QemuServer.pm.

By using only vm_running_locally instead of check_running, a cyclic
dependency to QemuConfig is avoided. This also means that the $nocheck
parameter serves no more purpose, and has thus been removed along with
vm_mon_cmd_nocheck.

Care has been taken to avoid errors resulting from this, and
occasionally a manual check for a VM's existance inserted on the
callsite.

Methods have been renamed to avoid redundant naming:
* vm_qmp_command -> qmp_cmd
* vm_mon_cmd -> mon_cmd
* vm_human_monitor_command -> hmp_cmd

mon_cmd is exported since it has many users. This patch also changes all
non-package users of vm_qmp_command to use the mon_cmd helper. Includes
mocking for tests.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
2019-11-20 16:29:23 +01:00

63 lines
1.2 KiB
Perl

package PVE::QemuServer::Monitor;
use strict;
use warnings;
use PVE::SafeSyslog;
use PVE::QemuServer::Helpers;
use PVE::QMPClient;
use base 'Exporter';
our @EXPORT_OK = qw(
mon_cmd
);
sub qmp_cmd {
my ($vmid, $cmd) = @_;
my $res;
my $timeout;
if ($cmd->{arguments}) {
$timeout = delete $cmd->{arguments}->{timeout};
}
eval {
die "VM $vmid not running\n" if !PVE::QemuServer::Helpers::vm_running_locally($vmid);
my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid);
if (-e $sname) { # test if VM is reasonably new and supports qmp/qga
my $qmpclient = PVE::QMPClient->new();
$res = $qmpclient->cmd($vmid, $cmd, $timeout);
} else {
die "unable to open monitor socket\n";
}
};
if (my $err = $@) {
syslog("err", "VM $vmid qmp command failed - $err");
die $err;
}
return $res;
}
sub mon_cmd {
my ($vmid, $execute, %params) = @_;
my $cmd = { execute => $execute, arguments => \%params };
qmp_cmd($vmid, $cmd);
}
sub hmp_cmd {
my ($vmid, $cmdline) = @_;
my $cmd = {
execute => 'human-monitor-command',
arguments => { 'command-line' => $cmdline },
};
return qmp_cmd($vmid, $cmd);
}
1;