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