]>
git.proxmox.com Git - pve-manager.git/blob - PVE/NodeConfig.pm
1 package PVE
::NodeConfig
;
7 use PVE
::JSONSchema
qw(get_standard_option);
8 use PVE
::Tools
qw(file_get_contents file_set_contents lock_file);
11 # register up to 20 domain names
14 my $node_config_lock = '/var/lock/pvenode.lock';
16 PVE
::JSONSchema
::register_format
('pve-acme-domain', sub {
17 my ($domain, $noerr) = @_;
19 my $label = qr/[a-z0-9][a-z0-9_-]*/i;
21 return $domain if $domain =~ /^$label(?:\.$label)+$/;
22 return undef if $noerr;
23 die "value does not look like a valid domain name";
29 return "/etc/pve/nodes/${node}/config";
35 my $filename = config_file
($node);
36 my $raw = eval { PVE
::Tools
::file_get_contents
($filename); };
39 return parse_node_config
($raw);
43 my ($node, $conf) = @_;
45 my $filename = config_file
($node);
47 my $raw = write_node_config
($conf);
49 PVE
::Tools
::file_set_contents
($filename, $raw);
53 my ($node, $realcode, @param) = @_;
55 # make sure configuration file is up-to-date
57 PVE
::Cluster
::cfs_update
();
61 my $res = lock_file
($node_config_lock, 10, $code, @param);
71 description
=> 'Node description/comment.',
76 description
=> 'MAC address for wake on LAN',
80 'startall-onboot-delay' => {
81 description
=> 'Initial delay in seconds, before starting all the Virtual Guests with on-boot enabled.',
90 my $acme_domain_desc = {
93 format
=> 'pve-acme-domain',
94 format_description
=> 'domain',
95 description
=> 'domain for this node\'s ACME certificate',
100 format
=> 'pve-configid',
101 description
=> 'The ACME plugin ID',
102 format_description
=> 'name of the plugin configuration',
104 default => 'standalone',
108 format
=> 'pve-acme-domain',
109 format_description
=> 'domain',
110 description
=> 'Alias for the Domain to verify ACME Challenge over DNS',
116 account
=> get_standard_option
('pve-acme-account-name'),
119 format
=> 'pve-acme-domain-list',
120 format_description
=> 'domain[;domain;...]',
121 description
=> 'List of domains for this node\'s ACME certificate',
126 $confdesc->{acme
} = {
128 description
=> 'Node specific ACME settings.',
133 for my $i (0..$MAXDOMAINS) {
134 $confdesc->{"acmedomain$i"} = {
136 description
=> 'ACME domain and validation plugin',
137 format
=> $acme_domain_desc,
143 my ($key, $value) = @_;
145 die "unknown setting '$key'\n" if !$confdesc->{$key};
147 my $type = $confdesc->{$key}->{type
};
149 if (!defined($value)) {
150 die "got undefined value\n";
153 if ($value =~ m/[\n\r]/) {
154 die "property contains a line feed\n";
157 if ($type eq 'boolean') {
158 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
159 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
160 die "type check ('boolean') failed - got '$value'\n";
161 } elsif ($type eq 'integer') {
162 return int($1) if $value =~ m/^(\d+)$/;
163 die "type check ('integer') failed - got '$value'\n";
164 } elsif ($type eq 'number') {
165 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
166 die "type check ('number') failed - got '$value'\n";
167 } elsif ($type eq 'string') {
168 if (my $fmt = $confdesc->{$key}->{format
}) {
169 PVE
::JSONSchema
::check_format
($fmt, $value);
171 } elsif (my $pattern = $confdesc->{$key}->{pattern
}) {
172 if ($value !~ m/^$pattern$/) {
173 die "value does not match the regex pattern\n";
182 sub parse_node_config
{
185 return undef if !defined($content);
188 digest
=> Digest
::SHA
::sha1_hex
($content),
192 my @lines = split(/\n/, $content);
193 foreach my $line (@lines) {
194 if ($line =~ /^\#(.*)\s*$/ || $line =~ /^description:\s*(.*\S)\s*$/) {
195 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
198 if ($line =~ /^([a-z][a-z-_]*\d*):\s*(\S.*)\s*$/) {
201 $value = eval { check_type
($key, $value) };
202 die "cannot parse value of '$key' in node config: $@" if $@;
203 $conf->{$key} = $value;
205 warn "cannot parse line '$line' in node config\n";
209 $conf->{description
} = $descr if $descr;
214 sub write_node_config
{
218 # add description as comment to top of file
219 my $descr = $conf->{description
} || '';
220 foreach my $cl (split(/\n/, $descr)) {
221 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
224 for my $key (sort keys %$conf) {
225 next if ($key eq 'description');
226 next if ($key eq 'digest');
228 my $value = $conf->{$key};
229 die "detected invalid newline inside property '$key'\n"
231 $raw .= "$key: $value\n";
238 my ($node_conf, $noerr) = @_;
243 if (defined($node_conf->{acme
})) {
245 PVE
::JSONSchema
::parse_property_string
($acmedesc, $node_conf->{acme
})
248 return undef if $noerr;
251 my $standalone_domains = delete($res->{domains
}) // '';
252 for my $domain (split(";", $standalone_domains)) {
253 $res->{domains
}->{$domain}->{plugin
} = 'standalone';
254 $res->{domains
}->{$domain}->{_configkey
} = 'acme';
258 $res->{account
} //= 'default';
260 for my $index (0..$MAXDOMAINS) {
261 my $domain_rec = $node_conf->{"acmedomain$index"};
262 next if !defined($domain_rec);
265 PVE
::JSONSchema
::parse_property_string
($acme_domain_desc, $domain_rec)
268 return undef if $noerr;
271 my $domain = delete $parsed->{domain
};
272 if (my $exists = $res->{domains
}->{$domain}) {
273 return undef if $noerr;
274 die "duplicate domain '$domain' in ACME config properties"
275 ." 'acmedomain$index' and '$exists->{_configkey}'\n";
277 $parsed->{plugin
} //= 'standalone';
278 $parsed->{_configkey
} = "acmedomain$index";
279 $res->{domains
}->{$domain} = $parsed;
285 # expects that basic format verification was already done, this is more higher
288 my ($node_conf) = @_;
290 # verify ACME domain uniqueness
291 my $tmp = get_acme_conf
($node_conf);
298 sub get_nodeconfig_schema
{