]>
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;
54 my ($class, $skip_type) = @_;
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 next if $skip_type && $p eq 'type';
76 if (!$propertyList->{$p}->{optional
}) {
77 $props->{$p} = $propertyList->{$p};
83 my $copts = $class->options();
84 $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional
};
86 foreach my $t (keys %$plugins) {
87 my $opts = $pdata->{options
}->{$t} || {};
88 $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional
};
92 # make a copy, because we modify the optional property
93 my $res = &$copy_property($propertyList->{$p});
97 $props->{$p} = $propertyList->{$p};
103 additionalProperties
=> 0,
104 properties
=> $props,
109 my ($class, $single_class) = @_;
111 my $pdata = $class->private();
112 my $propertyList = $pdata->{propertyList
};
113 my $plugins = $pdata->{plugins
};
117 my $filter_type = $class->type() if $single_class;
119 foreach my $p (keys %$propertyList) {
120 next if $p eq 'type';
122 my $copts = $class->options();
124 next if defined($filter_type) && !defined($copts->{$p});
126 if (!$propertyList->{$p}->{optional
}) {
127 $props->{$p} = $propertyList->{$p};
133 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed
};
135 foreach my $t (keys %$plugins) {
136 my $opts = $pdata->{options
}->{$t} || {};
137 next if !defined($opts->{$p});
138 $modifyable = 1 if !$opts->{$p}->{fixed
};
140 next if !$modifyable;
142 $props->{$p} = $propertyList->{$p};
145 $props->{digest
} = get_standard_option
('pve-config-digest');
148 type
=> 'string', format
=> 'pve-configid-list',
149 description
=> "A list of settings you want to delete.",
156 additionalProperties
=> 0,
157 properties
=> $props,
164 my $pdata = $class->private();
166 foreach my $k (qw(options plugins plugindata propertyList)) {
167 $pdata->{$k} = {} if !$pdata->{$k};
170 my $plugins = $pdata->{plugins
};
171 my $propertyList = $pdata->{propertyList
};
173 foreach my $type (keys %$plugins) {
174 my $props = $plugins->{$type}->properties();
175 foreach my $p (keys %$props) {
176 die "duplicate property '$p'" if defined($propertyList->{$p});
177 my $res = $propertyList->{$p} = {};
178 my $data = $props->{$p};
179 for my $a (keys %$data) {
180 $res->{$a} = $data->{$a};
182 $res->{optional
} = 1;
186 foreach my $type (keys %$plugins) {
187 my $opts = $plugins->{$type}->options();
188 foreach my $p (keys %$opts) {
189 die "undefined property '$p'" if !$propertyList->{$p};
191 $pdata->{options
}->{$type} = $opts;
194 $propertyList->{type
}->{type
} = 'string';
195 $propertyList->{type
}->{enum
} = [sort keys %$plugins];
199 my ($class, $type) = @_;
201 my $pdata = $class->private();
202 my $plugin = $pdata->{plugins
}->{$type};
204 die "unknown section type '$type'\n" if !$plugin;
212 my $pdata = $class->private();
214 return [ sort keys %{$pdata->{plugins
}} ];
218 my ($class, $type, $key, $value) = @_;
224 my ($class, $type, $key, $value) = @_;
230 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
232 my $pdata = $class->private();
234 return $value if $key eq 'type' && $type eq $value;
236 my $opts = $pdata->{options
}->{$type};
237 die "unknown section type '$type'\n" if !$opts;
239 die "unexpected property '$key'\n" if !defined($opts->{$key});
241 my $schema = $pdata->{propertyList
}->{$key};
242 die "unknown property type\n" if !$schema;
244 my $ct = $schema->{type
};
246 $value = 1 if $ct eq 'boolean' && !defined($value);
248 die "got undefined value\n" if !defined($value);
250 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
252 if (!$skipSchemaCheck) {
254 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
255 if (scalar(keys %$errors)) {
256 die "$errors->{$key}\n" if $errors->{$key};
257 die "$errors->{_root}\n" if $errors->{_root
};
258 die "unknown error\n";
265 sub parse_section_header
{
266 my ($class, $line) = @_;
268 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
269 my ($type, $sectionId) = ($1, $2);
270 my $errmsg = undef; # set if you want to skip whole section
271 my $config = {}; # to return additional attributes
272 return ($type, $sectionId, $errmsg, $config);
277 sub format_section_header
{
278 my ($class, $type, $sectionId) = @_;
280 return "$type: $sectionId\n";
285 my ($class, $filename, $raw) = @_;
287 my $pdata = $class->private();
292 $raw = '' if !defined($raw);
294 my $digest = Digest
::SHA
::sha1_hex
($raw);
299 my @lines = split(/\n/, $raw);
301 while (my $line = shift @lines) {
303 return $line if $line !~ /^\s*(?:#|$)/;
307 while (my $line = &$nextline()) {
308 my $errprefix = "file $filename line $lineno";
310 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
319 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
322 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
324 if (!($plugin = $pdata->{plugins
}->{$type})) {
326 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
330 while ($line = &$nextline()) {
331 next if $ignore; # skip
333 $errprefix = "file $filename line $lineno";
335 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
336 my ($k, $v) = ($1, $3);
339 die "duplicate attribute\n" if defined($config->{$k});
340 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
342 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
345 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
349 if (!$ignore && $type && $plugin && $config) {
350 $config->{type
} = $type;
351 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
352 warn "$errprefix (skip section '$sectionId'): $@" if $@;
353 $order->{$sectionId} = $pri++;
357 warn "$errprefix - ignore config line: $line\n";
362 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
368 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
370 my $type = $class->type();
371 my $pdata = $class->private();
372 my $opts = $pdata->{options
}->{$type};
374 my $settings = { type
=> $type };
376 foreach my $k (keys %$config) {
377 my $value = $config->{$k};
379 die "can't change value of fixed parameter '$k'\n"
380 if !$create && $opts->{$k}->{fixed
};
382 if (defined($value)) {
383 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
384 $settings->{$k} = $class->decode_value($type, $k, $tmp);
386 die "got undefined value for option '$k'\n";
391 # check if we have a value for all required options
392 foreach my $k (keys %$opts) {
393 next if $opts->{$k}->{optional
};
394 die "missing value for required option '$k'\n"
395 if !defined($config->{$k});
402 my $format_config_line = sub {
403 my ($schema, $key, $value) = @_;
405 my $ct = $schema->{type
};
407 die "property '$key' contains a line feed\n"
408 if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
410 if ($ct eq 'boolean') {
411 return "\t$key " . ($value ?
1 : 0) . "\n"
414 return "\t$key $value\n" if "$value" ne '';
419 my ($class, $filename, $cfg) = @_;
421 my $pdata = $class->private();
422 my $propertyList = $pdata->{propertyList
};
426 my $ids = $cfg->{ids
};
427 my $order = $cfg->{order
};
430 foreach my $sectionId (keys %$ids) {
431 my $pri = $order->{$sectionId};
432 $maxpri = $pri if $pri && $pri > $maxpri;
434 foreach my $sectionId (keys %$ids) {
435 if (!defined ($order->{$sectionId})) {
436 $order->{$sectionId} = ++$maxpri;
440 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
441 my $scfg = $ids->{$sectionId};
442 my $type = $scfg->{type
};
443 my $opts = $pdata->{options
}->{$type};
445 die "unknown section type '$type'\n" if !$opts;
447 my $data = $class->format_section_header($type, $sectionId);
448 if ($scfg->{comment
}) {
450 my $v = $class->encode_value($type, $k, $scfg->{$k});
451 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
454 $data .= "\tdisable\n" if $scfg->{disable
};
456 my $done_hash = { comment
=> 1, disable
=> 1};
458 my @option_keys = sort keys %$opts;
459 foreach my $k (@option_keys) {
460 next if $opts->{$k}->{optional
};
461 $done_hash->{$k} = 1;
463 die "section '$sectionId' - missing value for required option '$k'\n"
465 $v = $class->encode_value($type, $k, $v);
466 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
469 foreach my $k (@option_keys) {
470 next if defined($done_hash->{$k});
472 next if !defined($v);
473 $v = $class->encode_value($type, $k, $v);
474 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
483 sub assert_if_modified
{
484 my ($cfg, $digest) = @_;
486 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);