]>
Commit | Line | Data |
---|---|---|
aff192e6 DM |
1 | package PVE::API2::Nodes::Nodeinfo; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5a378425 TL |
5 | |
6 | use Digest::MD5; | |
7 | use Digest::SHA; | |
aff192e6 | 8 | use Filesys::Df; |
40993593 | 9 | use HTTP::Status qw(:constants); |
5a378425 TL |
10 | use JSON; |
11 | use POSIX qw(LONG_MAX); | |
12 | use Time::Local qw(timegm_nocheck); | |
13 | use Socket; | |
17711ff8 | 14 | use IO::Socket::SSL; |
5a378425 | 15 | |
b289829f | 16 | use PVE::API2Tools; |
5a378425 TL |
17 | use PVE::APLInfo; |
18 | use PVE::AccessControl; | |
c9164975 | 19 | use PVE::Cluster qw(cfs_read_file); |
5a378425 | 20 | use PVE::DataCenterConfig; |
05b252dc | 21 | use PVE::Exception qw(raise raise_perm_exc raise_param_exc); |
4a07fced | 22 | use PVE::Firewall; |
7e5cb2f0 | 23 | use PVE::HA::Config; |
5a378425 TL |
24 | use PVE::HA::Env::PVE2; |
25 | use PVE::INotify; | |
26 | use PVE::JSONSchema qw(get_standard_option); | |
27 | use PVE::LXC; | |
3f83a033 | 28 | use PVE::NodeConfig; |
5a378425 | 29 | use PVE::ProcFSTools; |
7141ae25 | 30 | use PVE::QemuConfig; |
b92400b6 | 31 | use PVE::QemuServer; |
ebb71cb5 | 32 | use PVE::RESTEnvironment qw(log_warn); |
5a378425 TL |
33 | use PVE::RESTHandler; |
34 | use PVE::RPCEnvironment; | |
35 | use PVE::RRD; | |
36 | use PVE::Report; | |
37 | use PVE::SafeSyslog; | |
38 | use PVE::Storage; | |
81fd95cf | 39 | use PVE::Tools qw(file_get_contents); |
5a378425 TL |
40 | use PVE::pvecfg; |
41 | ||
21299915 | 42 | use PVE::API2::APT; |
c6c4b278 | 43 | use PVE::API2::Capabilities; |
38db610a | 44 | use PVE::API2::Ceph; |
036475f8 | 45 | use PVE::API2::Certificates; |
e781363f | 46 | use PVE::API2::Disks; |
5a378425 TL |
47 | use PVE::API2::Firewall::Host; |
48 | use PVE::API2::Hardware; | |
49 | use PVE::API2::LXC::Status; | |
50 | use PVE::API2::LXC; | |
51 | use PVE::API2::Network; | |
52 | use PVE::API2::NodeConfig; | |
53 | use PVE::API2::Qemu::CPU; | |
5a378425 TL |
54 | use PVE::API2::Qemu; |
55 | use PVE::API2::Replication; | |
56 | use PVE::API2::Services; | |
57 | use PVE::API2::Storage::Scan; | |
58 | use PVE::API2::Storage::Status; | |
59 | use PVE::API2::Subscription; | |
60 | use PVE::API2::Tasks; | |
61 | use PVE::API2::VZDump; | |
aff192e6 | 62 | |
4be427a0 AD |
63 | my $have_sdn; |
64 | eval { | |
bb654699 | 65 | require PVE::API2::Network::SDN::Zones::Status; |
4be427a0 AD |
66 | $have_sdn = 1; |
67 | }; | |
68 | ||
aff192e6 DM |
69 | use base qw(PVE::RESTHandler); |
70 | ||
9018237e FG |
71 | my $verify_command_item_desc = { |
72 | description => "An array of objects describing endpoints, methods and arguments.", | |
73 | type => "array", | |
74 | items => { | |
75 | type => "object", | |
76 | properties => { | |
77 | path => { | |
78 | description => "A relative path to an API endpoint on this node.", | |
79 | type => "string", | |
80 | optional => 0, | |
81 | }, | |
82 | method => { | |
83 | description => "A method related to the API endpoint (GET, POST etc.).", | |
84 | type => "string", | |
85 | pattern => "(GET|POST|PUT|DELETE)", | |
86 | optional => 0, | |
87 | }, | |
88 | args => { | |
89 | description => "A set of parameter names and their values.", | |
90 | type => "object", | |
91 | optional => 1, | |
92 | }, | |
93 | }, | |
94 | } | |
95 | }; | |
96 | ||
a1de4410 SS |
97 | PVE::JSONSchema::register_format('pve-command-batch', \&verify_command_batch); |
98 | sub verify_command_batch { | |
99 | my ($value, $noerr) = @_; | |
100 | my $commands = eval { decode_json($value); }; | |
101 | ||
79d62026 | 102 | return if $noerr && $@; |
a1de4410 SS |
103 | die "commands param did not contain valid JSON: $@" if $@; |
104 | ||
79d62026 | 105 | eval { PVE::JSONSchema::validate($commands, $verify_command_item_desc) }; |
a1de4410 | 106 | |
79d62026 | 107 | return $commands if !$@; |
a1de4410 | 108 | |
79d62026 TL |
109 | return if $noerr; |
110 | die "commands is not a valid array of commands: $@"; | |
a1de4410 SS |
111 | } |
112 | ||
aff192e6 | 113 | __PACKAGE__->register_method ({ |
f9d26e09 | 114 | subclass => "PVE::API2::Qemu", |
aff192e6 DM |
115 | path => 'qemu', |
116 | }); | |
117 | ||
989b7743 | 118 | __PACKAGE__->register_method ({ |
f9d26e09 | 119 | subclass => "PVE::API2::LXC", |
989b7743 DM |
120 | path => 'lxc', |
121 | }); | |
122 | ||
38db610a | 123 | __PACKAGE__->register_method ({ |
f9d26e09 | 124 | subclass => "PVE::API2::Ceph", |
38db610a DM |
125 | path => 'ceph', |
126 | }); | |
127 | ||
bf58f8dd | 128 | __PACKAGE__->register_method ({ |
f9d26e09 | 129 | subclass => "PVE::API2::VZDump", |
bf58f8dd DM |
130 | path => 'vzdump', |
131 | }); | |
132 | ||
aff192e6 | 133 | __PACKAGE__->register_method ({ |
f9d26e09 | 134 | subclass => "PVE::API2::Services", |
aff192e6 DM |
135 | path => 'services', |
136 | }); | |
137 | ||
21ace8d3 | 138 | __PACKAGE__->register_method ({ |
f9d26e09 | 139 | subclass => "PVE::API2::Subscription", |
21ace8d3 DM |
140 | path => 'subscription', |
141 | }); | |
142 | ||
aff192e6 | 143 | __PACKAGE__->register_method ({ |
f9d26e09 | 144 | subclass => "PVE::API2::Network", |
aff192e6 DM |
145 | path => 'network', |
146 | }); | |
147 | ||
148 | __PACKAGE__->register_method ({ | |
f9d26e09 | 149 | subclass => "PVE::API2::Tasks", |
aff192e6 DM |
150 | path => 'tasks', |
151 | }); | |
152 | ||
153 | __PACKAGE__->register_method ({ | |
f5b6ccb1 | 154 | subclass => "PVE::API2::Storage::Scan", |
aff192e6 DM |
155 | path => 'scan', |
156 | }); | |
157 | ||
523d5f48 TL |
158 | __PACKAGE__->register_method ({ |
159 | subclass => "PVE::API2::Hardware", | |
160 | path => 'hardware', | |
161 | }); | |
162 | ||
c6c4b278 TL |
163 | __PACKAGE__->register_method ({ |
164 | subclass => "PVE::API2::Capabilities", | |
165 | path => 'capabilities', | |
166 | }); | |
523d5f48 | 167 | |
aff192e6 | 168 | __PACKAGE__->register_method ({ |
f9d26e09 | 169 | subclass => "PVE::API2::Storage::Status", |
aff192e6 DM |
170 | path => 'storage', |
171 | }); | |
172 | ||
e781363f DC |
173 | __PACKAGE__->register_method ({ |
174 | subclass => "PVE::API2::Disks", | |
175 | path => 'disks', | |
176 | }); | |
177 | ||
21299915 | 178 | __PACKAGE__->register_method ({ |
f9d26e09 | 179 | subclass => "PVE::API2::APT", |
21299915 DM |
180 | path => 'apt', |
181 | }); | |
182 | ||
4a07fced | 183 | __PACKAGE__->register_method ({ |
f9d26e09 | 184 | subclass => "PVE::API2::Firewall::Host", |
4a07fced DM |
185 | path => 'firewall', |
186 | }); | |
187 | ||
892821fd DM |
188 | __PACKAGE__->register_method ({ |
189 | subclass => "PVE::API2::Replication", | |
190 | path => 'replication', | |
191 | }); | |
192 | ||
036475f8 FG |
193 | __PACKAGE__->register_method ({ |
194 | subclass => "PVE::API2::Certificates", | |
195 | path => 'certificates', | |
196 | }); | |
197 | ||
198 | ||
c4f78bb7 FG |
199 | __PACKAGE__->register_method ({ |
200 | subclass => "PVE::API2::NodeConfig", | |
201 | path => 'config', | |
202 | }); | |
203 | ||
4be427a0 AD |
204 | if ($have_sdn) { |
205 | __PACKAGE__->register_method ({ | |
bb654699 AD |
206 | subclass => "PVE::API2::Network::SDN::Zones::Status", |
207 | path => 'sdn/zones', | |
4be427a0 | 208 | }); |
48181607 TL |
209 | |
210 | __PACKAGE__->register_method ({ | |
211 | name => 'sdnindex', | |
212 | path => 'sdn', | |
213 | method => 'GET', | |
214 | permissions => { user => 'all' }, | |
215 | description => "SDN index.", | |
216 | parameters => { | |
217 | additionalProperties => 0, | |
218 | properties => { | |
219 | node => get_standard_option('pve-node'), | |
220 | }, | |
221 | }, | |
222 | returns => { | |
223 | type => 'array', | |
224 | items => { | |
225 | type => "object", | |
226 | properties => {}, | |
227 | }, | |
228 | links => [ { rel => 'child', href => "{name}" } ], | |
229 | }, | |
230 | code => sub { | |
231 | my ($param) = @_; | |
232 | ||
233 | my $result = [ | |
234 | { name => 'zones' }, | |
235 | ]; | |
236 | return $result; | |
237 | }}); | |
4be427a0 AD |
238 | } |
239 | ||
aff192e6 | 240 | __PACKAGE__->register_method ({ |
f9d26e09 TL |
241 | name => 'index', |
242 | path => '', | |
aff192e6 DM |
243 | method => 'GET', |
244 | permissions => { user => 'all' }, | |
245 | description => "Node index.", | |
246 | parameters => { | |
6110ed03 | 247 | additionalProperties => 0, |
aff192e6 DM |
248 | properties => { |
249 | node => get_standard_option('pve-node'), | |
250 | }, | |
251 | }, | |
252 | returns => { | |
253 | type => 'array', | |
254 | items => { | |
255 | type => "object", | |
256 | properties => {}, | |
257 | }, | |
258 | links => [ { rel => 'child', href => "{name}" } ], | |
259 | }, | |
260 | code => sub { | |
261 | my ($param) = @_; | |
f9d26e09 | 262 | |
aff192e6 | 263 | my $result = [ |
784d6ee9 TL |
264 | { name => 'aplinfo' }, |
265 | { name => 'apt' }, | |
c6c4b278 | 266 | { name => 'capabilities' }, |
38db610a | 267 | { name => 'ceph' }, |
784d6ee9 TL |
268 | { name => 'certificates' }, |
269 | { name => 'config' }, | |
e781363f | 270 | { name => 'disks' }, |
784d6ee9 TL |
271 | { name => 'dns' }, |
272 | { name => 'firewall' }, | |
2f410501 | 273 | { name => 'hardware' }, |
784d6ee9 | 274 | { name => 'hosts' }, |
952280b4 | 275 | { name => 'journal' }, |
784d6ee9 | 276 | { name => 'lxc' }, |
8b3c353e | 277 | { name => 'migrateall' }, |
784d6ee9 TL |
278 | { name => 'netstat' }, |
279 | { name => 'network' }, | |
280 | { name => 'qemu' }, | |
17711ff8 | 281 | { name => 'query-url-metadata' }, |
784d6ee9 | 282 | { name => 'replication' }, |
34ada77a | 283 | { name => 'report' }, |
aff192e6 DM |
284 | { name => 'rrd' }, # fixme: remove? |
285 | { name => 'rrddata' },# fixme: remove? | |
aff192e6 | 286 | { name => 'scan' }, |
784d6ee9 TL |
287 | { name => 'services' }, |
288 | { name => 'spiceshell' }, | |
b92400b6 | 289 | { name => 'startall' }, |
784d6ee9 | 290 | { name => 'status' }, |
b92400b6 | 291 | { name => 'stopall' }, |
784d6ee9 TL |
292 | { name => 'storage' }, |
293 | { name => 'subscription' }, | |
5f04abc2 | 294 | { name => 'suspendall' }, |
784d6ee9 TL |
295 | { name => 'syslog' }, |
296 | { name => 'tasks' }, | |
297 | { name => 'termproxy' }, | |
298 | { name => 'time' }, | |
299 | { name => 'version' }, | |
300 | { name => 'vncshell' }, | |
301 | { name => 'vzdump' }, | |
302 | { name => 'wakeonlan' }, | |
952280b4 | 303 | ]; |
aff192e6 | 304 | |
bb654699 AD |
305 | push @$result, { name => 'sdn' } if $have_sdn; |
306 | ||
aff192e6 DM |
307 | return $result; |
308 | }}); | |
309 | ||
8747a9ec | 310 | __PACKAGE__->register_method ({ |
f9d26e09 | 311 | name => 'version', |
8747a9ec DM |
312 | path => 'version', |
313 | method => 'GET', | |
314 | proxyto => 'node', | |
315 | permissions => { user => 'all' }, | |
316 | description => "API version details", | |
317 | parameters => { | |
6110ed03 | 318 | additionalProperties => 0, |
8747a9ec DM |
319 | properties => { |
320 | node => get_standard_option('pve-node'), | |
321 | }, | |
322 | }, | |
323 | returns => { | |
324 | type => "object", | |
325 | properties => { | |
180a86d3 TL |
326 | version => { |
327 | type => 'string', | |
328 | description => 'The current installed pve-manager package version', | |
329 | }, | |
330 | release => { | |
331 | type => 'string', | |
332 | description => 'The current installed Proxmox VE Release', | |
333 | }, | |
334 | repoid => { | |
335 | type => 'string', | |
336 | description => 'The short git commit hash ID from which this version was build', | |
337 | }, | |
8747a9ec DM |
338 | }, |
339 | }, | |
340 | code => sub { | |
341 | my ($resp, $param) = @_; | |
f9d26e09 | 342 | |
8747a9ec DM |
343 | return PVE::pvecfg::version_info(); |
344 | }}); | |
345 | ||
20ad4e0e TL |
346 | my sub get_current_kernel_info { |
347 | my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); | |
348 | ||
349 | my $kernel_version_string = "$sysname $release $version"; # for legacy compat | |
350 | my $current_kernel = { | |
351 | sysname => $sysname, | |
352 | release => $release, | |
353 | version => $version, | |
354 | machine => $machine, | |
355 | }; | |
356 | return ($current_kernel, $kernel_version_string); | |
357 | } | |
358 | ||
92759ae1 | 359 | my $boot_mode_info_cache; |
81fd95cf | 360 | my sub get_boot_mode_info { |
92759ae1 TL |
361 | return $boot_mode_info_cache if defined($boot_mode_info_cache); |
362 | ||
81fd95cf TL |
363 | my $is_efi_booted = -d "/sys/firmware/efi"; |
364 | ||
92759ae1 | 365 | $boot_mode_info_cache = { |
81fd95cf TL |
366 | mode => $is_efi_booted ? 'efi' : 'legacy-bios', |
367 | }; | |
368 | ||
8113229e FG |
369 | my $efi_var = "/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"; |
370 | ||
371 | if ($is_efi_booted && -e $efi_var) { | |
372 | my $efi_var_sec_boot_entry = eval { file_get_contents($efi_var) }; | |
81fd95cf TL |
373 | if ($@) { |
374 | warn "Failed to read secure boot state: $@\n"; | |
375 | } else { | |
376 | my @secureboot = unpack("CCCCC", $efi_var_sec_boot_entry); | |
92759ae1 | 377 | $boot_mode_info_cache->{secureboot} = $secureboot[4] == 1 ? 1 : 0; |
81fd95cf TL |
378 | } |
379 | } | |
92759ae1 | 380 | return $boot_mode_info_cache; |
81fd95cf TL |
381 | } |
382 | ||
aff192e6 | 383 | __PACKAGE__->register_method({ |
f9d26e09 TL |
384 | name => 'status', |
385 | path => 'status', | |
aff192e6 DM |
386 | method => 'GET', |
387 | permissions => { | |
7d020b42 | 388 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
aff192e6 DM |
389 | }, |
390 | description => "Read node status", | |
391 | proxyto => 'node', | |
392 | parameters => { | |
6110ed03 | 393 | additionalProperties => 0, |
aff192e6 DM |
394 | properties => { |
395 | node => get_standard_option('pve-node'), | |
396 | }, | |
397 | }, | |
398 | returns => { | |
399 | type => "object", | |
4467a5d2 | 400 | additionalProperties => 1, |
aff192e6 | 401 | properties => { |
4467a5d2 TL |
402 | # TODO: document remaing ones |
403 | 'boot-info' => { | |
404 | description => "Meta-information about the boot mode.", | |
405 | type => 'object', | |
406 | properties => { | |
407 | mode => { | |
408 | description => 'Through which firmware the system got booted.', | |
409 | type => 'string', | |
410 | enum => [qw(efi legacy-bios)], | |
411 | }, | |
412 | secureboot => { | |
413 | description => 'System is booted in secure mode, only applicable for the "efi" mode.', | |
414 | type => 'boolean', | |
415 | optional => 1, | |
416 | }, | |
417 | }, | |
418 | }, | |
419 | 'current-kernel' => { | |
420 | description => "The uptime of the system in seconds.", | |
421 | type => 'object', | |
422 | properties => { | |
423 | sysname => { | |
424 | description => 'OS kernel name (e.g., "Linux")', | |
425 | type => 'string', | |
426 | }, | |
427 | release => { | |
428 | description => 'OS kernel release (e.g., "6.8.0")', | |
429 | type => 'string', | |
430 | }, | |
431 | version => { | |
432 | description => 'OS kernel version with build info', | |
433 | type => 'string', | |
434 | }, | |
435 | machine => { | |
436 | description => 'Hardware (architecture) type', | |
437 | type => 'string', | |
438 | }, | |
439 | }, | |
440 | }, | |
aff192e6 DM |
441 | }, |
442 | }, | |
443 | code => sub { | |
444 | my ($param) = @_; | |
445 | ||
446 | my $res = { | |
447 | uptime => 0, | |
448 | idle => 0, | |
449 | }; | |
450 | ||
451 | my ($uptime, $idle) = PVE::ProcFSTools::read_proc_uptime(); | |
452 | $res->{uptime} = $uptime; | |
f9d26e09 | 453 | |
aff192e6 DM |
454 | my ($avg1, $avg5, $avg15) = PVE::ProcFSTools::read_loadavg(); |
455 | $res->{loadavg} = [ $avg1, $avg5, $avg15]; | |
f9d26e09 | 456 | |
20ad4e0e TL |
457 | my ($current_kernel_info, $kversion_string) = get_current_kernel_info(); |
458 | $res->{kversion} = $kversion_string; | |
459 | $res->{'current-kernel'} = $current_kernel_info; | |
aff192e6 | 460 | |
81fd95cf TL |
461 | $res->{'boot-info'} = get_boot_mode_info(); |
462 | ||
aff192e6 DM |
463 | $res->{cpuinfo} = PVE::ProcFSTools::read_cpuinfo(); |
464 | ||
465 | my $stat = PVE::ProcFSTools::read_proc_stat(); | |
466 | $res->{cpu} = $stat->{cpu}; | |
467 | $res->{wait} = $stat->{wait}; | |
468 | ||
469 | my $meminfo = PVE::ProcFSTools::read_meminfo(); | |
470 | $res->{memory} = { | |
471 | free => $meminfo->{memfree}, | |
472 | total => $meminfo->{memtotal}, | |
473 | used => $meminfo->{memused}, | |
474 | }; | |
f9d26e09 | 475 | |
20539e0c DM |
476 | $res->{ksm} = { |
477 | shared => $meminfo->{memshared}, | |
478 | }; | |
479 | ||
aff192e6 DM |
480 | $res->{swap} = { |
481 | free => $meminfo->{swapfree}, | |
482 | total => $meminfo->{swaptotal}, | |
483 | used => $meminfo->{swapused}, | |
484 | }; | |
485 | ||
486 | $res->{pveversion} = PVE::pvecfg::package() . "/" . | |
8747a9ec | 487 | PVE::pvecfg::version_text(); |
aff192e6 DM |
488 | |
489 | my $dinfo = df('/', 1); # output is bytes | |
490 | ||
491 | $res->{rootfs} = { | |
492 | total => $dinfo->{blocks}, | |
493 | avail => $dinfo->{bavail}, | |
494 | used => $dinfo->{used}, | |
3d0fcc46 | 495 | free => $dinfo->{blocks} - $dinfo->{used}, |
aff192e6 DM |
496 | }; |
497 | ||
498 | return $res; | |
499 | }}); | |
500 | ||
0455911d SP |
501 | __PACKAGE__->register_method({ |
502 | name => 'netstat', | |
503 | path => 'netstat', | |
504 | method => 'GET', | |
505 | permissions => { | |
506 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], | |
507 | }, | |
508 | description => "Read tap/vm network device interface counters", | |
509 | proxyto => 'node', | |
510 | parameters => { | |
511 | additionalProperties => 0, | |
512 | properties => { | |
513 | node => get_standard_option('pve-node'), | |
514 | }, | |
515 | }, | |
516 | returns => { | |
d6e7fa04 TL |
517 | type => "array", |
518 | items => { | |
519 | type => "object", | |
520 | properties => {}, | |
521 | }, | |
0455911d SP |
522 | }, |
523 | code => sub { | |
524 | my ($param) = @_; | |
525 | ||
526 | my $res = [ ]; | |
527 | ||
528 | my $netdev = PVE::ProcFSTools::read_proc_net_dev(); | |
d6e7fa04 TL |
529 | foreach my $dev (sort keys %$netdev) { |
530 | next if $dev !~ m/^(?:tap|veth)([1-9]\d*)i(\d+)$/; | |
531 | my ($vmid, $netid) = ($1, $2); | |
532 | ||
533 | push @$res, { | |
534 | vmid => $vmid, | |
535 | dev => "net$netid", | |
536 | in => $netdev->{$dev}->{transmit}, | |
537 | out => $netdev->{$dev}->{receive}, | |
538 | }; | |
0455911d SP |
539 | } |
540 | ||
541 | return $res; | |
542 | }}); | |
543 | ||
87c3e931 SP |
544 | __PACKAGE__->register_method({ |
545 | name => 'execute', | |
546 | path => 'execute', | |
40993593 | 547 | method => 'POST', |
eb859354 | 548 | description => "Execute multiple commands in order, root only.", |
87c3e931 | 549 | proxyto => 'node', |
40993593 | 550 | protected => 1, # avoid problems with proxy code |
87c3e931 SP |
551 | parameters => { |
552 | additionalProperties => 0, | |
553 | properties => { | |
554 | node => get_standard_option('pve-node'), | |
40993593 DM |
555 | commands => { |
556 | description => "JSON encoded array of commands.", | |
557 | type => "string", | |
9018237e FG |
558 | verbose_description => "JSON encoded array of commands, where each command is an object with the following properties:\n" |
559 | . PVE::RESTHandler::dump_properties($verify_command_item_desc->{items}->{properties}, 'full'), | |
a1de4410 | 560 | format => "pve-command-batch", |
40993593 | 561 | } |
87c3e931 SP |
562 | }, |
563 | }, | |
564 | returns => { | |
565 | type => 'array', | |
72db67a2 SS |
566 | items => { |
567 | type => "object", | |
568 | properties => {}, | |
87c3e931 SP |
569 | }, |
570 | }, | |
571 | code => sub { | |
572 | my ($param) = @_; | |
573 | my $res = []; | |
574 | ||
40993593 DM |
575 | my $rpcenv = PVE::RPCEnvironment::get(); |
576 | my $user = $rpcenv->get_user(); | |
a1de4410 | 577 | # just parse the json again, it should already be validated |
40993593 DM |
578 | my $commands = eval { decode_json($param->{commands}); }; |
579 | ||
58ab77d1 | 580 | foreach my $cmd (@$commands) { |
87c3e931 | 581 | eval { |
40993593 DM |
582 | $cmd->{args} //= {}; |
583 | ||
584 | my $path = "nodes/$param->{node}/$cmd->{path}"; | |
f9d26e09 | 585 | |
40993593 DM |
586 | my $uri_param = {}; |
587 | my ($handler, $info) = PVE::API2->find_handler($cmd->{method}, $path, $uri_param); | |
588 | if (!$handler || !$info) { | |
589 | die "no handler for '$path'\n"; | |
590 | } | |
591 | ||
592 | foreach my $p (keys %{$cmd->{args}}) { | |
593 | raise_param_exc({ $p => "duplicate parameter" }) if defined($uri_param->{$p}); | |
594 | $uri_param->{$p} = $cmd->{args}->{$p}; | |
595 | } | |
596 | ||
597 | # check access permissions | |
598 | $rpcenv->check_api2_permissions($info->{permissions}, $user, $uri_param); | |
599 | ||
600 | push @$res, { | |
601 | status => HTTP_OK, | |
a51ceeb7 | 602 | data => $handler->handle($info, $uri_param), |
40993593 | 603 | }; |
87c3e931 | 604 | }; |
40993593 DM |
605 | if (my $err = $@) { |
606 | my $resp = { status => HTTP_INTERNAL_SERVER_ERROR }; | |
607 | if (ref($err) eq "PVE::Exception") { | |
608 | $resp->{status} = $err->{code} if $err->{code}; | |
609 | $resp->{errors} = $err->{errors} if $err->{errors}; | |
610 | $resp->{message} = $err->{msg}; | |
611 | } else { | |
612 | $resp->{message} = $err; | |
613 | } | |
614 | push @$res, $resp; | |
615 | } | |
87c3e931 SP |
616 | } |
617 | ||
618 | return $res; | |
619 | }}); | |
620 | ||
621 | ||
aff192e6 | 622 | __PACKAGE__->register_method({ |
f9d26e09 TL |
623 | name => 'node_cmd', |
624 | path => 'status', | |
aff192e6 DM |
625 | method => 'POST', |
626 | permissions => { | |
7d020b42 | 627 | check => ['perm', '/nodes/{node}', [ 'Sys.PowerMgmt' ]], |
aff192e6 DM |
628 | }, |
629 | protected => 1, | |
630 | description => "Reboot or shutdown a node.", | |
631 | proxyto => 'node', | |
632 | parameters => { | |
6110ed03 | 633 | additionalProperties => 0, |
aff192e6 DM |
634 | properties => { |
635 | node => get_standard_option('pve-node'), | |
636 | command => { | |
637 | description => "Specify the command.", | |
638 | type => 'string', | |
639 | enum => [qw(reboot shutdown)], | |
640 | }, | |
641 | }, | |
642 | }, | |
643 | returns => { type => "null" }, | |
644 | code => sub { | |
645 | my ($param) = @_; | |
646 | ||
647 | if ($param->{command} eq 'reboot') { | |
648 | system ("(sleep 2;/sbin/reboot)&"); | |
649 | } elsif ($param->{command} eq 'shutdown') { | |
650 | system ("(sleep 2;/sbin/poweroff)&"); | |
651 | } | |
652 | ||
79d62026 | 653 | return; |
aff192e6 DM |
654 | }}); |
655 | ||
b3d84542 CE |
656 | __PACKAGE__->register_method({ |
657 | name => 'wakeonlan', | |
658 | path => 'wakeonlan', | |
659 | method => 'POST', | |
660 | permissions => { | |
661 | check => ['perm', '/nodes/{node}', [ 'Sys.PowerMgmt' ]], | |
662 | }, | |
663 | protected => 1, | |
664 | description => "Try to wake a node via 'wake on LAN' network packet.", | |
665 | parameters => { | |
666 | additionalProperties => 0, | |
667 | properties => { | |
668 | node => get_standard_option('pve-node', { | |
669 | description => 'target node for wake on LAN packet', | |
5aa7b909 CE |
670 | completion => sub { |
671 | my $members = PVE::Cluster::get_members(); | |
672 | return [ grep { !$members->{$_}->{online} } keys %$members ]; | |
673 | } | |
b3d84542 CE |
674 | }), |
675 | }, | |
676 | }, | |
0f615ea9 CE |
677 | returns => { |
678 | type => 'string', | |
679 | format => 'mac-addr', | |
ec178804 | 680 | description => 'MAC address used to assemble the WoL magic packet.', |
0f615ea9 | 681 | }, |
b3d84542 CE |
682 | code => sub { |
683 | my ($param) = @_; | |
684 | ||
82436996 | 685 | my $node = $param->{node}; |
869c155c | 686 | my $local_node = PVE::INotify::nodename(); |
82436996 TL |
687 | |
688 | die "'$node' is local node, cannot wake my self!\n" | |
869c155c | 689 | if $node eq 'localhost' || $node eq $local_node; |
82436996 TL |
690 | |
691 | PVE::Cluster::check_node_exists($node); | |
692 | ||
693 | my $config = PVE::NodeConfig::load_config($node); | |
3f83a033 CE |
694 | my $wol_config = PVE::NodeConfig::get_wakeonlan_config($config); |
695 | my $mac_addr = $wol_config->{mac}; | |
b3d84542 | 696 | if (!defined($mac_addr)) { |
82436996 | 697 | die "No wake on LAN MAC address defined for '$node'!\n"; |
b3d84542 CE |
698 | } |
699 | ||
869c155c CE |
700 | my $local_config = PVE::NodeConfig::load_config($local_node); |
701 | my $local_wol_config = PVE::NodeConfig::get_wakeonlan_config($local_config); | |
702 | my $bind_iface = $local_wol_config->{'bind-interface'}; | |
a967ff65 | 703 | my $broadcast_addr = $local_wol_config->{'broadcast-address'} // '255.255.255.255'; |
869c155c | 704 | |
b3d84542 CE |
705 | $mac_addr =~ s/://g; |
706 | my $packet = chr(0xff) x 6 . pack('H*', $mac_addr) x 16; | |
707 | ||
a967ff65 | 708 | my $addr = gethostbyname($broadcast_addr); |
b3d84542 CE |
709 | my $port = getservbyname('discard', 'udp'); |
710 | my $to = Socket::pack_sockaddr_in($port, $addr); | |
82436996 | 711 | |
b3d84542 CE |
712 | socket(my $sock, Socket::AF_INET, Socket::SOCK_DGRAM, Socket::IPPROTO_UDP) |
713 | || die "Unable to open socket: $!\n"; | |
714 | setsockopt($sock, Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1) | |
715 | || die "Unable to set socket option: $!\n"; | |
716 | ||
869c155c CE |
717 | if (defined($bind_iface)) { |
718 | # Null terminated interface name | |
719 | my $bind_iface_raw = pack('Z*', $bind_iface); | |
720 | setsockopt($sock, Socket::SOL_SOCKET, Socket::SO_BINDTODEVICE, $bind_iface_raw) | |
721 | || die "Unable to bind socket to interface '$bind_iface': $!\n"; | |
722 | } | |
723 | ||
b3d84542 CE |
724 | send($sock, $packet, 0, $to) |
725 | || die "Unable to send packet: $!\n"; | |
726 | ||
727 | close($sock); | |
728 | ||
3f83a033 | 729 | return $wol_config->{mac}; |
b3d84542 | 730 | }}); |
aff192e6 DM |
731 | |
732 | __PACKAGE__->register_method({ | |
f9d26e09 TL |
733 | name => 'rrd', |
734 | path => 'rrd', | |
aff192e6 DM |
735 | method => 'GET', |
736 | protected => 1, # fixme: can we avoid that? | |
737 | permissions => { | |
7d020b42 | 738 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
aff192e6 DM |
739 | }, |
740 | description => "Read node RRD statistics (returns PNG)", | |
741 | parameters => { | |
6110ed03 | 742 | additionalProperties => 0, |
aff192e6 DM |
743 | properties => { |
744 | node => get_standard_option('pve-node'), | |
745 | timeframe => { | |
746 | description => "Specify the time frame you are interested in.", | |
747 | type => 'string', | |
748 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
749 | }, | |
750 | ds => { | |
751 | description => "The list of datasources you want to display.", | |
58ab77d1 | 752 | type => 'string', format => 'pve-configid-list', |
aff192e6 DM |
753 | }, |
754 | cf => { | |
755 | description => "The RRD consolidation function", | |
58ab77d1 | 756 | type => 'string', |
aff192e6 DM |
757 | enum => [ 'AVERAGE', 'MAX' ], |
758 | optional => 1, | |
759 | }, | |
760 | }, | |
761 | }, | |
762 | returns => { | |
763 | type => "object", | |
764 | properties => { | |
765 | filename => { type => 'string' }, | |
766 | }, | |
767 | }, | |
768 | code => sub { | |
769 | my ($param) = @_; | |
770 | ||
516a7948 | 771 | return PVE::RRD::create_rrd_graph( |
f9d26e09 | 772 | "pve2-node/$param->{node}", $param->{timeframe}, |
aff192e6 DM |
773 | $param->{ds}, $param->{cf}); |
774 | ||
775 | }}); | |
776 | ||
777 | __PACKAGE__->register_method({ | |
f9d26e09 TL |
778 | name => 'rrddata', |
779 | path => 'rrddata', | |
aff192e6 DM |
780 | method => 'GET', |
781 | protected => 1, # fixme: can we avoid that? | |
782 | permissions => { | |
7d020b42 | 783 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
aff192e6 DM |
784 | }, |
785 | description => "Read node RRD statistics", | |
786 | parameters => { | |
6110ed03 | 787 | additionalProperties => 0, |
aff192e6 DM |
788 | properties => { |
789 | node => get_standard_option('pve-node'), | |
790 | timeframe => { | |
791 | description => "Specify the time frame you are interested in.", | |
792 | type => 'string', | |
793 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
794 | }, | |
795 | cf => { | |
796 | description => "The RRD consolidation function", | |
58ab77d1 | 797 | type => 'string', |
aff192e6 DM |
798 | enum => [ 'AVERAGE', 'MAX' ], |
799 | optional => 1, | |
800 | }, | |
801 | }, | |
802 | }, | |
803 | returns => { | |
804 | type => "array", | |
805 | items => { | |
806 | type => "object", | |
807 | properties => {}, | |
808 | }, | |
809 | }, | |
810 | code => sub { | |
811 | my ($param) = @_; | |
812 | ||
516a7948 | 813 | return PVE::RRD::create_rrd_data( |
aff192e6 DM |
814 | "pve2-node/$param->{node}", $param->{timeframe}, $param->{cf}); |
815 | }}); | |
816 | ||
817 | __PACKAGE__->register_method({ | |
f9d26e09 TL |
818 | name => 'syslog', |
819 | path => 'syslog', | |
aff192e6 DM |
820 | method => 'GET', |
821 | description => "Read system log", | |
822 | proxyto => 'node', | |
823 | permissions => { | |
7d020b42 | 824 | check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]], |
aff192e6 DM |
825 | }, |
826 | protected => 1, | |
827 | parameters => { | |
6110ed03 | 828 | additionalProperties => 0, |
aff192e6 DM |
829 | properties => { |
830 | node => get_standard_option('pve-node'), | |
831 | start => { | |
832 | type => 'integer', | |
833 | minimum => 0, | |
834 | optional => 1, | |
835 | }, | |
836 | limit => { | |
837 | type => 'integer', | |
838 | minimum => 0, | |
839 | optional => 1, | |
840 | }, | |
01b753b6 TL |
841 | since => { |
842 | type=> 'string', | |
843 | pattern => '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$', | |
844 | description => "Display all log since this date-time string.", | |
845 | optional => 1, | |
846 | }, | |
847 | until => { | |
848 | type=> 'string', | |
849 | pattern => '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$', | |
850 | description => "Display all log until this date-time string.", | |
851 | optional => 1, | |
852 | }, | |
34142272 DC |
853 | service => { |
854 | description => "Service ID", | |
855 | type => 'string', | |
856 | maxLength => 128, | |
857 | optional => 1, | |
858 | }, | |
aff192e6 DM |
859 | }, |
860 | }, | |
861 | returns => { | |
862 | type => 'array', | |
f9d26e09 | 863 | items => { |
aff192e6 DM |
864 | type => "object", |
865 | properties => { | |
866 | n => { | |
867 | description=> "Line number", | |
868 | type=> 'integer', | |
869 | }, | |
870 | t => { | |
871 | description=> "Line text", | |
872 | type => 'string', | |
873 | } | |
874 | } | |
875 | } | |
876 | }, | |
877 | code => sub { | |
878 | my ($param) = @_; | |
879 | ||
aff192e6 DM |
880 | my $rpcenv = PVE::RPCEnvironment::get(); |
881 | my $user = $rpcenv->get_user(); | |
882 | my $node = $param->{node}; | |
34142272 DC |
883 | my $service; |
884 | ||
885 | if ($param->{service}) { | |
886 | my $service_aliases = { | |
887 | 'postfix' => 'postfix@-', | |
888 | }; | |
889 | ||
890 | $service = $service_aliases->{$param->{service}} // $param->{service}; | |
891 | } | |
aff192e6 | 892 | |
01b753b6 | 893 | my ($count, $lines) = PVE::Tools::dump_journal($param->{start}, $param->{limit}, |
34142272 | 894 | $param->{since}, $param->{until}, $service); |
ff9c330c DM |
895 | |
896 | $rpcenv->set_result_attrib('total', $count); | |
01b753b6 TL |
897 | |
898 | return $lines; | |
ff9c330c DM |
899 | }}); |
900 | ||
1d397a83 DC |
901 | __PACKAGE__->register_method({ |
902 | name => 'journal', | |
903 | path => 'journal', | |
904 | method => 'GET', | |
905 | description => "Read Journal", | |
906 | proxyto => 'node', | |
907 | permissions => { | |
908 | check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]], | |
909 | }, | |
910 | protected => 1, | |
911 | parameters => { | |
912 | additionalProperties => 0, | |
913 | properties => { | |
914 | node => get_standard_option('pve-node'), | |
915 | since => { | |
961399a3 TL |
916 | type=> 'integer', |
917 | minimum => 0, | |
f9b08743 | 918 | description => "Display all log since this UNIX epoch. Conflicts with 'startcursor'.", |
1d397a83 DC |
919 | optional => 1, |
920 | }, | |
921 | until => { | |
961399a3 TL |
922 | type=> 'integer', |
923 | minimum => 0, | |
f9b08743 | 924 | description => "Display all log until this UNIX epoch. Conflicts with 'endcursor'.", |
1d397a83 DC |
925 | optional => 1, |
926 | }, | |
927 | lastentries => { | |
a03cb8b8 | 928 | description => "Limit to the last X lines. Conflicts with a range.", |
1d397a83 | 929 | type => 'integer', |
961399a3 | 930 | minimum => 0, |
1d397a83 DC |
931 | optional => 1, |
932 | }, | |
933 | startcursor => { | |
f9b08743 | 934 | description => "Start after the given Cursor. Conflicts with 'since'", |
1d397a83 DC |
935 | type => 'string', |
936 | optional => 1, | |
937 | }, | |
938 | endcursor => { | |
f9b08743 | 939 | description => "End before the given Cursor. Conflicts with 'until'", |
1d397a83 DC |
940 | type => 'string', |
941 | optional => 1, | |
942 | }, | |
943 | }, | |
944 | }, | |
945 | returns => { | |
946 | type => 'array', | |
947 | items => { | |
948 | type => "string", | |
949 | } | |
950 | }, | |
951 | code => sub { | |
952 | my ($param) = @_; | |
953 | ||
954 | my $rpcenv = PVE::RPCEnvironment::get(); | |
955 | my $user = $rpcenv->get_user(); | |
956 | ||
68df9496 | 957 | my $cmd = ["/usr/bin/mini-journalreader", "-j"]; |
a03cb8b8 TL |
958 | push @$cmd, '-n', $param->{lastentries} if $param->{lastentries}; |
959 | push @$cmd, '-b', $param->{since} if $param->{since}; | |
960 | push @$cmd, '-e', $param->{until} if $param->{until}; | |
68df9496 DC |
961 | push @$cmd, '-f', PVE::Tools::shellquote($param->{startcursor}) if $param->{startcursor}; |
962 | push @$cmd, '-t', PVE::Tools::shellquote($param->{endcursor}) if $param->{endcursor}; | |
963 | push @$cmd, ' | gzip '; | |
964 | ||
965 | open(my $fh, "-|", join(' ', @$cmd)) | |
966 | or die "could not start mini-journalreader"; | |
967 | ||
968 | return { | |
969 | download => { | |
970 | fh => $fh, | |
971 | stream => 1, | |
972 | 'content-type' => 'application/json', | |
973 | 'content-encoding' => 'gzip', | |
974 | }, | |
975 | }, | |
1d397a83 DC |
976 | }}); |
977 | ||
aff192e6 DM |
978 | my $sslcert; |
979 | ||
dab7a849 | 980 | my $shell_cmd_map = { |
f24be7e7 TL |
981 | 'login' => { |
982 | cmd => [ '/bin/login', '-f', 'root' ], | |
983 | }, | |
984 | 'upgrade' => { | |
985 | cmd => [ '/usr/bin/pveupgrade', '--shell' ], | |
986 | }, | |
987 | 'ceph_install' => { | |
988 | cmd => [ '/usr/bin/pveceph', 'install' ], | |
989 | allow_args => 1, | |
990 | }, | |
d03d7e1e TM |
991 | }; |
992 | ||
993 | sub get_shell_command { | |
f24be7e7 | 994 | my ($user, $shellcmd, $args) = @_; |
d03d7e1e | 995 | |
f24be7e7 | 996 | my $cmd; |
d03d7e1e | 997 | if ($user eq 'root@pam') { |
fc1da3b0 | 998 | if (defined($shellcmd) && exists($shell_cmd_map->{$shellcmd})) { |
f24be7e7 TL |
999 | my $def = $shell_cmd_map->{$shellcmd}; |
1000 | $cmd = [ @{$def->{cmd}} ]; # clone | |
1001 | if (defined($args) && $def->{allow_args}) { | |
1002 | push @$cmd, split("\0", $args); | |
1003 | } | |
d03d7e1e | 1004 | } else { |
f24be7e7 | 1005 | $cmd = [ '/bin/login', '-f', 'root' ]; |
d03d7e1e TM |
1006 | } |
1007 | } else { | |
f24be7e7 TL |
1008 | # non-root must always login for now, we do not have a superuser role! |
1009 | $cmd = [ '/bin/login' ]; | |
d03d7e1e | 1010 | } |
f24be7e7 | 1011 | return $cmd; |
d03d7e1e TM |
1012 | } |
1013 | ||
677bee7c TL |
1014 | my $get_vnc_connection_info = sub { |
1015 | my $node = shift; | |
1016 | ||
1017 | my $remote_cmd = []; | |
1018 | ||
1019 | my ($remip, $family); | |
1020 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
1021 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); | |
1022 | $remote_cmd = ['/usr/bin/ssh', '-e', 'none', '-t', $remip , '--']; | |
1023 | } else { | |
1024 | $family = PVE::Tools::get_host_address_family($node); | |
1025 | } | |
1026 | my $port = PVE::Tools::next_vnc_port($family); | |
1027 | ||
1028 | return ($port, $remote_cmd); | |
1029 | }; | |
1030 | ||
aff192e6 | 1031 | __PACKAGE__->register_method ({ |
f9d26e09 TL |
1032 | name => 'vncshell', |
1033 | path => 'vncshell', | |
aff192e6 DM |
1034 | method => 'POST', |
1035 | protected => 1, | |
1036 | permissions => { | |
7d020b42 | 1037 | check => ['perm', '/nodes/{node}', [ 'Sys.Console' ]], |
aff192e6 DM |
1038 | }, |
1039 | description => "Creates a VNC Shell proxy.", | |
1040 | parameters => { | |
6110ed03 | 1041 | additionalProperties => 0, |
aff192e6 DM |
1042 | properties => { |
1043 | node => get_standard_option('pve-node'), | |
d03d7e1e TM |
1044 | cmd => { |
1045 | type => 'string', | |
4fb92ae8 | 1046 | description => "Run specific command or default to login (requires 'root\@pam')", |
dab7a849 | 1047 | enum => [keys %$shell_cmd_map], |
d03d7e1e TM |
1048 | optional => 1, |
1049 | default => 'login', | |
1050 | }, | |
f24be7e7 TL |
1051 | 'cmd-opts' => { |
1052 | type => 'string', | |
1053 | description => "Add parameters to a command. Encoded as null terminated strings.", | |
1054 | requires => 'cmd', | |
1055 | optional => 1, | |
1056 | default => '', | |
1057 | }, | |
8dfca17e DM |
1058 | websocket => { |
1059 | optional => 1, | |
1060 | type => 'boolean', | |
1061 | description => "use websocket instead of standard vnc.", | |
1062 | }, | |
b8ac8b0c DC |
1063 | width => { |
1064 | optional => 1, | |
1065 | description => "sets the width of the console in pixels.", | |
1066 | type => 'integer', | |
1067 | minimum => 16, | |
1068 | maximum => 4096, | |
1069 | }, | |
1070 | height => { | |
1071 | optional => 1, | |
1072 | description => "sets the height of the console in pixels.", | |
1073 | type => 'integer', | |
1074 | minimum => 16, | |
1075 | maximum => 2160, | |
1076 | }, | |
aff192e6 DM |
1077 | }, |
1078 | }, | |
f9d26e09 | 1079 | returns => { |
6110ed03 | 1080 | additionalProperties => 0, |
aff192e6 DM |
1081 | properties => { |
1082 | user => { type => 'string' }, | |
1083 | ticket => { type => 'string' }, | |
1084 | cert => { type => 'string' }, | |
1085 | port => { type => 'integer' }, | |
1086 | upid => { type => 'string' }, | |
1087 | }, | |
1088 | }, | |
1089 | code => sub { | |
1090 | my ($param) = @_; | |
1091 | ||
1092 | my $rpcenv = PVE::RPCEnvironment::get(); | |
d553e535 | 1093 | my ($user, undef, $realm) = PVE::AccessControl::verify_username($rpcenv->get_user()); |
d0289a19 | 1094 | |
0c8e509e | 1095 | |
4fb92ae8 | 1096 | if (defined($param->{cmd}) && $param->{cmd} ne 'login' && $user ne 'root@pam') { |
0c8e509e FE |
1097 | raise_perm_exc('user != root@pam'); |
1098 | } | |
3a76893d | 1099 | |
aff192e6 DM |
1100 | my $node = $param->{node}; |
1101 | ||
57ebda08 | 1102 | my $authpath = "/nodes/$node"; |
57ebda08 DM |
1103 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($user, $authpath); |
1104 | ||
aff192e6 DM |
1105 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) |
1106 | if !$sslcert; | |
1107 | ||
677bee7c | 1108 | my ($port, $remcmd) = $get_vnc_connection_info->($node); |
d4a25f0b | 1109 | |
f24be7e7 | 1110 | my $shcmd = get_shell_command($user, $param->{cmd}, $param->{'cmd-opts'}); |
aff192e6 | 1111 | |
f9d26e09 | 1112 | my $timeout = 10; |
aff192e6 | 1113 | |
677bee7c TL |
1114 | my $cmd = ['/usr/bin/vncterm', |
1115 | '-rfbport', $port, | |
1116 | '-timeout', $timeout, | |
1117 | '-authpath', $authpath, | |
1118 | '-perm', 'Sys.Console', | |
1119 | ]; | |
b8ac8b0c | 1120 | |
677bee7c TL |
1121 | push @$cmd, '-width', $param->{width} if $param->{width}; |
1122 | push @$cmd, '-height', $param->{height} if $param->{height}; | |
b8ac8b0c | 1123 | |
8dfca17e | 1124 | if ($param->{websocket}) { |
f9d26e09 | 1125 | $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm |
8dfca17e DM |
1126 | push @$cmd, '-notls', '-listen', 'localhost'; |
1127 | } | |
1128 | ||
1129 | push @$cmd, '-c', @$remcmd, @$shcmd; | |
aff192e6 DM |
1130 | |
1131 | my $realcmd = sub { | |
1132 | my $upid = shift; | |
1133 | ||
1134 | syslog ('info', "starting vnc proxy $upid\n"); | |
1135 | ||
6d394492 | 1136 | my $cmdstr = join (' ', @$cmd); |
aff192e6 DM |
1137 | syslog ('info', "launch command: $cmdstr"); |
1138 | ||
f9d26e09 | 1139 | eval { |
6d394492 | 1140 | foreach my $k (keys %ENV) { |
8dfca17e | 1141 | next if $k eq 'PVE_VNC_TICKET'; |
b0d4b407 | 1142 | next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME' || $k eq 'LANG' || $k eq 'LANGUAGE'; |
6d394492 DM |
1143 | delete $ENV{$k}; |
1144 | } | |
1145 | $ENV{PWD} = '/'; | |
1146 | ||
b0d4b407 | 1147 | PVE::Tools::run_command($cmd, errmsg => "vncterm failed", keeplocale => 1); |
6d394492 DM |
1148 | }; |
1149 | if (my $err = $@) { | |
1150 | syslog ('err', $err); | |
aff192e6 DM |
1151 | } |
1152 | ||
1153 | return; | |
1154 | }; | |
1155 | ||
1156 | my $upid = $rpcenv->fork_worker('vncshell', "", $user, $realcmd); | |
f9d26e09 | 1157 | |
6806a0f8 | 1158 | PVE::Tools::wait_for_vnc_port($port); |
aff192e6 DM |
1159 | |
1160 | return { | |
1161 | user => $user, | |
1162 | ticket => $ticket, | |
f9d26e09 TL |
1163 | port => $port, |
1164 | upid => $upid, | |
1165 | cert => $sslcert, | |
aff192e6 DM |
1166 | }; |
1167 | }}); | |
1168 | ||
4b168c27 DC |
1169 | __PACKAGE__->register_method ({ |
1170 | name => 'termproxy', | |
1171 | path => 'termproxy', | |
1172 | method => 'POST', | |
1173 | protected => 1, | |
1174 | permissions => { | |
4b168c27 DC |
1175 | check => ['perm', '/nodes/{node}', [ 'Sys.Console' ]], |
1176 | }, | |
1177 | description => "Creates a VNC Shell proxy.", | |
1178 | parameters => { | |
1179 | additionalProperties => 0, | |
1180 | properties => { | |
1181 | node => get_standard_option('pve-node'), | |
d03d7e1e TM |
1182 | cmd => { |
1183 | type => 'string', | |
4fb92ae8 | 1184 | description => "Run specific command or default to login (requires 'root\@pam')", |
dab7a849 | 1185 | enum => [keys %$shell_cmd_map], |
d03d7e1e TM |
1186 | optional => 1, |
1187 | default => 'login', | |
1188 | }, | |
f24be7e7 TL |
1189 | 'cmd-opts' => { |
1190 | type => 'string', | |
1191 | description => "Add parameters to a command. Encoded as null terminated strings.", | |
1192 | requires => 'cmd', | |
1193 | optional => 1, | |
1194 | default => '', | |
1195 | }, | |
4b168c27 DC |
1196 | }, |
1197 | }, | |
1198 | returns => { | |
1199 | additionalProperties => 0, | |
1200 | properties => { | |
1201 | user => { type => 'string' }, | |
1202 | ticket => { type => 'string' }, | |
1203 | port => { type => 'integer' }, | |
1204 | upid => { type => 'string' }, | |
1205 | }, | |
1206 | }, | |
1207 | code => sub { | |
1208 | my ($param) = @_; | |
1209 | ||
1210 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4b168c27 | 1211 | my ($user, undef, $realm) = PVE::AccessControl::verify_username($rpcenv->get_user()); |
4b168c27 DC |
1212 | |
1213 | my $node = $param->{node}; | |
4b168c27 | 1214 | my $authpath = "/nodes/$node"; |
4b168c27 DC |
1215 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($user, $authpath); |
1216 | ||
677bee7c | 1217 | my ($port, $remcmd) = $get_vnc_connection_info->($node); |
4b168c27 | 1218 | |
f24be7e7 | 1219 | my $shcmd = get_shell_command($user, $param->{cmd}, $param->{'cmd-opts'}); |
4b168c27 DC |
1220 | |
1221 | my $realcmd = sub { | |
1222 | my $upid = shift; | |
1223 | ||
1224 | syslog ('info', "starting termproxy $upid\n"); | |
1225 | ||
f24be7e7 TL |
1226 | my $cmd = [ |
1227 | '/usr/bin/termproxy', | |
1228 | $port, | |
677bee7c TL |
1229 | '--path', $authpath, |
1230 | '--perm', 'Sys.Console', | |
1231 | '--' | |
1232 | ]; | |
d03d7e1e | 1233 | push @$cmd, @$remcmd, @$shcmd; |
4b168c27 DC |
1234 | |
1235 | PVE::Tools::run_command($cmd); | |
1236 | }; | |
4b168c27 DC |
1237 | my $upid = $rpcenv->fork_worker('vncshell', "", $user, $realcmd); |
1238 | ||
1239 | PVE::Tools::wait_for_vnc_port($port); | |
1240 | ||
1241 | return { | |
1242 | user => $user, | |
1243 | ticket => $ticket, | |
1244 | port => $port, | |
1245 | upid => $upid, | |
1246 | }; | |
1247 | }}); | |
1248 | ||
8dfca17e DM |
1249 | __PACKAGE__->register_method({ |
1250 | name => 'vncwebsocket', | |
1251 | path => 'vncwebsocket', | |
1252 | method => 'GET', | |
f9d26e09 | 1253 | permissions => { |
7914f5e7 | 1254 | description => "You also need to pass a valid ticket (vncticket).", |
8dfca17e DM |
1255 | check => ['perm', '/nodes/{node}', [ 'Sys.Console' ]], |
1256 | }, | |
0e68b116 | 1257 | description => "Opens a websocket for VNC traffic.", |
8dfca17e | 1258 | parameters => { |
6110ed03 | 1259 | additionalProperties => 0, |
8dfca17e DM |
1260 | properties => { |
1261 | node => get_standard_option('pve-node'), | |
1262 | vncticket => { | |
1263 | description => "Ticket from previous call to vncproxy.", | |
1264 | type => 'string', | |
1265 | maxLength => 512, | |
1266 | }, | |
1267 | port => { | |
1268 | description => "Port number returned by previous vncproxy call.", | |
1269 | type => 'integer', | |
1270 | minimum => 5900, | |
1271 | maximum => 5999, | |
1272 | }, | |
1273 | }, | |
1274 | }, | |
1275 | returns => { | |
1276 | type => "object", | |
1277 | properties => { | |
1278 | port => { type => 'string' }, | |
1279 | }, | |
1280 | }, | |
1281 | code => sub { | |
1282 | my ($param) = @_; | |
1283 | ||
1284 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1285 | ||
1286 | my ($user, undef, $realm) = PVE::AccessControl::verify_username($rpcenv->get_user()); | |
1287 | ||
8dfca17e DM |
1288 | my $authpath = "/nodes/$param->{node}"; |
1289 | ||
1290 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $user, $authpath); | |
1291 | ||
1292 | my $port = $param->{port}; | |
f9d26e09 | 1293 | |
8dfca17e DM |
1294 | return { port => $port }; |
1295 | }}); | |
1296 | ||
2d802f8c | 1297 | __PACKAGE__->register_method ({ |
f9d26e09 TL |
1298 | name => 'spiceshell', |
1299 | path => 'spiceshell', | |
2d802f8c DM |
1300 | method => 'POST', |
1301 | protected => 1, | |
1302 | proxyto => 'node', | |
1303 | permissions => { | |
2d802f8c DM |
1304 | check => ['perm', '/nodes/{node}', [ 'Sys.Console' ]], |
1305 | }, | |
7774df78 | 1306 | description => "Creates a SPICE shell.", |
2d802f8c | 1307 | parameters => { |
6110ed03 | 1308 | additionalProperties => 0, |
2d802f8c DM |
1309 | properties => { |
1310 | node => get_standard_option('pve-node'), | |
7774df78 | 1311 | proxy => get_standard_option('spice-proxy', { optional => 1 }), |
d03d7e1e TM |
1312 | cmd => { |
1313 | type => 'string', | |
4fb92ae8 | 1314 | description => "Run specific command or default to login (requires 'root\@pam')", |
dab7a849 | 1315 | enum => [keys %$shell_cmd_map], |
d03d7e1e TM |
1316 | optional => 1, |
1317 | default => 'login', | |
1318 | }, | |
f24be7e7 TL |
1319 | 'cmd-opts' => { |
1320 | type => 'string', | |
1321 | description => "Add parameters to a command. Encoded as null terminated strings.", | |
1322 | requires => 'cmd', | |
1323 | optional => 1, | |
1324 | default => '', | |
1325 | }, | |
2d802f8c DM |
1326 | }, |
1327 | }, | |
7774df78 | 1328 | returns => get_standard_option('remote-viewer-config'), |
2d802f8c DM |
1329 | code => sub { |
1330 | my ($param) = @_; | |
1331 | ||
1332 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1333 | my $authuser = $rpcenv->get_user(); | |
1334 | ||
1335 | my ($user, undef, $realm) = PVE::AccessControl::verify_username($authuser); | |
1336 | ||
b270a6b4 | 1337 | |
4fb92ae8 | 1338 | if (defined($param->{cmd}) && $param->{cmd} ne 'login' && $user ne 'root@pam') { |
b270a6b4 FE |
1339 | raise_perm_exc('user != root@pam'); |
1340 | } | |
2d802f8c DM |
1341 | |
1342 | my $node = $param->{node}; | |
1343 | my $proxy = $param->{proxy}; | |
2d802f8c DM |
1344 | |
1345 | my $authpath = "/nodes/$node"; | |
b289829f | 1346 | my $permissions = 'Sys.Console'; |
b270a6b4 | 1347 | |
f24be7e7 | 1348 | my $shcmd = get_shell_command($user, $param->{cmd}, $param->{'cmd-opts'}); |
2d802f8c | 1349 | |
b289829f | 1350 | my $title = "Shell on '$node'"; |
2d802f8c | 1351 | |
b289829f | 1352 | return PVE::API2Tools::run_spiceterm($authpath, $permissions, 0, $node, $proxy, $title, $shcmd); |
2d802f8c DM |
1353 | }}); |
1354 | ||
aff192e6 | 1355 | __PACKAGE__->register_method({ |
f9d26e09 TL |
1356 | name => 'dns', |
1357 | path => 'dns', | |
aff192e6 DM |
1358 | method => 'GET', |
1359 | permissions => { | |
7d020b42 | 1360 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
aff192e6 DM |
1361 | }, |
1362 | description => "Read DNS settings.", | |
1363 | proxyto => 'node', | |
1364 | parameters => { | |
6110ed03 | 1365 | additionalProperties => 0, |
aff192e6 DM |
1366 | properties => { |
1367 | node => get_standard_option('pve-node'), | |
1368 | }, | |
1369 | }, | |
1370 | returns => { | |
1371 | type => "object", | |
6110ed03 | 1372 | additionalProperties => 0, |
aff192e6 DM |
1373 | properties => { |
1374 | search => { | |
1375 | description => "Search domain for host-name lookup.", | |
1376 | type => 'string', | |
1377 | optional => 1, | |
1378 | }, | |
1379 | dns1 => { | |
1380 | description => 'First name server IP address.', | |
1381 | type => 'string', | |
1382 | optional => 1, | |
f9d26e09 | 1383 | }, |
aff192e6 DM |
1384 | dns2 => { |
1385 | description => 'Second name server IP address.', | |
1386 | type => 'string', | |
1387 | optional => 1, | |
f9d26e09 | 1388 | }, |
aff192e6 DM |
1389 | dns3 => { |
1390 | description => 'Third name server IP address.', | |
1391 | type => 'string', | |
1392 | optional => 1, | |
f9d26e09 | 1393 | }, |
aff192e6 DM |
1394 | }, |
1395 | }, | |
1396 | code => sub { | |
1397 | my ($param) = @_; | |
1398 | ||
1399 | my $res = PVE::INotify::read_file('resolvconf'); | |
1400 | ||
1401 | return $res; | |
1402 | }}); | |
1403 | ||
1404 | __PACKAGE__->register_method({ | |
f9d26e09 TL |
1405 | name => 'update_dns', |
1406 | path => 'dns', | |
aff192e6 DM |
1407 | method => 'PUT', |
1408 | description => "Write DNS settings.", | |
d0289a19 DM |
1409 | permissions => { |
1410 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
1411 | }, | |
aff192e6 DM |
1412 | proxyto => 'node', |
1413 | protected => 1, | |
1414 | parameters => { | |
6110ed03 | 1415 | additionalProperties => 0, |
aff192e6 DM |
1416 | properties => { |
1417 | node => get_standard_option('pve-node'), | |
1418 | search => { | |
1419 | description => "Search domain for host-name lookup.", | |
1420 | type => 'string', | |
1421 | }, | |
1422 | dns1 => { | |
1423 | description => 'First name server IP address.', | |
7a9486a7 | 1424 | type => 'string', format => 'ip', |
aff192e6 | 1425 | optional => 1, |
f9d26e09 | 1426 | }, |
aff192e6 DM |
1427 | dns2 => { |
1428 | description => 'Second name server IP address.', | |
7a9486a7 | 1429 | type => 'string', format => 'ip', |
aff192e6 | 1430 | optional => 1, |
f9d26e09 | 1431 | }, |
aff192e6 DM |
1432 | dns3 => { |
1433 | description => 'Third name server IP address.', | |
7a9486a7 | 1434 | type => 'string', format => 'ip', |
aff192e6 | 1435 | optional => 1, |
f9d26e09 | 1436 | }, |
aff192e6 DM |
1437 | }, |
1438 | }, | |
1439 | returns => { type => "null" }, | |
1440 | code => sub { | |
1441 | my ($param) = @_; | |
1442 | ||
1443 | PVE::INotify::update_file('resolvconf', $param); | |
1444 | ||
79d62026 | 1445 | return; |
aff192e6 DM |
1446 | }}); |
1447 | ||
1448 | __PACKAGE__->register_method({ | |
f9d26e09 TL |
1449 | name => 'time', |
1450 | path => 'time', | |
aff192e6 DM |
1451 | method => 'GET', |
1452 | permissions => { | |
7d020b42 DM |
1453 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
1454 | }, | |
aff192e6 DM |
1455 | description => "Read server time and time zone settings.", |
1456 | proxyto => 'node', | |
1457 | parameters => { | |
58ab77d1 | 1458 | additionalProperties => 0, |
aff192e6 DM |
1459 | properties => { |
1460 | node => get_standard_option('pve-node'), | |
1461 | }, | |
1462 | }, | |
1463 | returns => { | |
1464 | type => "object", | |
6110ed03 | 1465 | additionalProperties => 0, |
aff192e6 DM |
1466 | properties => { |
1467 | timezone => { | |
1468 | description => "Time zone", | |
1469 | type => 'string', | |
1470 | }, | |
1471 | time => { | |
1472 | description => "Seconds since 1970-01-01 00:00:00 UTC.", | |
1473 | type => 'integer', | |
1474 | minimum => 1297163644, | |
bed5fdfc | 1475 | renderer => 'timestamp', |
aff192e6 DM |
1476 | }, |
1477 | localtime => { | |
1478 | description => "Seconds since 1970-01-01 00:00:00 (local time)", | |
1479 | type => 'integer', | |
1480 | minimum => 1297163644, | |
bed5fdfc | 1481 | renderer => 'timestamp_gmt', |
aff192e6 | 1482 | }, |
58ab77d1 | 1483 | }, |
aff192e6 DM |
1484 | }, |
1485 | code => sub { | |
1486 | my ($param) = @_; | |
1487 | ||
1488 | my $ctime = time(); | |
1489 | my $ltime = timegm_nocheck(localtime($ctime)); | |
1490 | my $res = { | |
1491 | timezone => PVE::INotify::read_file('timezone'), | |
bed5fdfc | 1492 | time => $ctime, |
aff192e6 DM |
1493 | localtime => $ltime, |
1494 | }; | |
1495 | ||
1496 | return $res; | |
1497 | }}); | |
1498 | ||
1499 | __PACKAGE__->register_method({ | |
f9d26e09 TL |
1500 | name => 'set_timezone', |
1501 | path => 'time', | |
aff192e6 DM |
1502 | method => 'PUT', |
1503 | description => "Set time zone.", | |
d0289a19 DM |
1504 | permissions => { |
1505 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
1506 | }, | |
aff192e6 DM |
1507 | proxyto => 'node', |
1508 | protected => 1, | |
1509 | parameters => { | |
6110ed03 | 1510 | additionalProperties => 0, |
aff192e6 DM |
1511 | properties => { |
1512 | node => get_standard_option('pve-node'), | |
1513 | timezone => { | |
1514 | description => "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.", | |
1515 | type => 'string', | |
1516 | }, | |
1517 | }, | |
1518 | }, | |
1519 | returns => { type => "null" }, | |
1520 | code => sub { | |
1521 | my ($param) = @_; | |
1522 | ||
1523 | PVE::INotify::write_file('timezone', $param->{timezone}); | |
1524 | ||
79d62026 | 1525 | return; |
aff192e6 DM |
1526 | }}); |
1527 | ||
c9164975 | 1528 | __PACKAGE__->register_method({ |
f9d26e09 TL |
1529 | name => 'aplinfo', |
1530 | path => 'aplinfo', | |
c9164975 DM |
1531 | method => 'GET', |
1532 | permissions => { | |
1533 | user => 'all', | |
1534 | }, | |
1535 | description => "Get list of appliances.", | |
1536 | proxyto => 'node', | |
1537 | parameters => { | |
6110ed03 | 1538 | additionalProperties => 0, |
c9164975 DM |
1539 | properties => { |
1540 | node => get_standard_option('pve-node'), | |
1541 | }, | |
1542 | }, | |
1543 | returns => { | |
1544 | type => 'array', | |
1545 | items => { | |
1546 | type => "object", | |
1547 | properties => {}, | |
1548 | }, | |
1549 | }, | |
1550 | code => sub { | |
1551 | my ($param) = @_; | |
1552 | ||
c9164975 DM |
1553 | my $list = PVE::APLInfo::load_data(); |
1554 | ||
0d213945 TL |
1555 | my $res = []; |
1556 | for my $appliance (values %{$list->{all}}) { | |
1557 | next if $appliance->{'package'} eq 'pve-web-news'; | |
1558 | push @$res, $appliance; | |
c9164975 DM |
1559 | } |
1560 | ||
1561 | return $res; | |
1562 | }}); | |
1563 | ||
0532bd28 | 1564 | __PACKAGE__->register_method({ |
f9d26e09 TL |
1565 | name => 'apl_download', |
1566 | path => 'aplinfo', | |
0532bd28 DM |
1567 | method => 'POST', |
1568 | permissions => { | |
1569 | check => ['perm', '/storage/{storage}', ['Datastore.AllocateTemplate']], | |
1570 | }, | |
1571 | description => "Download appliance templates.", | |
1572 | proxyto => 'node', | |
1573 | protected => 1, | |
1574 | parameters => { | |
6110ed03 | 1575 | additionalProperties => 0, |
0532bd28 DM |
1576 | properties => { |
1577 | node => get_standard_option('pve-node'), | |
82282acf | 1578 | storage => get_standard_option('pve-storage-id', { |
62180d0f | 1579 | description => "The storage where the template will be stored", |
82282acf WL |
1580 | completion => \&PVE::Storage::complete_storage_enabled, |
1581 | }), | |
6110ed03 TL |
1582 | template => { |
1583 | type => 'string', | |
1584 | description => "The template which will downloaded", | |
1585 | maxLength => 255, | |
1586 | completion => \&complete_templet_repo, | |
82282acf | 1587 | }, |
0532bd28 DM |
1588 | }, |
1589 | }, | |
1590 | returns => { type => "string" }, | |
1591 | code => sub { | |
1592 | my ($param) = @_; | |
1593 | ||
1594 | my $rpcenv = PVE::RPCEnvironment::get(); | |
0532bd28 | 1595 | my $user = $rpcenv->get_user(); |
0532bd28 | 1596 | |
aee25c2e | 1597 | my $node = $param->{node}; |
0532bd28 | 1598 | my $template = $param->{template}; |
0532bd28 | 1599 | |
aee25c2e TL |
1600 | my $list = PVE::APLInfo::load_data(); |
1601 | my $appliance = $list->{all}->{$template}; | |
1602 | raise_param_exc({ template => "no such template"}) if !$appliance; | |
0532bd28 | 1603 | |
bbcfdc08 | 1604 | my $cfg = PVE::Storage::config(); |
0532bd28 DM |
1605 | my $scfg = PVE::Storage::storage_check_enabled($cfg, $param->{storage}, $node); |
1606 | ||
aee25c2e TL |
1607 | die "unknown template type '$appliance->{type}'\n" |
1608 | if !($appliance->{type} eq 'openvz' || $appliance->{type} eq 'lxc'); | |
0532bd28 | 1609 | |
f9d26e09 | 1610 | die "storage '$param->{storage}' does not support templates\n" |
0532bd28 DM |
1611 | if !$scfg->{content}->{vztmpl}; |
1612 | ||
0532bd28 | 1613 | my $tmpldir = PVE::Storage::get_vztmpl_dir($cfg, $param->{storage}); |
0532bd28 | 1614 | |
fac5d57e | 1615 | my $worker = sub { |
aee25c2e TL |
1616 | my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg'); |
1617 | ||
1618 | PVE::Tools::download_file_from_url("$tmpldir/$template", $appliance->{location}, { | |
1619 | hash_required => 1, | |
1620 | sha512sum => $appliance->{sha512sum}, | |
1621 | md5sum => $appliance->{md5sum}, | |
1622 | http_proxy => $dccfg->{http_proxy}, | |
1623 | }); | |
fac5d57e | 1624 | }; |
0532bd28 | 1625 | |
fac5d57e | 1626 | my $upid = $rpcenv->fork_worker('download', $template, $user, $worker); |
0532bd28 | 1627 | |
fac5d57e | 1628 | return $upid; |
0532bd28 DM |
1629 | }}); |
1630 | ||
17711ff8 LS |
1631 | __PACKAGE__->register_method({ |
1632 | name => 'query_url_metadata', | |
1633 | path => 'query-url-metadata', | |
1634 | method => 'GET', | |
1635 | description => "Query metadata of an URL: file size, file name and mime type.", | |
1636 | proxyto => 'node', | |
1637 | permissions => { | |
6cf1a48d TL |
1638 | check => ['or', |
1639 | ['perm', '/', [ 'Sys.Audit', 'Sys.Modify' ]], | |
1640 | ['perm', '/nodes/{node}', [ 'Sys.AccessNetwork' ]], | |
1641 | ], | |
17711ff8 LS |
1642 | }, |
1643 | parameters => { | |
1644 | additionalProperties => 0, | |
1645 | properties => { | |
1646 | node => get_standard_option('pve-node'), | |
1647 | url => { | |
1648 | description => "The URL to query the metadata from.", | |
1649 | type => 'string', | |
1650 | pattern => 'https?://.*', | |
1651 | }, | |
1652 | 'verify-certificates' => { | |
1653 | description => "If false, no SSL/TLS certificates will be verified.", | |
1654 | type => 'boolean', | |
1655 | optional => 1, | |
1656 | default => 1, | |
d61728e2 | 1657 | }, |
17711ff8 LS |
1658 | }, |
1659 | }, | |
1660 | returns => { | |
1661 | type => "object", | |
1662 | properties => { | |
1663 | filename => { | |
1664 | type => 'string', | |
1665 | optional => 1, | |
1666 | }, | |
1667 | size => { | |
1668 | type => 'integer', | |
1669 | renderer => 'bytes', | |
1670 | optional => 1, | |
1671 | }, | |
1672 | mimetype => { | |
1673 | type => 'string', | |
1674 | optional => 1, | |
1675 | }, | |
1676 | }, | |
1677 | }, | |
1678 | code => sub { | |
1679 | my ($param) = @_; | |
1680 | ||
1681 | my $url = $param->{url}; | |
1682 | ||
1683 | my $ua = LWP::UserAgent->new(); | |
dfde1eec | 1684 | $ua->agent("Proxmox VE"); |
17711ff8 LS |
1685 | |
1686 | my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
1687 | if ($dccfg->{http_proxy}) { | |
1688 | $ua->proxy('http', $dccfg->{http_proxy}); | |
1689 | } | |
1690 | ||
1691 | my $verify = $param->{'verify-certificates'} // 1; | |
1692 | if (!$verify) { | |
1693 | $ua->ssl_opts( | |
1694 | verify_hostname => 0, | |
1695 | SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, | |
1696 | ); | |
1697 | } | |
1698 | ||
1699 | my $req = HTTP::Request->new(HEAD => $url); | |
1700 | my $res = $ua->request($req); | |
1701 | ||
1702 | die "invalid server response: '" . $res->status_line() . "'\n" if ($res->code() != 200); | |
1703 | ||
1704 | my $size = $res->header("Content-Length"); | |
1705 | my $disposition = $res->header("Content-Disposition"); | |
1706 | my $type = $res->header("Content-Type"); | |
1707 | ||
1708 | my $filename; | |
1709 | ||
1710 | if ($disposition && ($disposition =~ m/filename="([^"]*)"/ || $disposition =~ m/filename=([^;]*)/)) { | |
1711 | $filename = $1; | |
1712 | } elsif ($url =~ m!^[^?]+/([^?/]*)(?:\?.*)?$!) { | |
1713 | $filename = $1; | |
1714 | } | |
1715 | ||
1716 | # Content-Type: text/html; charset=utf-8 | |
1717 | if ($type && $type =~ m/^([^;]+);/) { | |
1718 | $type = $1; | |
1719 | } | |
1720 | ||
1721 | my $ret = {}; | |
1722 | $ret->{filename} = $filename if $filename; | |
1723 | $ret->{size} = $size + 0 if $size; | |
1724 | $ret->{mimetype} = $type if $type; | |
1725 | ||
1726 | return $ret; | |
1727 | }}); | |
1728 | ||
34ada77a EK |
1729 | __PACKAGE__->register_method({ |
1730 | name => 'report', | |
1731 | path => 'report', | |
1732 | method => 'GET', | |
1733 | permissions => { | |
1734 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], | |
1735 | }, | |
6bbdf981 | 1736 | protected => 1, |
34ada77a EK |
1737 | description => "Gather various systems information about a node", |
1738 | proxyto => 'node', | |
1739 | parameters => { | |
6110ed03 | 1740 | additionalProperties => 0, |
34ada77a EK |
1741 | properties => { |
1742 | node => get_standard_option('pve-node'), | |
1743 | }, | |
1744 | }, | |
1745 | returns => { | |
cdb5a209 | 1746 | type => 'string', |
34ada77a EK |
1747 | }, |
1748 | code => sub { | |
cdb5a209 | 1749 | return PVE::Report::generate(); |
34ada77a | 1750 | }}); |
0532bd28 | 1751 | |
b72cdbb7 TL |
1752 | # returns a list of VMIDs, those can be filtered by |
1753 | # * current parent node | |
1754 | # * vmid whitelist | |
1755 | # * guest is a template (default: skip) | |
1756 | # * guest is HA manged (default: skip) | |
1757 | my $get_filtered_vmlist = sub { | |
1758 | my ($nodename, $vmfilter, $templates, $ha_managed) = @_; | |
b92400b6 | 1759 | |
b92400b6 DM |
1760 | my $vmlist = PVE::Cluster::get_vmlist(); |
1761 | ||
dbfcac27 | 1762 | my $vms_allowed; |
2a498506 | 1763 | if (defined($vmfilter)) { |
dbfcac27 | 1764 | $vms_allowed = { map { $_ => 1 } PVE::Tools::split_list($vmfilter) }; |
2a498506 DC |
1765 | } |
1766 | ||
b72cdbb7 | 1767 | my $res = {}; |
b92400b6 | 1768 | foreach my $vmid (keys %{$vmlist->{ids}}) { |
dbfcac27 | 1769 | next if defined($vms_allowed) && !$vms_allowed->{$vmid}; |
b92400b6 | 1770 | |
b72cdbb7 TL |
1771 | my $d = $vmlist->{ids}->{$vmid}; |
1772 | next if $nodename && $d->{node} ne $nodename; | |
b92400b6 | 1773 | |
b72cdbb7 TL |
1774 | eval { |
1775 | my $class; | |
2c27e4b7 | 1776 | if ($d->{type} eq 'lxc') { |
b72cdbb7 | 1777 | $class = 'PVE::LXC::Config'; |
2c27e4b7 | 1778 | } elsif ($d->{type} eq 'qemu') { |
b72cdbb7 | 1779 | $class = 'PVE::QemuConfig'; |
b92400b6 | 1780 | } else { |
dbfcac27 | 1781 | die "unknown virtual guest type '$d->{type}'\n"; |
b92400b6 DM |
1782 | } |
1783 | ||
b72cdbb7 TL |
1784 | my $conf = $class->load_config($vmid); |
1785 | return if !$templates && $class->is_template($conf); | |
1786 | return if !$ha_managed && PVE::HA::Config::vm_is_ha_managed($vmid); | |
04b2004b | 1787 | |
b2bb6d77 | 1788 | $res->{$vmid}->{conf} = $conf; |
b72cdbb7 | 1789 | $res->{$vmid}->{type} = $d->{type}; |
1c8dc310 | 1790 | $res->{$vmid}->{class} = $class; |
b92400b6 DM |
1791 | }; |
1792 | warn $@ if $@; | |
1793 | } | |
1794 | ||
b72cdbb7 TL |
1795 | return $res; |
1796 | }; | |
1797 | ||
1798 | # return all VMs which should get started/stopped on power up/down | |
1799 | my $get_start_stop_list = sub { | |
1800 | my ($nodename, $autostart, $vmfilter) = @_; | |
1801 | ||
ae9d10ca TL |
1802 | # do not skip HA vms on force or if a specific VMID set is wanted |
1803 | my $include_ha_managed = defined($vmfilter) ? 1 : 0; | |
1804 | ||
dbfcac27 | 1805 | my $vmlist = $get_filtered_vmlist->($nodename, $vmfilter, undef, $include_ha_managed); |
b72cdbb7 TL |
1806 | |
1807 | my $resList = {}; | |
1808 | foreach my $vmid (keys %$vmlist) { | |
b2bb6d77 | 1809 | my $conf = $vmlist->{$vmid}->{conf}; |
b72cdbb7 TL |
1810 | next if $autostart && !$conf->{onboot}; |
1811 | ||
dbfcac27 TL |
1812 | my $startup = $conf->{startup} ? PVE::JSONSchema::pve_parse_startup_order($conf->{startup}) : {}; |
1813 | my $order = $startup->{order} = $startup->{order} // LONG_MAX; | |
b72cdbb7 | 1814 | |
dbfcac27 TL |
1815 | $resList->{$order}->{$vmid} = $startup; |
1816 | $resList->{$order}->{$vmid}->{type} = $vmlist->{$vmid}->{type}; | |
b72cdbb7 TL |
1817 | } |
1818 | ||
b92400b6 DM |
1819 | return $resList; |
1820 | }; | |
1821 | ||
1c8dc310 TL |
1822 | my $remove_locks_on_startup = sub { |
1823 | my ($nodename) = @_; | |
1824 | ||
1825 | my $vmlist = &$get_filtered_vmlist($nodename, undef, undef, 1); | |
1826 | ||
1827 | foreach my $vmid (keys %$vmlist) { | |
1828 | my $conf = $vmlist->{$vmid}->{conf}; | |
23b54109 | 1829 | my $class = $vmlist->{$vmid}->{class}; |
1c8dc310 TL |
1830 | |
1831 | eval { | |
1832 | if ($class->has_lock($conf, 'backup')) { | |
1833 | $class->remove_lock($vmid, 'backup'); | |
1834 | my $msg = "removed left over backup lock from '$vmid'!"; | |
1835 | warn "$msg\n"; # prints to task log | |
1836 | syslog('warning', $msg); | |
1837 | } | |
1838 | }; warn $@ if $@; | |
1839 | } | |
1840 | }; | |
1841 | ||
b92400b6 | 1842 | __PACKAGE__->register_method ({ |
f9d26e09 TL |
1843 | name => 'startall', |
1844 | path => 'startall', | |
b92400b6 DM |
1845 | method => 'POST', |
1846 | protected => 1, | |
17e3b3b2 | 1847 | permissions => { |
6aec4565 FE |
1848 | description => "The 'VM.PowerMgmt' permission is required on '/' or on '/vms/<ID>' for " |
1849 | ."each ID passed via the 'vms' parameter.", | |
1850 | user => 'all', | |
17e3b3b2 | 1851 | }, |
9c8a09a7 | 1852 | proxyto => 'node', |
e1b57809 | 1853 | description => "Start all VMs and containers located on this node (by default only those with onboot=1).", |
b92400b6 | 1854 | parameters => { |
6110ed03 | 1855 | additionalProperties => 0, |
b92400b6 DM |
1856 | properties => { |
1857 | node => get_standard_option('pve-node'), | |
c09c7160 AD |
1858 | force => { |
1859 | optional => 1, | |
1860 | type => 'boolean', | |
e1b57809 TL |
1861 | default => 'off', |
1862 | description => "Issue start command even if virtual guest have 'onboot' not set or set to off.", | |
c09c7160 | 1863 | }, |
2a498506 | 1864 | vms => { |
a740deff | 1865 | description => "Only consider guests from this comma separated list of VMIDs.", |
2a498506 DC |
1866 | type => 'string', format => 'pve-vmid-list', |
1867 | optional => 1, | |
1868 | }, | |
b92400b6 DM |
1869 | }, |
1870 | }, | |
1871 | returns => { | |
1872 | type => 'string', | |
1873 | }, | |
1874 | code => sub { | |
1875 | my ($param) = @_; | |
1876 | ||
1877 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1878 | my $authuser = $rpcenv->get_user(); | |
1879 | ||
6aec4565 FE |
1880 | if (!$rpcenv->check($authuser, "/", [ 'VM.PowerMgmt' ], 1)) { |
1881 | my @vms = PVE::Tools::split_list($param->{vms}); | |
1882 | if (scalar(@vms) > 0) { | |
1883 | $rpcenv->check($authuser, "/vms/$_", [ 'VM.PowerMgmt' ]) for @vms; | |
1884 | } else { | |
1885 | raise_perm_exc("/, VM.PowerMgmt"); | |
1886 | } | |
1887 | } | |
1888 | ||
b92400b6 DM |
1889 | my $nodename = $param->{node}; |
1890 | $nodename = PVE::INotify::nodename() if $nodename eq 'localhost'; | |
1891 | ||
c09c7160 AD |
1892 | my $force = $param->{force}; |
1893 | ||
b92400b6 | 1894 | my $code = sub { |
b92400b6 DM |
1895 | $rpcenv->{type} = 'priv'; # to start tasks in background |
1896 | ||
3947d0a0 DM |
1897 | if (!PVE::Cluster::check_cfs_quorum(1)) { |
1898 | print "waiting for quorum ...\n"; | |
1899 | do { | |
1900 | sleep(1); | |
1901 | } while (!PVE::Cluster::check_cfs_quorum(1)); | |
1902 | print "got quorum\n"; | |
b92400b6 | 1903 | } |
1c8dc310 TL |
1904 | |
1905 | eval { # remove backup locks, but avoid running into a scheduled backup job | |
1906 | PVE::Tools::lock_file('/var/run/vzdump.lock', 10, $remove_locks_on_startup, $nodename); | |
f77faab2 TL |
1907 | }; |
1908 | warn $@ if $@; | |
1c8dc310 | 1909 | |
c09c7160 | 1910 | my $autostart = $force ? undef : 1; |
f77faab2 | 1911 | my $startList = $get_start_stop_list->($nodename, $autostart, $param->{vms}); |
3f12bcfb DM |
1912 | |
1913 | # Note: use numeric sorting with <=> | |
f77faab2 | 1914 | for my $order (sort {$a <=> $b} keys %$startList) { |
b92400b6 DM |
1915 | my $vmlist = $startList->{$order}; |
1916 | ||
f77faab2 | 1917 | for my $vmid (sort {$a <=> $b} keys %$vmlist) { |
b92400b6 DM |
1918 | my $d = $vmlist->{$vmid}; |
1919 | ||
1920 | PVE::Cluster::check_cfs_quorum(); # abort when we loose quorum | |
b72cdbb7 | 1921 | |
b92400b6 DM |
1922 | eval { |
1923 | my $default_delay = 0; | |
1924 | my $upid; | |
1925 | ||
2c27e4b7 DM |
1926 | if ($d->{type} eq 'lxc') { |
1927 | return if PVE::LXC::check_running($vmid); | |
1928 | print STDERR "Starting CT $vmid\n"; | |
1b5e56f2 | 1929 | $upid = PVE::API2::LXC::Status->vm_start({node => $nodename, vmid => $vmid }); |
2c27e4b7 | 1930 | } elsif ($d->{type} eq 'qemu') { |
d6c49392 | 1931 | $default_delay = 3; # to reduce load |
b92400b6 DM |
1932 | return if PVE::QemuServer::check_running($vmid, 1); |
1933 | print STDERR "Starting VM $vmid\n"; | |
1934 | $upid = PVE::API2::Qemu->vm_start({node => $nodename, vmid => $vmid }); | |
1935 | } else { | |
1936 | die "unknown VM type '$d->{type}'\n"; | |
1937 | } | |
1938 | ||
f77faab2 TL |
1939 | my $task = PVE::Tools::upid_decode($upid); |
1940 | while (PVE::ProcFSTools::check_process_running($task->{pid})) { | |
b92400b6 DM |
1941 | sleep(1); |
1942 | } | |
1943 | ||
1944 | my $status = PVE::Tools::upid_read_status($upid); | |
0a7de337 | 1945 | if (!PVE::Tools::upid_status_is_error($status)) { |
b92400b6 DM |
1946 | # use default delay to reduce load |
1947 | my $delay = defined($d->{up}) ? int($d->{up}) : $default_delay; | |
1948 | if ($delay > 0) { | |
1949 | print STDERR "Waiting for $delay seconds (startup delay)\n" if $d->{up}; | |
1950 | for (my $i = 0; $i < $delay; $i++) { | |
1951 | sleep(1); | |
1952 | } | |
1953 | } | |
1954 | } else { | |
b27ebcad TL |
1955 | my $rendered_type = $d->{type} eq 'lxc' ? 'CT' : 'VM'; |
1956 | print STDERR "Starting $rendered_type $vmid failed: $status\n"; | |
b92400b6 DM |
1957 | } |
1958 | }; | |
1959 | warn $@ if $@; | |
1960 | } | |
1961 | } | |
1962 | return; | |
1963 | }; | |
1964 | ||
1965 | return $rpcenv->fork_worker('startall', undef, $authuser, $code); | |
1966 | }}); | |
1967 | ||
1968 | my $create_stop_worker = sub { | |
11543293 | 1969 | my ($nodename, $type, $vmid, $timeout, $force_stop) = @_; |
b92400b6 | 1970 | |
2c27e4b7 DM |
1971 | if ($type eq 'lxc') { |
1972 | return if !PVE::LXC::check_running($vmid); | |
2c27e4b7 | 1973 | print STDERR "Stopping CT $vmid (timeout = $timeout seconds)\n"; |
79d62026 | 1974 | return PVE::API2::LXC::Status->vm_shutdown( |
11543293 | 1975 | { node => $nodename, vmid => $vmid, timeout => $timeout, forceStop => $force_stop } |
79d62026 | 1976 | ); |
2c27e4b7 | 1977 | } elsif ($type eq 'qemu') { |
b92400b6 | 1978 | return if !PVE::QemuServer::check_running($vmid, 1); |
88ba9a1d | 1979 | print STDERR "Stopping VM $vmid (timeout = $timeout seconds)\n"; |
79d62026 | 1980 | return PVE::API2::Qemu->vm_shutdown( |
11543293 | 1981 | { node => $nodename, vmid => $vmid, timeout => $timeout, forceStop => $force_stop } |
79d62026 | 1982 | ); |
b92400b6 DM |
1983 | } else { |
1984 | die "unknown VM type '$type'\n"; | |
1985 | } | |
b92400b6 DM |
1986 | }; |
1987 | ||
1988 | __PACKAGE__->register_method ({ | |
f9d26e09 TL |
1989 | name => 'stopall', |
1990 | path => 'stopall', | |
b92400b6 DM |
1991 | method => 'POST', |
1992 | protected => 1, | |
17e3b3b2 | 1993 | permissions => { |
6aec4565 FE |
1994 | description => "The 'VM.PowerMgmt' permission is required on '/' or on '/vms/<ID>' for " |
1995 | ."each ID passed via the 'vms' parameter.", | |
1996 | user => 'all', | |
17e3b3b2 | 1997 | }, |
9c8a09a7 | 1998 | proxyto => 'node', |
b92400b6 DM |
1999 | description => "Stop all VMs and Containers.", |
2000 | parameters => { | |
6110ed03 | 2001 | additionalProperties => 0, |
b92400b6 DM |
2002 | properties => { |
2003 | node => get_standard_option('pve-node'), | |
2a498506 DC |
2004 | vms => { |
2005 | description => "Only consider Guests with these IDs.", | |
2006 | type => 'string', format => 'pve-vmid-list', | |
2007 | optional => 1, | |
2008 | }, | |
11543293 TL |
2009 | 'force-stop' => { |
2010 | description => 'Force a hard-stop after the timeout.', | |
2011 | type => 'boolean', | |
2012 | default => 1, | |
2013 | optional => 1, | |
2014 | }, | |
8f063fbc | 2015 | 'timeout' => { |
853ce1ea TL |
2016 | description => 'Timeout for each guest shutdown task. Depending on `force-stop`,' |
2017 | .' the shutdown gets then simply aborted or a hard-stop is forced.', | |
8f063fbc TL |
2018 | type => 'integer', |
2019 | optional => 1, | |
2020 | default => 180, | |
853ce1ea TL |
2021 | minimum => 0, |
2022 | maximum => 2 * 3600, # mostly arbitrary, but we do not want to high timeouts | |
8f063fbc | 2023 | }, |
b92400b6 DM |
2024 | }, |
2025 | }, | |
2026 | returns => { | |
2027 | type => 'string', | |
2028 | }, | |
2029 | code => sub { | |
2030 | my ($param) = @_; | |
2031 | ||
2032 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2033 | my $authuser = $rpcenv->get_user(); | |
2034 | ||
6aec4565 FE |
2035 | if (!$rpcenv->check($authuser, "/", [ 'VM.PowerMgmt' ], 1)) { |
2036 | my @vms = PVE::Tools::split_list($param->{vms}); | |
2037 | if (scalar(@vms) > 0) { | |
2038 | $rpcenv->check($authuser, "/vms/$_", [ 'VM.PowerMgmt' ]) for @vms; | |
2039 | } else { | |
2040 | raise_perm_exc("/, VM.PowerMgmt"); | |
2041 | } | |
2042 | } | |
2043 | ||
b92400b6 DM |
2044 | my $nodename = $param->{node}; |
2045 | $nodename = PVE::INotify::nodename() if $nodename eq 'localhost'; | |
2046 | ||
2047 | my $code = sub { | |
2048 | ||
2049 | $rpcenv->{type} = 'priv'; # to start tasks in background | |
2050 | ||
f77faab2 | 2051 | my $stopList = $get_start_stop_list->($nodename, undef, $param->{vms}); |
b92400b6 DM |
2052 | |
2053 | my $cpuinfo = PVE::ProcFSTools::read_cpuinfo(); | |
89ceb802 TL |
2054 | my $datacenterconfig = cfs_read_file('datacenter.cfg'); |
2055 | # if not set by user spawn max cpu count number of workers | |
2056 | my $maxWorkers = $datacenterconfig->{max_workers} || $cpuinfo->{cpus}; | |
b92400b6 | 2057 | |
f77faab2 | 2058 | for my $order (sort {$b <=> $a} keys %$stopList) { |
b92400b6 DM |
2059 | my $vmlist = $stopList->{$order}; |
2060 | my $workers = {}; | |
37bec895 DM |
2061 | |
2062 | my $finish_worker = sub { | |
2063 | my $pid = shift; | |
f77faab2 | 2064 | my $worker = delete $workers->{$pid} || return; |
37bec895 | 2065 | |
f77faab2 | 2066 | syslog('info', "end task $worker->{upid}"); |
37bec895 DM |
2067 | }; |
2068 | ||
f77faab2 | 2069 | for my $vmid (sort {$b <=> $a} keys %$vmlist) { |
b92400b6 | 2070 | my $d = $vmlist->{$vmid}; |
8f063fbc | 2071 | my $timeout = int($d->{down} // $param->{timeout} // 180); |
11543293 TL |
2072 | my $upid = eval { |
2073 | $create_stop_worker->( | |
2074 | $nodename, $d->{type}, $vmid, $timeout, $param->{'force-stop'} // 1) | |
2075 | }; | |
b92400b6 | 2076 | warn $@ if $@; |
37bec895 DM |
2077 | next if !$upid; |
2078 | ||
f77faab2 TL |
2079 | my $task = PVE::Tools::upid_decode($upid, 1); |
2080 | next if !$task; | |
37bec895 | 2081 | |
f77faab2 | 2082 | my $pid = $task->{pid}; |
37bec895 DM |
2083 | |
2084 | $workers->{$pid} = { type => $d->{type}, upid => $upid, vmid => $vmid }; | |
b92400b6 DM |
2085 | while (scalar(keys %$workers) >= $maxWorkers) { |
2086 | foreach my $p (keys %$workers) { | |
2087 | if (!PVE::ProcFSTools::check_process_running($p)) { | |
f77faab2 | 2088 | $finish_worker->($p); |
b92400b6 DM |
2089 | } |
2090 | } | |
2091 | sleep(1); | |
2092 | } | |
2093 | } | |
2094 | while (scalar(keys %$workers)) { | |
f77faab2 | 2095 | for my $p (keys %$workers) { |
b92400b6 | 2096 | if (!PVE::ProcFSTools::check_process_running($p)) { |
f77faab2 | 2097 | $finish_worker->($p); |
b92400b6 DM |
2098 | } |
2099 | } | |
2100 | sleep(1); | |
2101 | } | |
2102 | } | |
37bec895 DM |
2103 | |
2104 | syslog('info', "all VMs and CTs stopped"); | |
2105 | ||
b92400b6 DM |
2106 | return; |
2107 | }; | |
2108 | ||
2109 | return $rpcenv->fork_worker('stopall', undef, $authuser, $code); | |
b92400b6 DM |
2110 | }}); |
2111 | ||
5f04abc2 HL |
2112 | my $create_suspend_worker = sub { |
2113 | my ($nodename, $vmid) = @_; | |
25c0052a TL |
2114 | if (!PVE::QemuServer::check_running($vmid, 1)) { |
2115 | print "VM $vmid not running, skipping suspension\n"; | |
2116 | return; | |
2117 | } | |
5f04abc2 HL |
2118 | print STDERR "Suspending VM $vmid\n"; |
2119 | return PVE::API2::Qemu->vm_suspend( | |
2120 | { node => $nodename, vmid => $vmid, todisk => 1 } | |
2121 | ); | |
2122 | }; | |
2123 | ||
2124 | __PACKAGE__->register_method ({ | |
2125 | name => 'suspendall', | |
2126 | path => 'suspendall', | |
2127 | method => 'POST', | |
2128 | protected => 1, | |
2129 | permissions => { | |
2130 | description => "The 'VM.PowerMgmt' permission is required on '/' or on '/vms/<ID>' for each" | |
2131 | ." ID passed via the 'vms' parameter. Additionally, you need 'VM.Config.Disk' on the" | |
2132 | ." '/vms/{vmid}' path and 'Datastore.AllocateSpace' for the configured state-storage(s)", | |
2133 | user => 'all', | |
2134 | }, | |
2135 | proxyto => 'node', | |
2136 | description => "Suspend all VMs.", | |
2137 | parameters => { | |
2138 | additionalProperties => 0, | |
2139 | properties => { | |
2140 | node => get_standard_option('pve-node'), | |
2141 | vms => { | |
2142 | description => "Only consider Guests with these IDs.", | |
2143 | type => 'string', format => 'pve-vmid-list', | |
2144 | optional => 1, | |
2145 | }, | |
2146 | }, | |
2147 | }, | |
2148 | returns => { | |
2149 | type => 'string', | |
2150 | }, | |
2151 | code => sub { | |
2152 | my ($param) = @_; | |
2153 | ||
2154 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2155 | my $authuser = $rpcenv->get_user(); | |
2156 | ||
2157 | # we cannot really check access to the state-storage here, that's happening per worker. | |
2158 | if (!$rpcenv->check($authuser, "/", [ 'VM.PowerMgmt', 'VM.Config.Disk' ], 1)) { | |
2159 | my @vms = PVE::Tools::split_list($param->{vms}); | |
2160 | if (scalar(@vms) > 0) { | |
2161 | $rpcenv->check($authuser, "/vms/$_", [ 'VM.PowerMgmt' ]) for @vms; | |
2162 | } else { | |
84e1e9d9 | 2163 | raise_perm_exc("/, VM.PowerMgmt && VM.Config.Disk"); |
5f04abc2 HL |
2164 | } |
2165 | } | |
2166 | ||
2167 | my $nodename = $param->{node}; | |
2168 | $nodename = PVE::INotify::nodename() if $nodename eq 'localhost'; | |
2169 | ||
2170 | my $code = sub { | |
2171 | ||
2172 | $rpcenv->{type} = 'priv'; # to start tasks in background | |
2173 | ||
84e1e9d9 | 2174 | my $toSuspendList = $get_start_stop_list->($nodename, undef, $param->{vms}); |
5f04abc2 HL |
2175 | |
2176 | my $cpuinfo = PVE::ProcFSTools::read_cpuinfo(); | |
2177 | my $datacenterconfig = cfs_read_file('datacenter.cfg'); | |
2178 | # if not set by user spawn max cpu count number of workers | |
2179 | my $maxWorkers = $datacenterconfig->{max_workers} || $cpuinfo->{cpus}; | |
2180 | ||
84e1e9d9 TL |
2181 | for my $order (sort {$b <=> $a} keys %$toSuspendList) { |
2182 | my $vmlist = $toSuspendList->{$order}; | |
5f04abc2 HL |
2183 | my $workers = {}; |
2184 | ||
2185 | my $finish_worker = sub { | |
2186 | my $pid = shift; | |
2187 | my $worker = delete $workers->{$pid} || return; | |
2188 | ||
2189 | syslog('info', "end task $worker->{upid}"); | |
2190 | }; | |
2191 | ||
2192 | for my $vmid (sort {$b <=> $a} keys %$vmlist) { | |
2193 | my $d = $vmlist->{$vmid}; | |
ebb71cb5 TL |
2194 | if ($d->{type} ne 'qemu') { |
2195 | log_warn("skipping $vmid, only VMs can be suspended"); | |
5f04abc2 HL |
2196 | next; |
2197 | } | |
2198 | my $upid = eval { | |
2199 | $create_suspend_worker->($nodename, $vmid) | |
2200 | }; | |
2201 | warn $@ if $@; | |
2202 | next if !$upid; | |
2203 | ||
2204 | my $task = PVE::Tools::upid_decode($upid, 1); | |
2205 | next if !$task; | |
2206 | ||
2207 | my $pid = $task->{pid}; | |
5f04abc2 | 2208 | $workers->{$pid} = { type => $d->{type}, upid => $upid, vmid => $vmid }; |
84e1e9d9 | 2209 | |
5f04abc2 | 2210 | while (scalar(keys %$workers) >= $maxWorkers) { |
84e1e9d9 | 2211 | for my $p (keys %$workers) { |
5f04abc2 HL |
2212 | if (!PVE::ProcFSTools::check_process_running($p)) { |
2213 | $finish_worker->($p); | |
2214 | } | |
2215 | } | |
2216 | sleep(1); | |
2217 | } | |
2218 | } | |
2219 | while (scalar(keys %$workers)) { | |
2220 | for my $p (keys %$workers) { | |
2221 | if (!PVE::ProcFSTools::check_process_running($p)) { | |
2222 | $finish_worker->($p); | |
2223 | } | |
2224 | } | |
2225 | sleep(1); | |
2226 | } | |
2227 | } | |
2228 | ||
2229 | syslog('info', "all VMs suspended"); | |
2230 | ||
2231 | return; | |
2232 | }; | |
2233 | ||
2234 | return $rpcenv->fork_worker('suspendall', undef, $authuser, $code); | |
2235 | }}); | |
2236 | ||
2237 | ||
9c8a09a7 | 2238 | my $create_migrate_worker = sub { |
fc6b77a1 | 2239 | my ($nodename, $type, $vmid, $target, $with_local_disks) = @_; |
9c8a09a7 AD |
2240 | |
2241 | my $upid; | |
2c27e4b7 DM |
2242 | if ($type eq 'lxc') { |
2243 | my $online = PVE::LXC::check_running($vmid) ? 1 : 0; | |
2244 | print STDERR "Migrating CT $vmid\n"; | |
f77faab2 TL |
2245 | $upid = PVE::API2::LXC->migrate_vm( |
2246 | { node => $nodename, vmid => $vmid, target => $target, restart => $online }); | |
2c27e4b7 | 2247 | } elsif ($type eq 'qemu') { |
0b54f653 TL |
2248 | print STDERR "Check VM $vmid: "; |
2249 | *STDERR->flush(); | |
9c8a09a7 | 2250 | my $online = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0; |
f77faab2 TL |
2251 | my $preconditions = PVE::API2::Qemu->migrate_vm_precondition( |
2252 | {node => $nodename, vmid => $vmid, target => $target}); | |
0b54f653 TL |
2253 | my $invalidConditions = ''; |
2254 | if ($online && !$with_local_disks && scalar @{$preconditions->{local_disks}}) { | |
f77faab2 TL |
2255 | $invalidConditions .= "\n Has local disks: "; |
2256 | $invalidConditions .= join(', ', map { $_->{volid} } @{$preconditions->{local_disks}}); | |
d8d17271 TM |
2257 | } |
2258 | ||
2259 | if (@{$preconditions->{local_resources}}) { | |
f77faab2 TL |
2260 | $invalidConditions .= "\n Has local resources: "; |
2261 | $invalidConditions .= join(', ', @{$preconditions->{local_resources}}); | |
d8d17271 TM |
2262 | } |
2263 | ||
0b54f653 TL |
2264 | if ($invalidConditions && $invalidConditions ne '') { |
2265 | print STDERR "skip VM $vmid - precondition check failed:"; | |
2266 | die "$invalidConditions\n"; | |
2267 | } | |
2268 | print STDERR "precondition check passed\n"; | |
9c8a09a7 | 2269 | print STDERR "Migrating VM $vmid\n"; |
71fd3de9 TL |
2270 | |
2271 | my $params = { | |
6f3d18dd TL |
2272 | node => $nodename, |
2273 | vmid => $vmid, | |
2274 | target => $target, | |
2275 | online => $online, | |
71fd3de9 | 2276 | }; |
13411f99 | 2277 | $params->{'with-local-disks'} = $with_local_disks if defined($with_local_disks); |
71fd3de9 TL |
2278 | |
2279 | $upid = PVE::API2::Qemu->migrate_vm($params); | |
9c8a09a7 AD |
2280 | } else { |
2281 | die "unknown VM type '$type'\n"; | |
2282 | } | |
2283 | ||
f77faab2 | 2284 | my $task = PVE::Tools::upid_decode($upid); |
9c8a09a7 | 2285 | |
f77faab2 | 2286 | return $task->{pid}; |
9c8a09a7 AD |
2287 | }; |
2288 | ||
2289 | __PACKAGE__->register_method ({ | |
2290 | name => 'migrateall', | |
2291 | path => 'migrateall', | |
2292 | method => 'POST', | |
2293 | proxyto => 'node', | |
2294 | protected => 1, | |
17e3b3b2 | 2295 | permissions => { |
6aec4565 FE |
2296 | description => "The 'VM.Migrate' permission is required on '/' or on '/vms/<ID>' for each " |
2297 | ."ID passed via the 'vms' parameter.", | |
2298 | user => 'all', | |
17e3b3b2 | 2299 | }, |
9c8a09a7 AD |
2300 | description => "Migrate all VMs and Containers.", |
2301 | parameters => { | |
2302 | additionalProperties => 0, | |
2303 | properties => { | |
2304 | node => get_standard_option('pve-node'), | |
58ab77d1 | 2305 | target => get_standard_option('pve-node', { description => "Target node." }), |
f77faab2 TL |
2306 | maxworkers => { |
2307 | description => "Maximal number of parallel migration job. If not set, uses" | |
2308 | ."'max_workers' from datacenter.cfg. One of both must be set!", | |
89ceb802 | 2309 | optional => 1, |
f77faab2 TL |
2310 | type => 'integer', |
2311 | minimum => 1 | |
58ab77d1 | 2312 | }, |
2a498506 DC |
2313 | vms => { |
2314 | description => "Only consider Guests with these IDs.", | |
2315 | type => 'string', format => 'pve-vmid-list', | |
2316 | optional => 1, | |
2317 | }, | |
fc6b77a1 TM |
2318 | "with-local-disks" => { |
2319 | type => 'boolean', | |
2320 | description => "Enable live storage migration for local disk", | |
2321 | optional => 1, | |
2322 | }, | |
9c8a09a7 AD |
2323 | }, |
2324 | }, | |
2325 | returns => { | |
2326 | type => 'string', | |
2327 | }, | |
2328 | code => sub { | |
2329 | my ($param) = @_; | |
2330 | ||
2331 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2332 | my $authuser = $rpcenv->get_user(); | |
2333 | ||
6aec4565 FE |
2334 | if (!$rpcenv->check($authuser, "/", [ 'VM.Migrate' ], 1)) { |
2335 | my @vms = PVE::Tools::split_list($param->{vms}); | |
2336 | if (scalar(@vms) > 0) { | |
2337 | $rpcenv->check($authuser, "/vms/$_", [ 'VM.Migrate' ]) for @vms; | |
2338 | } else { | |
2339 | raise_perm_exc("/, VM.Migrate"); | |
2340 | } | |
2341 | } | |
2342 | ||
9c8a09a7 AD |
2343 | my $nodename = $param->{node}; |
2344 | $nodename = PVE::INotify::nodename() if $nodename eq 'localhost'; | |
2345 | ||
73981e39 | 2346 | my $target = $param->{target}; |
fc6b77a1 | 2347 | my $with_local_disks = $param->{'with-local-disks'}; |
73981e39 DC |
2348 | raise_param_exc({ target => "target is local node."}) if $target eq $nodename; |
2349 | ||
2350 | PVE::Cluster::check_cfs_quorum(); | |
2351 | ||
2352 | PVE::Cluster::check_node_exists($target); | |
89ceb802 TL |
2353 | |
2354 | my $datacenterconfig = cfs_read_file('datacenter.cfg'); | |
2355 | # prefer parameter over datacenter cfg settings | |
2356 | my $maxWorkers = $param->{maxworkers} || $datacenterconfig->{max_workers} || | |
2357 | die "either 'maxworkers' parameter or max_workers in datacenter.cfg must be set!\n"; | |
9c8a09a7 AD |
2358 | |
2359 | my $code = sub { | |
9c8a09a7 AD |
2360 | $rpcenv->{type} = 'priv'; # to start tasks in background |
2361 | ||
f5c1dde5 | 2362 | my $vmlist = &$get_filtered_vmlist($nodename, $param->{vms}, 1, 1); |
49652c46 TL |
2363 | if (!scalar(keys %$vmlist)) { |
2364 | warn "no virtual guests matched, nothing to do..\n"; | |
2365 | return; | |
2366 | } | |
9c8a09a7 | 2367 | |
f5c1dde5 | 2368 | my $workers = {}; |
49652c46 | 2369 | my $workers_started = 0; |
f5c1dde5 TL |
2370 | foreach my $vmid (sort keys %$vmlist) { |
2371 | my $d = $vmlist->{$vmid}; | |
2372 | my $pid; | |
fc6b77a1 | 2373 | eval { $pid = &$create_migrate_worker($nodename, $d->{type}, $vmid, $target, $with_local_disks); }; |
f5c1dde5 TL |
2374 | warn $@ if $@; |
2375 | next if !$pid; | |
9c8a09a7 | 2376 | |
49652c46 | 2377 | $workers_started++; |
f5c1dde5 TL |
2378 | $workers->{$pid} = 1; |
2379 | while (scalar(keys %$workers) >= $maxWorkers) { | |
9c8a09a7 AD |
2380 | foreach my $p (keys %$workers) { |
2381 | if (!PVE::ProcFSTools::check_process_running($p)) { | |
2382 | delete $workers->{$p}; | |
2383 | } | |
2384 | } | |
2385 | sleep(1); | |
2386 | } | |
2387 | } | |
f5c1dde5 TL |
2388 | while (scalar(keys %$workers)) { |
2389 | foreach my $p (keys %$workers) { | |
8e4bee65 | 2390 | # FIXME: what about PID re-use ?!?! |
f5c1dde5 TL |
2391 | if (!PVE::ProcFSTools::check_process_running($p)) { |
2392 | delete $workers->{$p}; | |
2393 | } | |
2394 | } | |
2395 | sleep(1); | |
2396 | } | |
49652c46 TL |
2397 | if ($workers_started <= 0) { |
2398 | die "no migrations worker started...\n"; | |
2399 | } | |
2400 | print STDERR "All jobs finished, used $workers_started workers in total.\n"; | |
9c8a09a7 AD |
2401 | return; |
2402 | }; | |
2403 | ||
2404 | return $rpcenv->fork_worker('migrateall', undef, $authuser, $code); | |
2405 | ||
2406 | }}); | |
2407 | ||
820d0458 DC |
2408 | __PACKAGE__->register_method ({ |
2409 | name => 'get_etc_hosts', | |
2410 | path => 'hosts', | |
2411 | method => 'GET', | |
2412 | proxyto => 'node', | |
2413 | protected => 1, | |
2414 | permissions => { | |
2415 | check => ['perm', '/', [ 'Sys.Audit' ]], | |
2416 | }, | |
2417 | description => "Get the content of /etc/hosts.", | |
2418 | parameters => { | |
2419 | additionalProperties => 0, | |
2420 | properties => { | |
2421 | node => get_standard_option('pve-node'), | |
2422 | }, | |
2423 | }, | |
2424 | returns => { | |
2425 | type => 'object', | |
2426 | properties => { | |
2427 | digest => get_standard_option('pve-config-digest'), | |
2428 | data => { | |
2429 | type => 'string', | |
2430 | description => 'The content of /etc/hosts.' | |
2431 | }, | |
2432 | }, | |
2433 | }, | |
2434 | code => sub { | |
2435 | my ($param) = @_; | |
2436 | ||
2437 | return PVE::INotify::read_file('etchosts'); | |
2438 | ||
2439 | }}); | |
2440 | ||
2441 | __PACKAGE__->register_method ({ | |
2442 | name => 'write_etc_hosts', | |
2443 | path => 'hosts', | |
2444 | method => 'POST', | |
2445 | proxyto => 'node', | |
2446 | protected => 1, | |
2447 | permissions => { | |
2448 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
2449 | }, | |
2450 | description => "Write /etc/hosts.", | |
2451 | parameters => { | |
2452 | additionalProperties => 0, | |
2453 | properties => { | |
2454 | node => get_standard_option('pve-node'), | |
2455 | digest => get_standard_option('pve-config-digest'), | |
2456 | data => { | |
2457 | type => 'string', | |
2458 | description => 'The target content of /etc/hosts.' | |
2459 | }, | |
2460 | }, | |
2461 | }, | |
2462 | returns => { | |
2463 | type => 'null', | |
2464 | }, | |
2465 | code => sub { | |
2466 | my ($param) = @_; | |
2467 | ||
79d62026 | 2468 | PVE::Tools::lock_file('/var/lock/pve-etchosts.lck', undef, sub { |
820d0458 DC |
2469 | if ($param->{digest}) { |
2470 | my $hosts = PVE::INotify::read_file('etchosts'); | |
2471 | PVE::Tools::assert_if_modified($hosts->{digest}, $param->{digest}); | |
2472 | } | |
2473 | PVE::INotify::write_file('etchosts', $param->{data}); | |
2474 | }); | |
2475 | die $@ if $@; | |
2476 | ||
79d62026 | 2477 | return; |
820d0458 DC |
2478 | }}); |
2479 | ||
82282acf WL |
2480 | # bash completion helper |
2481 | ||
2482 | sub complete_templet_repo { | |
2483 | my ($cmdname, $pname, $cvalue) = @_; | |
2484 | ||
2485 | my $repo = PVE::APLInfo::load_data(); | |
2486 | my $res = []; | |
2487 | foreach my $templ (keys %{$repo->{all}}) { | |
2488 | next if $templ !~ m/^$cvalue/; | |
2489 | push @$res, $templ; | |
2490 | } | |
2491 | ||
2492 | return $res; | |
2493 | } | |
2494 | ||
aff192e6 DM |
2495 | package PVE::API2::Nodes; |
2496 | ||
2497 | use strict; | |
2498 | use warnings; | |
2499 | ||
2500 | use PVE::SafeSyslog; | |
2501 | use PVE::Cluster; | |
2502 | use PVE::RESTHandler; | |
2503 | use PVE::RPCEnvironment; | |
b193e4ac | 2504 | use PVE::API2Tools; |
f57cbe5d | 2505 | use PVE::JSONSchema qw(get_standard_option); |
aff192e6 DM |
2506 | |
2507 | use base qw(PVE::RESTHandler); | |
2508 | ||
2509 | __PACKAGE__->register_method ({ | |
f9d26e09 | 2510 | subclass => "PVE::API2::Nodes::Nodeinfo", |
aff192e6 DM |
2511 | path => '{node}', |
2512 | }); | |
2513 | ||
2514 | __PACKAGE__->register_method ({ | |
f9d26e09 TL |
2515 | name => 'index', |
2516 | path => '', | |
aff192e6 DM |
2517 | method => 'GET', |
2518 | permissions => { user => 'all' }, | |
2519 | description => "Cluster node index.", | |
2520 | parameters => { | |
6110ed03 | 2521 | additionalProperties => 0, |
aff192e6 DM |
2522 | properties => {}, |
2523 | }, | |
2524 | returns => { | |
2525 | type => 'array', | |
2526 | items => { | |
2527 | type => "object", | |
f57cbe5d DM |
2528 | properties => { |
2529 | node => get_standard_option('pve-node'), | |
2530 | status => { | |
2531 | description => "Node status.", | |
2532 | type => 'string', | |
2533 | enum => ['unknown', 'online', 'offline'], | |
2534 | }, | |
2535 | cpu => { | |
2536 | description => "CPU utilization.", | |
2537 | type => 'number', | |
2538 | optional => 1, | |
2539 | renderer => 'fraction_as_percentage', | |
2540 | }, | |
2541 | maxcpu => { | |
2542 | description => "Number of available CPUs.", | |
2543 | type => 'integer', | |
2544 | optional => 1, | |
2545 | }, | |
2546 | mem => { | |
2547 | description => "Used memory in bytes.", | |
4a512d7a | 2548 | type => 'integer', |
f57cbe5d DM |
2549 | optional => 1, |
2550 | renderer => 'bytes', | |
2551 | }, | |
2552 | maxmem => { | |
2553 | description => "Number of available memory in bytes.", | |
2554 | type => 'integer', | |
2555 | optional => 1, | |
2556 | renderer => 'bytes', | |
2557 | }, | |
2558 | level => { | |
2559 | description => "Support level.", | |
2560 | type => 'string', | |
2561 | optional => 1, | |
2562 | }, | |
2563 | uptime => { | |
2564 | description => "Node uptime in seconds.", | |
2565 | type => 'integer', | |
2566 | optional => 1, | |
2567 | renderer => 'duration', | |
2568 | }, | |
2569 | ssl_fingerprint => { | |
2570 | description => "The SSL fingerprint for the node certificate.", | |
2571 | type => 'string', | |
2572 | optional => 1, | |
2573 | }, | |
2574 | }, | |
aff192e6 | 2575 | }, |
b193e4ac | 2576 | links => [ { rel => 'child', href => "{node}" } ], |
aff192e6 DM |
2577 | }, |
2578 | code => sub { | |
2579 | my ($param) = @_; | |
f9d26e09 | 2580 | |
57d56896 TL |
2581 | my $rpcenv = PVE::RPCEnvironment::get(); |
2582 | my $authuser = $rpcenv->get_user(); | |
2583 | ||
aff192e6 DM |
2584 | my $clinfo = PVE::Cluster::get_clinfo(); |
2585 | my $res = []; | |
2586 | ||
b193e4ac DM |
2587 | my $nodelist = PVE::Cluster::get_nodelist(); |
2588 | my $members = PVE::Cluster::get_members(); | |
aff192e6 DM |
2589 | my $rrd = PVE::Cluster::rrd_dump(); |
2590 | ||
b193e4ac | 2591 | foreach my $node (@$nodelist) { |
57d56896 TL |
2592 | my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1); |
2593 | my $entry = PVE::API2Tools::extract_node_stats($node, $members, $rrd, !$can_audit); | |
1a664b0f FG |
2594 | |
2595 | $entry->{ssl_fingerprint} = eval { PVE::Cluster::get_node_fingerprint($node) }; | |
2596 | warn "$@" if $@; | |
2597 | ||
aff192e6 DM |
2598 | push @$res, $entry; |
2599 | } | |
2600 | ||
2601 | return $res; | |
2602 | }}); | |
2603 | ||
2604 | 1; |