]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/GuestStatus.pm
4a50164ba78f9677cc2658a3d07af1ded153bf0c
[pve-client.git] / PVE / APIClient / Commands / GuestStatus.pm
1 package PVE::APIClient::Commands::GuestStatus;
2
3 use strict;
4 use warnings;
5
6 use PVE::APIClient::Helpers;
7 use PVE::APIClient::Config;
8
9 use PVE::JSONSchema qw(get_standard_option);
10
11 use File::Temp qw(tempfile);
12
13 use PVE::CLIHandler;
14
15 use base qw(PVE::CLIHandler);
16
17 my $guest_status_command = sub {
18 my ($remote, $vmid, $cmd, $param) = @_,
19
20 my $config = PVE::APIClient::Config->load();
21 my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
22
23 my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $vmid);
24
25 my $upid = $conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/status/$cmd", $param);
26
27 print PVE::APIClient::Helpers::poll_task($conn, $resource->{node}, $upid) . "\n";
28 };
29
30 __PACKAGE__->register_method ({
31 name => 'start',
32 path => 'start',
33 method => 'POST',
34 description => "Start a guest (VM/Container).",
35 parameters => {
36 additionalProperties => 0,
37 properties => {
38 remote => get_standard_option('pveclient-remote-name'),
39 vmid => get_standard_option('pve-vmid'),
40 },
41 },
42 returns => { type => 'null'},
43 code => sub {
44 my ($param) = @_;
45
46 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
47 my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
48
49 $guest_status_command->($remote, $vmid, 'start', $param);
50
51 return undef;
52 }});
53
54 __PACKAGE__->register_method ({
55 name => 'stop',
56 path => 'stop',
57 method => 'POST',
58 description => "Stop a guest (VM/Container).",
59 parameters => {
60 additionalProperties => 0,
61 properties => {
62 remote => get_standard_option('pveclient-remote-name'),
63 vmid => get_standard_option('pve-vmid'),
64 },
65 },
66 returns => { type => 'null'},
67 code => sub {
68 my ($param) = @_;
69
70 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
71 my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
72
73 $guest_status_command->($remote, $vmid, 'stop', $param);
74
75 return undef;
76 }});
77
78 __PACKAGE__->register_method ({
79 name => 'spice',
80 path => 'spice',
81 method => 'POST',
82 description => "Run the spice client for a guest (VM/Container)",
83 parameters => {
84 additionalProperties => 0,
85 properties => {
86 remote => get_standard_option('pveclient-remote-name'),
87 vmid => get_standard_option('pve-vmid'),
88 },
89 },
90 returns => { type => 'null'},
91 code => sub {
92 my ($param) = @_;
93
94 my $remote = PVE::Tools::extract_param($param, 'remote');
95 my $vmid = PVE::Tools::extract_param($param, 'vmid');
96
97 my $config = PVE::APIClient::Config->load();
98 my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
99
100 my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $vmid);
101
102 my $res = $conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/spiceproxy", {});
103
104 my $vvsetup = "[virt-viewer]\n";
105 foreach my $k (keys %$res) {
106 $vvsetup .= "$k=$res->{$k}\n";
107 }
108
109 my ($fh, $filename) = tempfile( "tempXXXXX", SUFFIX => '.vv', TMPDIR => 1);
110 syswrite($fh, $vvsetup);
111
112 system("nohup remote-viewer $filename 1>/dev/null 2>&1 &");
113 if ($? != 0) {
114 print "failed to execute remote-viewer: $!\n";
115 }
116
117 return undef;
118 }});
119
120 1;