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