]> git.proxmox.com Git - pve-cluster.git/blame - data/PVE/Cluster.pm
Cluster: fix typo
[pve-cluster.git] / data / PVE / Cluster.pm
CommitLineData
fe000966
DM
1package PVE::Cluster;
2
3use strict;
7181f622 4use warnings;
57d8355e 5
57d8355e 6use Encode;
fe000966 7use File::stat qw();
462c16b7 8use File::Path qw(make_path);
57d8355e 9use JSON;
26784563 10use Net::SSLeay;
c5204e14 11use POSIX qw(ENOENT);
57d8355e
TL
12use Socket;
13use Storable qw(dclone);
57d8355e 14
c92b7716 15use PVE::Certificate;
fe000966
DM
16use PVE::INotify;
17use PVE::IPCC;
d0ad18e8 18use PVE::JSONSchema;
54d487bf 19use PVE::Network;
57d8355e
TL
20use PVE::SafeSyslog;
21use PVE::Tools qw(run_command);
22
95c44445 23use PVE::Cluster::IPCConst;
57d8355e 24
fe000966
DM
25use base 'Exporter';
26
27our @EXPORT_OK = qw(
28cfs_read_file
29cfs_write_file
30cfs_register_file
31cfs_lock_file);
32
fe000966
DM
33# x509 certificate utils
34
35my $basedir = "/etc/pve";
36my $authdir = "$basedir/priv";
37my $lockdir = "/etc/pve/priv/lock";
38
68491de3 39# cfs and corosync files
e02bdaae
TL
40my $dbfile = "/var/lib/pve-cluster/config.db";
41my $dbbackupdir = "/var/lib/pve-cluster/backup";
fe000966 42
34180e03
TL
43# this is just a readonly copy, the relevant one is in status.c from pmxcfs
44# observed files are the one we can get directly through IPCC, they are cached
45# using a computed version and only those can be used by the cfs_*_file methods
fe000966 46my $observed = {
e1735a61 47 'vzdump.cron' => 1,
1fdf150e 48 'jobs.cfg' => 1,
fe000966
DM
49 'storage.cfg' => 1,
50 'datacenter.cfg' => 1,
f6de131a 51 'replication.cfg' => 1,
cafc7309
DM
52 'corosync.conf' => 1,
53 'corosync.conf.new' => 1,
fe000966
DM
54 'user.cfg' => 1,
55 'domains.cfg' => 1,
56 'priv/shadow.cfg' => 1,
454c3a2c 57 'priv/tfa.cfg' => 1,
7dac5e0e 58 'priv/token.cfg' => 1,
8b84a18a 59 'priv/acme/plugins.cfg' => 1,
fc28c2f8 60 'priv/ipam.db' => 1,
fe000966 61 '/qemu-server/' => 1,
f71eee41 62 '/openvz/' => 1,
7f66b436 63 '/lxc/' => 1,
5a5417e6
DM
64 'ha/crm_commands' => 1,
65 'ha/manager_status' => 1,
66 'ha/resources.cfg' => 1,
67 'ha/groups.cfg' => 1,
e9af3eb7 68 'ha/fence.cfg' => 1,
9d4f69ff 69 'status.cfg' => 1,
22e2ed76 70 'ceph.conf' => 1,
1bb7107a 71 'sdn/vnets.cfg' => 1,
1bb7107a 72 'sdn/zones.cfg' => 1,
1bb7107a 73 'sdn/controllers.cfg' => 1,
c20823f8 74 'sdn/subnets.cfg' => 1,
be1aa34b 75 'sdn/ipams.cfg' => 1,
9d3ea5ef 76 'sdn/dns.cfg' => 1,
a3d44df8 77 'sdn/.running-config' => 1,
9bb0fbe9 78 'virtual-guest/cpu-models.conf' => 1,
fe000966
DM
79};
80
462c16b7
TL
81sub prepare_observed_file_basedirs {
82
393742f6 83 if (!check_cfs_is_mounted(1)) {
462c16b7
TL
84 warn "pmxcfs isn't mounted (/etc/pve), chickening out..\n";
85 return;
86 }
87
88 for my $f (sort keys %$observed) {
89 next if $f !~ m!^(.*)/[^/]+$!;
90 my $dir = "$basedir/$1";
91 next if -e $dir; # can also be a link, so just use -e xist check
92 print "creating directory '$dir' for observerd files\n";
93 make_path($dir);
94 }
95}
96
c5204e14
FG
97sub base_dir {
98 return $basedir;
99}
fe000966 100
c5204e14
FG
101sub auth_dir {
102 return $authdir;
fe000966
DM
103}
104
105sub check_cfs_quorum {
01dddfb9
DM
106 my ($noerr) = @_;
107
fe000966
DM
108 # note: -w filename always return 1 for root, so wee need
109 # to use File::lstat here
110 my $st = File::stat::lstat("$basedir/local");
01dddfb9
DM
111 my $quorate = ($st && (($st->mode & 0200) != 0));
112
113 die "cluster not ready - no quorum?\n" if !$quorate && !$noerr;
114
115 return $quorate;
fe000966
DM
116}
117
118sub check_cfs_is_mounted {
119 my ($noerr) = @_;
120
121 my $res = -l "$basedir/local";
122
123 die "pve configuration filesystem not mounted\n"
124 if !$res && !$noerr;
125
126 return $res;
127}
128
fe000966
DM
129my $versions = {};
130my $vmlist = {};
131my $clinfo = {};
132
133my $ipcc_send_rec = sub {
134 my ($msgid, $data) = @_;
135
136 my $res = PVE::IPCC::ipcc_send_rec($msgid, $data);
137
dd856b4d 138 die "ipcc_send_rec[$msgid] failed: $!\n" if !defined($res) && ($! != 0);
fe000966
DM
139
140 return $res;
141};
142
143my $ipcc_send_rec_json = sub {
144 my ($msgid, $data) = @_;
145
146 my $res = PVE::IPCC::ipcc_send_rec($msgid, $data);
147
dd856b4d 148 die "ipcc_send_rec[$msgid] failed: $!\n" if !defined($res) && ($! != 0);
fe000966
DM
149
150 return decode_json($res);
151};
152
153my $ipcc_get_config = sub {
154 my ($path) = @_;
155
156 my $bindata = pack "Z*", $path;
95c44445 157 my $res = PVE::IPCC::ipcc_send_rec(CFS_IPC_GET_CONFIG, $bindata);
2db32d95 158 if (!defined($res)) {
7bac9ca5
WB
159 if ($! != 0) {
160 return undef if $! == ENOENT;
161 die "$!\n";
162 }
2db32d95
DM
163 return '';
164 }
165
166 return $res;
fe000966
DM
167};
168
169my $ipcc_get_status = sub {
170 my ($name, $nodename) = @_;
171
172 my $bindata = pack "Z[256]Z[256]", $name, ($nodename || "");
95c44445 173 return PVE::IPCC::ipcc_send_rec(CFS_IPC_GET_STATUS, $bindata);
fe000966
DM
174};
175
71cc17bc
DC
176my $ipcc_remove_status = sub {
177 my ($name) = @_;
0adf0c5a
TL
178 # we just omit the data payload, pmxcfs takes this as hint and removes this
179 # key from the status hashtable
71cc17bc
DC
180 my $bindata = pack "Z[256]", $name;
181 return &$ipcc_send_rec(CFS_IPC_SET_STATUS, $bindata);
182};
183
fe000966
DM
184my $ipcc_update_status = sub {
185 my ($name, $data) = @_;
186
187 my $raw = ref($data) ? encode_json($data) : $data;
188 # update status
189 my $bindata = pack "Z[256]Z*", $name, $raw;
190
95c44445 191 return &$ipcc_send_rec(CFS_IPC_SET_STATUS, $bindata);
fe000966
DM
192};
193
194my $ipcc_log = sub {
195 my ($priority, $ident, $tag, $msg) = @_;
196
197 my $bindata = pack "CCCZ*Z*Z*", $priority, bytes::length($ident) + 1,
198 bytes::length($tag) + 1, $ident, $tag, $msg;
199
95c44445 200 return &$ipcc_send_rec(CFS_IPC_LOG_CLUSTER_MSG, $bindata);
fe000966
DM
201};
202
203my $ipcc_get_cluster_log = sub {
204 my ($user, $max) = @_;
205
206 $max = 0 if !defined($max);
207
208 my $bindata = pack "VVVVZ*", $max, 0, 0, 0, ($user || "");
95c44445 209 return &$ipcc_send_rec(CFS_IPC_GET_CLUSTER_LOG, $bindata);
fe000966
DM
210};
211
7dac5e0e
FG
212my $ipcc_verify_token = sub {
213 my ($full_token) = @_;
214
215 my $bindata = pack "Z*", $full_token;
216 my $res = PVE::IPCC::ipcc_send_rec(CFS_IPC_VERIFY_TOKEN, $bindata);
217
218 return 1 if $! == 0;
219 return 0 if $! == ENOENT;
220
221 die "$!\n";
222};
223
fe000966
DM
224my $ccache = {};
225
226sub cfs_update {
686801e4 227 my ($fail) = @_;
fe000966 228 eval {
95c44445 229 my $res = &$ipcc_send_rec_json(CFS_IPC_GET_FS_VERSION);
fe000966
DM
230 die "no starttime\n" if !$res->{starttime};
231
232 if (!$res->{starttime} || !$versions->{starttime} ||
233 $res->{starttime} != $versions->{starttime}) {
234 #print "detected changed starttime\n";
235 $vmlist = {};
236 $clinfo = {};
237 $ccache = {};
238 }
239
240 $versions = $res;
241 };
242 my $err = $@;
243 if ($err) {
244 $versions = {};
245 $vmlist = {};
246 $clinfo = {};
247 $ccache = {};
686801e4 248 die $err if $fail;
fe000966
DM
249 warn $err;
250 }
251
252 eval {
253 if (!$clinfo->{version} || $clinfo->{version} != $versions->{clinfo}) {
254 #warn "detected new clinfo\n";
95c44445 255 $clinfo = &$ipcc_send_rec_json(CFS_IPC_GET_CLUSTER_INFO);
fe000966
DM
256 }
257 };
258 $err = $@;
259 if ($err) {
260 $clinfo = {};
686801e4 261 die $err if $fail;
fe000966
DM
262 warn $err;
263 }
264
265 eval {
266 if (!$vmlist->{version} || $vmlist->{version} != $versions->{vmlist}) {
267 #warn "detected new vmlist1\n";
95c44445 268 $vmlist = &$ipcc_send_rec_json(CFS_IPC_GET_GUEST_LIST);
fe000966
DM
269 }
270 };
271 $err = $@;
272 if ($err) {
273 $vmlist = {};
686801e4 274 die $err if $fail;
fe000966
DM
275 warn $err;
276 }
277}
278
279sub get_vmlist {
280 return $vmlist;
281}
282
283sub get_clinfo {
284 return $clinfo;
285}
286
9ddd4ae9
DM
287sub get_members {
288 return $clinfo->{nodelist};
289}
290
fe000966 291sub get_nodelist {
fe000966
DM
292 my $nodelist = $clinfo->{nodelist};
293
fe000966
DM
294 my $nodename = PVE::INotify::nodename();
295
296 if (!$nodelist || !$nodelist->{$nodename}) {
297 return [ $nodename ];
298 }
299
300 return [ keys %$nodelist ];
301}
302
0adf0c5a
TL
303# only stored in a in-memory hashtable inside pmxcfs, local data is gone after
304# a restart (of pmxcfs or the node), peer data is still available then
305# best used for status data, like running (ceph) services, package versions, ...
a26b18e1
DC
306sub broadcast_node_kv {
307 my ($key, $data) = @_;
308
309 if (!defined($data)) {
4d04cad0 310 eval { $ipcc_remove_status->("kv/$key") };
a26b18e1
DC
311 } else {
312 die "cannot send a reference\n" if ref($data);
313 my $size = length($data);
0adf0c5a 314 die "data for '$key' too big\n" if $size >= (32 * 1024); # limit from pmxfs
a26b18e1 315
4d04cad0 316 eval { $ipcc_update_status->("kv/$key", $data) };
a26b18e1 317 }
a26b18e1
DC
318 warn $@ if $@;
319}
320
0adf0c5a 321# nodename is optional
a26b18e1
DC
322sub get_node_kv {
323 my ($key, $nodename) = @_;
324
325 my $res = {};
326 my $get_node_data = sub {
327 my ($node) = @_;
328 my $raw = $ipcc_get_status->("kv/$key", $node);
14000cd7 329 $res->{$node} = unpack("Z*", $raw) if $raw;
a26b18e1
DC
330 };
331
332 if ($nodename) {
333 $get_node_data->($nodename);
334 } else {
4d04cad0 335 for my $node (get_nodelist()->@*) {
a26b18e1
DC
336 $get_node_data->($node);
337 }
338 }
339
340 return $res;
341}
342
cf1b19d9
TL
343# property: a config property you want to get, e.g., this is perfect to get
344# the 'lock' entry of a guest _fast_ (>100 faster than manual parsing here)
8a93eeae 345# vmid: optional, if a valid is passed we only check that one, else return all
cf1b19d9
TL
346# NOTE: does *not* searches snapshot and PENDING entries sections!
347sub get_guest_config_property {
348 my ($property, $vmid) = @_;
349
350 die "property is required" if !defined($property);
351
352 my $bindata = pack "VZ*", $vmid // 0, $property;
353 my $res = $ipcc_send_rec_json->(CFS_IPC_GET_GUEST_CONFIG_PROPERTY, $bindata);
354
355 return $res;
356}
357
1b36b6b1 358# $data must be a chronological descending ordered array of tasks
fe000966
DM
359sub broadcast_tasklist {
360 my ($data) = @_;
361
1b36b6b1
TL
362 # the serialized list may not get bigger than 32kb (CFS_MAX_STATUS_SIZE
363 # from pmxcfs) - drop older items until we satisfy this constraint
364 my $size = length(encode_json($data));
365 while ($size >= (32 * 1024)) {
366 pop @$data;
367 $size = length(encode_json($data));
368 }
369
fe000966
DM
370 eval {
371 &$ipcc_update_status("tasklist", $data);
372 };
373
374 warn $@ if $@;
375}
376
377my $tasklistcache = {};
378
379sub get_tasklist {
380 my ($nodename) = @_;
381
382 my $kvstore = $versions->{kvstore} || {};
383
384 my $nodelist = get_nodelist();
385
386 my $res = [];
387 foreach my $node (@$nodelist) {
388 next if $nodename && ($nodename ne $node);
389 eval {
390 my $ver = $kvstore->{$node}->{tasklist} if $kvstore->{$node};
1f1c5c43
TL
391 my $cache = $tasklistcache->{$node};
392 if (!$cache || !$ver || !$cache->{version} || ($cache->{version} != $ver)) {
393 my $tasks = [];
394 if (my $raw = $ipcc_get_status->("tasklist", $node)) {
aea0f5a6
TL
395 my $json_str = unpack("Z*", $raw);
396 $tasks = decode_json($json_str);
1f1c5c43
TL
397 }
398 push @$res, @$tasks;
399 $tasklistcache->{$node} = {
400 data => $tasks,
fe000966
DM
401 version => $ver,
402 };
1f1c5c43
TL
403 } elsif ($cache && $cache->{data}) {
404 push @$res, $cache->{data}->@*;
fe000966
DM
405 }
406 };
407 my $err = $@;
408 syslog('err', $err) if $err;
409 }
410
411 return $res;
412}
413
414sub broadcast_rrd {
415 my ($rrdid, $data) = @_;
416
417 eval {
418 &$ipcc_update_status("rrd/$rrdid", $data);
419 };
420 my $err = $@;
421
422 warn $err if $err;
423}
424
425my $last_rrd_dump = 0;
426my $last_rrd_data = "";
427
428sub rrd_dump {
429
430 my $ctime = time();
431
432 my $diff = $ctime - $last_rrd_dump;
433 if ($diff < 2) {
434 return $last_rrd_data;
435 }
436
437 my $raw;
438 eval {
95c44445 439 $raw = &$ipcc_send_rec(CFS_IPC_GET_RRD_DUMP);
fe000966
DM
440 };
441 my $err = $@;
442
443 if ($err) {
444 warn $err;
445 return {};
446 }
447
448 my $res = {};
449
c3fabca7
DM
450 if ($raw) {
451 while ($raw =~ s/^(.*)\n//) {
452 my ($key, @ela) = split(/:/, $1);
453 next if !$key;
454 next if !(scalar(@ela) > 1);
d2aae33e 455 $res->{$key} = [ map { $_ eq 'U' ? undef : $_ } @ela ];
c3fabca7 456 }
fe000966
DM
457 }
458
459 $last_rrd_dump = $ctime;
460 $last_rrd_data = $res;
461
462 return $res;
463}
464
fe000966
DM
465
466# a fast way to read files (avoid fuse overhead)
467sub get_config {
468 my ($path) = @_;
469
d3a92ba7 470 return &$ipcc_get_config($path);
fe000966
DM
471}
472
473sub get_cluster_log {
474 my ($user, $max) = @_;
475
476 return &$ipcc_get_cluster_log($user, $max);
477}
478
7dac5e0e
FG
479sub verify_token {
480 my ($userid, $token) = @_;
481
482 return &$ipcc_verify_token("$userid $token");
483}
484
fe000966
DM
485my $file_info = {};
486
487sub cfs_register_file {
488 my ($filename, $parser, $writer) = @_;
489
490 $observed->{$filename} || die "unknown file '$filename'";
491
492 die "file '$filename' already registered" if $file_info->{$filename};
493
494 $file_info->{$filename} = {
495 parser => $parser,
496 writer => $writer,
497 };
498}
499
500my $ccache_read = sub {
501 my ($filename, $parser, $version) = @_;
502
503 $ccache->{$filename} = {} if !$ccache->{$filename};
504
505 my $ci = $ccache->{$filename};
506
d3a92ba7 507 if (!$ci->{version} || !$version || $ci->{version} != $version) {
a79c7743 508 # we always call the parser, even when the file does not exist
d3a92ba7 509 # (in that case $data is undef)
fe000966 510 my $data = get_config($filename);
fe000966
DM
511 $ci->{data} = &$parser("/etc/pve/$filename", $data);
512 $ci->{version} = $version;
513 }
514
515 my $res = ref($ci->{data}) ? dclone($ci->{data}) : $ci->{data};
516
517 return $res;
518};
519
520sub cfs_file_version {
521 my ($filename) = @_;
522
523 my $version;
524 my $infotag;
6e73d5c2 525 if ($filename =~ m!^nodes/[^/]+/(openvz|lxc|qemu-server)/(\d+)\.conf$!) {
f71eee41 526 my ($type, $vmid) = ($1, $2);
fe000966
DM
527 if ($vmlist && $vmlist->{ids} && $vmlist->{ids}->{$vmid}) {
528 $version = $vmlist->{ids}->{$vmid}->{version};
529 }
f71eee41 530 $infotag = "/$type/";
fe000966
DM
531 } else {
532 $infotag = $filename;
533 $version = $versions->{$filename};
534 }
535
536 my $info = $file_info->{$infotag} ||
537 die "unknown file type '$filename'\n";
538
539 return wantarray ? ($version, $info) : $version;
540}
541
542sub cfs_read_file {
543 my ($filename) = @_;
544
c53b111f 545 my ($version, $info) = cfs_file_version($filename);
fe000966
DM
546 my $parser = $info->{parser};
547
548 return &$ccache_read($filename, $parser, $version);
549}
550
551sub cfs_write_file {
552 my ($filename, $data) = @_;
553
c53b111f 554 my ($version, $info) = cfs_file_version($filename);
fe000966
DM
555
556 my $writer = $info->{writer} || die "no writer defined";
557
558 my $fsname = "/etc/pve/$filename";
559
560 my $raw = &$writer($fsname, $data);
561
562 if (my $ci = $ccache->{$filename}) {
563 $ci->{version} = undef;
564 }
565
566 PVE::Tools::file_set_contents($fsname, $raw);
567}
568
569my $cfs_lock = sub {
570 my ($lockid, $timeout, $code, @param) = @_;
571
00ea8428
TL
572 my $prev_alarm = alarm(0); # suspend outer alarm early
573
fe000966 574 my $res;
bcdb1b3a 575 my $got_lock = 0;
fe000966 576
e8c6be91 577 # this timeout is for acquire the lock
fe000966
DM
578 $timeout = 10 if !$timeout;
579
580 my $filename = "$lockdir/$lockid";
581
e085fe6f 582 my $is_code_err = 0;
fe000966
DM
583 eval {
584
585 mkdir $lockdir;
586
587 if (! -d $lockdir) {
6e13e20a 588 die "pve cluster filesystem not online.\n";
fe000966
DM
589 }
590
6e13e20a 591 my $timeout_err = sub { die "got lock request timeout\n"; };
bcdb1b3a 592 local $SIG{ALRM} = $timeout_err;
fe000966 593
bcdb1b3a
TL
594 while (1) {
595 alarm ($timeout);
596 $got_lock = mkdir($filename);
3fb23b5b 597 $timeout = alarm(0) - 1; # we'll sleep for 1s, see down below
bcdb1b3a
TL
598
599 last if $got_lock;
600
3fb23b5b 601 $timeout_err->() if $timeout <= 0;
fe000966 602
e8c6be91 603 print STDERR "trying to acquire cfs lock '$lockid' ...\n";
bcdb1b3a
TL
604 utime (0, 0, $filename); # cfs unlock request
605 sleep(1);
fe000966
DM
606 }
607
608 # fixed command timeout: cfs locks have a timeout of 120
609 # using 60 gives us another 60 seconds to abort the task
e085fe6f 610 local $SIG{ALRM} = sub { die "'$lockid'-locked command timed out - aborting\n"; };
00ea8428 611 alarm(60);
fe000966 612
9c206b2b
DM
613 cfs_update(); # make sure we read latest versions inside code()
614
e085fe6f
TL
615 $is_code_err = 1; # allows to differ between locking and actual-work errors
616
fe000966
DM
617 $res = &$code(@param);
618
619 alarm(0);
620 };
621
622 my $err = $@;
623
6e13e20a 624 $err = "no quorum!\n" if !$got_lock && !check_cfs_quorum(1);
fe000966 625
96857ee4 626 rmdir $filename if $got_lock; # if we held the lock always unlock again
fe000966 627
00ea8428
TL
628 alarm($prev_alarm);
629
fe000966 630 if ($err) {
e085fe6f 631 if (ref($err) eq 'PVE::Exception' || $is_code_err) {
c09b0af5
FG
632 # re-raise defined exceptions
633 $@ = $err;
634 } else {
e085fe6f
TL
635 # add lock info for plain errors comming from the locking itself
636 $@ = "cfs-lock '$lockid' error: $err";
c09b0af5 637 }
fe000966
DM
638 return undef;
639 }
640
641 $@ = undef;
642
643 return $res;
644};
645
646sub cfs_lock_file {
647 my ($filename, $timeout, $code, @param) = @_;
648
649 my $info = $observed->{$filename} || die "unknown file '$filename'";
650
651 my $lockid = "file-$filename";
652 $lockid =~ s/[.\/]/_/g;
653
654 &$cfs_lock($lockid, $timeout, $code, @param);
655}
656
657sub cfs_lock_storage {
658 my ($storeid, $timeout, $code, @param) = @_;
659
660 my $lockid = "storage-$storeid";
661
662 &$cfs_lock($lockid, $timeout, $code, @param);
663}
664
78897707
TL
665sub cfs_lock_domain {
666 my ($domainname, $timeout, $code, @param) = @_;
667
668 my $lockid = "domain-$domainname";
669
670 &$cfs_lock($lockid, $timeout, $code, @param);
671}
672
4790f9f4
FG
673sub cfs_lock_acme {
674 my ($account, $timeout, $code, @param) = @_;
675
676 my $lockid = "acme-$account";
677
678 &$cfs_lock($lockid, $timeout, $code, @param);
679}
680
900b50d9
FG
681sub cfs_lock_authkey {
682 my ($timeout, $code, @param) = @_;
683
684 $cfs_lock->('authkey', $timeout, $code, @param);
31c78985
FG
685}
686
687sub cfs_lock_firewall {
688 my ($scope, $timeout, $code, @param) = @_;
689
690 my $lockid = "firewall-$scope";
691
692 $cfs_lock->($lockid, $timeout, $code, @param);
900b50d9
FG
693}
694
fe000966
DM
695my $log_levels = {
696 "emerg" => 0,
697 "alert" => 1,
698 "crit" => 2,
699 "critical" => 2,
700 "err" => 3,
701 "error" => 3,
702 "warn" => 4,
703 "warning" => 4,
704 "notice" => 5,
705 "info" => 6,
706 "debug" => 7,
707};
708
709sub log_msg {
710 my ($priority, $ident, $msg) = @_;
711
712 if (my $tmp = $log_levels->{$priority}) {
713 $priority = $tmp;
714 }
715
716 die "need numeric log priority" if $priority !~ /^\d+$/;
717
718 my $tag = PVE::SafeSyslog::tag();
719
720 $msg = "empty message" if !$msg;
721
722 $ident = "" if !$ident;
8f2d54ff 723 $ident = encode("ascii", $ident,
fe000966
DM
724 sub { sprintf "\\u%04x", shift });
725
8f2d54ff 726 my $ascii = encode("ascii", $msg, sub { sprintf "\\u%04x", shift });
fe000966
DM
727
728 if ($ident) {
729 syslog($priority, "<%s> %s", $ident, $ascii);
730 } else {
731 syslog($priority, "%s", $ascii);
732 }
733
734 eval { &$ipcc_log($priority, $ident, $tag, $ascii); };
735
736 syslog("err", "writing cluster log failed: $@") if $@;
737}
738
9d76a1bb
DM
739sub check_vmid_unused {
740 my ($vmid, $noerr) = @_;
c53b111f 741
9d76a1bb
DM
742 my $vmlist = get_vmlist();
743
744 my $d = $vmlist->{ids}->{$vmid};
745 return 1 if !defined($d);
c53b111f 746
9d76a1bb
DM
747 return undef if $noerr;
748
4f66b109 749 my $vmtypestr = $d->{type} eq 'qemu' ? 'VM' : 'CT';
e75ccbee 750 die "$vmtypestr $vmid already exists on node '$d->{node}'\n";
9d76a1bb
DM
751}
752
65ff467f
DM
753sub check_node_exists {
754 my ($nodename, $noerr) = @_;
755
756 my $nodelist = $clinfo->{nodelist};
757 return 1 if $nodelist && $nodelist->{$nodename};
758
759 return undef if $noerr;
760
761 die "no such cluster node '$nodename'\n";
762}
763
fe000966
DM
764# this is also used to get the IP of the local node
765sub remote_node_ip {
766 my ($nodename, $noerr) = @_;
767
768 my $nodelist = $clinfo->{nodelist};
769 if ($nodelist && $nodelist->{$nodename}) {
770 if (my $ip = $nodelist->{$nodename}->{ip}) {
fc31f517
WB
771 return $ip if !wantarray;
772 my $family = $nodelist->{$nodename}->{address_family};
773 if (!$family) {
774 $nodelist->{$nodename}->{address_family} =
775 $family =
776 PVE::Tools::get_host_address_family($ip);
777 }
14830160 778 return wantarray ? ($ip, $family) : $ip;
fe000966
DM
779 }
780 }
781
782 # fallback: try to get IP by other means
e064c9b0 783 return PVE::Network::get_ip_from_hostname($nodename, $noerr);
fe000966
DM
784}
785
1904862a
TL
786sub get_node_fingerprint {
787 my ($node) = @_;
788
789 my $cert_path = "/etc/pve/nodes/$node/pve-ssl.pem";
790 my $custom_cert_path = "/etc/pve/nodes/$node/pveproxy-ssl.pem";
791
792 $cert_path = $custom_cert_path if -f $custom_cert_path;
793
c92b7716 794 return PVE::Certificate::get_certificate_fingerprint($cert_path);
1904862a
TL
795}
796
15df58e6
DM
797# bash completion helpers
798
799sub complete_next_vmid {
800
801 my $vmlist = get_vmlist() || {};
802 my $idlist = $vmlist->{ids} || {};
803
804 for (my $i = 100; $i < 10000; $i++) {
805 return [$i] if !defined($idlist->{$i});
806 }
807
808 return [];
809}
810
87515b25
DM
811sub complete_vmid {
812
813 my $vmlist = get_vmlist();
814 my $ids = $vmlist->{ids} || {};
815
816 return [ keys %$ids ];
817}
818
15df58e6
DM
819sub complete_local_vmid {
820
821 my $vmlist = get_vmlist();
822 my $ids = $vmlist->{ids} || {};
823
824 my $nodename = PVE::INotify::nodename();
825
826 my $res = [];
827 foreach my $vmid (keys %$ids) {
828 my $d = $ids->{$vmid};
829 next if !$d->{node} || $d->{node} ne $nodename;
830 push @$res, $vmid;
831 }
832
833 return $res;
834}
835
4dd189df
DM
836sub complete_migration_target {
837
838 my $res = [];
839
840 my $nodename = PVE::INotify::nodename();
841
842 my $nodelist = get_nodelist();
843 foreach my $node (@$nodelist) {
844 next if $node eq $nodename;
845 push @$res, $node;
846 }
847
848 return $res;
849}
850
1f1aef8b 851
a9965358 852# NOTE: filesystem must be offline here, no DB changes allowed
c5204e14 853sub cfs_backup_database {
e02bdaae
TL
854 mkdir $dbbackupdir;
855
e02bdaae 856 my $ctime = time();
a9965358 857 my $backup_fn = "$dbbackupdir/config-$ctime.sql.gz";
e02bdaae 858
a9965358 859 print "backup old database to '$backup_fn'\n";
e02bdaae 860
a9965358
TL
861 my $cmd = [ ['sqlite3', $dbfile, '.dump'], ['gzip', '-', \ ">${backup_fn}"] ];
862 run_command($cmd, 'errmsg' => "cannot backup old database\n");
e02bdaae 863
a9965358
TL
864 my $maxfiles = 10; # purge older backup
865 my $backups = [ sort { $b cmp $a } <$dbbackupdir/config-*.sql.gz> ];
866
867 if ((my $count = scalar(@$backups)) > $maxfiles) {
868 foreach my $f (@$backups[$maxfiles..$count-1]) {
ee0daa88 869 next if $f !~ m/^(\S+)$/; # untaint
a9965358
TL
870 print "delete old backup '$1'\n";
871 unlink $1;
872 }
e02bdaae 873 }
8f64504c 874
c5204e14 875 return $dbfile;
8f64504c 876}
e02bdaae 877
ac68281b 8781;