From: René Jochum Date: Tue, 12 Jun 2018 11:49:01 +0000 (+0200) Subject: Add start command X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=commitdiff_plain;h=2f964a751cc71d669bc03df031a147eebc8ebe35 Add start command 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 --- 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 b83829b..28fd1c4 100644 --- a/PVE/APIClient/Helpers.pm +++ b/PVE/APIClient/Helpers.pm @@ -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; diff --git a/pveclient b/pveclient index 70db84a..8ef37a9 100755 --- 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']],