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