]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/GuestStatus.pm
fix copyright date
[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::APIClient::JSONSchema qw(get_standard_option);
10
11 use File::Temp qw(tempfile);
12
13 use PVE::APIClient::CLIHandler;
14
15 use base qw(PVE::APIClient::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 => 'shutdown',
80 path => 'shutdown',
81 method => 'POST',
82 description => "Stop 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 force => {
89 description => "Make sure the Container/VM stops.",
90 type => 'boolean',
91 optional => 1,
92 },
93 timeout => {
94 description => "Timeout in seconds",
95 type => 'integer',
96 minimum => 1,
97 optional => 1,
98 },
99 },
100 },
101 returns => { type => 'null'},
102 code => sub {
103 my ($param) = @_;
104
105 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
106 my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
107
108 $guest_status_command->($remote, $vmid, 'shutdown', $param);
109
110 return undef;
111 }});
112
113 __PACKAGE__->register_method ({
114 name => 'suspend',
115 path => 'suspend',
116 method => 'POST',
117 description => "Suspend a guest VM.",
118 parameters => {
119 additionalProperties => 0,
120 properties => {
121 remote => get_standard_option('pveclient-remote-name'),
122 vmid => get_standard_option('pve-vmid'),
123 },
124 },
125 returns => { type => 'null'},
126 code => sub {
127 my ($param) = @_;
128
129 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
130 my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
131
132 $guest_status_command->($remote, $vmid, 'suspend', $param);
133
134 return undef;
135 }});
136
137 __PACKAGE__->register_method ({
138 name => 'resume',
139 path => 'resume',
140 method => 'POST',
141 description => "Resume a guest VM.",
142 parameters => {
143 additionalProperties => 0,
144 properties => {
145 remote => get_standard_option('pveclient-remote-name'),
146 vmid => get_standard_option('pve-vmid'),
147 },
148 },
149 returns => { type => 'null'},
150 code => sub {
151 my ($param) = @_;
152
153 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
154 my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
155
156 $guest_status_command->($remote, $vmid, 'resume', $param);
157
158 return undef;
159 }});
160
161 __PACKAGE__->register_method ({
162 name => 'spice',
163 path => 'spice',
164 method => 'POST',
165 description => "Run the spice client for a guest (VM/Container)",
166 parameters => {
167 additionalProperties => 0,
168 properties => {
169 remote => get_standard_option('pveclient-remote-name'),
170 vmid => get_standard_option('pve-vmid'),
171 },
172 },
173 returns => { type => 'null'},
174 code => sub {
175 my ($param) = @_;
176
177 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
178 my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
179
180 my $config = PVE::APIClient::Config->load();
181 my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
182
183 my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $vmid);
184
185 my $res = $conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/spiceproxy", {});
186
187 my $vvsetup = "[virt-viewer]\n";
188 foreach my $k (keys %$res) {
189 $vvsetup .= "$k=$res->{$k}\n";
190 }
191
192 my ($fh, $filename) = tempfile( "tempXXXXX", SUFFIX => '.vv', TMPDIR => 1);
193 syswrite($fh, $vvsetup);
194
195 system("nohup remote-viewer $filename 1>/dev/null 2>&1 &");
196 if ($? != 0) {
197 print "failed to execute remote-viewer: $!\n";
198 }
199
200 return undef;
201 }});
202
203 1;