]> git.proxmox.com Git - pve-common.git/blob - src/PVE/SectionConfig.pm
SectionConfig: parse_config: add errors to result
[pve-common.git] / src / PVE / SectionConfig.pm
1 package PVE::SectionConfig;
2
3 use strict;
4 use warnings;
5
6 use Carp;
7 use Digest::SHA;
8
9 use PVE::Exception qw(raise_param_exc);
10 use PVE::JSONSchema qw(get_standard_option);
11
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
30 die "duplicate plugin registration (type = $type)"
31 if defined($pdata->{plugins}->{$type});
32
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 {};
48 }
49
50 sub plugindata {
51 return {};
52 }
53
54 sub createSchema {
55 my ($class, $skip_type) = @_;
56
57 my $pdata = $class->private();
58 my $propertyList = $pdata->{propertyList};
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) {
75 next if $skip_type && $p eq 'type';
76
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 }
101
102 return {
103 type => "object",
104 additionalProperties => 0,
105 properties => $props,
106 };
107 }
108
109 sub updateSchema {
110 my ($class, $single_class) = @_;
111
112 my $pdata = $class->private();
113 my $propertyList = $pdata->{propertyList};
114 my $plugins = $pdata->{plugins};
115
116 my $props = {};
117
118 my $filter_type = $class->type() if $single_class;
119
120 foreach my $p (keys %$propertyList) {
121 next if $p eq 'type';
122
123 my $copts = $class->options();
124
125 next if defined($filter_type) && !defined($copts->{$p});
126
127 if (!$propertyList->{$p}->{optional}) {
128 $props->{$p} = $propertyList->{$p};
129 next;
130 }
131
132 my $modifyable = 0;
133
134 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed};
135
136 foreach my $t (keys %$plugins) {
137 my $opts = $pdata->{options}->{$t} || {};
138 next if !defined($opts->{$p});
139 $modifyable = 1 if !$opts->{$p}->{fixed};
140 }
141 next if !$modifyable;
142
143 $props->{$p} = $propertyList->{$p};
144 }
145
146 $props->{digest} = get_standard_option('pve-config-digest');
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) {
177 die "duplicate property '$p'" if defined($propertyList->{$p});
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';
196 $propertyList->{type}->{enum} = [sort keys %$plugins];
197 }
198
199 sub lookup {
200 my ($class, $type) = @_;
201
202 croak "cannot lookup undefined type!" if !defined($type);
203
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();
216
217 return [ sort keys %{$pdata->{plugins}} ];
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};
240 die "unknown section type '$type'\n" if !$opts;
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 = {};
257 PVE::JSONSchema::check_prop($value, $schema, '', $errors);
258 if (scalar(keys %$errors)) {
259 die "$errors->{$key}\n" if $errors->{$key};
260 die "$errors->{_root}\n" if $errors->{_root};
261 die "unknown error\n";
262 }
263 }
264
265 if ($ct eq 'boolean' || $ct eq 'integer' || $ct eq 'number') {
266 return $value + 0; # convert to number
267 }
268
269 return $value;
270 }
271
272 sub parse_section_header {
273 my ($class, $line) = @_;
274
275 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
276 my ($type, $sectionId) = ($1, $2);
277 my $errmsg = undef; # set if you want to skip whole section
278 my $config = {}; # to return additional attributes
279 return ($type, $sectionId, $errmsg, $config);
280 }
281 return undef;
282 }
283
284 sub format_section_header {
285 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
286
287 return "$type: $sectionId\n";
288 }
289
290
291 sub parse_config {
292 my ($class, $filename, $raw) = @_;
293
294 my $pdata = $class->private();
295
296 my $ids = {};
297 my $order = {};
298
299 $raw = '' if !defined($raw);
300
301 my $digest = Digest::SHA::sha1_hex($raw);
302
303 my $pri = 1;
304
305 my $lineno = 0;
306 my @lines = split(/\n/, $raw);
307 my $nextline = sub {
308 while (defined(my $line = shift @lines)) {
309 $lineno++;
310 return $line if ($line !~ /^\s*#/);
311 }
312 };
313
314 my $errors = [];
315 while (@lines) {
316 my $line = $nextline->();
317 next if !$line;
318
319 my $errprefix = "file $filename line $lineno";
320
321 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
322 if ($config) {
323 my $ignore = 0;
324
325 my $plugin;
326
327 if ($errmsg) {
328 $ignore = 1;
329 chomp $errmsg;
330 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
331 } elsif (!$type) {
332 $ignore = 1;
333 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
334 } else {
335 if (!($plugin = $pdata->{plugins}->{$type})) {
336 $ignore = 1;
337 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
338 }
339 }
340
341 while ($line = $nextline->()) {
342 next if $ignore; # skip
343
344 $errprefix = "file $filename line $lineno";
345
346 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
347 my ($k, $v) = ($1, $3);
348
349 eval {
350 die "duplicate attribute\n" if defined($config->{$k});
351 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
352 };
353 if (my $err = $@) {
354 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $err";
355 push @$errors, {
356 context => $errprefix,
357 section => $sectionId,
358 key => $k,
359 err => $err,
360 };
361 }
362
363 } else {
364 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
365 }
366 }
367
368 if (!$ignore && $type && $plugin && $config) {
369 $config->{type} = $type;
370 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
371 warn "$errprefix (skip section '$sectionId'): $@" if $@;
372 $order->{$sectionId} = $pri++;
373 }
374
375 } else {
376 warn "$errprefix - ignore config line: $line\n";
377 }
378 }
379
380 my $cfg = {
381 ids => $ids,
382 order => $order,
383 digest => $digest
384 };
385 $cfg->{errors} = $errors if scalar(@$errors) > 0;
386
387 return $cfg;
388 }
389
390 sub check_config {
391 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
392
393 my $type = $class->type();
394 my $pdata = $class->private();
395 my $opts = $pdata->{options}->{$type};
396
397 my $settings = { type => $type };
398
399 foreach my $k (keys %$config) {
400 my $value = $config->{$k};
401
402 die "can't change value of fixed parameter '$k'\n"
403 if !$create && defined($opts->{$k}) && $opts->{$k}->{fixed};
404
405 if (defined($value)) {
406 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
407 $settings->{$k} = $class->decode_value($type, $k, $tmp);
408 } else {
409 die "got undefined value for option '$k'\n";
410 }
411 }
412
413 if ($create) {
414 # check if we have a value for all required options
415 foreach my $k (keys %$opts) {
416 next if $opts->{$k}->{optional};
417 die "missing value for required option '$k'\n"
418 if !defined($config->{$k});
419 }
420 }
421
422 return $settings;
423 }
424
425 my $format_config_line = sub {
426 my ($schema, $key, $value) = @_;
427
428 my $ct = $schema->{type};
429
430 die "property '$key' contains a line feed\n"
431 if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
432
433 if ($ct eq 'boolean') {
434 return "\t$key " . ($value ? 1 : 0) . "\n"
435 if defined($value);
436 } else {
437 return "\t$key $value\n" if "$value" ne '';
438 }
439 };
440
441 sub write_config {
442 my ($class, $filename, $cfg) = @_;
443
444 my $pdata = $class->private();
445 my $propertyList = $pdata->{propertyList};
446
447 my $out = '';
448
449 my $ids = $cfg->{ids};
450 my $order = $cfg->{order};
451
452 my $maxpri = 0;
453 foreach my $sectionId (keys %$ids) {
454 my $pri = $order->{$sectionId};
455 $maxpri = $pri if $pri && $pri > $maxpri;
456 }
457 foreach my $sectionId (keys %$ids) {
458 if (!defined ($order->{$sectionId})) {
459 $order->{$sectionId} = ++$maxpri;
460 }
461 }
462
463 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
464 my $scfg = $ids->{$sectionId};
465 my $type = $scfg->{type};
466 my $opts = $pdata->{options}->{$type};
467
468 die "unknown section type '$type'\n" if !$opts;
469
470 my $done_hash = {};
471
472 my $data = $class->format_section_header($type, $sectionId, $scfg, $done_hash);
473 if ($scfg->{comment} && !$done_hash->{comment}) {
474 my $k = 'comment';
475 my $v = $class->encode_value($type, $k, $scfg->{$k});
476 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
477 }
478
479 $data .= "\tdisable\n" if $scfg->{disable} && !$done_hash->{disable};
480
481 $done_hash->{comment} = 1;
482 $done_hash->{disable} = 1;
483
484 my @option_keys = sort keys %$opts;
485 foreach my $k (@option_keys) {
486 next if defined($done_hash->{$k});
487 next if $opts->{$k}->{optional};
488 $done_hash->{$k} = 1;
489 my $v = $scfg->{$k};
490 die "section '$sectionId' - missing value for required option '$k'\n"
491 if !defined ($v);
492 $v = $class->encode_value($type, $k, $v);
493 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
494 }
495
496 foreach my $k (@option_keys) {
497 next if defined($done_hash->{$k});
498 my $v = $scfg->{$k};
499 next if !defined($v);
500 $v = $class->encode_value($type, $k, $v);
501 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
502 }
503
504 $out .= "$data\n";
505 }
506
507 return $out;
508 }
509
510 sub assert_if_modified {
511 my ($cfg, $digest) = @_;
512
513 PVE::Tools::assert_if_modified($cfg->{digest}, $digest);
514 }
515
516 1;