]>
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
};
58 additionalProperties
=> 0,
59 properties
=> $propertyList,
66 my $pdata = $class->private();
67 my $propertyList = $pdata->{propertyList
};
68 my $plugins = $pdata->{plugins
};
72 foreach my $p (keys %$propertyList) {
74 if (!$propertyList->{$p}->{optional
}) {
75 $props->{$p} = $propertyList->{$p};
81 foreach my $t (keys %$plugins) {
82 my $opts = $pdata->{options
}->{$t} || {};
83 next if !defined($opts->{$p});
84 $modifyable = 1 if !$opts->{$p}->{fixed
};
88 $props->{$p} = $propertyList->{$p};
91 $props->{digest
} = get_standard_option
('pve-config-digest');
94 type
=> 'string', format
=> 'pve-configid-list',
95 description
=> "A list of settings you want to delete.",
102 additionalProperties
=> 0,
103 properties
=> $props,
110 my $pdata = $class->private();
112 foreach my $k (qw(options plugins plugindata propertyList)) {
113 $pdata->{$k} = {} if !$pdata->{$k};
116 my $plugins = $pdata->{plugins
};
117 my $propertyList = $pdata->{propertyList
};
119 foreach my $type (keys %$plugins) {
120 my $props = $plugins->{$type}->properties();
121 foreach my $p (keys %$props) {
122 die "duplicate property '$p'" if defined($propertyList->{$p});
123 my $res = $propertyList->{$p} = {};
124 my $data = $props->{$p};
125 for my $a (keys %$data) {
126 $res->{$a} = $data->{$a};
128 $res->{optional
} = 1;
132 foreach my $type (keys %$plugins) {
133 my $opts = $plugins->{$type}->options();
134 foreach my $p (keys %$opts) {
135 die "undefined property '$p'" if !$propertyList->{$p};
137 $pdata->{options
}->{$type} = $opts;
140 $propertyList->{type
}->{type
} = 'string';
141 $propertyList->{type
}->{enum
} = [keys %$plugins];
145 my ($class, $type) = @_;
147 my $pdata = $class->private();
148 my $plugin = $pdata->{plugins
}->{$type};
150 die "unknown section type '$type'\n" if !$plugin;
158 my $pdata = $class->private();
160 return [ keys %{$pdata->{plugins
}} ];
164 my ($class, $type, $key, $value) = @_;
170 my ($class, $type, $key, $value) = @_;
176 my ($class, $type, $key, $value, $storeid, $skipSchemaCheck) = @_;
178 my $pdata = $class->private();
180 return $value if $key eq 'type' && $type eq $value;
182 my $opts = $pdata->{options
}->{$type};
183 die "unknown section type '$type'\n" if !$opts;
185 die "unexpected property '$key'\n" if !defined($opts->{$key});
187 my $schema = $pdata->{propertyList
}->{$key};
188 die "unknown property type\n" if !$schema;
190 my $ct = $schema->{type
};
192 $value = 1 if $ct eq 'boolean' && !defined($value);
194 die "got undefined value\n" if !defined($value);
196 die "property contains a line feed\n" if $value =~ m/[\n\r]/;
198 if (!$skipSchemaCheck) {
200 PVE
::JSONSchema
::check_prop
($value, $schema, '', $errors);
201 if (scalar(keys %$errors)) {
202 die "$errors->{$key}\n" if $errors->{$key};
203 die "$errors->{_root}\n" if $errors->{_root
};
204 die "unknown error\n";
211 sub parse_section_header
{
212 my ($class, $line) = @_;
214 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
215 my ($type, $sectionId) = ($1, $2);
216 my $errmsg = undef; # set if you want to skip whole section
217 my $config = {}; # to return additional attributes
218 return ($type, $sectionId, $errmsg, $config);
223 sub format_section_header
{
224 my ($class, $type, $sectionId) = @_;
226 return "$type: $sectionId\n";
231 my ($class, $filename, $raw) = @_;
233 my $pdata = $class->private();
238 $raw = '' if !defined($raw);
240 my $digest = Digest
::SHA
::sha1_hex
($raw);
245 my @lines = split(/\n/, $raw);
247 while (my $line = shift @lines) {
249 return $line if $line !~ /^\s*(?:#|$)/;
253 while (my $line = &$nextline()) {
254 my $errprefix = "file $filename line $lineno";
256 my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
265 warn "$errprefix (skip section '$sectionId'): $errmsg\n";
268 warn "$errprefix (skip section '$sectionId'): missing type - internal error\n";
270 if (!($plugin = $pdata->{plugins
}->{$type})) {
272 warn "$errprefix (skip section '$sectionId'): unsupported type '$type'\n";
276 while ($line = &$nextline()) {
277 next if $ignore; # skip
279 $errprefix = "file $filename line $lineno";
281 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
282 my ($k, $v) = ($1, $3);
285 die "duplicate attribute\n" if defined($config->{$k});
286 $config->{$k} = $plugin->check_value($type, $k, $v, $sectionId);
288 warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $@" if $@;
291 warn "$errprefix (section '$sectionId') - ignore config line: $line\n";
295 if (!$ignore && $type && $plugin && $config) {
296 $config->{type
} = $type;
297 eval { $ids->{$sectionId} = $plugin->check_config($sectionId, $config, 1, 1); };
298 warn "$errprefix (skip section '$sectionId'): $@" if $@;
299 $order->{$sectionId} = $pri++;
303 warn "$errprefix - ignore config line: $line\n";
308 my $cfg = { ids
=> $ids, order
=> $order, digest
=> $digest};
314 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
316 my $type = $class->type();
317 my $pdata = $class->private();
318 my $opts = $pdata->{options
}->{$type};
320 my $settings = { type
=> $type };
322 foreach my $k (keys %$config) {
323 my $value = $config->{$k};
325 die "can't change value of fixed parameter '$k'\n"
326 if !$create && $opts->{$k}->{fixed
};
328 if (defined($value)) {
329 my $tmp = $class->check_value($type, $k, $value, $sectionId, $skipSchemaCheck);
330 $settings->{$k} = $class->decode_value($type, $k, $tmp);
332 die "got undefined value for option '$k'\n";
337 # check if we have a value for all required options
338 foreach my $k (keys %$opts) {
339 next if $opts->{$k}->{optional
};
340 die "missing value for required option '$k'\n"
341 if !defined($config->{$k});
348 my $format_config_line = sub {
349 my ($schema, $key, $value) = @_;
351 my $ct = $schema->{type
};
353 if ($ct eq 'boolean') {
354 return $value ?
"\t$key\n" : '';
356 return "\t$key $value\n" if "$value" ne '';
361 my ($class, $filename, $cfg) = @_;
363 my $pdata = $class->private();
364 my $propertyList = $pdata->{propertyList
};
368 my $ids = $cfg->{ids
};
369 my $order = $cfg->{order
};
372 foreach my $sectionId (keys %$ids) {
373 my $pri = $order->{$sectionId};
374 $maxpri = $pri if $pri && $pri > $maxpri;
376 foreach my $sectionId (keys %$ids) {
377 if (!defined ($order->{$sectionId})) {
378 $order->{$sectionId} = ++$maxpri;
382 foreach my $sectionId (sort {$order->{$a} <=> $order->{$b}} keys %$ids) {
383 my $scfg = $ids->{$sectionId};
384 my $type = $scfg->{type
};
385 my $opts = $pdata->{options
}->{$type};
387 die "unknown section type '$type'\n" if !$opts;
389 my $data = $class->format_section_header($type, $sectionId);
390 if ($scfg->{comment
}) {
392 my $v = $class->encode_value($type, $k, $scfg->{$k});
393 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
396 $data .= "\tdisable\n" if $scfg->{disable
};
398 my $done_hash = { comment
=> 1, disable
=> 1};
400 foreach my $k (keys %$opts) {
401 next if $opts->{$k}->{optional
};
402 $done_hash->{$k} = 1;
404 die "section '$sectionId' - missing value for required option '$k'\n"
406 $v = $class->encode_value($type, $k, $v);
407 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
410 foreach my $k (keys %$opts) {
411 next if defined($done_hash->{$k});
413 next if !defined($v);
414 $v = $class->encode_value($type, $k, $v);
415 $data .= &$format_config_line($propertyList->{$k}, $k, $v);
424 sub assert_if_modified
{
425 my ($cfg, $digest) = @_;
427 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);