]>
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 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 my $copts = $class->options();
123 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed
};
125 foreach my $t (keys %$plugins) {
126 my $opts = $pdata->{options
}->{$t} || {};
127 next if !defined($opts->{$p});
128 $modifyable = 1 if !$opts->{$p}->{fixed
};
130 next if !$modifyable;
132 $props->{$p} = $propertyList->{$p};
135 $props->{digest
} = get_standard_option
('pve-config-digest');
138 type
=> 'string', format
=> 'pve-configid-list',
139 description
=> "A list of settings you want to delete.",
146 additionalProperties
=> 0,
147 properties
=> $props,
154 my $pdata = $class->private();
156 foreach my $k (qw(options plugins plugindata propertyList)) {
157 $pdata->{$k} = {} if !$pdata->{$k};
160 my $plugins = $pdata->{plugins
};
161 my $propertyList = $pdata->{propertyList
};
163 foreach my $type (keys %$plugins) {
164 my $props = $plugins->{$type}->properties();
165 foreach my $p (keys %$props) {
166 die "duplicate property '$p'" if defined($propertyList->{$p});
167 my $res = $propertyList->{$p} = {};
168 my $data = $props->{$p};
169 for my $a (keys %$data) {
170 $res->{$a} = $data->{$a};
172 $res->{optional
} = 1;
176 foreach my $type (keys %$plugins) {
177 my $opts = $plugins->{$type}->options();
178 foreach my $p (keys %$opts) {
179 die "undefined property '$p'" if !$propertyList->{$p};
181 $pdata->{options
}->{$type} = $opts;
184 $propertyList->{type
}->{type
} = 'string';
185 $propertyList->{type
}->{enum
} = [keys %$plugins];
189 my ($class, $type) = @_;
191 my $pdata = $class->private();
192 my $plugin = $pdata->{plugins
}->{$type};
194 die "unknown section type '$type'\n" if !$plugin;
202 my $pdata = $class->private();
204 return [ keys %{$pdata->{plugins
}} ];
208 my ($class, $type, $key, $value) = @_;
214 my ($class, $type, $key, $value) = @_;
220 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
222 my $pdata = $class->private();
224 return $value if $key eq 'type' && $type eq $value;
226 my $opts = $pdata->{options
}->{$type};
227 die "unknown section type '$type'\n" if !$opts;
229 die "unexpected property '$key'\n" if !defined($opts->{$key});
231 my $schema = $pdata->{propertyList
}->{$key};
232 die "unknown property type\n" if !$schema;
234 my $ct = $schema->{type
};
236 $value = 1 if $ct eq 'boolean' && !defined($value);
238 die "got undefined value\n" if !defined($value);
240 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
242 if (!$skipSchemaCheck) {
244 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
245 if (scalar(keys %$errors)) {
246 die "$errors->{$key}\n" if $errors->{$key};
247 die "$errors->{_root}\n" if $errors->{_root
};
248 die "unknown error\n";
255 sub parse_section_header
{
256 my ($class, $line) = @_;
258 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
259 my ($type, $sectionId) = ($1, $2);
260 my $errmsg = undef; # set if you want to skip whole section
261 my $config = {}; # to return additional attributes
262 return ($type, $sectionId, $errmsg, $config);
267 sub format_section_header
{
268 my ($class, $type, $sectionId) = @_;
270 return "$type: $sectionId\n";
275 my ($class, $filename, $raw) = @_;
277 my $pdata = $class->private();
282 $raw = '' if !defined($raw);
284 my $digest = Digest
::SHA
::sha1_hex
($raw);
289 my @lines = split(/\n/, $raw);
291 while (my $line = shift @lines) {
293 return $line if $line !~ /^\s*(?:#|$)/;
297 while (my $line = &$nextline()) {
298 my $errprefix = "file $filename line $lineno";
300 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
309 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
312 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
314 if (!($plugin = $pdata->{plugins
}->{$type})) {
316 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
320 while ($line = &$nextline()) {
321 next if $ignore; # skip
323 $errprefix = "file $filename line $lineno";
325 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
326 my ($k, $v) = ($1, $3);
329 die "duplicate attribute\n" if defined($config->{$k});
330 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
332 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
335 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
339 if (!$ignore && $type && $plugin && $config) {
340 $config->{type
} = $type;
341 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
342 warn "$errprefix (skip section '$sectionId'): $@" if $@;
343 $order->{$sectionId} = $pri++;
347 warn "$errprefix - ignore config line: $line\n";
352 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
358 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
360 my $type = $class->type();
361 my $pdata = $class->private();
362 my $opts = $pdata->{options
}->{$type};
364 my $settings = { type
=> $type };
366 foreach my $k (keys %$config) {
367 my $value = $config->{$k};
369 die "can't change value of fixed parameter '$k'\n"
370 if !$create && $opts->{$k}->{fixed
};
372 if (defined($value)) {
373 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
374 $settings->{$k} = $class->decode_value($type, $k, $tmp);
376 die "got undefined value for option '$k'\n";
381 # check if we have a value for all required options
382 foreach my $k (keys %$opts) {
383 next if $opts->{$k}->{optional
};
384 die "missing value for required option '$k'\n"
385 if !defined($config->{$k});
392 my $format_config_line = sub {
393 my ($schema, $key, $value) = @_;
395 my $ct = $schema->{type
};
397 if ($ct eq 'boolean') {
398 return $value ?
"\t$key\n" : '';
400 return "\t$key $value\n" if "$value" ne '';
405 my ($class, $filename, $cfg) = @_;
407 my $pdata = $class->private();
408 my $propertyList = $pdata->{propertyList
};
412 my $ids = $cfg->{ids
};
413 my $order = $cfg->{order
};
416 foreach my $sectionId (keys %$ids) {
417 my $pri = $order->{$sectionId};
418 $maxpri = $pri if $pri && $pri > $maxpri;
420 foreach my $sectionId (keys %$ids) {
421 if (!defined ($order->{$sectionId})) {
422 $order->{$sectionId} = ++$maxpri;
426 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
427 my $scfg = $ids->{$sectionId};
428 my $type = $scfg->{type
};
429 my $opts = $pdata->{options
}->{$type};
431 die "unknown section type '$type'\n" if !$opts;
433 my $data = $class->format_section_header($type, $sectionId);
434 if ($scfg->{comment
}) {
436 my $v = $class->encode_value($type, $k, $scfg->{$k});
437 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
440 $data .= "\tdisable\n" if $scfg->{disable
};
442 my $done_hash = { comment
=> 1, disable
=> 1};
444 foreach my $k (keys %$opts) {
445 next if $opts->{$k}->{optional
};
446 $done_hash->{$k} = 1;
448 die "section '$sectionId' - missing value for required option '$k'\n"
450 $v = $class->encode_value($type, $k, $v);
451 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
454 foreach my $k (keys %$opts) {
455 next if defined($done_hash->{$k});
457 next if !defined($v);
458 $v = $class->encode_value($type, $k, $v);
459 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
468 sub assert_if_modified
{
469 my ($cfg, $digest) = @_;
471 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);