[pve-devel] [PATCH] support to execute multiple commands in one request

Stefan Priebe s.priebe at profihost.ag
Fri Dec 6 14:13:49 CET 2013


---
 PVE/API2/Nodes.pm |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index 4cfcfd2..d349764 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -110,6 +110,7 @@ __PACKAGE__->register_method ({
 	my $result = [
 	    { name => 'apt' },
 	    { name => 'version' },
+	    { name => 'execute' },
 	    { name => 'syslog' },
 	    { name => 'bootlog' },
 	    { name => 'status' },
@@ -329,6 +330,59 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
+    name => 'execute',
+    path => 'execute',
+    method => 'GET',
+    permissions => {
+	check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
+    },
+    description => "Execute commands in order",
+    proxyto => 'node',
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    commands => { type => "JSON_STRING" }
+	},
+    },
+    returns => {
+	type => 'array',
+	properties => {
+
+	},
+    },
+    code => sub {
+	my ($param) = @_;
+	my $res = [];
+
+	my $commands = eval {
+		decode_json($param->{commands});
+	};
+	die "commands param did not contain valid JSON: $@" if $@;
+	die "commands is not an array" if ref($commands) ne "ARRAY";
+
+        foreach my $cmd (@$commands) {
+	    die "$cmd is not a valid command" if (ref($cmd) ne "HASH" || !$cmd->{path} || !$cmd->{method});
+
+	    $cmd->{args} //= {};
+
+	    my $path = "nodes/".$param->{node}."/".$cmd->{path};
+	    my ($handler, $info) = PVE::API2->find_handler($cmd->{method}, $path, $cmd->{args});
+	    if (!$handler || !$info) {
+		die "no handler for '$path'\n";
+	    }
+
+	    eval {
+		push(@$res, $handler->handle($info, $cmd->{args}) );
+	    };
+	    push(@$res, $@) if $@;
+	}
+
+	return $res;
+    }});
+
+
+__PACKAGE__->register_method({
     name => 'node_cmd', 
     path => 'status', 
     method => 'POST',
-- 
1.7.10.4




More information about the pve-devel mailing list