]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Commands/GuestStatus.pm
Add spice command
[pve-client.git] / PVE / APIClient / Commands / GuestStatus.pm
index 7903a2b7d59d7eddc99b63c137bb1c9a89175ad1..50730db3885015f053dbe6dbabf66e41b15d5c0b 100644 (file)
@@ -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;