Add start command
authorRené Jochum <r.jochum@proxmox.com>
Tue, 12 Jun 2018 11:49:01 +0000 (13:49 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 13 Jun 2018 06:39:51 +0000 (08:39 +0200)
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.jochum@proxmox.com>
PVE/APIClient/Commands/start.pm [new file with mode: 0644]
PVE/APIClient/Helpers.pm
pveclient

diff --git a/PVE/APIClient/Commands/start.pm b/PVE/APIClient/Commands/start.pm
new file mode 100644 (file)
index 0000000..2b217a5
--- /dev/null
@@ -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;
index b83829b..28fd1c4 100644 (file)
@@ -183,4 +183,43 @@ sub extract_path_info {
     return $info;
 }
 
+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;
index 70db84a..8ef37a9 100755 (executable)
--- a/pveclient
+++ b/pveclient
@@ -19,6 +19,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;
 
@@ -161,6 +162,8 @@ our $cmddef = {
     list => $PVE::APIClient::Commands::list::cmddef,
     lxc => $PVE::APIClient::Commands::lxc::cmddef,
     remote => $PVE::APIClient::Commands::remote::cmddef,
+    
+    start => $PVE::APIClient::Commands::start::cmddef,
 
     api => {
        get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path']],