]>
Commit | Line | Data |
---|---|---|
fe000966 DM |
1 | package PVE::Cluster; |
2 | ||
3 | use strict; | |
7181f622 | 4 | use warnings; |
62613060 | 5 | use POSIX qw(EEXIST); |
fe000966 DM |
6 | use File::stat qw(); |
7 | use Socket; | |
8 | use Storable qw(dclone); | |
9 | use IO::File; | |
10 | use MIME::Base64; | |
ec48ec22 | 11 | use XML::Parser; |
440121dc | 12 | use Digest::SHA; |
fe000966 DM |
13 | use Digest::HMAC_SHA1; |
14 | use PVE::Tools; | |
15 | use PVE::INotify; | |
16 | use PVE::IPCC; | |
17 | use PVE::SafeSyslog; | |
d0ad18e8 | 18 | use PVE::JSONSchema; |
fe000966 DM |
19 | use JSON; |
20 | use RRDs; | |
21 | use Encode; | |
22 | use base 'Exporter'; | |
23 | ||
24 | our @EXPORT_OK = qw( | |
25 | cfs_read_file | |
26 | cfs_write_file | |
27 | cfs_register_file | |
28 | cfs_lock_file); | |
29 | ||
30 | use Data::Dumper; # fixme: remove | |
31 | ||
32 | # x509 certificate utils | |
33 | ||
34 | my $basedir = "/etc/pve"; | |
35 | my $authdir = "$basedir/priv"; | |
36 | my $lockdir = "/etc/pve/priv/lock"; | |
37 | ||
38 | my $authprivkeyfn = "$authdir/authkey.key"; | |
39 | my $authpubkeyfn = "$basedir/authkey.pub"; | |
40 | my $pveca_key_fn = "$authdir/pve-root-ca.key"; | |
41 | my $pveca_srl_fn = "$authdir/pve-root-ca.srl"; | |
42 | my $pveca_cert_fn = "$basedir/pve-root-ca.pem"; | |
43 | # this is just a secret accessable by the web browser | |
44 | # and is used for CSRF prevention | |
45 | my $pvewww_key_fn = "$basedir/pve-www.key"; | |
46 | ||
47 | # ssh related files | |
48 | my $ssh_rsa_id_priv = "/root/.ssh/id_rsa"; | |
49 | my $ssh_rsa_id = "/root/.ssh/id_rsa.pub"; | |
50 | my $ssh_host_rsa_id = "/etc/ssh/ssh_host_rsa_key.pub"; | |
51 | my $sshglobalknownhosts = "/etc/ssh/ssh_known_hosts"; | |
52 | my $sshknownhosts = "/etc/pve/priv/known_hosts"; | |
53 | my $sshauthkeys = "/etc/pve/priv/authorized_keys"; | |
54 | my $rootsshauthkeys = "/root/.ssh/authorized_keys"; | |
6056578e | 55 | my $rootsshauthkeysbackup = "${rootsshauthkeys}.org"; |
f666cdde | 56 | my $rootsshconfig = "/root/.ssh/config"; |
fe000966 DM |
57 | |
58 | my $observed = { | |
e1735a61 | 59 | 'vzdump.cron' => 1, |
fe000966 DM |
60 | 'storage.cfg' => 1, |
61 | 'datacenter.cfg' => 1, | |
cafc7309 DM |
62 | 'corosync.conf' => 1, |
63 | 'corosync.conf.new' => 1, | |
fe000966 DM |
64 | 'user.cfg' => 1, |
65 | 'domains.cfg' => 1, | |
66 | 'priv/shadow.cfg' => 1, | |
67 | '/qemu-server/' => 1, | |
f71eee41 | 68 | '/openvz/' => 1, |
5a5417e6 DM |
69 | 'ha/crm_commands' => 1, |
70 | 'ha/manager_status' => 1, | |
71 | 'ha/resources.cfg' => 1, | |
72 | 'ha/groups.cfg' => 1, | |
fe000966 DM |
73 | }; |
74 | ||
75 | # only write output if something fails | |
76 | sub run_silent_cmd { | |
77 | my ($cmd) = @_; | |
78 | ||
79 | my $outbuf = ''; | |
80 | ||
81 | my $record_output = sub { | |
82 | $outbuf .= shift; | |
83 | $outbuf .= "\n"; | |
84 | }; | |
85 | ||
86 | eval { | |
87 | PVE::Tools::run_command($cmd, outfunc => $record_output, | |
88 | errfunc => $record_output); | |
89 | }; | |
90 | ||
91 | my $err = $@; | |
92 | ||
93 | if ($err) { | |
94 | print STDERR $outbuf; | |
95 | die $err; | |
96 | } | |
97 | } | |
98 | ||
99 | sub check_cfs_quorum { | |
01dddfb9 DM |
100 | my ($noerr) = @_; |
101 | ||
fe000966 DM |
102 | # note: -w filename always return 1 for root, so wee need |
103 | # to use File::lstat here | |
104 | my $st = File::stat::lstat("$basedir/local"); | |
01dddfb9 DM |
105 | my $quorate = ($st && (($st->mode & 0200) != 0)); |
106 | ||
107 | die "cluster not ready - no quorum?\n" if !$quorate && !$noerr; | |
108 | ||
109 | return $quorate; | |
fe000966 DM |
110 | } |
111 | ||
112 | sub check_cfs_is_mounted { | |
113 | my ($noerr) = @_; | |
114 | ||
115 | my $res = -l "$basedir/local"; | |
116 | ||
117 | die "pve configuration filesystem not mounted\n" | |
118 | if !$res && !$noerr; | |
119 | ||
120 | return $res; | |
121 | } | |
122 | ||
123 | sub gen_local_dirs { | |
124 | my ($nodename) = @_; | |
125 | ||
126 | check_cfs_is_mounted(); | |
127 | ||
128 | my @required_dirs = ( | |
129 | "$basedir/priv", | |
130 | "$basedir/nodes", | |
131 | "$basedir/nodes/$nodename", | |
a1c08cfa DM |
132 | "$basedir/nodes/$nodename/qemu-server", |
133 | "$basedir/nodes/$nodename/openvz", | |
fe000966 DM |
134 | "$basedir/nodes/$nodename/priv"); |
135 | ||
136 | foreach my $dir (@required_dirs) { | |
137 | if (! -d $dir) { | |
62613060 | 138 | mkdir($dir) || $! == EEXIST || die "unable to create directory '$dir' - $!\n"; |
fe000966 DM |
139 | } |
140 | } | |
141 | } | |
142 | ||
143 | sub gen_auth_key { | |
144 | ||
145 | return if -f "$authprivkeyfn"; | |
146 | ||
147 | check_cfs_is_mounted(); | |
148 | ||
62613060 | 149 | mkdir $authdir || $! == EEXIST || die "unable to create dir '$authdir' - $!\n"; |
fe000966 DM |
150 | |
151 | my $cmd = "openssl genrsa -out '$authprivkeyfn' 2048"; | |
152 | run_silent_cmd($cmd); | |
153 | ||
154 | $cmd = "openssl rsa -in '$authprivkeyfn' -pubout -out '$authpubkeyfn'"; | |
155 | run_silent_cmd($cmd) | |
156 | } | |
157 | ||
158 | sub gen_pveca_key { | |
159 | ||
160 | return if -f $pveca_key_fn; | |
161 | ||
162 | eval { | |
163 | run_silent_cmd(['openssl', 'genrsa', '-out', $pveca_key_fn, '2048']); | |
164 | }; | |
165 | ||
166 | die "unable to generate pve ca key:\n$@" if $@; | |
167 | } | |
168 | ||
169 | sub gen_pveca_cert { | |
170 | ||
171 | if (-f $pveca_key_fn && -f $pveca_cert_fn) { | |
172 | return 0; | |
173 | } | |
174 | ||
175 | gen_pveca_key(); | |
176 | ||
177 | # we try to generate an unique 'subject' to avoid browser problems | |
178 | # (reused serial numbers, ..) | |
179 | my $nid = (split (/\s/, `md5sum '$pveca_key_fn'`))[0] || time(); | |
180 | ||
181 | eval { | |
182 | run_silent_cmd(['openssl', 'req', '-batch', '-days', '3650', '-new', | |
183 | '-x509', '-nodes', '-key', | |
184 | $pveca_key_fn, '-out', $pveca_cert_fn, '-subj', | |
185 | "/CN=Proxmox Virtual Environment/OU=$nid/O=PVE Cluster Manager CA/"]); | |
186 | }; | |
187 | ||
188 | die "generating pve root certificate failed:\n$@" if $@; | |
189 | ||
190 | return 1; | |
191 | } | |
192 | ||
193 | sub gen_pve_ssl_key { | |
194 | my ($nodename) = @_; | |
195 | ||
196 | die "no node name specified" if !$nodename; | |
197 | ||
198 | my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key"; | |
199 | ||
200 | return if -f $pvessl_key_fn; | |
201 | ||
202 | eval { | |
203 | run_silent_cmd(['openssl', 'genrsa', '-out', $pvessl_key_fn, '2048']); | |
204 | }; | |
205 | ||
206 | die "unable to generate pve ssl key for node '$nodename':\n$@" if $@; | |
207 | } | |
208 | ||
209 | sub gen_pve_www_key { | |
210 | ||
211 | return if -f $pvewww_key_fn; | |
212 | ||
213 | eval { | |
214 | run_silent_cmd(['openssl', 'genrsa', '-out', $pvewww_key_fn, '2048']); | |
215 | }; | |
216 | ||
217 | die "unable to generate pve www key:\n$@" if $@; | |
218 | } | |
219 | ||
220 | sub update_serial { | |
221 | my ($serial) = @_; | |
222 | ||
223 | PVE::Tools::file_set_contents($pveca_srl_fn, $serial); | |
224 | } | |
225 | ||
226 | sub gen_pve_ssl_cert { | |
227 | my ($force, $nodename, $ip) = @_; | |
228 | ||
229 | die "no node name specified" if !$nodename; | |
230 | die "no IP specified" if !$ip; | |
231 | ||
232 | my $pvessl_cert_fn = "$basedir/nodes/$nodename/pve-ssl.pem"; | |
233 | ||
234 | return if !$force && -f $pvessl_cert_fn; | |
235 | ||
236 | my $names = "IP:127.0.0.1,DNS:localhost"; | |
237 | ||
238 | my $rc = PVE::INotify::read_file('resolvconf'); | |
239 | ||
240 | $names .= ",IP:$ip"; | |
241 | ||
242 | my $fqdn = $nodename; | |
243 | ||
244 | $names .= ",DNS:$nodename"; | |
245 | ||
246 | if ($rc && $rc->{search}) { | |
247 | $fqdn = $nodename . "." . $rc->{search}; | |
248 | $names .= ",DNS:$fqdn"; | |
249 | } | |
250 | ||
251 | my $sslconf = <<__EOD; | |
252 | RANDFILE = /root/.rnd | |
253 | extensions = v3_req | |
254 | ||
255 | [ req ] | |
256 | default_bits = 2048 | |
257 | distinguished_name = req_distinguished_name | |
258 | req_extensions = v3_req | |
259 | prompt = no | |
260 | string_mask = nombstr | |
261 | ||
262 | [ req_distinguished_name ] | |
263 | organizationalUnitName = PVE Cluster Node | |
264 | organizationName = Proxmox Virtual Environment | |
265 | commonName = $fqdn | |
266 | ||
267 | [ v3_req ] | |
268 | basicConstraints = CA:FALSE | |
269 | nsCertType = server | |
270 | keyUsage = nonRepudiation, digitalSignature, keyEncipherment | |
271 | subjectAltName = $names | |
272 | __EOD | |
273 | ||
274 | my $cfgfn = "/tmp/pvesslconf-$$.tmp"; | |
275 | my $fh = IO::File->new ($cfgfn, "w"); | |
276 | print $fh $sslconf; | |
277 | close ($fh); | |
278 | ||
279 | my $reqfn = "/tmp/pvecertreq-$$.tmp"; | |
280 | unlink $reqfn; | |
281 | ||
282 | my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key"; | |
283 | eval { | |
284 | run_silent_cmd(['openssl', 'req', '-batch', '-new', '-config', $cfgfn, | |
285 | '-key', $pvessl_key_fn, '-out', $reqfn]); | |
286 | }; | |
287 | ||
288 | if (my $err = $@) { | |
289 | unlink $reqfn; | |
290 | unlink $cfgfn; | |
291 | die "unable to generate pve certificate request:\n$err"; | |
292 | } | |
293 | ||
294 | update_serial("0000000000000000") if ! -f $pveca_srl_fn; | |
295 | ||
296 | eval { | |
297 | run_silent_cmd(['openssl', 'x509', '-req', '-in', $reqfn, '-days', '3650', | |
298 | '-out', $pvessl_cert_fn, '-CAkey', $pveca_key_fn, | |
299 | '-CA', $pveca_cert_fn, '-CAserial', $pveca_srl_fn, | |
300 | '-extfile', $cfgfn]); | |
301 | }; | |
302 | ||
303 | if (my $err = $@) { | |
304 | unlink $reqfn; | |
305 | unlink $cfgfn; | |
306 | die "unable to generate pve ssl certificate:\n$err"; | |
307 | } | |
308 | ||
309 | unlink $cfgfn; | |
310 | unlink $reqfn; | |
311 | } | |
312 | ||
313 | sub gen_pve_node_files { | |
314 | my ($nodename, $ip, $opt_force) = @_; | |
315 | ||
316 | gen_local_dirs($nodename); | |
317 | ||
318 | gen_auth_key(); | |
319 | ||
320 | # make sure we have a (cluster wide) secret | |
321 | # for CSRFR prevention | |
322 | gen_pve_www_key(); | |
323 | ||
324 | # make sure we have a (per node) private key | |
325 | gen_pve_ssl_key($nodename); | |
326 | ||
327 | # make sure we have a CA | |
328 | my $force = gen_pveca_cert(); | |
329 | ||
330 | $force = 1 if $opt_force; | |
331 | ||
332 | gen_pve_ssl_cert($force, $nodename, $ip); | |
333 | } | |
334 | ||
bd0ae7ff DM |
335 | my $vzdump_cron_dummy = <<__EOD; |
336 | # cluster wide vzdump cron schedule | |
337 | # Atomatically generated file - do not edit | |
338 | ||
339 | PATH="/usr/sbin:/usr/bin:/sbin:/bin" | |
340 | ||
341 | __EOD | |
342 | ||
343 | sub gen_pve_vzdump_symlink { | |
344 | ||
e1735a61 | 345 | my $filename = "/etc/pve/vzdump.cron"; |
bd0ae7ff DM |
346 | |
347 | my $link_fn = "/etc/cron.d/vzdump"; | |
348 | ||
349 | if ((-f $filename) && (! -l $link_fn)) { | |
350 | rename($link_fn, "/root/etc_cron_vzdump.org"); # make backup if file exists | |
351 | symlink($filename, $link_fn); | |
352 | } | |
353 | } | |
354 | ||
355 | sub gen_pve_vzdump_files { | |
356 | ||
e1735a61 | 357 | my $filename = "/etc/pve/vzdump.cron"; |
bd0ae7ff DM |
358 | |
359 | PVE::Tools::file_set_contents($filename, $vzdump_cron_dummy) | |
360 | if ! -f $filename; | |
361 | ||
362 | gen_pve_vzdump_symlink(); | |
363 | }; | |
364 | ||
fe000966 DM |
365 | my $versions = {}; |
366 | my $vmlist = {}; | |
367 | my $clinfo = {}; | |
368 | ||
369 | my $ipcc_send_rec = sub { | |
370 | my ($msgid, $data) = @_; | |
371 | ||
372 | my $res = PVE::IPCC::ipcc_send_rec($msgid, $data); | |
373 | ||
374 | die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0); | |
375 | ||
376 | return $res; | |
377 | }; | |
378 | ||
379 | my $ipcc_send_rec_json = sub { | |
380 | my ($msgid, $data) = @_; | |
381 | ||
382 | my $res = PVE::IPCC::ipcc_send_rec($msgid, $data); | |
383 | ||
384 | die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0); | |
385 | ||
386 | return decode_json($res); | |
387 | }; | |
388 | ||
389 | my $ipcc_get_config = sub { | |
390 | my ($path) = @_; | |
391 | ||
392 | my $bindata = pack "Z*", $path; | |
2db32d95 DM |
393 | my $res = PVE::IPCC::ipcc_send_rec(6, $bindata); |
394 | if (!defined($res)) { | |
395 | return undef if ($! != 0); | |
396 | return ''; | |
397 | } | |
398 | ||
399 | return $res; | |
fe000966 DM |
400 | }; |
401 | ||
402 | my $ipcc_get_status = sub { | |
403 | my ($name, $nodename) = @_; | |
404 | ||
405 | my $bindata = pack "Z[256]Z[256]", $name, ($nodename || ""); | |
406 | return PVE::IPCC::ipcc_send_rec(5, $bindata); | |
407 | }; | |
408 | ||
409 | my $ipcc_update_status = sub { | |
410 | my ($name, $data) = @_; | |
411 | ||
412 | my $raw = ref($data) ? encode_json($data) : $data; | |
413 | # update status | |
414 | my $bindata = pack "Z[256]Z*", $name, $raw; | |
415 | ||
416 | return &$ipcc_send_rec(4, $bindata); | |
417 | }; | |
418 | ||
419 | my $ipcc_log = sub { | |
420 | my ($priority, $ident, $tag, $msg) = @_; | |
421 | ||
422 | my $bindata = pack "CCCZ*Z*Z*", $priority, bytes::length($ident) + 1, | |
423 | bytes::length($tag) + 1, $ident, $tag, $msg; | |
424 | ||
425 | return &$ipcc_send_rec(7, $bindata); | |
426 | }; | |
427 | ||
428 | my $ipcc_get_cluster_log = sub { | |
429 | my ($user, $max) = @_; | |
430 | ||
431 | $max = 0 if !defined($max); | |
432 | ||
433 | my $bindata = pack "VVVVZ*", $max, 0, 0, 0, ($user || ""); | |
434 | return &$ipcc_send_rec(8, $bindata); | |
435 | }; | |
436 | ||
437 | my $ccache = {}; | |
438 | ||
439 | sub cfs_update { | |
440 | eval { | |
441 | my $res = &$ipcc_send_rec_json(1); | |
442 | #warn "GOT1: " . Dumper($res); | |
443 | die "no starttime\n" if !$res->{starttime}; | |
444 | ||
445 | if (!$res->{starttime} || !$versions->{starttime} || | |
446 | $res->{starttime} != $versions->{starttime}) { | |
447 | #print "detected changed starttime\n"; | |
448 | $vmlist = {}; | |
449 | $clinfo = {}; | |
450 | $ccache = {}; | |
451 | } | |
452 | ||
453 | $versions = $res; | |
454 | }; | |
455 | my $err = $@; | |
456 | if ($err) { | |
457 | $versions = {}; | |
458 | $vmlist = {}; | |
459 | $clinfo = {}; | |
460 | $ccache = {}; | |
461 | warn $err; | |
462 | } | |
463 | ||
464 | eval { | |
465 | if (!$clinfo->{version} || $clinfo->{version} != $versions->{clinfo}) { | |
466 | #warn "detected new clinfo\n"; | |
467 | $clinfo = &$ipcc_send_rec_json(2); | |
468 | } | |
469 | }; | |
470 | $err = $@; | |
471 | if ($err) { | |
472 | $clinfo = {}; | |
473 | warn $err; | |
474 | } | |
475 | ||
476 | eval { | |
477 | if (!$vmlist->{version} || $vmlist->{version} != $versions->{vmlist}) { | |
478 | #warn "detected new vmlist1\n"; | |
479 | $vmlist = &$ipcc_send_rec_json(3); | |
480 | } | |
481 | }; | |
482 | $err = $@; | |
483 | if ($err) { | |
484 | $vmlist = {}; | |
485 | warn $err; | |
486 | } | |
487 | } | |
488 | ||
489 | sub get_vmlist { | |
490 | return $vmlist; | |
491 | } | |
492 | ||
493 | sub get_clinfo { | |
494 | return $clinfo; | |
495 | } | |
496 | ||
9ddd4ae9 DM |
497 | sub get_members { |
498 | return $clinfo->{nodelist}; | |
499 | } | |
500 | ||
fe000966 DM |
501 | sub get_nodelist { |
502 | ||
503 | my $nodelist = $clinfo->{nodelist}; | |
504 | ||
505 | my $result = []; | |
506 | ||
507 | my $nodename = PVE::INotify::nodename(); | |
508 | ||
509 | if (!$nodelist || !$nodelist->{$nodename}) { | |
510 | return [ $nodename ]; | |
511 | } | |
512 | ||
513 | return [ keys %$nodelist ]; | |
514 | } | |
515 | ||
516 | sub broadcast_tasklist { | |
517 | my ($data) = @_; | |
518 | ||
519 | eval { | |
520 | &$ipcc_update_status("tasklist", $data); | |
521 | }; | |
522 | ||
523 | warn $@ if $@; | |
524 | } | |
525 | ||
526 | my $tasklistcache = {}; | |
527 | ||
528 | sub get_tasklist { | |
529 | my ($nodename) = @_; | |
530 | ||
531 | my $kvstore = $versions->{kvstore} || {}; | |
532 | ||
533 | my $nodelist = get_nodelist(); | |
534 | ||
535 | my $res = []; | |
536 | foreach my $node (@$nodelist) { | |
537 | next if $nodename && ($nodename ne $node); | |
538 | eval { | |
539 | my $ver = $kvstore->{$node}->{tasklist} if $kvstore->{$node}; | |
540 | my $cd = $tasklistcache->{$node}; | |
cebe16ec DM |
541 | if (!$cd || !$ver || !$cd->{version} || |
542 | ($cd->{version} != $ver)) { | |
fe000966 DM |
543 | my $raw = &$ipcc_get_status("tasklist", $node) || '[]'; |
544 | my $data = decode_json($raw); | |
545 | push @$res, @$data; | |
546 | $cd = $tasklistcache->{$node} = { | |
547 | data => $data, | |
548 | version => $ver, | |
549 | }; | |
550 | } elsif ($cd && $cd->{data}) { | |
551 | push @$res, @{$cd->{data}}; | |
552 | } | |
553 | }; | |
554 | my $err = $@; | |
555 | syslog('err', $err) if $err; | |
556 | } | |
557 | ||
558 | return $res; | |
559 | } | |
560 | ||
561 | sub broadcast_rrd { | |
562 | my ($rrdid, $data) = @_; | |
563 | ||
564 | eval { | |
565 | &$ipcc_update_status("rrd/$rrdid", $data); | |
566 | }; | |
567 | my $err = $@; | |
568 | ||
569 | warn $err if $err; | |
570 | } | |
571 | ||
572 | my $last_rrd_dump = 0; | |
573 | my $last_rrd_data = ""; | |
574 | ||
575 | sub rrd_dump { | |
576 | ||
577 | my $ctime = time(); | |
578 | ||
579 | my $diff = $ctime - $last_rrd_dump; | |
580 | if ($diff < 2) { | |
581 | return $last_rrd_data; | |
582 | } | |
583 | ||
584 | my $raw; | |
585 | eval { | |
586 | $raw = &$ipcc_send_rec(10); | |
587 | }; | |
588 | my $err = $@; | |
589 | ||
590 | if ($err) { | |
591 | warn $err; | |
592 | return {}; | |
593 | } | |
594 | ||
595 | my $res = {}; | |
596 | ||
c3fabca7 DM |
597 | if ($raw) { |
598 | while ($raw =~ s/^(.*)\n//) { | |
599 | my ($key, @ela) = split(/:/, $1); | |
600 | next if !$key; | |
601 | next if !(scalar(@ela) > 1); | |
602 | $res->{$key} = \@ela; | |
603 | } | |
fe000966 DM |
604 | } |
605 | ||
606 | $last_rrd_dump = $ctime; | |
607 | $last_rrd_data = $res; | |
608 | ||
609 | return $res; | |
610 | } | |
611 | ||
612 | sub create_rrd_data { | |
613 | my ($rrdname, $timeframe, $cf) = @_; | |
614 | ||
615 | my $rrddir = "/var/lib/rrdcached/db"; | |
616 | ||
617 | my $rrd = "$rrddir/$rrdname"; | |
618 | ||
619 | my $setup = { | |
620 | hour => [ 60, 70 ], | |
621 | day => [ 60*30, 70 ], | |
622 | week => [ 60*180, 70 ], | |
623 | month => [ 60*720, 70 ], | |
624 | year => [ 60*10080, 70 ], | |
625 | }; | |
626 | ||
627 | my ($reso, $count) = @{$setup->{$timeframe}}; | |
628 | my $ctime = $reso*int(time()/$reso); | |
629 | my $req_start = $ctime - $reso*$count; | |
630 | ||
631 | $cf = "AVERAGE" if !$cf; | |
632 | ||
633 | my @args = ( | |
634 | "-s" => $req_start, | |
635 | "-e" => $ctime - 1, | |
636 | "-r" => $reso, | |
637 | ); | |
638 | ||
639 | my $socket = "/var/run/rrdcached.sock"; | |
640 | push @args, "--daemon" => "unix:$socket" if -S $socket; | |
641 | ||
642 | my ($start, $step, $names, $data) = RRDs::fetch($rrd, $cf, @args); | |
643 | ||
644 | my $err = RRDs::error; | |
645 | die "RRD error: $err\n" if $err; | |
646 | ||
647 | die "got wrong time resolution ($step != $reso)\n" | |
648 | if $step != $reso; | |
649 | ||
650 | my $res = []; | |
651 | my $fields = scalar(@$names); | |
652 | for my $line (@$data) { | |
653 | my $entry = { 'time' => $start }; | |
654 | $start += $step; | |
655 | my $found_undefs; | |
656 | for (my $i = 0; $i < $fields; $i++) { | |
657 | my $name = $names->[$i]; | |
658 | if (defined(my $val = $line->[$i])) { | |
659 | $entry->{$name} = $val; | |
660 | } else { | |
661 | # we only add entryies with all data defined | |
662 | # extjs chart has problems with undefined values | |
663 | $found_undefs = 1; | |
664 | } | |
665 | } | |
666 | push @$res, $entry if !$found_undefs; | |
667 | } | |
668 | ||
669 | return $res; | |
670 | } | |
671 | ||
672 | sub create_rrd_graph { | |
673 | my ($rrdname, $timeframe, $ds, $cf) = @_; | |
674 | ||
675 | # Using RRD graph is clumsy - maybe it | |
676 | # is better to simply fetch the data, and do all display | |
677 | # related things with javascript (new extjs html5 graph library). | |
678 | ||
679 | my $rrddir = "/var/lib/rrdcached/db"; | |
680 | ||
681 | my $rrd = "$rrddir/$rrdname"; | |
682 | ||
31938ad4 DM |
683 | my @ids = PVE::Tools::split_list($ds); |
684 | ||
685 | my $ds_txt = join('_', @ids); | |
686 | ||
687 | my $filename = "${rrd}_${ds_txt}.png"; | |
fe000966 DM |
688 | |
689 | my $setup = { | |
690 | hour => [ 60, 60 ], | |
691 | day => [ 60*30, 70 ], | |
692 | week => [ 60*180, 70 ], | |
693 | month => [ 60*720, 70 ], | |
694 | year => [ 60*10080, 70 ], | |
695 | }; | |
696 | ||
697 | my ($reso, $count) = @{$setup->{$timeframe}}; | |
698 | ||
699 | my @args = ( | |
700 | "--imgformat" => "PNG", | |
701 | "--border" => 0, | |
702 | "--height" => 200, | |
703 | "--width" => 800, | |
704 | "--start" => - $reso*$count, | |
705 | "--end" => 'now' , | |
706 | ); | |
707 | ||
708 | my $socket = "/var/run/rrdcached.sock"; | |
709 | push @args, "--daemon" => "unix:$socket" if -S $socket; | |
710 | ||
fe000966 DM |
711 | my @coldef = ('#00ddff', '#ff0000'); |
712 | ||
713 | $cf = "AVERAGE" if !$cf; | |
714 | ||
715 | my $i = 0; | |
716 | foreach my $id (@ids) { | |
717 | my $col = $coldef[$i++] || die "fixme: no color definition"; | |
718 | push @args, "DEF:${id}=$rrd:${id}:$cf"; | |
719 | my $dataid = $id; | |
720 | if ($id eq 'cpu' || $id eq 'iowait') { | |
721 | push @args, "CDEF:${id}_per=${id},100,*"; | |
722 | $dataid = "${id}_per"; | |
723 | } | |
724 | push @args, "LINE2:${dataid}${col}:${id}"; | |
725 | } | |
726 | ||
a665376e DM |
727 | push @args, '--full-size-mode'; |
728 | ||
31938ad4 | 729 | # we do not really store data into the file |
b871db9c | 730 | my $res = RRDs::graphv('', @args); |
fe000966 DM |
731 | |
732 | my $err = RRDs::error; | |
733 | die "RRD error: $err\n" if $err; | |
734 | ||
31938ad4 | 735 | return { filename => $filename, image => $res->{image} }; |
fe000966 DM |
736 | } |
737 | ||
738 | # a fast way to read files (avoid fuse overhead) | |
739 | sub get_config { | |
740 | my ($path) = @_; | |
741 | ||
d3a92ba7 | 742 | return &$ipcc_get_config($path); |
fe000966 DM |
743 | } |
744 | ||
745 | sub get_cluster_log { | |
746 | my ($user, $max) = @_; | |
747 | ||
748 | return &$ipcc_get_cluster_log($user, $max); | |
749 | } | |
750 | ||
751 | my $file_info = {}; | |
752 | ||
753 | sub cfs_register_file { | |
754 | my ($filename, $parser, $writer) = @_; | |
755 | ||
756 | $observed->{$filename} || die "unknown file '$filename'"; | |
757 | ||
758 | die "file '$filename' already registered" if $file_info->{$filename}; | |
759 | ||
760 | $file_info->{$filename} = { | |
761 | parser => $parser, | |
762 | writer => $writer, | |
763 | }; | |
764 | } | |
765 | ||
766 | my $ccache_read = sub { | |
767 | my ($filename, $parser, $version) = @_; | |
768 | ||
769 | $ccache->{$filename} = {} if !$ccache->{$filename}; | |
770 | ||
771 | my $ci = $ccache->{$filename}; | |
772 | ||
d3a92ba7 DM |
773 | if (!$ci->{version} || !$version || $ci->{version} != $version) { |
774 | # we always call the parser, even when the file does not exists | |
775 | # (in that case $data is undef) | |
fe000966 | 776 | my $data = get_config($filename); |
fe000966 DM |
777 | $ci->{data} = &$parser("/etc/pve/$filename", $data); |
778 | $ci->{version} = $version; | |
779 | } | |
780 | ||
781 | my $res = ref($ci->{data}) ? dclone($ci->{data}) : $ci->{data}; | |
782 | ||
783 | return $res; | |
784 | }; | |
785 | ||
786 | sub cfs_file_version { | |
787 | my ($filename) = @_; | |
788 | ||
789 | my $version; | |
790 | my $infotag; | |
f71eee41 DM |
791 | if ($filename =~ m!^nodes/[^/]+/(openvz|qemu-server)/(\d+)\.conf$!) { |
792 | my ($type, $vmid) = ($1, $2); | |
fe000966 DM |
793 | if ($vmlist && $vmlist->{ids} && $vmlist->{ids}->{$vmid}) { |
794 | $version = $vmlist->{ids}->{$vmid}->{version}; | |
795 | } | |
f71eee41 | 796 | $infotag = "/$type/"; |
fe000966 DM |
797 | } else { |
798 | $infotag = $filename; | |
799 | $version = $versions->{$filename}; | |
800 | } | |
801 | ||
802 | my $info = $file_info->{$infotag} || | |
803 | die "unknown file type '$filename'\n"; | |
804 | ||
805 | return wantarray ? ($version, $info) : $version; | |
806 | } | |
807 | ||
808 | sub cfs_read_file { | |
809 | my ($filename) = @_; | |
810 | ||
811 | my ($version, $info) = cfs_file_version($filename); | |
812 | my $parser = $info->{parser}; | |
813 | ||
814 | return &$ccache_read($filename, $parser, $version); | |
815 | } | |
816 | ||
817 | sub cfs_write_file { | |
818 | my ($filename, $data) = @_; | |
819 | ||
adb84d35 | 820 | my ($version, $info) = cfs_file_version($filename); |
fe000966 DM |
821 | |
822 | my $writer = $info->{writer} || die "no writer defined"; | |
823 | ||
824 | my $fsname = "/etc/pve/$filename"; | |
825 | ||
826 | my $raw = &$writer($fsname, $data); | |
827 | ||
828 | if (my $ci = $ccache->{$filename}) { | |
829 | $ci->{version} = undef; | |
830 | } | |
831 | ||
832 | PVE::Tools::file_set_contents($fsname, $raw); | |
833 | } | |
834 | ||
835 | my $cfs_lock = sub { | |
836 | my ($lockid, $timeout, $code, @param) = @_; | |
837 | ||
838 | my $res; | |
839 | ||
840 | # this timeout is for aquire the lock | |
841 | $timeout = 10 if !$timeout; | |
842 | ||
843 | my $filename = "$lockdir/$lockid"; | |
844 | ||
845 | my $msg = "can't aquire cfs lock '$lockid'"; | |
846 | ||
847 | eval { | |
848 | ||
849 | mkdir $lockdir; | |
850 | ||
851 | if (! -d $lockdir) { | |
852 | die "$msg: pve cluster filesystem not online.\n"; | |
853 | } | |
854 | ||
855 | local $SIG{ALRM} = sub { die "got lock request timeout\n"; }; | |
856 | ||
857 | alarm ($timeout); | |
858 | ||
859 | if (!(mkdir $filename)) { | |
860 | print STDERR "trying to aquire cfs lock '$lockid' ..."; | |
861 | while (1) { | |
862 | if (!(mkdir $filename)) { | |
863 | (utime 0, 0, $filename); # cfs unlock request | |
864 | } else { | |
865 | print STDERR " OK\n"; | |
866 | last; | |
867 | } | |
868 | sleep(1); | |
869 | } | |
870 | } | |
871 | ||
872 | # fixed command timeout: cfs locks have a timeout of 120 | |
873 | # using 60 gives us another 60 seconds to abort the task | |
874 | alarm(60); | |
875 | local $SIG{ALRM} = sub { die "got lock timeout - aborting command\n"; }; | |
876 | ||
9c206b2b DM |
877 | cfs_update(); # make sure we read latest versions inside code() |
878 | ||
fe000966 DM |
879 | $res = &$code(@param); |
880 | ||
881 | alarm(0); | |
882 | }; | |
883 | ||
884 | my $err = $@; | |
885 | ||
886 | alarm(0); | |
887 | ||
888 | if ($err && ($err eq "got lock request timeout\n") && | |
889 | !check_cfs_quorum()){ | |
890 | $err = "$msg: no quorum!\n"; | |
891 | } | |
892 | ||
893 | if (!$err || $err !~ /^got lock timeout -/) { | |
894 | rmdir $filename; # cfs unlock | |
895 | } | |
896 | ||
897 | if ($err) { | |
898 | $@ = $err; | |
899 | return undef; | |
900 | } | |
901 | ||
902 | $@ = undef; | |
903 | ||
904 | return $res; | |
905 | }; | |
906 | ||
907 | sub cfs_lock_file { | |
908 | my ($filename, $timeout, $code, @param) = @_; | |
909 | ||
910 | my $info = $observed->{$filename} || die "unknown file '$filename'"; | |
911 | ||
912 | my $lockid = "file-$filename"; | |
913 | $lockid =~ s/[.\/]/_/g; | |
914 | ||
915 | &$cfs_lock($lockid, $timeout, $code, @param); | |
916 | } | |
917 | ||
918 | sub cfs_lock_storage { | |
919 | my ($storeid, $timeout, $code, @param) = @_; | |
920 | ||
921 | my $lockid = "storage-$storeid"; | |
922 | ||
923 | &$cfs_lock($lockid, $timeout, $code, @param); | |
924 | } | |
925 | ||
926 | my $log_levels = { | |
927 | "emerg" => 0, | |
928 | "alert" => 1, | |
929 | "crit" => 2, | |
930 | "critical" => 2, | |
931 | "err" => 3, | |
932 | "error" => 3, | |
933 | "warn" => 4, | |
934 | "warning" => 4, | |
935 | "notice" => 5, | |
936 | "info" => 6, | |
937 | "debug" => 7, | |
938 | }; | |
939 | ||
940 | sub log_msg { | |
941 | my ($priority, $ident, $msg) = @_; | |
942 | ||
943 | if (my $tmp = $log_levels->{$priority}) { | |
944 | $priority = $tmp; | |
945 | } | |
946 | ||
947 | die "need numeric log priority" if $priority !~ /^\d+$/; | |
948 | ||
949 | my $tag = PVE::SafeSyslog::tag(); | |
950 | ||
951 | $msg = "empty message" if !$msg; | |
952 | ||
953 | $ident = "" if !$ident; | |
954 | $ident = encode("ascii", decode_utf8($ident), | |
955 | sub { sprintf "\\u%04x", shift }); | |
956 | ||
957 | my $utf8 = decode_utf8($msg); | |
958 | ||
959 | my $ascii = encode("ascii", $utf8, sub { sprintf "\\u%04x", shift }); | |
960 | ||
961 | if ($ident) { | |
962 | syslog($priority, "<%s> %s", $ident, $ascii); | |
963 | } else { | |
964 | syslog($priority, "%s", $ascii); | |
965 | } | |
966 | ||
967 | eval { &$ipcc_log($priority, $ident, $tag, $ascii); }; | |
968 | ||
969 | syslog("err", "writing cluster log failed: $@") if $@; | |
970 | } | |
971 | ||
65ff467f DM |
972 | sub check_node_exists { |
973 | my ($nodename, $noerr) = @_; | |
974 | ||
975 | my $nodelist = $clinfo->{nodelist}; | |
976 | return 1 if $nodelist && $nodelist->{$nodename}; | |
977 | ||
978 | return undef if $noerr; | |
979 | ||
980 | die "no such cluster node '$nodename'\n"; | |
981 | } | |
982 | ||
fe000966 DM |
983 | # this is also used to get the IP of the local node |
984 | sub remote_node_ip { | |
985 | my ($nodename, $noerr) = @_; | |
986 | ||
987 | my $nodelist = $clinfo->{nodelist}; | |
988 | if ($nodelist && $nodelist->{$nodename}) { | |
989 | if (my $ip = $nodelist->{$nodename}->{ip}) { | |
990 | return $ip; | |
991 | } | |
992 | } | |
993 | ||
994 | # fallback: try to get IP by other means | |
995 | my $packed_ip = gethostbyname($nodename); | |
996 | if (defined $packed_ip) { | |
997 | my $ip = inet_ntoa($packed_ip); | |
998 | ||
999 | if ($ip =~ m/^127\./) { | |
1000 | die "hostname lookup failed - got local IP address ($nodename = $ip)\n" if !$noerr; | |
1001 | return undef; | |
1002 | } | |
1003 | ||
1004 | return $ip; | |
1005 | } | |
1006 | ||
1007 | die "unable to get IP for node '$nodename' - node offline?\n" if !$noerr; | |
1008 | ||
1009 | return undef; | |
1010 | } | |
1011 | ||
1012 | # ssh related utility functions | |
1013 | ||
1014 | sub ssh_merge_keys { | |
1015 | # remove duplicate keys in $sshauthkeys | |
1016 | # ssh-copy-id simply add keys, so the file can grow to large | |
1017 | ||
1018 | my $data = ''; | |
1019 | if (-f $sshauthkeys) { | |
1020 | $data = PVE::Tools::file_get_contents($sshauthkeys, 128*1024); | |
1021 | chomp($data); | |
1022 | } | |
1023 | ||
6056578e DM |
1024 | my $found_backup; |
1025 | if (-f $rootsshauthkeysbackup) { | |
404343d7 | 1026 | $data .= "\n"; |
6056578e DM |
1027 | $data .= PVE::Tools::file_get_contents($rootsshauthkeysbackup, 128*1024); |
1028 | chomp($data); | |
1029 | $found_backup = 1; | |
1030 | } | |
1031 | ||
fe000966 DM |
1032 | # always add ourself |
1033 | if (-f $ssh_rsa_id) { | |
1034 | my $pub = PVE::Tools::file_get_contents($ssh_rsa_id); | |
1035 | chomp($pub); | |
1036 | $data .= "\n$pub\n"; | |
1037 | } | |
1038 | ||
1039 | my $newdata = ""; | |
1040 | my $vhash = {}; | |
2055b0a9 DM |
1041 | my @lines = split(/\n/, $data); |
1042 | foreach my $line (@lines) { | |
7eb37d8d SP |
1043 | if ($line !~ /^#/ && $line =~ m/(^|\s)ssh-(rsa|dsa)\s+(\S+)\s+\S+$/) { |
1044 | next if $vhash->{$3}++; | |
fe000966 | 1045 | } |
2055b0a9 | 1046 | $newdata .= "$line\n"; |
fe000966 | 1047 | } |
fe000966 DM |
1048 | |
1049 | PVE::Tools::file_set_contents($sshauthkeys, $newdata, 0600); | |
6056578e DM |
1050 | |
1051 | if ($found_backup && -l $rootsshauthkeys) { | |
1052 | # everything went well, so we can remove the backup | |
1053 | unlink $rootsshauthkeysbackup; | |
1054 | } | |
fe000966 DM |
1055 | } |
1056 | ||
f666cdde SP |
1057 | sub setup_rootsshconfig { |
1058 | ||
39df71df DM |
1059 | # create ssh key if it does not exist |
1060 | if (! -f $ssh_rsa_id) { | |
1061 | mkdir '/root/.ssh/'; | |
1062 | system ("echo|ssh-keygen -t rsa -N '' -b 2048 -f ${ssh_rsa_id_priv}"); | |
1063 | } | |
1064 | ||
f666cdde SP |
1065 | # create ssh config if it does not exist |
1066 | if (! -f $rootsshconfig) { | |
9aabc24b DM |
1067 | mkdir '/root/.ssh'; |
1068 | if (my $fh = IO::File->new($rootsshconfig, O_CREAT|O_WRONLY|O_EXCL, 0640)) { | |
f666cdde | 1069 | # this is the default ciphers list from debian openssl0.9.8 except blowfish is added as prefered |
9aabc24b | 1070 | print $fh "Ciphers blowfish-cbc,aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc\n"; |
f666cdde SP |
1071 | close($fh); |
1072 | } | |
1073 | } | |
1074 | } | |
1075 | ||
fe000966 DM |
1076 | sub setup_ssh_keys { |
1077 | ||
fe000966 DM |
1078 | mkdir $authdir; |
1079 | ||
6056578e DM |
1080 | my $import_ok; |
1081 | ||
fe000966 | 1082 | if (! -f $sshauthkeys) { |
6056578e DM |
1083 | my $old; |
1084 | if (-f $rootsshauthkeys) { | |
1085 | $old = PVE::Tools::file_get_contents($rootsshauthkeys, 128*1024); | |
1086 | } | |
fe000966 | 1087 | if (my $fh = IO::File->new ($sshauthkeys, O_CREAT|O_WRONLY|O_EXCL, 0400)) { |
6056578e | 1088 | PVE::Tools::safe_print($sshauthkeys, $fh, $old) if $old; |
fe000966 | 1089 | close($fh); |
6056578e | 1090 | $import_ok = 1; |
fe000966 DM |
1091 | } |
1092 | } | |
1093 | ||
1094 | warn "can't create shared ssh key database '$sshauthkeys'\n" | |
1095 | if ! -f $sshauthkeys; | |
1096 | ||
404343d7 | 1097 | if (-f $rootsshauthkeys && ! -l $rootsshauthkeys) { |
6056578e DM |
1098 | if (!rename($rootsshauthkeys , $rootsshauthkeysbackup)) { |
1099 | warn "rename $rootsshauthkeys failed - $!\n"; | |
1100 | } | |
fe000966 DM |
1101 | } |
1102 | ||
1103 | if (! -l $rootsshauthkeys) { | |
1104 | symlink $sshauthkeys, $rootsshauthkeys; | |
1105 | } | |
fe000966 | 1106 | |
6056578e DM |
1107 | if (! -l $rootsshauthkeys) { |
1108 | warn "can't create symlink for ssh keys '$rootsshauthkeys' -> '$sshauthkeys'\n"; | |
1109 | } else { | |
1110 | unlink $rootsshauthkeysbackup if $import_ok; | |
1111 | } | |
fe000966 DM |
1112 | } |
1113 | ||
1114 | sub ssh_unmerge_known_hosts { | |
1115 | return if ! -l $sshglobalknownhosts; | |
1116 | ||
1117 | my $old = ''; | |
1118 | $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024) | |
1119 | if -f $sshknownhosts; | |
1120 | ||
1121 | PVE::Tools::file_set_contents($sshglobalknownhosts, $old); | |
1122 | } | |
1123 | ||
1124 | sub ssh_merge_known_hosts { | |
1125 | my ($nodename, $ip_address, $createLink) = @_; | |
1126 | ||
1127 | die "no node name specified" if !$nodename; | |
1128 | die "no ip address specified" if !$ip_address; | |
1129 | ||
1130 | mkdir $authdir; | |
1131 | ||
1132 | if (! -f $sshknownhosts) { | |
1133 | if (my $fh = IO::File->new($sshknownhosts, O_CREAT|O_WRONLY|O_EXCL, 0600)) { | |
1134 | close($fh); | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | my $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024); | |
1139 | ||
1140 | my $new = ''; | |
1141 | ||
1142 | if ((! -l $sshglobalknownhosts) && (-f $sshglobalknownhosts)) { | |
1143 | $new = PVE::Tools::file_get_contents($sshglobalknownhosts, 128*1024); | |
1144 | } | |
1145 | ||
1146 | my $hostkey = PVE::Tools::file_get_contents($ssh_host_rsa_id); | |
1147 | die "can't parse $ssh_rsa_id" if $hostkey !~ m/^(ssh-rsa\s\S+)(\s.*)?$/; | |
1148 | $hostkey = $1; | |
1149 | ||
1150 | my $data = ''; | |
1151 | my $vhash = {}; | |
1152 | ||
1153 | my $found_nodename; | |
1154 | my $found_local_ip; | |
1155 | ||
1156 | my $merge_line = sub { | |
1157 | my ($line, $all) = @_; | |
1158 | ||
1159 | if ($line =~ m/^(\S+)\s(ssh-rsa\s\S+)(\s.*)?$/) { | |
1160 | my $key = $1; | |
1161 | my $rsakey = $2; | |
1162 | if (!$vhash->{$key}) { | |
1163 | $vhash->{$key} = 1; | |
1164 | if ($key =~ m/\|1\|([^\|\s]+)\|([^\|\s]+)$/) { | |
1165 | my $salt = decode_base64($1); | |
1166 | my $digest = $2; | |
1167 | my $hmac = Digest::HMAC_SHA1->new($salt); | |
1168 | $hmac->add($nodename); | |
1169 | my $hd = $hmac->b64digest . '='; | |
1170 | if ($digest eq $hd) { | |
1171 | if ($rsakey eq $hostkey) { | |
1172 | $found_nodename = 1; | |
1173 | $data .= $line; | |
1174 | } | |
1175 | return; | |
1176 | } | |
1177 | $hmac = Digest::HMAC_SHA1->new($salt); | |
1178 | $hmac->add($ip_address); | |
1179 | $hd = $hmac->b64digest . '='; | |
1180 | if ($digest eq $hd) { | |
1181 | if ($rsakey eq $hostkey) { | |
1182 | $found_local_ip = 1; | |
1183 | $data .= $line; | |
1184 | } | |
1185 | return; | |
1186 | } | |
1187 | } | |
1188 | $data .= $line; | |
1189 | } | |
1190 | } elsif ($all) { | |
1191 | $data .= $line; | |
1192 | } | |
1193 | }; | |
1194 | ||
1195 | while ($old && $old =~ s/^((.*?)(\n|$))//) { | |
1196 | my $line = "$2\n"; | |
1197 | next if $line =~ m/^\s*$/; # skip empty lines | |
1198 | next if $line =~ m/^#/; # skip comments | |
1199 | &$merge_line($line, 1); | |
1200 | } | |
1201 | ||
1202 | while ($new && $new =~ s/^((.*?)(\n|$))//) { | |
1203 | my $line = "$2\n"; | |
1204 | next if $line =~ m/^\s*$/; # skip empty lines | |
1205 | next if $line =~ m/^#/; # skip comments | |
1206 | &$merge_line($line); | |
1207 | } | |
1208 | ||
1209 | my $addIndex = $$; | |
1210 | my $add_known_hosts_entry = sub { | |
1211 | my ($name, $hostkey) = @_; | |
1212 | $addIndex++; | |
1213 | my $hmac = Digest::HMAC_SHA1->new("$addIndex" . time()); | |
1214 | my $b64salt = $hmac->b64digest . '='; | |
1215 | $hmac = Digest::HMAC_SHA1->new(decode_base64($b64salt)); | |
1216 | $hmac->add($name); | |
1217 | my $digest = $hmac->b64digest . '='; | |
1218 | $data .= "|1|$b64salt|$digest $hostkey\n"; | |
1219 | }; | |
1220 | ||
1221 | if (!$found_nodename || !$found_local_ip) { | |
1222 | &$add_known_hosts_entry($nodename, $hostkey) if !$found_nodename; | |
1223 | &$add_known_hosts_entry($ip_address, $hostkey) if !$found_local_ip; | |
1224 | } | |
1225 | ||
1226 | PVE::Tools::file_set_contents($sshknownhosts, $data); | |
1227 | ||
1228 | return if !$createLink; | |
1229 | ||
1230 | unlink $sshglobalknownhosts; | |
1231 | symlink $sshknownhosts, $sshglobalknownhosts; | |
1232 | ||
1233 | warn "can't create symlink for ssh known hosts '$sshglobalknownhosts' -> '$sshknownhosts'\n" | |
1234 | if ! -l $sshglobalknownhosts; | |
1235 | ||
1236 | } | |
1237 | ||
fe000966 DM |
1238 | my $datacenter_schema = { |
1239 | type => "object", | |
1240 | additionalProperties => 0, | |
1241 | properties => { | |
1242 | keyboard => { | |
1243 | optional => 1, | |
1244 | type => 'string', | |
1245 | description => "Default keybord layout for vnc server.", | |
c59334cb | 1246 | enum => PVE::Tools::kvmkeymaplist(), |
fe000966 DM |
1247 | }, |
1248 | language => { | |
1249 | optional => 1, | |
1250 | type => 'string', | |
1251 | description => "Default GUI language.", | |
1252 | enum => [ 'en', 'de' ], | |
1253 | }, | |
1254 | http_proxy => { | |
1255 | optional => 1, | |
1256 | type => 'string', | |
1257 | description => "Specify external http proxy which is used for downloads (example: 'http://username:password\@host:port/')", | |
1258 | pattern => "http://.*", | |
1259 | }, | |
a9323ef0 SP |
1260 | migration_unsecure => { |
1261 | optional => 1, | |
1262 | type => 'boolean', | |
1263 | description => "Migration is secure using SSH tunnel by default. For secure private networks you can disable it to speed up migration.", | |
1264 | }, | |
dce47328 DM |
1265 | console => { |
1266 | optional => 1, | |
1267 | type => 'string', | |
66a15f27 DM |
1268 | description => "Select the default Console viewer. You can either use the builtin java applet (VNC), an external virt-viewer comtatible application (SPICE), or an HTML5 based viewer (noVNC).", |
1269 | enum => ['applet', 'vv', 'html5'], | |
dce47328 | 1270 | }, |
8548bd87 SGE |
1271 | email_from => { |
1272 | optional => 1, | |
1273 | type => 'string', | |
a05baf53 | 1274 | format => 'email-opt', |
8548bd87 SGE |
1275 | description => "Specify email address to send notification from (default is root@\$hostname)", |
1276 | }, | |
fe000966 DM |
1277 | }, |
1278 | }; | |
1279 | ||
1280 | # make schema accessible from outside (for documentation) | |
1281 | sub get_datacenter_schema { return $datacenter_schema }; | |
1282 | ||
1283 | sub parse_datacenter_config { | |
1284 | my ($filename, $raw) = @_; | |
1285 | ||
1286 | return PVE::JSONSchema::parse_config($datacenter_schema, $filename, $raw); | |
1287 | } | |
1288 | ||
1289 | sub write_datacenter_config { | |
1290 | my ($filename, $cfg) = @_; | |
1291 | ||
1292 | return PVE::JSONSchema::dump_config($datacenter_schema, $filename, $cfg); | |
1293 | } | |
1294 | ||
1295 | cfs_register_file('datacenter.cfg', | |
1296 | \&parse_datacenter_config, | |
1297 | \&write_datacenter_config); | |
ec48ec22 | 1298 | |
cafc7309 DM |
1299 | # a very simply parser ... |
1300 | sub parse_corosync_conf { | |
ec48ec22 DM |
1301 | my ($filename, $raw) = @_; |
1302 | ||
cafc7309 | 1303 | return {} if !$raw; |
ec48ec22 | 1304 | |
440121dc | 1305 | my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : ''); |
ec48ec22 | 1306 | |
cafc7309 DM |
1307 | $raw =~ s/#.*$//mg; |
1308 | $raw =~ s/\r?\n/ /g; | |
1309 | $raw =~ s/\s+/ /g; | |
1310 | $raw =~ s/^\s+//; | |
1311 | $raw =~ s/\s*$//; | |
1312 | ||
cafc7309 DM |
1313 | my @tokens = split(/\s/, $raw); |
1314 | ||
1315 | my $conf = { section => 'main', children => [] }; | |
1d01c3f6 | 1316 | |
cafc7309 DM |
1317 | my $stack = []; |
1318 | my $section = $conf; | |
1319 | ||
1320 | while (defined(my $token = shift @tokens)) { | |
1321 | my $nexttok = $tokens[0]; | |
1322 | ||
1323 | if ($nexttok && ($nexttok eq '{')) { | |
1324 | shift @tokens; # skip '{' | |
1325 | my $new_section = { | |
1326 | section => $token, | |
1327 | children => [], | |
1328 | }; | |
1329 | push @{$section->{children}}, $new_section; | |
1330 | push @$stack, $section; | |
1331 | $section = $new_section; | |
1332 | next; | |
1d01c3f6 | 1333 | } |
1d01c3f6 | 1334 | |
cafc7309 DM |
1335 | if ($token eq '}') { |
1336 | $section = pop @$stack; | |
1337 | die "parse error - uncexpected '}'\n" if !$section; | |
1338 | next; | |
1339 | } | |
1d01c3f6 | 1340 | |
cafc7309 DM |
1341 | my $key = $token; |
1342 | die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//); | |
1343 | ||
1344 | die "parse error - no value for '$key'\n" if !defined($nexttok); | |
1345 | my $value = shift @tokens; | |
1d01c3f6 | 1346 | |
cafc7309 DM |
1347 | push @{$section->{children}}, { key => $key, value => $value }; |
1348 | } | |
1d01c3f6 | 1349 | |
cafc7309 | 1350 | $conf->{digest} = $digest; |
1d01c3f6 | 1351 | |
cafc7309 | 1352 | return $conf; |
1d01c3f6 DM |
1353 | } |
1354 | ||
cafc7309 DM |
1355 | my $dump_corosync_section; |
1356 | $dump_corosync_section = sub { | |
1357 | my ($section, $prefix) = @_; | |
1d01c3f6 | 1358 | |
cafc7309 DM |
1359 | my $raw = $prefix . $section->{section} . " {\n"; |
1360 | ||
1361 | my @list = grep { defined($_->{key}) } @{$section->{children}}; | |
1362 | foreach my $child (sort {$a->{key} cmp $b->{key}} @list) { | |
1363 | $raw .= $prefix . " $child->{key}: $child->{value}\n"; | |
1d01c3f6 | 1364 | } |
cafc7309 DM |
1365 | |
1366 | @list = grep { defined($_->{section}) } @{$section->{children}}; | |
1367 | foreach my $child (sort {$a->{section} cmp $b->{section}} @list) { | |
1368 | $raw .= &$dump_corosync_section($child, "$prefix "); | |
1d01c3f6 DM |
1369 | } |
1370 | ||
cafc7309 DM |
1371 | $raw .= $prefix . "}\n\n"; |
1372 | ||
1373 | return $raw; | |
1374 | ||
1375 | }; | |
1d01c3f6 | 1376 | |
cafc7309 DM |
1377 | sub write_corosync_conf { |
1378 | my ($filename, $conf) = @_; | |
ec48ec22 | 1379 | |
cafc7309 | 1380 | my $raw = ''; |
ec48ec22 | 1381 | |
cafc7309 DM |
1382 | my $prefix = ''; |
1383 | ||
1384 | die "no main section" if $conf->{section} ne 'main'; | |
ec48ec22 | 1385 | |
cafc7309 DM |
1386 | my @list = grep { defined($_->{key}) } @{$conf->{children}}; |
1387 | foreach my $child (sort {$a->{key} cmp $b->{key}} @list) { | |
1388 | $raw .= "$child->{key}: $child->{value}\n"; | |
1d01c3f6 DM |
1389 | } |
1390 | ||
cafc7309 DM |
1391 | @list = grep { defined($_->{section}) } @{$conf->{children}}; |
1392 | foreach my $child (sort {$a->{section} cmp $b->{section}} @list) { | |
1393 | $raw .= &$dump_corosync_section($child, $prefix); | |
1394 | } | |
ec48ec22 | 1395 | |
cafc7309 | 1396 | return $raw; |
ec48ec22 DM |
1397 | } |
1398 | ||
cafc7309 DM |
1399 | sub corosync_conf_version { |
1400 | my ($conf, $noerr, $new_value) = @_; | |
1401 | ||
1402 | foreach my $child (@{$conf->{children}}) { | |
1403 | next if !defined($child->{section}); | |
1404 | if ($child->{section} eq 'totem') { | |
1405 | foreach my $e (@{$child->{children}}) { | |
1406 | next if !defined($e->{key}); | |
1407 | if ($e->{key} eq 'config_version') { | |
1408 | if ($new_value) { | |
1409 | $e->{value} = $new_value; | |
1410 | return $new_value; | |
1411 | } elsif (my $version = int($e->{value})) { | |
1412 | return $version; | |
1413 | } | |
1414 | last; | |
1415 | } | |
1416 | } | |
ec48ec22 | 1417 | } |
ec48ec22 | 1418 | } |
cafc7309 DM |
1419 | |
1420 | return undef if $noerr; | |
ec48ec22 | 1421 | |
cafc7309 | 1422 | die "invalid corosync config - unable to read version\n"; |
ec48ec22 DM |
1423 | } |
1424 | ||
cafc7309 DM |
1425 | # read only - use "rename corosync.conf.new corosync.conf" to write |
1426 | PVE::Cluster::cfs_register_file('corosync.conf', \&parse_corosync_conf); | |
ec48ec22 | 1427 | # this is read/write |
cafc7309 DM |
1428 | PVE::Cluster::cfs_register_file('corosync.conf.new', \&parse_corosync_conf, |
1429 | \&write_corosync_conf); | |
2c66fb58 | 1430 | |
ac68281b | 1431 | 1; |