[pve-devel] [qemu-server cli cleanup 6/6] qemu guest *: use print_api_result

Dietmar Maurer dietmar at proxmox.com
Thu Aug 9 11:43:55 CEST 2018


And improve return schema definition for 'exec' and 'exec-status' cli commands.

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/API2/Qemu/Agent.pm  | 37 +-----------------------------
 PVE/CLI/qm.pm           | 60 ++++++++++++++++++++++++++++---------------------
 PVE/QemuServer/Agent.pm | 43 +++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 62 deletions(-)

diff --git a/PVE/API2/Qemu/Agent.pm b/PVE/API2/Qemu/Agent.pm
index 839146c..8ba0231 100644
--- a/PVE/API2/Qemu/Agent.pm
+++ b/PVE/API2/Qemu/Agent.pm
@@ -319,42 +319,7 @@ __PACKAGE__->register_method({
     },
     returns => {
 	type => 'object',
-	properties => {
-	    exited => {
-		type => 'boolean',
-		description => 'Tells if the given command has exited yet.',
-	    },
-	    exitcode => {
-		type => 'integer',
-		optional => 1,
-		description => 'process exit code if it was normally terminated.',
-	    },
-	    signal=> {
-		type => 'integer',
-		optional => 1,
-		description => 'signal number or exception code if the process was abnormally terminated.',
-	    },
-	    'out-data' => {
-		type => 'string',
-		optional => 1,
-		description => 'stdout of the process',
-	    },
-	    'err-data' => {
-		type => 'string',
-		optional => 1,
-		description => 'stderr of the process',
-	    },
-	    'out-truncated' => {
-		type => 'boolean',
-		optional => 1,
-		description => 'true if stdout was not fully captured',
-	    },
-	    'err-truncated' => {
-		type => 'boolean',
-		optional => 1,
-		description => 'true if stderr was not fully captured',
-	    },
-	},
+	properties => $PVE::QemuServer::Agent::qemu_status_properties,
     },
     code => sub {
 	my ($param) = @_;
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 043370c..2f1150d 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -28,6 +28,7 @@ use JSON;
 use PVE::JSONSchema qw(get_standard_option);
 use Term::ReadLine;
 
+use PVE::CLIFormatter;
 use PVE::RESTHandler;
 use PVE::CLIHandler;
 
@@ -620,6 +621,7 @@ __PACKAGE__->register_method({
     },
     returns => {
 	type => 'object',
+        properties => $PVE::QemuServer::Agent::qemu_status_properties,
     },
     code => sub {
 	my ($param) = @_;
@@ -654,31 +656,9 @@ __PACKAGE__->register_method({
 	    }
 	}
 
-	return { result => $res };
+	return $res;
     }});
 
-my $print_agent_result = sub {
-    my ($data) = @_;
-
-    my $result = $data->{result} // $data;
-    return if !defined($result);
-
-    my $class = ref($result);
-
-    if (!$class) {
-	chomp $result;
-	return if $result =~ m/^\s*$/;
-	print "$result\n";
-	return;
-    }
-
-    if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
-	return;
-    }
-
-    print to_json($result, { pretty => 1, canonical => 1});
-};
-
 sub param_mapping {
     my ($name) = @_;
 
@@ -828,10 +808,38 @@ our $cmddef = {
     agent  => { alias => 'guest cmd' },
 
     guest => {
-	cmd  => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { node => $nodename }, $print_agent_result ],
+	cmd  => [
+	    "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { node => $nodename },
+	    sub {
+		my ($data, undef, $options) = @_;
+
+		my $result = $data->{result};
+		return if !defined($result);
+
+		my $class = ref($result);
+		if (!$class) {
+		    chomp $result;
+		    return if $result =~ m/^\s*$/;
+		} elsif (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
+		    return;
+		}
+
+		PVE::CLIFormatter::print_api_result($result, undef, undef, $options);
+
+	    }, $PVE::RESTHandler::standard_output_options],
 	passwd => [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { node => $nodename }],
-	exec => [ __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { node => $nodename }, $print_agent_result],
-	'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { node => $nodename }, $print_agent_result],
+	'exec' => [
+	    __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { node => $nodename },
+	    sub {
+		my ($data, $schema, $options) = @_;
+		PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
+	    } , $PVE::RESTHandler::standard_output_options],
+	'exec-status' => [
+	    "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { node => $nodename },
+	    sub {
+		my ($data, $schema, $options) = @_;
+		PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
+	    }, $PVE::RESTHandler::standard_output_options],
     },
 
     mtunnel => [ __PACKAGE__, 'mtunnel', []],
diff --git a/PVE/QemuServer/Agent.pm b/PVE/QemuServer/Agent.pm
index 586ac3a..1e7bc7a 100644
--- a/PVE/QemuServer/Agent.pm
+++ b/PVE/QemuServer/Agent.pm
@@ -14,6 +14,49 @@ agent_available
 agent_cmd
 );
 
+our $qemu_status_properties = {
+    pid => {
+	type => 'integer',
+	description => "The PID of the process started by the guest-agent.",
+	optional => 1,
+    },
+    exited => {
+	type => 'boolean',
+	description => 'Tells if the given command has exited yet.',
+	optional => 1,
+    },
+    exitcode => {
+	type => 'integer',
+	optional => 1,
+	description => 'process exit code if it was normally terminated.',
+    },
+    signal=> {
+	type => 'integer',
+	optional => 1,
+	description => 'signal number or exception code if the process was abnormally terminated.',
+    },
+    'out-data' => {
+	type => 'string',
+	optional => 1,
+	description => 'stdout of the process',
+    },
+    'err-data' => {
+	type => 'string',
+	optional => 1,
+	description => 'stderr of the process',
+    },
+    'out-truncated' => {
+	type => 'boolean',
+	optional => 1,
+	description => 'true if stdout was not fully captured',
+    },
+    'err-truncated' => {
+	type => 'boolean',
+	optional => 1,
+	description => 'true if stderr was not fully captured',
+    },
+};
+
 sub check_agent_error {
     my ($result, $errmsg, $noerr) = @_;
 
-- 
2.11.0




More information about the pve-devel mailing list