]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/SectionConfig.pm
f6646f3398a1ec69e9e6c3ae1a71025641d4fbc4
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 my $plugindata = $class->plugindata();
30 $pdata->{plugindata
}->{$type} = $plugindata;
31 $pdata->{plugins
}->{$type} = $class;
53 my $pdata = $class->private();
54 my $propertyList = $pdata->{propertyList
};
55 my $plugins = $pdata->{plugins
};
59 my $copy_property = sub {
63 foreach my $k (keys %$src) {
64 $res->{$k} = $src->{$k};
70 foreach my $p (keys %$propertyList) {
72 if (!$propertyList->{$p}->{optional
}) {
73 $props->{$p} = $propertyList->{$p};
79 my $copts = $class->options();
80 $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional
};
82 foreach my $t (keys %$plugins) {
83 my $opts = $pdata->{options
}->{$t} || {};
84 $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional
};
88 # make a copy, because we modify the optional property
89 my $res = &$copy_property($propertyList->{$p});
93 $props->{$p} = $propertyList->{$p};
99 additionalProperties
=> 0,
100 properties
=> $props,
107 my $pdata = $class->private();
108 my $propertyList = $pdata->{propertyList
};
109 my $plugins = $pdata->{plugins
};
113 foreach my $p (keys %$propertyList) {
114 next if $p eq 'type';
115 if (!$propertyList->{$p}->{optional
}) {
116 $props->{$p} = $propertyList->{$p};
122 foreach my $t (keys %$plugins) {
123 my $opts = $pdata->{options
}->{$t} || {};
124 next if !defined($opts->{$p});
125 $modifyable = 1 if !$opts->{$p}->{fixed
};
127 next if !$modifyable;
129 $props->{$p} = $propertyList->{$p};
132 $props->{digest
} = get_standard_option
('pve-config-digest');
135 type
=> 'string', format
=> 'pve-configid-list',
136 description
=> "A list of settings you want to delete.",
143 additionalProperties
=> 0,
144 properties
=> $props,
151 my $pdata = $class->private();
153 foreach my $k (qw(options plugins plugindata propertyList)) {
154 $pdata->{$k} = {} if !$pdata->{$k};
157 my $plugins = $pdata->{plugins
};
158 my $propertyList = $pdata->{propertyList
};
160 foreach my $type (keys %$plugins) {
161 my $props = $plugins->{$type}->properties();
162 foreach my $p (keys %$props) {
163 die "duplicate property '$p'" if defined($propertyList->{$p});
164 my $res = $propertyList->{$p} = {};
165 my $data = $props->{$p};
166 for my $a (keys %$data) {
167 $res->{$a} = $data->{$a};
169 $res->{optional
} = 1;
173 foreach my $type (keys %$plugins) {
174 my $opts = $plugins->{$type}->options();
175 foreach my $p (keys %$opts) {
176 die "undefined property '$p'" if !$propertyList->{$p};
178 $pdata->{options
}->{$type} = $opts;
181 $propertyList->{type
}->{type
} = 'string';
182 $propertyList->{type
}->{enum
} = [keys %$plugins];
186 my ($class, $type) = @_;
188 my $pdata = $class->private();
189 my $plugin = $pdata->{plugins
}->{$type};
191 die "unknown section type '$type'\n" if !$plugin;
199 my $pdata = $class->private();
201 return [ keys %{$pdata->{plugins
}} ];
205 my ($class, $type, $key, $value) = @_;
211 my ($class, $type, $key, $value) = @_;
217 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
219 my $pdata = $class->private();
221 return $value if $key eq 'type' && $type eq $value;
223 my $opts = $pdata->{options
}->{$type};
224 die "unknown section type '$type'\n" if !$opts;
226 die "unexpected property '$key'\n" if !defined($opts->{$key});
228 my $schema = $pdata->{propertyList
}->{$key};
229 die "unknown property type\n" if !$schema;
231 my $ct = $schema->{type
};
233 $value = 1 if $ct eq 'boolean' && !defined($value);
235 die "got undefined value\n" if !defined($value);
237 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
239 if (!$skipSchemaCheck) {
241 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
242 if (scalar(keys %$errors)) {
243 die "$errors->{$key}\n" if $errors->{$key};
244 die "$errors->{_root}\n" if $errors->{_root
};
245 die "unknown error\n";
252 sub parse_section_header
{
253 my ($class, $line) = @_;
255 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
256 my ($type, $sectionId) = ($1, $2);
257 my $errmsg = undef; # set if you want to skip whole section
258 my $config = {}; # to return additional attributes
259 return ($type, $sectionId, $errmsg, $config);
264 sub format_section_header
{
265 my ($class, $type, $sectionId) = @_;
267 return "$type: $sectionId\n";
272 my ($class, $filename, $raw) = @_;
274 my $pdata = $class->private();
279 $raw = '' if !defined($raw);
281 my $digest = Digest
::SHA
::sha1_hex
($raw);
286 my @lines = split(/\n/, $raw);
288 while (my $line = shift @lines) {
290 return $line if $line !~ /^\s*(?:#|$)/;
294 while (my $line = &$nextline()) {
295 my $errprefix = "file $filename line $lineno";
297 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
306 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
309 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
311 if (!($plugin = $pdata->{plugins
}->{$type})) {
313 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
317 while ($line = &$nextline()) {
318 next if $ignore; # skip
320 $errprefix = "file $filename line $lineno";
322 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
323 my ($k, $v) = ($1, $3);
326 die "duplicate attribute\n" if defined($config->{$k});
327 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
329 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
332 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
336 if (!$ignore && $type && $plugin && $config) {
337 $config->{type
} = $type;
338 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
339 warn "$errprefix (skip section '$sectionId'): $@" if $@;
340 $order->{$sectionId} = $pri++;
344 warn "$errprefix - ignore config line: $line\n";
349 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
355 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
357 my $type = $class->type();
358 my $pdata = $class->private();
359 my $opts = $pdata->{options
}->{$type};
361 my $settings = { type
=> $type };
363 foreach my $k (keys %$config) {
364 my $value = $config->{$k};
366 die "can't change value of fixed parameter '$k'\n"
367 if !$create && $opts->{$k}->{fixed
};
369 if (defined($value)) {
370 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
371 $settings->{$k} = $class->decode_value($type, $k, $tmp);
373 die "got undefined value for option '$k'\n";
378 # check if we have a value for all required options
379 foreach my $k (keys %$opts) {
380 next if $opts->{$k}->{optional
};
381 die "missing value for required option '$k'\n"
382 if !defined($config->{$k});
389 my $format_config_line = sub {
390 my ($schema, $key, $value) = @_;
392 my $ct = $schema->{type
};
394 if ($ct eq 'boolean') {
395 return $value ?
"\t$key\n" : '';
397 return "\t$key $value\n" if "$value" ne '';
402 my ($class, $filename, $cfg) = @_;
404 my $pdata = $class->private();
405 my $propertyList = $pdata->{propertyList
};
409 my $ids = $cfg->{ids
};
410 my $order = $cfg->{order
};
413 foreach my $sectionId (keys %$ids) {
414 my $pri = $order->{$sectionId};
415 $maxpri = $pri if $pri && $pri > $maxpri;
417 foreach my $sectionId (keys %$ids) {
418 if (!defined ($order->{$sectionId})) {
419 $order->{$sectionId} = ++$maxpri;
423 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
424 my $scfg = $ids->{$sectionId};
425 my $type = $scfg->{type
};
426 my $opts = $pdata->{options
}->{$type};
428 die "unknown section type '$type'\n" if !$opts;
430 my $data = $class->format_section_header($type, $sectionId);
431 if ($scfg->{comment
}) {
433 my $v = $class->encode_value($type, $k, $scfg->{$k});
434 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
437 $data .= "\tdisable\n" if $scfg->{disable
};
439 my $done_hash = { comment
=> 1, disable
=> 1};
441 foreach my $k (keys %$opts) {
442 next if $opts->{$k}->{optional
};
443 $done_hash->{$k} = 1;
445 die "section '$sectionId' - missing value for required option '$k'\n"
447 $v = $class->encode_value($type, $k, $v);
448 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
451 foreach my $k (keys %$opts) {
452 next if defined($done_hash->{$k});
454 next if !defined($v);
455 $v = $class->encode_value($type, $k, $v);
456 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
465 sub assert_if_modified
{
466 my ($cfg, $digest) = @_;
468 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);