mirror of
https://git.proxmox.com/git/qemu-server
synced 2025-05-01 17:05:54 +00:00
add Agent helper package
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
ce9b0a3807
commit
3824765e64
@ -6,6 +6,7 @@ use warnings;
|
|||||||
use PVE::RESTHandler;
|
use PVE::RESTHandler;
|
||||||
use PVE::JSONSchema qw(get_standard_option);
|
use PVE::JSONSchema qw(get_standard_option);
|
||||||
use PVE::QemuServer;
|
use PVE::QemuServer;
|
||||||
|
use PVE::QemuServer::Agent qw(agent_available);
|
||||||
|
|
||||||
use base qw(PVE::RESTHandler);
|
use base qw(PVE::RESTHandler);
|
||||||
|
|
||||||
@ -172,9 +173,7 @@ sub register_command {
|
|||||||
|
|
||||||
my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
|
my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
|
||||||
|
|
||||||
die "No Qemu Guest Agent\n" if !defined($conf->{agent});
|
agent_available($vmid, $conf);
|
||||||
die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
|
|
||||||
die "Qemu Guest Agent is not running\n" if !PVE::QemuServer::qga_check_running($vmid, 1);
|
|
||||||
|
|
||||||
my $cmd = $param->{command} // $command;
|
my $cmd = $param->{command} // $command;
|
||||||
my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd");
|
my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd");
|
||||||
|
65
PVE/QemuServer/Agent.pm
Normal file
65
PVE/QemuServer/Agent.pm
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package PVE::QemuServer::Agent;
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PVE::QemuServer;
|
||||||
|
use base 'Exporter';
|
||||||
|
|
||||||
|
our @EXPORT_OK = qw(
|
||||||
|
check_agent_error
|
||||||
|
agent_available
|
||||||
|
agent_cmd
|
||||||
|
);
|
||||||
|
|
||||||
|
sub check_agent_error {
|
||||||
|
my ($result, $errmsg, $noerr) = @_;
|
||||||
|
|
||||||
|
$errmsg //= '';
|
||||||
|
my $error = '';
|
||||||
|
if (ref($result) eq 'HASH' && $result->{error} && $result->{error}->{desc}) {
|
||||||
|
$error = "Agent Error: $result->{error}->{desc}\n";
|
||||||
|
} elsif (!defined($result)) {
|
||||||
|
$error = "Agent Error: $errmsg\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($error) {
|
||||||
|
die $error if !$noerr;
|
||||||
|
|
||||||
|
warn $error;
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub agent_available {
|
||||||
|
my ($vmid, $conf, $noerr) = @_;
|
||||||
|
|
||||||
|
eval {
|
||||||
|
die "No Qemu Guest Agent\n" if !defined($conf->{agent});
|
||||||
|
die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
|
||||||
|
die "Qemu Guest Agent is not running\n" if !PVE::QemuServer::qga_check_running($vmid, 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (my $err = $@) {
|
||||||
|
die $err if !$noerr;
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
# loads config, checks if available, executes command, checks for errors
|
||||||
|
sub agent_cmd {
|
||||||
|
my ($vmid, $cmd, $params, $errormsg, $noerr) = @_;
|
||||||
|
|
||||||
|
my $conf = PVE::QemuConfig->load_config($vmid); # also checks if VM exists
|
||||||
|
agent_available($vmid, $conf, $noerr);
|
||||||
|
|
||||||
|
my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd", %$params);
|
||||||
|
check_agent_error($res, $errormsg, $noerr);
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
@ -4,6 +4,7 @@ SOURCES=PCI.pm \
|
|||||||
ImportDisk.pm \
|
ImportDisk.pm \
|
||||||
OVF.pm \
|
OVF.pm \
|
||||||
Cloudinit.pm \
|
Cloudinit.pm \
|
||||||
|
Agent.pm \
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: ${SOURCES}
|
install: ${SOURCES}
|
||||||
|
Loading…
Reference in New Issue
Block a user