]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Status.pm
api status: cleanup nested closures
[pve-container.git] / src / PVE / API2 / LXC / Status.pm
1 package PVE::API2::LXC::Status;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param run_command);
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::INotify;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::Firewall;
13 use PVE::Storage;
14 use PVE::RESTHandler;
15 use PVE::RPCEnvironment;
16 use PVE::LXC;
17 use PVE::LXC::Create;
18 use PVE::JSONSchema qw(get_standard_option);
19 use base qw(PVE::RESTHandler);
20
21 BEGIN {
22 if (!$ENV{PVE_GENERATING_DOCS}) {
23 require PVE::HA::Env::PVE2;
24 import PVE::HA::Env::PVE2;
25 require PVE::HA::Config;
26 import PVE::HA::Config;
27 }
28 }
29
30 __PACKAGE__->register_method({
31 name => 'vmcmdidx',
32 path => '',
33 method => 'GET',
34 proxyto => 'node',
35 description => "Directory index",
36 permissions => {
37 user => 'all',
38 },
39 parameters => {
40 additionalProperties => 0,
41 properties => {
42 node => get_standard_option('pve-node'),
43 vmid => get_standard_option('pve-vmid'),
44 },
45 },
46 returns => {
47 type => 'array',
48 items => {
49 type => "object",
50 properties => {
51 subdir => { type => 'string' },
52 },
53 },
54 links => [ { rel => 'child', href => "{subdir}" } ],
55 },
56 code => sub {
57 my ($param) = @_;
58
59 # test if VM exists
60 my $conf = PVE::LXC::Config->load_config($param->{vmid});
61
62 my $res = [
63 { subdir => 'current' },
64 { subdir => 'start' },
65 { subdir => 'stop' },
66 { subdir => 'shutdown' },
67 { subdir => 'migrate' },
68 ];
69
70 return $res;
71 }});
72
73 __PACKAGE__->register_method({
74 name => 'vm_status',
75 path => 'current',
76 method => 'GET',
77 proxyto => 'node',
78 protected => 1, # openvz /proc entries are only readable by root
79 description => "Get virtual machine status.",
80 permissions => {
81 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
82 },
83 parameters => {
84 additionalProperties => 0,
85 properties => {
86 node => get_standard_option('pve-node'),
87 vmid => get_standard_option('pve-vmid'),
88 },
89 },
90 returns => {
91 type => 'object',
92 properties => {
93 %$PVE::LXC::vmstatus_return_properties,
94 ha => {
95 description => "HA manager service status.",
96 type => 'object',
97 },
98 },
99 },
100 code => sub {
101 my ($param) = @_;
102
103 # test if VM exists
104 my $conf = PVE::LXC::Config->load_config($param->{vmid});
105
106 my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
107 my $status = $vmstatus->{$param->{vmid}};
108
109 $status->{ha} = PVE::HA::Config::get_service_status("ct:$param->{vmid}");
110
111 return $status;
112 }});
113
114 __PACKAGE__->register_method({
115 name => 'vm_start',
116 path => 'start',
117 method => 'POST',
118 protected => 1,
119 proxyto => 'node',
120 description => "Start the container.",
121 permissions => {
122 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
123 },
124 parameters => {
125 additionalProperties => 0,
126 properties => {
127 node => get_standard_option('pve-node'),
128 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
129 skiplock => get_standard_option('skiplock'),
130 },
131 },
132 returns => {
133 type => 'string',
134 },
135 code => sub {
136 my ($param) = @_;
137
138 my $rpcenv = PVE::RPCEnvironment::get();
139
140 my $authuser = $rpcenv->get_user();
141
142 my $node = extract_param($param, 'node');
143
144 my $vmid = extract_param($param, 'vmid');
145
146 my $skiplock = extract_param($param, 'skiplock');
147 raise_param_exc({ skiplock => "Only root may use this option." })
148 if $skiplock && $authuser ne 'root@pam';
149
150 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
151
152 PVE::Cluster::check_cfs_quorum();
153
154 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
155
156 my $hacmd = sub {
157 my $upid = shift;
158
159 my $service = "ct:$vmid";
160
161 my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
162
163 print "Requesting HA start for CT $vmid\n";
164
165 PVE::Tools::run_command($cmd);
166
167 return;
168 };
169
170 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
171
172 } else {
173
174 my $realcmd = sub {
175 my $upid = shift;
176
177 syslog('info', "starting CT $vmid: $upid\n");
178
179 my $conf = PVE::LXC::Config->load_config($vmid);
180
181 die "you can't start a CT if it's a template\n"
182 if PVE::LXC::Config->is_template($conf);
183
184 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
185 PVE::LXC::Config->check_lock($conf);
186 }
187
188 if ($conf->{unprivileged}) {
189 PVE::LXC::Config->foreach_mountpoint($conf, sub {
190 my ($ms, $mountpoint) = @_;
191 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota};
192 });
193 }
194
195 PVE::LXC::vm_start($vmid, $conf, $skiplock);
196 };
197
198 my $lockcmd = sub {
199 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
200 };
201
202 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
203 }
204 }});
205
206 __PACKAGE__->register_method({
207 name => 'vm_stop',
208 path => 'stop',
209 method => 'POST',
210 protected => 1,
211 proxyto => 'node',
212 description => "Stop the container. This will abruptly stop all processes running in the container.",
213 permissions => {
214 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
215 },
216 parameters => {
217 additionalProperties => 0,
218 properties => {
219 node => get_standard_option('pve-node'),
220 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
221 skiplock => get_standard_option('skiplock'),
222 },
223 },
224 returns => {
225 type => 'string',
226 },
227 code => sub {
228 my ($param) = @_;
229
230 my $rpcenv = PVE::RPCEnvironment::get();
231
232 my $authuser = $rpcenv->get_user();
233
234 my $node = extract_param($param, 'node');
235
236 my $vmid = extract_param($param, 'vmid');
237
238 my $skiplock = extract_param($param, 'skiplock');
239 raise_param_exc({ skiplock => "Only root may use this option." })
240 if $skiplock && $authuser ne 'root@pam';
241
242 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
243
244 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
245
246 my $hacmd = sub {
247 my $upid = shift;
248
249 my $service = "ct:$vmid";
250
251 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
252
253 print "Requesting HA stop for CT $vmid\n";
254
255 PVE::Tools::run_command($cmd);
256
257 return;
258 };
259
260 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
261
262 } else {
263
264 my $realcmd = sub {
265 my $upid = shift;
266
267 syslog('info', "stopping CT $vmid: $upid\n");
268
269 my $conf = PVE::LXC::Config->load_config($vmid);
270 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
271 PVE::LXC::Config->check_lock($conf);
272 }
273
274 PVE::LXC::vm_stop($vmid, 1);
275 };
276
277 my $lockcmd = sub {
278 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
279 };
280
281 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
282 }
283 }});
284
285 __PACKAGE__->register_method({
286 name => 'vm_shutdown',
287 path => 'shutdown',
288 method => 'POST',
289 protected => 1,
290 proxyto => 'node',
291 description => "Shutdown the container. This will trigger a clean shutdown " .
292 "of the container, see lxc-stop(1) for details.",
293 permissions => {
294 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
295 },
296 parameters => {
297 additionalProperties => 0,
298 properties => {
299 node => get_standard_option('pve-node'),
300 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
301 timeout => {
302 description => "Wait maximal timeout seconds.",
303 type => 'integer',
304 minimum => 0,
305 optional => 1,
306 default => 60,
307 },
308 forceStop => {
309 description => "Make sure the Container stops.",
310 type => 'boolean',
311 optional => 1,
312 default => 0,
313 }
314 },
315 },
316 returns => {
317 type => 'string',
318 },
319 code => sub {
320 my ($param) = @_;
321
322 my $rpcenv = PVE::RPCEnvironment::get();
323
324 my $authuser = $rpcenv->get_user();
325
326 my $node = extract_param($param, 'node');
327
328 my $vmid = extract_param($param, 'vmid');
329
330 my $timeout = extract_param($param, 'timeout') // 60;
331
332 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
333
334 if (PVE::HA::Config::vm_is_ha_managed($vmid) &&
335 $rpcenv->{type} ne 'ha') {
336
337 my $hacmd = sub {
338 my $upid = shift;
339
340 my $service = "ct:$vmid";
341
342 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
343
344 print "Requesting HA stop for CT $vmid\n";
345
346 PVE::Tools::run_command($cmd);
347
348 return;
349 };
350
351 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
352 }
353
354 my $realcmd = sub {
355 my $upid = shift;
356
357 syslog('info', "shutdown CT $vmid: $upid\n");
358
359 my $conf = PVE::LXC::Config->load_config($vmid);
360 PVE::LXC::Config->check_lock($conf);
361
362 PVE::LXC::vm_stop($vmid, 0, $timeout, $param->{forceStop});
363 };
364
365 my $lockcmd = sub {
366 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
367 };
368
369 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
370 }});
371
372 __PACKAGE__->register_method({
373 name => 'vm_suspend',
374 path => 'suspend',
375 method => 'POST',
376 protected => 1,
377 proxyto => 'node',
378 description => "Suspend the container.",
379 permissions => {
380 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
381 },
382 parameters => {
383 additionalProperties => 0,
384 properties => {
385 node => get_standard_option('pve-node'),
386 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
387 },
388 },
389 returns => {
390 type => 'string',
391 },
392 code => sub {
393 my ($param) = @_;
394
395 my $rpcenv = PVE::RPCEnvironment::get();
396 my $authuser = $rpcenv->get_user();
397
398 my $node = extract_param($param, 'node');
399 my $vmid = extract_param($param, 'vmid');
400
401 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
402
403 my $realcmd = sub {
404 my $upid = shift;
405
406 syslog('info', "suspend CT $vmid: $upid\n");
407
408 my $conf = PVE::LXC::Config->load_config($vmid);
409 PVE::LXC::Config->check_lock($conf);
410
411 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
412 run_command($cmd);
413 };
414
415 my $lockcmd = sub {
416 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
417 };
418
419 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
420 }});
421
422 __PACKAGE__->register_method({
423 name => 'vm_resume',
424 path => 'resume',
425 method => 'POST',
426 protected => 1,
427 proxyto => 'node',
428 description => "Resume the container.",
429 permissions => {
430 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
431 },
432 parameters => {
433 additionalProperties => 0,
434 properties => {
435 node => get_standard_option('pve-node'),
436 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
437 },
438 },
439 returns => {
440 type => 'string',
441 },
442 code => sub {
443 my ($param) = @_;
444
445 my $rpcenv = PVE::RPCEnvironment::get();
446 my $authuser = $rpcenv->get_user();
447
448 my $node = extract_param($param, 'node');
449 my $vmid = extract_param($param, 'vmid');
450
451 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
452
453 my $realcmd = sub {
454 my $upid = shift;
455
456 syslog('info', "resume CT $vmid: $upid\n");
457
458 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
459 run_command($cmd);
460 };
461
462 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
463
464 return $upid;
465 }});
466
467 1;