]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/SectionConfig.pm
1 package PVE
::SectionConfig
;
9 use PVE
::Exception
qw(raise_param_exc);
10 use PVE
::JSONSchema
qw(get_standard_option);
27 my $type = $class->type();
28 my $pdata = $class->private();
30 die "duplicate plugin registration (type = $type)"
31 if defined($pdata->{plugins
}->{$type});
33 my $plugindata = $class->plugindata();
34 $pdata->{plugindata
}->{$type} = $plugindata;
35 $pdata->{plugins
}->{$type} = $class;
55 my ($class, $skip_type) = @_;
57 my $pdata = $class->private();
58 my $propertyList = $pdata->{propertyList
};
59 my $plugins = $pdata->{plugins
};
63 my $copy_property = sub {
67 foreach my $k (keys %$src) {
68 $res->{$k} = $src->{$k};
74 foreach my $p (keys %$propertyList) {
75 next if $skip_type && $p eq 'type';
77 if (!$propertyList->{$p}->{optional
}) {
78 $props->{$p} = $propertyList->{$p};
84 my $copts = $class->options();
85 $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional
};
87 foreach my $t (keys %$plugins) {
88 my $opts = $pdata->{options
}->{$t} || {};
89 $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional
};
93 # make a copy, because we modify the optional property
94 my $res = &$copy_property($propertyList->{$p});
98 $props->{$p} = $propertyList->{$p};
104 additionalProperties
=> 0,
105 properties
=> $props,
110 my ($class, $single_class) = @_;
112 my $pdata = $class->private();
113 my $propertyList = $pdata->{propertyList
};
114 my $plugins = $pdata->{plugins
};
118 my $filter_type = $single_class ?
$class->type() : undef;
120 foreach my $p (keys %$propertyList) {
121 next if $p eq 'type';
123 my $copts = $class->options();
125 next if defined($filter_type) && !defined($copts->{$p});
127 if (!$propertyList->{$p}->{optional
}) {
128 $props->{$p} = $propertyList->{$p};
134 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed
};
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
};
141 next if !$modifyable;
143 $props->{$p} = $propertyList->{$p};
146 $props->{digest
} = get_standard_option
('pve-config-digest');
149 type
=> 'string', format
=> 'pve-configid-list',
150 description
=> "A list of settings you want to delete.",
157 additionalProperties
=> 0,
158 properties
=> $props,
165 my $pdata = $class->private();
167 foreach my $k (qw(options plugins plugindata propertyList)) {
168 $pdata->{$k} = {} if !$pdata->{$k};
171 my $plugins = $pdata->{plugins
};
172 my $propertyList = $pdata->{propertyList
};
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};
183 $res->{optional
} = 1;
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};
192 $pdata->{options
}->{$type} = $opts;
195 $propertyList->{type
}->{type
} = 'string';
196 $propertyList->{type
}->{enum
} = [sort keys %$plugins];
200 my ($class, $type) = @_;
202 croak
"cannot lookup undefined type!" if !defined($type);
204 my $pdata = $class->private();
205 my $plugin = $pdata->{plugins
}->{$type};
207 die "unknown section type '$type'\n" if !$plugin;
215 my $pdata = $class->private();
217 return [ sort keys %{$pdata->{plugins
}} ];
221 my ($class, $type, $key, $value) = @_;
227 my ($class, $type, $key, $value) = @_;
233 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
235 my $pdata = $class->private();
237 return $value if $key eq 'type' && $type eq $value;
239 my $opts = $pdata->{options
}->{$type};
240 die "unknown section type '$type'\n" if !$opts;
242 die "unexpected property '$key'\n" if !defined($opts->{$key});
244 my $schema = $pdata->{propertyList
}->{$key};
245 die "unknown property type\n" if !$schema;
247 my $ct = $schema->{type
};
249 $value = 1 if $ct eq 'boolean' && !defined($value);
251 die "got undefined value\n" if !defined($value);
253 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
255 if (!$skipSchemaCheck) {
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";
265 if ($ct eq 'boolean' || $ct eq 'integer' || $ct eq 'number') {
266 return $value + 0; # convert to number
272 sub parse_section_header
{
273 my ($class, $line) = @_;
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);
284 sub format_section_header
{
285 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
287 return "$type: $sectionId\n";
292 my ($class, $filename, $raw, $allow_unknown) = @_;
294 my $pdata = $class->private();
299 $raw = '' if !defined($raw);
301 my $digest = Digest
::SHA
::sha1_hex
($raw);
306 my @lines = split(/\n/, $raw);
308 while (defined(my $line = shift @lines)) {
310 return $line if ($line !~ /^\s*#/);
316 my $line = $nextline->();
319 my $errprefix = "file $filename line $lineno";
321 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
331 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
334 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
336 if (!($plugin = $pdata->{plugins
}->{$type})) {
337 if ($allow_unknown) {
341 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
346 while ($line = $nextline->()) {
347 next if $skip; # skip
349 $errprefix = "file $filename line $lineno";
351 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
352 my ($k, $v) = ($1, $3);
355 die "duplicate attribute\n" if defined($config->{$k});
357 $v = $plugin->check_value($type, $k, $v, $sectionId);
362 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $err";
364 context
=> $errprefix,
365 section
=> $sectionId,
372 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
377 $config->{type
} = $type;
378 $ids->{$sectionId} = $config;
379 $order->{$sectionId} = $pri++;
380 } elsif (!$skip && $type && $plugin && $config) {
381 $config->{type
} = $type;
383 $config = eval { $config = $plugin->check_config($sectionId, $config, 1, 1); };
384 warn "$errprefix (skip section '$sectionId'): $@" if $@;
386 $ids->{$sectionId} = $config;
387 $order->{$sectionId} = $pri++;
391 warn "$errprefix - ignore config line: $line\n";
400 $cfg->{errors
} = $errors if scalar(@$errors) > 0;
406 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
408 my $type = $class->type();
409 my $pdata = $class->private();
410 my $opts = $pdata->{options
}->{$type};
412 my $settings = { type
=> $type };
414 foreach my $k (keys %$config) {
415 my $value = $config->{$k};
417 die "can't change value of fixed parameter '$k'\n"
418 if !$create && defined($opts->{$k}) && $opts->{$k}->{fixed
};
420 if (defined($value)) {
421 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
422 $settings->{$k} = $class->decode_value($type, $k, $tmp);
424 die "got undefined value for option '$k'\n";
429 # check if we have a value for all required options
430 foreach my $k (keys %$opts) {
431 next if $opts->{$k}->{optional
};
432 die "missing value for required option '$k'\n"
433 if !defined($config->{$k});
440 my $format_config_line = sub {
441 my ($schema, $key, $value) = @_;
443 my $ct = $schema->{type
};
445 die "property '$key' contains a line feed\n"
446 if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
448 if ($ct eq 'boolean') {
449 return "\t$key " . ($value ?
1 : 0) . "\n"
452 return "\t$key $value\n" if "$value" ne '';
457 my ($class, $filename, $cfg, $allow_unknown) = @_;
459 my $pdata = $class->private();
460 my $propertyList = $pdata->{propertyList
};
464 my $ids = $cfg->{ids
};
465 my $order = $cfg->{order
};
468 foreach my $sectionId (keys %$ids) {
469 my $pri = $order->{$sectionId};
470 $maxpri = $pri if $pri && $pri > $maxpri;
472 foreach my $sectionId (keys %$ids) {
473 if (!defined ($order->{$sectionId})) {
474 $order->{$sectionId} = ++$maxpri;
478 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
479 my $scfg = $ids->{$sectionId};
480 my $type = $scfg->{type
};
481 my $opts = $pdata->{options
}->{$type};
483 die "unknown section type '$type'\n" if !$opts && !$allow_unknown;
487 my $data = $class->format_section_header($type, $sectionId, $scfg, $done_hash);
489 if (!$opts && $allow_unknown) {
490 $done_hash->{type
} = 1;
491 my @first = exists($scfg->{comment
}) ?
('comment') : ();
492 for my $k (@first, sort keys %$scfg) {
493 next if defined($done_hash->{$k});
494 $done_hash->{$k} = 1;
496 $data .= "\t$k $v\n";
503 if ($scfg->{comment
} && !$done_hash->{comment
}) {
505 my $v = $class->encode_value($type, $k, $scfg->{$k});
506 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
509 $data .= "\tdisable\n" if $scfg->{disable
} && !$done_hash->{disable
};
511 $done_hash->{comment
} = 1;
512 $done_hash->{disable
} = 1;
514 my @option_keys = sort keys %$opts;
515 foreach my $k (@option_keys) {
516 next if defined($done_hash->{$k});
517 next if $opts->{$k}->{optional
};
518 $done_hash->{$k} = 1;
520 die "section '$sectionId' - missing value for required option '$k'\n"
522 $v = $class->encode_value($type, $k, $v);
523 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
526 foreach my $k (@option_keys) {
527 next if defined($done_hash->{$k});
529 next if !defined($v);
530 $v = $class->encode_value($type, $k, $v);
531 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
540 sub assert_if_modified
{
541 my ($cfg, $digest) = @_;
543 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);
546 sub delete_from_config
{
547 my ($config, $option_schema, $new_options, $to_delete) = @_;
549 for my $k ($to_delete->@*) {
550 my $d = $option_schema->{$k} || die "no such option '$k'\n";
551 die "unable to delete required option '$k'\n" if !$d->{optional
};
552 die "unable to delete fixed option '$k'\n" if $d->{fixed
};
553 die "cannot set and delete property '$k' at the same time!\n"
554 if defined($new_options->{$k});
555 delete $config->{$k};