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