]>
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) {
71 if (!$propertyList->{$p}->{optional
}) {
72 $props->{$p} = $propertyList->{$p};
78 my $copts = $class->options();
79 $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional
};
81 foreach my $t (keys %$plugins) {
82 my $opts = $pdata->{options
}->{$t} || {};
83 $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional
};
87 # make a copy, because we modify the optional property
88 my $res = &$copy_property($propertyList->{$p});
92 $props->{$p} = $propertyList->{$p};
98 additionalProperties
=> 0,
106 my $pdata = $class->private();
107 my $propertyList = $pdata->{propertyList
};
108 my $plugins = $pdata->{plugins
};
112 foreach my $p (keys %$propertyList) {
113 next if $p eq 'type';
114 if (!$propertyList->{$p}->{optional
}) {
115 $props->{$p} = $propertyList->{$p};
121 my $copts = $class->options();
122 $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed
};
124 foreach my $t (keys %$plugins) {
125 my $opts = $pdata->{options
}->{$t} || {};
126 next if !defined($opts->{$p});
127 $modifyable = 1 if !$opts->{$p}->{fixed
};
129 next if !$modifyable;
131 $props->{$p} = $propertyList->{$p};
134 $props->{digest
} = get_standard_option
('pve-config-digest');
137 type
=> 'string', format
=> 'pve-configid-list',
138 description
=> "A list of settings you want to delete.",
145 additionalProperties
=> 0,
146 properties
=> $props,
153 my $pdata = $class->private();
155 foreach my $k (qw(options plugins plugindata propertyList)) {
156 $pdata->{$k} = {} if !$pdata->{$k};
159 my $plugins = $pdata->{plugins
};
160 my $propertyList = $pdata->{propertyList
};
162 foreach my $type (keys %$plugins) {
163 my $props = $plugins->{$type}->properties();
164 foreach my $p (keys %$props) {
165 die "duplicate property '$p'" if defined($propertyList->{$p});
166 my $res = $propertyList->{$p} = {};
167 my $data = $props->{$p};
168 for my $a (keys %$data) {
169 $res->{$a} = $data->{$a};
171 $res->{optional
} = 1;
175 foreach my $type (keys %$plugins) {
176 my $opts = $plugins->{$type}->options();
177 foreach my $p (keys %$opts) {
178 die "undefined property '$p'" if !$propertyList->{$p};
180 $pdata->{options
}->{$type} = $opts;
183 $propertyList->{type
}->{type
} = 'string';
184 $propertyList->{type
}->{enum
} = [keys %$plugins];
188 my ($class, $type) = @_;
190 my $pdata = $class->private();
191 my $plugin = $pdata->{plugins
}->{$type};
193 die "unknown section type '$type'\n" if !$plugin;
201 my $pdata = $class->private();
203 return [ keys %{$pdata->{plugins
}} ];
207 my ($class, $type, $key, $value) = @_;
213 my ($class, $type, $key, $value) = @_;
219 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
221 my $pdata = $class->private();
223 return $value if $key eq 'type' && $type eq $value;
225 my $opts = $pdata->{options
}->{$type};
226 die "unknown section type '$type'\n" if !$opts;
228 die "unexpected property '$key'\n" if !defined($opts->{$key});
230 my $schema = $pdata->{propertyList
}->{$key};
231 die "unknown property type\n" if !$schema;
233 my $ct = $schema->{type
};
235 $value = 1 if $ct eq 'boolean' && !defined($value);
237 die "got undefined value\n" if !defined($value);
239 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
241 if (!$skipSchemaCheck) {
243 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
244 if (scalar(keys %$errors)) {
245 die "$errors->{$key}\n" if $errors->{$key};
246 die "$errors->{_root}\n" if $errors->{_root
};
247 die "unknown error\n";
254 sub parse_section_header
{
255 my ($class, $line) = @_;
257 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
258 my ($type, $sectionId) = ($1, $2);
259 my $errmsg = undef; # set if you want to skip whole section
260 my $config = {}; # to return additional attributes
261 return ($type, $sectionId, $errmsg, $config);
266 sub format_section_header
{
267 my ($class, $type, $sectionId) = @_;
269 return "$type: $sectionId\n";
274 my ($class, $filename, $raw) = @_;
276 my $pdata = $class->private();
281 $raw = '' if !defined($raw);
283 my $digest = Digest
::SHA
::sha1_hex
($raw);
288 my @lines = split(/\n/, $raw);
290 while (my $line = shift @lines) {
292 return $line if $line !~ /^\s*(?:#|$)/;
296 while (my $line = &$nextline()) {
297 my $errprefix = "file $filename line $lineno";
299 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
308 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
311 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
313 if (!($plugin = $pdata->{plugins
}->{$type})) {
315 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
319 while ($line = &$nextline()) {
320 next if $ignore; # skip
322 $errprefix = "file $filename line $lineno";
324 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
325 my ($k, $v) = ($1, $3);
328 die "duplicate attribute\n" if defined($config->{$k});
329 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
331 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
334 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
338 if (!$ignore && $type && $plugin && $config) {
339 $config->{type
} = $type;
340 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
341 warn "$errprefix (skip section '$sectionId'): $@" if $@;
342 $order->{$sectionId} = $pri++;
346 warn "$errprefix - ignore config line: $line\n";
351 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
357 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
359 my $type = $class->type();
360 my $pdata = $class->private();
361 my $opts = $pdata->{options
}->{$type};
363 my $settings = { type
=> $type };
365 foreach my $k (keys %$config) {
366 my $value = $config->{$k};
368 die "can't change value of fixed parameter '$k'\n"
369 if !$create && $opts->{$k}->{fixed
};
371 if (defined($value)) {
372 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
373 $settings->{$k} = $class->decode_value($type, $k, $tmp);
375 die "got undefined value for option '$k'\n";
380 # check if we have a value for all required options
381 foreach my $k (keys %$opts) {
382 next if $opts->{$k}->{optional
};
383 die "missing value for required option '$k'\n"
384 if !defined($config->{$k});
391 my $format_config_line = sub {
392 my ($schema, $key, $value) = @_;
394 my $ct = $schema->{type
};
396 die "property '$key' contains a line feed\n"
397 if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
399 if ($ct eq 'boolean') {
400 return $value ?
"\t$key\n" : '';
402 return "\t$key $value\n" if "$value" ne '';
407 my ($class, $filename, $cfg) = @_;
409 my $pdata = $class->private();
410 my $propertyList = $pdata->{propertyList
};
414 my $ids = $cfg->{ids
};
415 my $order = $cfg->{order
};
418 foreach my $sectionId (keys %$ids) {
419 my $pri = $order->{$sectionId};
420 $maxpri = $pri if $pri && $pri > $maxpri;
422 foreach my $sectionId (keys %$ids) {
423 if (!defined ($order->{$sectionId})) {
424 $order->{$sectionId} = ++$maxpri;
428 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
429 my $scfg = $ids->{$sectionId};
430 my $type = $scfg->{type
};
431 my $opts = $pdata->{options
}->{$type};
433 die "unknown section type '$type'\n" if !$opts;
435 my $data = $class->format_section_header($type, $sectionId);
436 if ($scfg->{comment
}) {
438 my $v = $class->encode_value($type, $k, $scfg->{$k});
439 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
442 $data .= "\tdisable\n" if $scfg->{disable
};
444 my $done_hash = { comment
=> 1, disable
=> 1};
446 foreach my $k (keys %$opts) {
447 next if $opts->{$k}->{optional
};
448 $done_hash->{$k} = 1;
450 die "section '$sectionId' - missing value for required option '$k'\n"
452 $v = $class->encode_value($type, $k, $v);
453 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
456 foreach my $k (keys %$opts) {
457 next if defined($done_hash->{$k});
459 next if !defined($v);
460 $v = $class->encode_value($type, $k, $v);
461 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
470 sub assert_if_modified
{
471 my ($cfg, $digest) = @_;
473 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);