From 4cedebf640a4b4862a98eb492ddb2f3c56222677 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ren=C3=A9=20Jochum?= Date: Wed, 13 Jun 2018 10:44:59 +0200 Subject: [PATCH] Add spice command --- PVE/APIClient/Commands/GuestStatus.pm | 44 +++++++++++++++++++++++++++ pveclient | 3 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/PVE/APIClient/Commands/GuestStatus.pm b/PVE/APIClient/Commands/GuestStatus.pm index 7903a2b..50730db 100644 --- a/PVE/APIClient/Commands/GuestStatus.pm +++ b/PVE/APIClient/Commands/GuestStatus.pm @@ -8,6 +8,8 @@ use PVE::APIClient::Config; use PVE::JSONSchema qw(get_standard_option); +use File::Temp qw(tempfile); + use PVE::CLIHandler; use base qw(PVE::CLIHandler); @@ -73,4 +75,46 @@ __PACKAGE__->register_method ({ return undef; }}); +__PACKAGE__->register_method ({ + name => 'spice', + path => 'spice', + method => 'POST', + description => "Run the spice client for a guest (VM/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 $remote = PVE::Tools::extract_param($param, 'remote'); + my $vmid = PVE::Tools::extract_param($param, 'vmid'); + + my $config = PVE::APIClient::Config->load(); + my $conn = PVE::APIClient::Config->remote_conn($config, $remote); + + my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $vmid); + + my $res = $conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/spiceproxy", {}); + + my $vvsetup = "[virt-viewer]\n"; + foreach my $k (keys %$res) { + $vvsetup .= "$k=$res->{$k}\n"; + } + + my ($fh, $filename) = tempfile( "tempXXXXX", SUFFIX => '.vv', TMPDIR => 1); + syswrite($fh, $vvsetup); + + system("nohup remote-viewer $filename 1>/dev/null 2>&1 &"); + if ($? != 0) { + print "failed to execute remote-viewer: $!\n"; + } + + return undef; + }}); + 1; diff --git a/pveclient b/pveclient index 4abe7b7..01cb74b 100755 --- a/pveclient +++ b/pveclient @@ -161,7 +161,8 @@ our $cmddef = { list => $PVE::APIClient::Commands::list::cmddef, lxc => $PVE::APIClient::Commands::lxc::cmddef, remote => $PVE::APIClient::Commands::remote::cmddef, - + + spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']], start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']], stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']], -- 2.39.2