]>
Commit | Line | Data |
---|---|---|
212b08e8 DM |
1 | package PVE::SectionConfig; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
848231af | 5 | |
7db4e5d7 | 6 | use Carp; |
212b08e8 | 7 | use Digest::SHA; |
848231af | 8 | |
212b08e8 DM |
9 | use PVE::Exception qw(raise_param_exc); |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | ||
212b08e8 DM |
12 | my $defaultData = { |
13 | options => {}, | |
14 | plugins => {}, | |
15 | plugindata => {}, | |
16 | propertyList => {}, | |
17 | }; | |
18 | ||
19 | sub private { | |
20 | die "overwrite me"; | |
21 | return $defaultData; | |
22 | } | |
23 | ||
24 | sub register { | |
25 | my ($class) = @_; | |
26 | ||
27 | my $type = $class->type(); | |
28 | my $pdata = $class->private(); | |
29 | ||
ebd2b0ac DM |
30 | die "duplicate plugin registration (type = $type)" |
31 | if defined($pdata->{plugins}->{$type}); | |
32 | ||
212b08e8 DM |
33 | my $plugindata = $class->plugindata(); |
34 | $pdata->{plugindata}->{$type} = $plugindata; | |
35 | $pdata->{plugins}->{$type} = $class; | |
36 | } | |
37 | ||
38 | sub type { | |
39 | die "overwrite me"; | |
40 | } | |
41 | ||
42 | sub properties { | |
43 | return {}; | |
44 | } | |
45 | ||
46 | sub options { | |
47 | return {}; | |
cca9c864 | 48 | } |
212b08e8 DM |
49 | |
50 | sub plugindata { | |
51 | return {}; | |
cca9c864 | 52 | } |
212b08e8 DM |
53 | |
54 | sub createSchema { | |
9c2e4034 | 55 | my ($class, $skip_type) = @_; |
212b08e8 DM |
56 | |
57 | my $pdata = $class->private(); | |
58 | my $propertyList = $pdata->{propertyList}; | |
a3530f55 DM |
59 | my $plugins = $pdata->{plugins}; |
60 | ||
61 | my $props = {}; | |
62 | ||
63 | my $copy_property = sub { | |
64 | my ($src) = @_; | |
65 | ||
66 | my $res = {}; | |
67 | foreach my $k (keys %$src) { | |
68 | $res->{$k} = $src->{$k}; | |
69 | } | |
70 | ||
71 | return $res; | |
72 | }; | |
73 | ||
74 | foreach my $p (keys %$propertyList) { | |
9c2e4034 DM |
75 | next if $skip_type && $p eq 'type'; |
76 | ||
a3530f55 DM |
77 | if (!$propertyList->{$p}->{optional}) { |
78 | $props->{$p} = $propertyList->{$p}; | |
79 | next; | |
80 | } | |
81 | ||
82 | my $required = 1; | |
83 | ||
84 | my $copts = $class->options(); | |
85 | $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional}; | |
86 | ||
87 | foreach my $t (keys %$plugins) { | |
88 | my $opts = $pdata->{options}->{$t} || {}; | |
89 | $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional}; | |
90 | } | |
91 | ||
92 | if ($required) { | |
93 | # make a copy, because we modify the optional property | |
94 | my $res = &$copy_property($propertyList->{$p}); | |
95 | $res->{optional} = 0; | |
96 | $props->{$p} = $res; | |
97 | } else { | |
98 | $props->{$p} = $propertyList->{$p}; | |
99 | } | |
100 | } | |
212b08e8 DM |
101 | |
102 | return { | |
103 | type => "object", | |
104 | additionalProperties => 0, | |
a3530f55 | 105 | properties => $props, |
212b08e8 DM |
106 | }; |
107 | } | |
108 | ||
109 | sub updateSchema { | |
885641fb | 110 | my ($class, $single_class) = @_; |
212b08e8 DM |
111 | |
112 | my $pdata = $class->private(); | |
113 | my $propertyList = $pdata->{propertyList}; | |
114 | my $plugins = $pdata->{plugins}; | |
115 | ||
116 | my $props = {}; | |
117 | ||
967e9823 | 118 | my $filter_type = $single_class ? $class->type() : undef; |
885641fb | 119 | |
212b08e8 DM |
120 | foreach my $p (keys %$propertyList) { |
121 | next if $p eq 'type'; | |
885641fb DM |
122 | |
123 | my $copts = $class->options(); | |
124 | ||
125 | next if defined($filter_type) && !defined($copts->{$p}); | |
126 | ||
212b08e8 DM |
127 | if (!$propertyList->{$p}->{optional}) { |
128 | $props->{$p} = $propertyList->{$p}; | |
129 | next; | |
130 | } | |
516dfb55 DM |
131 | |
132 | my $modifyable = 0; | |
133 | ||
0d838035 DM |
134 | $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed}; |
135 | ||
212b08e8 | 136 | foreach my $t (keys %$plugins) { |
516dfb55 | 137 | my $opts = $pdata->{options}->{$t} || {}; |
212b08e8 | 138 | next if !defined($opts->{$p}); |
516dfb55 | 139 | $modifyable = 1 if !$opts->{$p}->{fixed}; |
212b08e8 | 140 | } |
516dfb55 DM |
141 | next if !$modifyable; |
142 | ||
143 | $props->{$p} = $propertyList->{$p}; | |
212b08e8 DM |
144 | } |
145 | ||
dc5eae7d | 146 | $props->{digest} = get_standard_option('pve-config-digest'); |
212b08e8 DM |
147 | |
148 | $props->{delete} = { | |
149 | type => 'string', format => 'pve-configid-list', | |
150 | description => "A list of settings you want to delete.", | |
151 | maxLength => 4096, | |
152 | optional => 1, | |
153 | }; | |
154 | ||
155 | return { | |
156 | type => "object", | |
157 | additionalProperties => 0, | |
158 | properties => $props, | |
159 | }; | |
160 | } | |
161 | ||
162 | sub init { | |
163 | my ($class) = @_; | |
164 | ||
165 | my $pdata = $class->private(); | |
166 | ||
167 | foreach my $k (qw(options plugins plugindata propertyList)) { | |
168 | $pdata->{$k} = {} if !$pdata->{$k}; | |
169 | } | |
170 | ||
171 | my $plugins = $pdata->{plugins}; | |
172 | my $propertyList = $pdata->{propertyList}; | |
173 | ||
174 | foreach my $type (keys %$plugins) { | |
175 | my $props = $plugins->{$type}->properties(); | |
176 | foreach my $p (keys %$props) { | |
b732bea7 | 177 | die "duplicate property '$p'" if defined($propertyList->{$p}); |
212b08e8 DM |
178 | my $res = $propertyList->{$p} = {}; |
179 | my $data = $props->{$p}; | |
180 | for my $a (keys %$data) { | |
181 | $res->{$a} = $data->{$a}; | |
182 | } | |
183 | $res->{optional} = 1; | |
184 | } | |
185 | } | |
186 | ||
187 | foreach my $type (keys %$plugins) { | |
188 | my $opts = $plugins->{$type}->options(); | |
189 | foreach my $p (keys %$opts) { | |
190 | die "undefined property '$p'" if !$propertyList->{$p}; | |
191 | } | |
192 | $pdata->{options}->{$type} = $opts; | |
193 | } | |
194 | ||
195 | $propertyList->{type}->{type} = 'string'; | |
08aa41d7 | 196 | $propertyList->{type}->{enum} = [sort keys %$plugins]; |
212b08e8 DM |
197 | } |
198 | ||
199 | sub lookup { | |
200 | my ($class, $type) = @_; | |
201 | ||
7db4e5d7 TL |
202 | croak "cannot lookup undefined type!" if !defined($type); |
203 | ||
212b08e8 DM |
204 | my $pdata = $class->private(); |
205 | my $plugin = $pdata->{plugins}->{$type}; | |
206 | ||
207 | die "unknown section type '$type'\n" if !$plugin; | |
208 | ||
209 | return $plugin; | |
210 | } | |
211 | ||
212 | sub lookup_types { | |
213 | my ($class) = @_; | |
214 | ||
215 | my $pdata = $class->private(); | |
cca9c864 | 216 | |
a7f30ebf | 217 | return [ sort keys %{$pdata->{plugins}} ]; |
212b08e8 DM |
218 | } |
219 | ||
220 | sub decode_value { | |
221 | my ($class, $type, $key, $value) = @_; | |
222 | ||
223 | return $value; | |
224 | } | |
225 | ||
226 | sub encode_value { | |
227 | my ($class, $type, $key, $value) = @_; | |
228 | ||
229 | return $value; | |
230 | } | |
231 | ||
232 | sub check_value { | |
233 | my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_; | |
234 | ||
235 | my $pdata = $class->private(); | |
236 | ||
237 | return $value if $key eq 'type' && $type eq $value; | |
238 | ||
239 | my $opts = $pdata->{options}->{$type}; | |
cca9c864 | 240 | die "unknown section type '$type'\n" if !$opts; |
212b08e8 DM |
241 | |
242 | die "unexpected property '$key'\n" if !defined($opts->{$key}); | |
243 | ||
244 | my $schema = $pdata->{propertyList}->{$key}; | |
245 | die "unknown property type\n" if !$schema; | |
246 | ||
247 | my $ct = $schema->{type}; | |
248 | ||
249 | $value = 1 if $ct eq 'boolean' && !defined($value); | |
250 | ||
251 | die "got undefined value\n" if !defined($value); | |
252 | ||
253 | die "property contains a line feed\n" if $value =~ m/[\n\r]/; | |
254 | ||
255 | if (!$skipSchemaCheck) { | |
256 | my $errors = {}; | |
69d9edcc DC |
257 | |
258 | my $checkschema = $schema; | |
259 | ||
260 | if ($ct eq 'array') { | |
261 | die "no item schema for array" if !defined($schema->{items}); | |
262 | $checkschema = $schema->{items}; | |
263 | } | |
264 | ||
265 | PVE::JSONSchema::check_prop($value, $checkschema, '', $errors); | |
212b08e8 DM |
266 | if (scalar(keys %$errors)) { |
267 | die "$errors->{$key}\n" if $errors->{$key}; | |
268 | die "$errors->{_root}\n" if $errors->{_root}; | |
269 | die "unknown error\n"; | |
270 | } | |
271 | } | |
272 | ||
bbca3bdc DM |
273 | if ($ct eq 'boolean' || $ct eq 'integer' || $ct eq 'number') { |
274 | return $value + 0; # convert to number | |
275 | } | |
276 | ||
212b08e8 DM |
277 | return $value; |
278 | } | |
279 | ||
280 | sub parse_section_header { | |
281 | my ($class, $line) = @_; | |
282 | ||
283 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
284 | my ($type, $sectionId) = ($1, $2); | |
285 | my $errmsg = undef; # set if you want to skip whole section | |
286 | my $config = {}; # to return additional attributes | |
287 | return ($type, $sectionId, $errmsg, $config); | |
288 | } | |
289 | return undef; | |
290 | } | |
291 | ||
292 | sub format_section_header { | |
2d63b598 | 293 | my ($class, $type, $sectionId, $scfg, $done_hash) = @_; |
212b08e8 DM |
294 | |
295 | return "$type: $sectionId\n"; | |
296 | } | |
297 | ||
298 | ||
299 | sub parse_config { | |
c9ede1c3 | 300 | my ($class, $filename, $raw, $allow_unknown) = @_; |
212b08e8 DM |
301 | |
302 | my $pdata = $class->private(); | |
303 | ||
304 | my $ids = {}; | |
305 | my $order = {}; | |
306 | ||
3c4d612a WB |
307 | $raw = '' if !defined($raw); |
308 | ||
309 | my $digest = Digest::SHA::sha1_hex($raw); | |
cca9c864 | 310 | |
212b08e8 DM |
311 | my $pri = 1; |
312 | ||
313 | my $lineno = 0; | |
3c4d612a WB |
314 | my @lines = split(/\n/, $raw); |
315 | my $nextline = sub { | |
e1fbb779 | 316 | while (defined(my $line = shift @lines)) { |
3c4d612a | 317 | $lineno++; |
e1fbb779 | 318 | return $line if ($line !~ /^\s*#/); |
3c4d612a WB |
319 | } |
320 | }; | |
212b08e8 | 321 | |
69d9edcc DC |
322 | my $is_array = sub { |
323 | my ($type, $key) = @_; | |
324 | ||
325 | my $schema = $pdata->{propertyList}->{$key}; | |
326 | die "unknown property type\n" if !$schema; | |
327 | ||
328 | return $schema->{type} eq 'array'; | |
329 | }; | |
330 | ||
4b32ef6e | 331 | my $errors = []; |
e1fbb779 FE |
332 | while (@lines) { |
333 | my $line = $nextline->(); | |
334 | next if !$line; | |
335 | ||
212b08e8 DM |
336 | my $errprefix = "file $filename line $lineno"; |
337 | ||
338 | my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line); | |
339 | if ($config) { | |
c9ede1c3 WB |
340 | my $skip = 0; |
341 | my $unknown = 0; | |
212b08e8 DM |
342 | |
343 | my $plugin; | |
344 | ||
345 | if ($errmsg) { | |
c9ede1c3 | 346 | $skip = 1; |
212b08e8 DM |
347 | chomp $errmsg; |
348 | warn "$errprefix (skip section '$sectionId'): $errmsg\n"; | |
349 | } elsif (!$type) { | |
c9ede1c3 | 350 | $skip = 1; |
212b08e8 DM |
351 | warn "$errprefix (skip section '$sectionId'): missing type - internal error\n"; |
352 | } else { | |
353 | if (!($plugin = $pdata->{plugins}->{$type})) { | |
c9ede1c3 WB |
354 | if ($allow_unknown) { |
355 | $unknown = 1; | |
356 | } else { | |
357 | $skip = 1; | |
358 | warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n"; | |
359 | } | |
212b08e8 DM |
360 | } |
361 | } | |
362 | ||
9e594bd4 | 363 | while ($line = $nextline->()) { |
c9ede1c3 | 364 | next if $skip; # skip |
212b08e8 DM |
365 | |
366 | $errprefix = "file $filename line $lineno"; | |
367 | ||
368 | if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) { | |
369 | my ($k, $v) = ($1, $3); | |
cca9c864 | 370 | |
212b08e8 | 371 | eval { |
69d9edcc DC |
372 | if ($is_array->($type, $k)) { |
373 | if (!$unknown) { | |
374 | $v = $plugin->check_value($type, $k, $v, $sectionId); | |
375 | } | |
376 | $config->{$k} = [] if !defined($config->{$k}); | |
377 | push $config->{$k}->@*, $v; | |
378 | } else { | |
379 | die "duplicate attribute\n" if defined($config->{$k}); | |
380 | if (!$unknown) { | |
381 | $v = $plugin->check_value($type, $k, $v, $sectionId); | |
382 | } | |
383 | $config->{$k} = $v; | |
c9ede1c3 | 384 | } |
212b08e8 | 385 | }; |
4b32ef6e FE |
386 | if (my $err = $@) { |
387 | warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $err"; | |
388 | push @$errors, { | |
389 | context => $errprefix, | |
390 | section => $sectionId, | |
391 | key => $k, | |
392 | err => $err, | |
393 | }; | |
394 | } | |
212b08e8 DM |
395 | |
396 | } else { | |
397 | warn "$errprefix (section '$sectionId') - ignore config line: $line\n"; | |
398 | } | |
399 | } | |
400 | ||
c9ede1c3 | 401 | if ($unknown) { |
212b08e8 | 402 | $config->{type} = $type; |
c9ede1c3 WB |
403 | $ids->{$sectionId} = $config; |
404 | $order->{$sectionId} = $pri++; | |
405 | } elsif (!$skip && $type && $plugin && $config) { | |
406 | $config->{type} = $type; | |
407 | if (!$unknown) { | |
408 | $config = eval { $config = $plugin->check_config($sectionId, $config, 1, 1); }; | |
409 | warn "$errprefix (skip section '$sectionId'): $@" if $@; | |
410 | } | |
411 | $ids->{$sectionId} = $config; | |
212b08e8 DM |
412 | $order->{$sectionId} = $pri++; |
413 | } | |
414 | ||
415 | } else { | |
416 | warn "$errprefix - ignore config line: $line\n"; | |
417 | } | |
418 | } | |
419 | ||
4b32ef6e FE |
420 | my $cfg = { |
421 | ids => $ids, | |
422 | order => $order, | |
423 | digest => $digest | |
424 | }; | |
425 | $cfg->{errors} = $errors if scalar(@$errors) > 0; | |
212b08e8 DM |
426 | |
427 | return $cfg; | |
428 | } | |
429 | ||
430 | sub check_config { | |
431 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
432 | ||
433 | my $type = $class->type(); | |
434 | my $pdata = $class->private(); | |
435 | my $opts = $pdata->{options}->{$type}; | |
436 | ||
437 | my $settings = { type => $type }; | |
438 | ||
439 | foreach my $k (keys %$config) { | |
440 | my $value = $config->{$k}; | |
d0ba18e1 | 441 | |
212b08e8 | 442 | die "can't change value of fixed parameter '$k'\n" |
95244fd7 | 443 | if !$create && defined($opts->{$k}) && $opts->{$k}->{fixed}; |
d0ba18e1 | 444 | |
212b08e8 DM |
445 | if (defined($value)) { |
446 | my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck); | |
447 | $settings->{$k} = $class->decode_value($type, $k, $tmp); | |
448 | } else { | |
449 | die "got undefined value for option '$k'\n"; | |
450 | } | |
451 | } | |
452 | ||
453 | if ($create) { | |
454 | # check if we have a value for all required options | |
455 | foreach my $k (keys %$opts) { | |
456 | next if $opts->{$k}->{optional}; | |
457 | die "missing value for required option '$k'\n" | |
458 | if !defined($config->{$k}); | |
459 | } | |
460 | } | |
461 | ||
462 | return $settings; | |
463 | } | |
464 | ||
465 | my $format_config_line = sub { | |
466 | my ($schema, $key, $value) = @_; | |
467 | ||
468 | my $ct = $schema->{type}; | |
469 | ||
0cb4d663 DM |
470 | die "property '$key' contains a line feed\n" |
471 | if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/); | |
472 | ||
212b08e8 | 473 | if ($ct eq 'boolean') { |
d152f8b0 WB |
474 | return "\t$key " . ($value ? 1 : 0) . "\n" |
475 | if defined($value); | |
69d9edcc DC |
476 | } elsif ($ct eq 'array') { |
477 | die "property '$key' is not an array" if ref($value) ne 'ARRAY'; | |
478 | my $result = ''; | |
479 | for my $line ($value->@*) { | |
480 | $result .= "\t$key $line\n" if $value ne ''; | |
481 | } | |
482 | return $result; | |
212b08e8 DM |
483 | } else { |
484 | return "\t$key $value\n" if "$value" ne ''; | |
485 | } | |
486 | }; | |
487 | ||
488 | sub write_config { | |
c9ede1c3 | 489 | my ($class, $filename, $cfg, $allow_unknown) = @_; |
212b08e8 DM |
490 | |
491 | my $pdata = $class->private(); | |
492 | my $propertyList = $pdata->{propertyList}; | |
493 | ||
494 | my $out = ''; | |
495 | ||
496 | my $ids = $cfg->{ids}; | |
497 | my $order = $cfg->{order}; | |
498 | ||
499 | my $maxpri = 0; | |
500 | foreach my $sectionId (keys %$ids) { | |
cca9c864 | 501 | my $pri = $order->{$sectionId}; |
212b08e8 DM |
502 | $maxpri = $pri if $pri && $pri > $maxpri; |
503 | } | |
504 | foreach my $sectionId (keys %$ids) { | |
505 | if (!defined ($order->{$sectionId})) { | |
506 | $order->{$sectionId} = ++$maxpri; | |
cca9c864 | 507 | } |
212b08e8 DM |
508 | } |
509 | ||
510 | foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) { | |
511 | my $scfg = $ids->{$sectionId}; | |
512 | my $type = $scfg->{type}; | |
513 | my $opts = $pdata->{options}->{$type}; | |
514 | ||
c9ede1c3 | 515 | die "unknown section type '$type'\n" if !$opts && !$allow_unknown; |
212b08e8 | 516 | |
2d63b598 DM |
517 | my $done_hash = {}; |
518 | ||
519 | my $data = $class->format_section_header($type, $sectionId, $scfg, $done_hash); | |
c9ede1c3 WB |
520 | |
521 | if (!$opts && $allow_unknown) { | |
522 | $done_hash->{type} = 1; | |
523 | my @first = exists($scfg->{comment}) ? ('comment') : (); | |
524 | for my $k (@first, sort keys %$scfg) { | |
525 | next if defined($done_hash->{$k}); | |
526 | $done_hash->{$k} = 1; | |
527 | my $v = $scfg->{$k}; | |
528 | $data .= "\t$k $v\n"; | |
529 | } | |
530 | $out .= "$data\n"; | |
531 | next; | |
532 | } | |
533 | ||
534 | ||
2d63b598 | 535 | if ($scfg->{comment} && !$done_hash->{comment}) { |
212b08e8 DM |
536 | my $k = 'comment'; |
537 | my $v = $class->encode_value($type, $k, $scfg->{$k}); | |
538 | $data .= &$format_config_line($propertyList->{$k}, $k, $v); | |
539 | } | |
540 | ||
2d63b598 | 541 | $data .= "\tdisable\n" if $scfg->{disable} && !$done_hash->{disable}; |
212b08e8 | 542 | |
2d63b598 DM |
543 | $done_hash->{comment} = 1; |
544 | $done_hash->{disable} = 1; | |
212b08e8 | 545 | |
df89e5eb DM |
546 | my @option_keys = sort keys %$opts; |
547 | foreach my $k (@option_keys) { | |
2d63b598 | 548 | next if defined($done_hash->{$k}); |
212b08e8 DM |
549 | next if $opts->{$k}->{optional}; |
550 | $done_hash->{$k} = 1; | |
551 | my $v = $scfg->{$k}; | |
552 | die "section '$sectionId' - missing value for required option '$k'\n" | |
553 | if !defined ($v); | |
554 | $v = $class->encode_value($type, $k, $v); | |
555 | $data .= &$format_config_line($propertyList->{$k}, $k, $v); | |
556 | } | |
557 | ||
df89e5eb | 558 | foreach my $k (@option_keys) { |
212b08e8 DM |
559 | next if defined($done_hash->{$k}); |
560 | my $v = $scfg->{$k}; | |
561 | next if !defined($v); | |
562 | $v = $class->encode_value($type, $k, $v); | |
563 | $data .= &$format_config_line($propertyList->{$k}, $k, $v); | |
564 | } | |
565 | ||
566 | $out .= "$data\n"; | |
567 | } | |
568 | ||
569 | return $out; | |
570 | } | |
571 | ||
572 | sub assert_if_modified { | |
573 | my ($cfg, $digest) = @_; | |
574 | ||
a345b9d5 | 575 | PVE::Tools::assert_if_modified($cfg->{digest}, $digest); |
212b08e8 DM |
576 | } |
577 | ||
5028848d DC |
578 | sub delete_from_config { |
579 | my ($config, $option_schema, $new_options, $to_delete) = @_; | |
580 | ||
581 | for my $k ($to_delete->@*) { | |
582 | my $d = $option_schema->{$k} || die "no such option '$k'\n"; | |
583 | die "unable to delete required option '$k'\n" if !$d->{optional}; | |
584 | die "unable to delete fixed option '$k'\n" if $d->{fixed}; | |
585 | die "cannot set and delete property '$k' at the same time!\n" | |
586 | if defined($new_options->{$k}); | |
587 | delete $config->{$k}; | |
588 | } | |
589 | ||
590 | return $config; | |
591 | } | |
592 | ||
212b08e8 | 593 | 1; |