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