[pve-devel] [PATCH qemu-server v2 3/8] move guest agent api call to its own file

Dominik Csapak d.csapak at proxmox.com
Thu Feb 15 14:04:48 CET 2018


so we do not pollute the Qemu.pm too much

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/API2/Makefile      |  1 +
 PVE/API2/Qemu.pm       | 70 ++++------------------------------------------
 PVE/API2/Qemu/Agent.pm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/API2/Qemu/Makefile |  6 ++++
 PVE/CLI/qm.pm          |  3 +-
 5 files changed, 91 insertions(+), 65 deletions(-)
 create mode 100644 PVE/API2/Qemu/Agent.pm
 create mode 100644 PVE/API2/Qemu/Makefile

diff --git a/PVE/API2/Makefile b/PVE/API2/Makefile
index 7d373c3..614ab5d 100644
--- a/PVE/API2/Makefile
+++ b/PVE/API2/Makefile
@@ -5,3 +5,4 @@ SOURCES=	\
 install:
 	install -d -m 0755 ${DESTDIR}${PERLDIR}/PVE/API2
 	install -m 0644 ${SOURCES} ${DESTDIR}${PERLDIR}/PVE/API2/
+	make -C Qemu install
diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 6c9ede9..5051cc9 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -26,6 +26,7 @@ use PVE::INotify;
 use PVE::Network;
 use PVE::Firewall;
 use PVE::API2::Firewall::VM;
+use PVE::API2::Qemu::Agent;
 
 BEGIN {
     if (!$ENV{PVE_GENERATING_DOCS}) {
@@ -631,6 +632,11 @@ __PACKAGE__->register_method ({
     path => '{vmid}/firewall',
 });
 
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Qemu::Agent",
+    path => '{vmid}/agent',
+});
+
 __PACKAGE__->register_method({
     name => 'rrd',
     path => '{vmid}/rrd',
@@ -3042,70 +3048,6 @@ __PACKAGE__->register_method({
 	return $res;
     }});
 
-my $guest_agent_commands = [
-    'ping',
-    'get-time',
-    'info',
-    'fsfreeze-status',
-    'fsfreeze-freeze',
-    'fsfreeze-thaw',
-    'fstrim',
-    'network-get-interfaces',
-    'get-vcpus',
-    'get-fsinfo',
-    'get-memory-blocks',
-    'get-memory-block-info',
-    'suspend-hybrid',
-    'suspend-ram',
-    'suspend-disk',
-    'shutdown',
-    ];
-
-__PACKAGE__->register_method({
-    name => 'agent',
-    path => '{vmid}/agent',
-    method => 'POST',
-    protected => 1,
-    proxyto => 'node',
-    description => "Execute Qemu Guest Agent commands.",
-    permissions => {
-	check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
-    },
-    parameters => {
-	additionalProperties => 0,
-	properties => {
-	    node => get_standard_option('pve-node'),
-	    vmid => get_standard_option('pve-vmid', {
-                   completion => \&PVE::QemuServer::complete_vmid_running }),
-	    command => {
-		type => 'string',
-		description => "The QGA command.",
-		enum => $guest_agent_commands,
-	    },
-	},
-    },
-    returns => {
-	type => 'object',
-	description => "Returns an object with a single `result` property. The type of that
-property depends on the executed command.",
-    },
-    code => sub {
-	my ($param) = @_;
-
-	my $vmid = $param->{vmid};
-
-	my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
-
-	die "No Qemu Guest Agent\n" if !defined($conf->{agent});
-	die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
-
-	my $cmd = $param->{command};
-
-	my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd");
-
-	return { result => $res };
-    }});
-
 __PACKAGE__->register_method({
     name => 'resize_vm',
     path => '{vmid}/resize',
diff --git a/PVE/API2/Qemu/Agent.pm b/PVE/API2/Qemu/Agent.pm
new file mode 100644
index 0000000..437d3f6
--- /dev/null
+++ b/PVE/API2/Qemu/Agent.pm
@@ -0,0 +1,76 @@
+package PVE::API2::Qemu::Agent;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::QemuServer;
+
+use base qw(PVE::RESTHandler);
+
+my $guest_agent_commands = [
+    'ping',
+    'get-time',
+    'info',
+    'fsfreeze-status',
+    'fsfreeze-freeze',
+    'fsfreeze-thaw',
+    'fstrim',
+    'network-get-interfaces',
+    'get-vcpus',
+    'get-fsinfo',
+    'get-memory-blocks',
+    'get-memory-block-info',
+    'suspend-hybrid',
+    'suspend-ram',
+    'suspend-disk',
+    'shutdown',
+    ];
+
+__PACKAGE__->register_method({
+    name => 'agent',
+    path => '',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Execute Qemu Guest Agent commands.",
+    permissions => {
+	check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid', {
+                   completion => \&PVE::QemuServer::complete_vmid_running }),
+	    command => {
+		type => 'string',
+		description => "The QGA command.",
+		enum => $guest_agent_commands,
+	    },
+	},
+    },
+    returns => {
+	type => 'object',
+	description => "Returns an object with a single `result` property. The type of that
+property depends on the executed command.",
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $vmid = $param->{vmid};
+
+	my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
+
+	die "No Qemu Guest Agent\n" if !defined($conf->{agent});
+	die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
+
+	my $cmd = $param->{command};
+
+	my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd");
+
+	return { result => $res };
+    }});
+
+1;
diff --git a/PVE/API2/Qemu/Makefile b/PVE/API2/Qemu/Makefile
new file mode 100644
index 0000000..20c2a6c
--- /dev/null
+++ b/PVE/API2/Qemu/Makefile
@@ -0,0 +1,6 @@
+SOURCES=Agent.pm
+
+.PHONY: install
+install:
+	install -d -m 0755 ${DESTDIR}${PERLDIR}/PVE/API2/Qemu
+	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/API2/Qemu/$$i; done
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 04beb48..d33b949 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -20,6 +20,7 @@ use PVE::QemuServer;
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::OVF;
 use PVE::API2::Qemu;
+use PVE::API2::Qemu::Agent;
 use JSON;
 use PVE::JSONSchema qw(get_standard_option);
 use Term::ReadLine;
@@ -786,7 +787,7 @@ our $cmddef = {
 
     monitor  => [ __PACKAGE__, 'monitor', ['vmid']],
 
-    agent  => [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
+    agent  => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'],
 		{ node => $nodename }, $print_agent_result ],
 
     mtunnel => [ __PACKAGE__, 'mtunnel', []],
-- 
2.11.0





More information about the pve-devel mailing list