]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/SectionConfig.pm
1 package PVE
::SectionConfig
;
6 use PVE
::Exception
qw(raise_param_exc);
7 use PVE
::JSONSchema
qw(get_standard_option);
26 my $type = $class->type();
27 my $pdata = $class->private();
29 die "duplicate plugin registration (type = $type)"
30 if defined($pdata->{plugins
}->{$type});
32 my $plugindata = $class->plugindata();
33 $pdata->{plugindata
}->{$type} = $plugindata;
34 $pdata->{plugins
}->{$type} = $class;
56 my $pdata = $class->private();
57 my $propertyList = $pdata->{propertyList
};
58 my $plugins = $pdata->{plugins
};
62 my $copy_property = sub {
66 foreach my $k (keys %$src) {
67 $res->{$k} = $src->{$k};
73 foreach my $p (keys %$propertyList) {
74 if (!$propertyList->{$p}->{optional
}) {
75 $props->{$p} = $propertyList->{$p};
81 my $copts = $class->options();
82 $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional
};
84 foreach my $t (keys %$plugins) {
85 my $opts = $pdata->{options
}->{$t} || {};
86 $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional
};
90 # make a copy, because we modify the optional property
91 my $res = &$copy_property($propertyList->{$p});
95 $props->{$p} = $propertyList->{$p};
101 additionalProperties
=> 0,
102 properties
=> $props,
107 my ($class, $single_class) = @_;
109 my $pdata = $class->private();
110 my $propertyList = $pdata->{propertyList
};
111 my $plugins = $pdata->{plugins
};
115 my $filter_type = $class->type() if $single_class;
117 foreach my $p (keys %$propertyList) {
118 next if $p eq 'type';
120 my $copts = $class->options();
122 next if defined($filter_type) && !defined($copts->{$p});
124 if (!$propertyList->{$p}->{optional
}) {
125 $props->{$p} = $propertyList->{$p};
131 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed
};
133 foreach my $t (keys %$plugins) {
134 my $opts = $pdata->{options
}->{$t} || {};
135 next if !defined($opts->{$p});
136 $modifyable = 1 if !$opts->{$p}->{fixed
};
138 next if !$modifyable;
140 $props->{$p} = $propertyList->{$p};
143 $props->{digest
} = get_standard_option
('pve-config-digest');
146 type
=> 'string', format
=> 'pve-configid-list',
147 description
=> "A list of settings you want to delete.",
154 additionalProperties
=> 0,
155 properties
=> $props,
162 my $pdata = $class->private();
164 foreach my $k (qw(options plugins plugindata propertyList)) {
165 $pdata->{$k} = {} if !$pdata->{$k};
168 my $plugins = $pdata->{plugins
};
169 my $propertyList = $pdata->{propertyList
};
171 foreach my $type (keys %$plugins) {
172 my $props = $plugins->{$type}->properties();
173 foreach my $p (keys %$props) {
174 die "duplicate property '$p'" if defined($propertyList->{$p});
175 my $res = $propertyList->{$p} = {};
176 my $data = $props->{$p};
177 for my $a (keys %$data) {
178 $res->{$a} = $data->{$a};
180 $res->{optional
} = 1;
184 foreach my $type (keys %$plugins) {
185 my $opts = $plugins->{$type}->options();
186 foreach my $p (keys %$opts) {
187 die "undefined property '$p'" if !$propertyList->{$p};
189 $pdata->{options
}->{$type} = $opts;
192 $propertyList->{type
}->{type
} = 'string';
193 $propertyList->{type
}->{enum
} = [sort keys %$plugins];
197 my ($class, $type) = @_;
199 my $pdata = $class->private();
200 my $plugin = $pdata->{plugins
}->{$type};
202 die "unknown section type '$type'\n" if !$plugin;
210 my $pdata = $class->private();
212 return [ sort keys %{$pdata->{plugins
}} ];
216 my ($class, $type, $key, $value) = @_;
222 my ($class, $type, $key, $value) = @_;
228 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
230 my $pdata = $class->private();
232 return $value if $key eq 'type' && $type eq $value;
234 my $opts = $pdata->{options
}->{$type};
235 die "unknown section type '$type'\n" if !$opts;
237 die "unexpected property '$key'\n" if !defined($opts->{$key});
239 my $schema = $pdata->{propertyList
}->{$key};
240 die "unknown property type\n" if !$schema;
242 my $ct = $schema->{type
};
244 $value = 1 if $ct eq 'boolean' && !defined($value);
246 die "got undefined value\n" if !defined($value);
248 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
250 if (!$skipSchemaCheck) {
252 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
253 if (scalar(keys %$errors)) {
254 die "$errors->{$key}\n" if $errors->{$key};
255 die "$errors->{_root}\n" if $errors->{_root
};
256 die "unknown error\n";
263 sub parse_section_header
{
264 my ($class, $line) = @_;
266 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
267 my ($type, $sectionId) = ($1, $2);
268 my $errmsg = undef; # set if you want to skip whole section
269 my $config = {}; # to return additional attributes
270 return ($type, $sectionId, $errmsg, $config);
275 sub format_section_header
{
276 my ($class, $type, $sectionId) = @_;
278 return "$type: $sectionId\n";
283 my ($class, $filename, $raw) = @_;
285 my $pdata = $class->private();
290 $raw = '' if !defined($raw);
292 my $digest = Digest
::SHA
::sha1_hex
($raw);
297 my @lines = split(/\n/, $raw);
299 while (my $line = shift @lines) {
301 return $line if $line !~ /^\s*(?:#|$)/;
305 while (my $line = &$nextline()) {
306 my $errprefix = "file $filename line $lineno";
308 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
317 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
320 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
322 if (!($plugin = $pdata->{plugins
}->{$type})) {
324 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
328 while ($line = &$nextline()) {
329 next if $ignore; # skip
331 $errprefix = "file $filename line $lineno";
333 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
334 my ($k, $v) = ($1, $3);
337 die "duplicate attribute\n" if defined($config->{$k});
338 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
340 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
343 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
347 if (!$ignore && $type && $plugin && $config) {
348 $config->{type
} = $type;
349 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
350 warn "$errprefix (skip section '$sectionId'): $@" if $@;
351 $order->{$sectionId} = $pri++;
355 warn "$errprefix - ignore config line: $line\n";
360 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
366 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
368 my $type = $class->type();
369 my $pdata = $class->private();
370 my $opts = $pdata->{options
}->{$type};
372 my $settings = { type
=> $type };
374 foreach my $k (keys %$config) {
375 my $value = $config->{$k};
377 die "can't change value of fixed parameter '$k'\n"
378 if !$create && $opts->{$k}->{fixed
};
380 if (defined($value)) {
381 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
382 $settings->{$k} = $class->decode_value($type, $k, $tmp);
384 die "got undefined value for option '$k'\n";
389 # check if we have a value for all required options
390 foreach my $k (keys %$opts) {
391 next if $opts->{$k}->{optional
};
392 die "missing value for required option '$k'\n"
393 if !defined($config->{$k});
400 my $format_config_line = sub {
401 my ($schema, $key, $value) = @_;
403 my $ct = $schema->{type
};
405 die "property '$key' contains a line feed\n"
406 if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
408 if ($ct eq 'boolean') {
409 return "\t$key " . ($value ?
1 : 0) . "\n"
412 return "\t$key $value\n" if "$value" ne '';
417 my ($class, $filename, $cfg) = @_;
419 my $pdata = $class->private();
420 my $propertyList = $pdata->{propertyList
};
424 my $ids = $cfg->{ids
};
425 my $order = $cfg->{order
};
428 foreach my $sectionId (keys %$ids) {
429 my $pri = $order->{$sectionId};
430 $maxpri = $pri if $pri && $pri > $maxpri;
432 foreach my $sectionId (keys %$ids) {
433 if (!defined ($order->{$sectionId})) {
434 $order->{$sectionId} = ++$maxpri;
438 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
439 my $scfg = $ids->{$sectionId};
440 my $type = $scfg->{type
};
441 my $opts = $pdata->{options
}->{$type};
443 die "unknown section type '$type'\n" if !$opts;
445 my $data = $class->format_section_header($type, $sectionId);
446 if ($scfg->{comment
}) {
448 my $v = $class->encode_value($type, $k, $scfg->{$k});
449 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
452 $data .= "\tdisable\n" if $scfg->{disable
};
454 my $done_hash = { comment
=> 1, disable
=> 1};
456 my @option_keys = sort keys %$opts;
457 foreach my $k (@option_keys) {
458 next if $opts->{$k}->{optional
};
459 $done_hash->{$k} = 1;
461 die "section '$sectionId' - missing value for required option '$k'\n"
463 $v = $class->encode_value($type, $k, $v);
464 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
467 foreach my $k (@option_keys) {
468 next if defined($done_hash->{$k});
470 next if !defined($v);
471 $v = $class->encode_value($type, $k, $v);
472 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
481 sub assert_if_modified
{
482 my ($cfg, $digest) = @_;
484 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);