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