I've added the logic to poll the task given by status/start until its
"stopped", this enables an usage like:

pveclient lxc create 999 && pveclient start 999 && pveclient enter 999

Signed-off-by: René Jochum <r.joc...@proxmox.com>
---
v2 is a rebase on master and I added the wait logic.

 PVE/APIClient/Commands/help.pm  |  2 ++
 PVE/APIClient/Commands/start.pm | 43 +++++++++++++++++++++++++++++++++++++++++
 PVE/APIClient/Helpers.pm        | 39 +++++++++++++++++++++++++++++++++++++
 pveclient                       |  2 ++
 4 files changed, 86 insertions(+)
 create mode 100644 PVE/APIClient/Commands/start.pm

diff --git a/PVE/APIClient/Commands/help.pm b/PVE/APIClient/Commands/help.pm
index efb964a..258a4c0 100644
--- a/PVE/APIClient/Commands/help.pm
+++ b/PVE/APIClient/Commands/help.pm
@@ -8,6 +8,7 @@ use PVE::APIClient::Commands::list;
 use PVE::APIClient::Commands::lxc;
 use PVE::APIClient::Commands::config;
 use PVE::APIClient::Commands::remote;
+use PVE::APIClient::Commands::start;
 
 use PVE::CLIHandler;
 
@@ -61,6 +62,7 @@ __PACKAGE__->register_method ({
        $assemble_usage_string->('lxc', $PVE::APIClient::Commands::lxc::cmddef);
        $assemble_usage_string->('remote', 
$PVE::APIClient::Commands::remote::cmddef);
        $assemble_usage_string->('config', 
$PVE::APIClient::Commands::config::cmddef);
+       $assemble_usage_string->('start', 
$PVE::APIClient::Commands::start::cmddef);
 
        $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
 
diff --git a/PVE/APIClient/Commands/start.pm b/PVE/APIClient/Commands/start.pm
new file mode 100644
index 0000000..2b217a5
--- /dev/null
+++ b/PVE/APIClient/Commands/start.pm
@@ -0,0 +1,43 @@
+package PVE::APIClient::Commands::start;
+
+use strict;
+use warnings;
+
+use PVE::APIClient::Helpers;
+use PVE::JSONSchema qw(get_standard_option);
+
+use PVE::CLIHandler;
+
+use base qw(PVE::CLIHandler);
+
+__PACKAGE__->register_method ({
+    name => 'start',
+    path => 'start',
+    method => 'POST',
+    description => "Start a Qemu VM/LinuX Container.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           remote => get_standard_option('pveclient-remote-name'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $config = PVE::APIClient::Config->load();
+       my $conn = PVE::APIClient::Config->remote_conn($config, 
$param->{remote});
+
+       my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, 
$param->{vmid});
+
+       my $upid = 
$conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/status/start",
 {});
+
+       print PVE::APIClient::Helpers::poll_task($conn, $resource->{node}, 
$upid) . "\n";
+
+       return undef;
+    }});
+
+our $cmddef = [ __PACKAGE__, 'start', ['remote', 'vmid']];
+
+1;
diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm
index d6d1a17..ba7f83e 100644
--- a/PVE/APIClient/Helpers.pm
+++ b/PVE/APIClient/Helpers.pm
@@ -143,4 +143,43 @@ sub complete_api_path {
     }
 }
 
+sub get_vmid_resource {
+    my ($conn, $vmid) = @_;
+
+    my $resources = $conn->get('api2/json/cluster/resources', {type => 'vm'});
+
+    my $resource;
+    for my $tmp (@$resources) {
+       if ($tmp->{vmid} eq $vmid) {
+           $resource = $tmp;
+           last;
+       }
+    }
+
+    if (!defined($resource)) {
+       die "\"$vmid\" not found";
+    }
+
+    return $resource;
+}
+
+sub poll_task {
+    my ($conn, $node, $upid) = @_;
+
+    my $path = "api2/json/nodes/$node/tasks/$upid/status";
+
+    my $task_status;
+    while(1) {
+       $task_status = $conn->get($path, {});
+
+       if ($task_status->{status} eq "stopped") {
+           last;
+       }
+
+       sleep(10);
+    }
+
+    return $task_status->{exitstatus};
+}
+
 1;
diff --git a/pveclient b/pveclient
index f18dab9..9d34559 100755
--- a/pveclient
+++ b/pveclient
@@ -17,6 +17,7 @@ use PVE::APIClient::Commands::remote;
 use PVE::APIClient::Commands::list;
 use PVE::APIClient::Commands::lxc;
 use PVE::APIClient::Commands::help;
+use PVE::APIClient::Commands::start;
 
 use JSON;
 
@@ -38,6 +39,7 @@ my $cli_class_handlers = {
     lxc => 'PVE::APIClient::Commands::lxc',
     remote => 'PVE::APIClient::Commands::remote',
     config => 'PVE::APIClient::Commands::config',
+    start => 'PVE::APIClient::Commands::start',
     help => 'PVE::APIClient::Commands::help',
 };
 
-- 
2.11.0

_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to