]>
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,
109 my $pdata = $class->private();
110 my $propertyList = $pdata->{propertyList
};
111 my $plugins = $pdata->{plugins
};
115 foreach my $p (keys %$propertyList) {
116 next if $p eq 'type';
117 if (!$propertyList->{$p}->{optional
}) {
118 $props->{$p} = $propertyList->{$p};
124 my $copts = $class->options();
125 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed
};
127 foreach my $t (keys %$plugins) {
128 my $opts = $pdata->{options
}->{$t} || {};
129 next if !defined($opts->{$p});
130 $modifyable = 1 if !$opts->{$p}->{fixed
};
132 next if !$modifyable;
134 $props->{$p} = $propertyList->{$p};
137 $props->{digest
} = get_standard_option
('pve-config-digest');
140 type
=> 'string', format
=> 'pve-configid-list',
141 description
=> "A list of settings you want to delete.",
148 additionalProperties
=> 0,
149 properties
=> $props,
156 my $pdata = $class->private();
158 foreach my $k (qw(options plugins plugindata propertyList)) {
159 $pdata->{$k} = {} if !$pdata->{$k};
162 my $plugins = $pdata->{plugins
};
163 my $propertyList = $pdata->{propertyList
};
165 foreach my $type (keys %$plugins) {
166 my $props = $plugins->{$type}->properties();
167 foreach my $p (keys %$props) {
168 die "duplicate property '$p'" if defined($propertyList->{$p});
169 my $res = $propertyList->{$p} = {};
170 my $data = $props->{$p};
171 for my $a (keys %$data) {
172 $res->{$a} = $data->{$a};
174 $res->{optional
} = 1;
178 foreach my $type (keys %$plugins) {
179 my $opts = $plugins->{$type}->options();
180 foreach my $p (keys %$opts) {
181 die "undefined property '$p'" if !$propertyList->{$p};
183 $pdata->{options
}->{$type} = $opts;
186 $propertyList->{type
}->{type
} = 'string';
187 $propertyList->{type
}->{enum
} = [sort keys %$plugins];
191 my ($class, $type) = @_;
193 my $pdata = $class->private();
194 my $plugin = $pdata->{plugins
}->{$type};
196 die "unknown section type '$type'\n" if !$plugin;
204 my $pdata = $class->private();
206 return [ sort keys %{$pdata->{plugins
}} ];
210 my ($class, $type, $key, $value) = @_;
216 my ($class, $type, $key, $value) = @_;
222 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
224 my $pdata = $class->private();
226 return $value if $key eq 'type' && $type eq $value;
228 my $opts = $pdata->{options
}->{$type};
229 die "unknown section type '$type'\n" if !$opts;
231 die "unexpected property '$key'\n" if !defined($opts->{$key});
233 my $schema = $pdata->{propertyList
}->{$key};
234 die "unknown property type\n" if !$schema;
236 my $ct = $schema->{type
};
238 $value = 1 if $ct eq 'boolean' && !defined($value);
240 die "got undefined value\n" if !defined($value);
242 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
244 if (!$skipSchemaCheck) {
246 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
247 if (scalar(keys %$errors)) {
248 die "$errors->{$key}\n" if $errors->{$key};
249 die "$errors->{_root}\n" if $errors->{_root
};
250 die "unknown error\n";
257 sub parse_section_header
{
258 my ($class, $line) = @_;
260 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
261 my ($type, $sectionId) = ($1, $2);
262 my $errmsg = undef; # set if you want to skip whole section
263 my $config = {}; # to return additional attributes
264 return ($type, $sectionId, $errmsg, $config);
269 sub format_section_header
{
270 my ($class, $type, $sectionId) = @_;
272 return "$type: $sectionId\n";
277 my ($class, $filename, $raw) = @_;
279 my $pdata = $class->private();
284 $raw = '' if !defined($raw);
286 my $digest = Digest
::SHA
::sha1_hex
($raw);
291 my @lines = split(/\n/, $raw);
293 while (my $line = shift @lines) {
295 return $line if $line !~ /^\s*(?:#|$)/;
299 while (my $line = &$nextline()) {
300 my $errprefix = "file $filename line $lineno";
302 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
311 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
314 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
316 if (!($plugin = $pdata->{plugins
}->{$type})) {
318 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
322 while ($line = &$nextline()) {
323 next if $ignore; # skip
325 $errprefix = "file $filename line $lineno";
327 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
328 my ($k, $v) = ($1, $3);
331 die "duplicate attribute\n" if defined($config->{$k});
332 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
334 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
337 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
341 if (!$ignore && $type && $plugin && $config) {
342 $config->{type
} = $type;
343 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
344 warn "$errprefix (skip section '$sectionId'): $@" if $@;
345 $order->{$sectionId} = $pri++;
349 warn "$errprefix - ignore config line: $line\n";
354 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
360 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
362 my $type = $class->type();
363 my $pdata = $class->private();
364 my $opts = $pdata->{options
}->{$type};
366 my $settings = { type
=> $type };
368 foreach my $k (keys %$config) {
369 my $value = $config->{$k};
371 die "can't change value of fixed parameter '$k'\n"
372 if !$create && $opts->{$k}->{fixed
};
374 if (defined($value)) {
375 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
376 $settings->{$k} = $class->decode_value($type, $k, $tmp);
378 die "got undefined value for option '$k'\n";
383 # check if we have a value for all required options
384 foreach my $k (keys %$opts) {
385 next if $opts->{$k}->{optional
};
386 die "missing value for required option '$k'\n"
387 if !defined($config->{$k});
394 my $format_config_line = sub {
395 my ($schema, $key, $value) = @_;
397 my $ct = $schema->{type
};
399 die "property '$key' contains a line feed\n"
400 if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
402 if ($ct eq 'boolean') {
403 return "\t$key " . ($value ?
1 : 0) . "\n"
406 return "\t$key $value\n" if "$value" ne '';
411 my ($class, $filename, $cfg) = @_;
413 my $pdata = $class->private();
414 my $propertyList = $pdata->{propertyList
};
418 my $ids = $cfg->{ids
};
419 my $order = $cfg->{order
};
422 foreach my $sectionId (keys %$ids) {
423 my $pri = $order->{$sectionId};
424 $maxpri = $pri if $pri && $pri > $maxpri;
426 foreach my $sectionId (keys %$ids) {
427 if (!defined ($order->{$sectionId})) {
428 $order->{$sectionId} = ++$maxpri;
432 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
433 my $scfg = $ids->{$sectionId};
434 my $type = $scfg->{type
};
435 my $opts = $pdata->{options
}->{$type};
437 die "unknown section type '$type'\n" if !$opts;
439 my $data = $class->format_section_header($type, $sectionId);
440 if ($scfg->{comment
}) {
442 my $v = $class->encode_value($type, $k, $scfg->{$k});
443 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
446 $data .= "\tdisable\n" if $scfg->{disable
};
448 my $done_hash = { comment
=> 1, disable
=> 1};
450 foreach my $k (keys %$opts) {
451 next if $opts->{$k}->{optional
};
452 $done_hash->{$k} = 1;
454 die "section '$sectionId' - missing value for required option '$k'\n"
456 $v = $class->encode_value($type, $k, $v);
457 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
460 foreach my $k (keys %$opts) {
461 next if defined($done_hash->{$k});
463 next if !defined($v);
464 $v = $class->encode_value($type, $k, $v);
465 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
474 sub assert_if_modified
{
475 my ($cfg, $digest) = @_;
477 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);