]>
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);
10 my $node_config_lock = '/var/lock/pvenode.lock';
12 PVE
::JSONSchema
::register_format
('pve-acme-domain', sub {
13 my ($domain, $noerr) = @_;
15 my $label = qr/[a-z0-9][a-z0-9_-]*/i;
17 return $domain if $domain =~ /^$label(?:\.$label)+$/;
18 return undef if $noerr;
19 die "value does not look like a valid domain name";
25 return "/etc/pve/nodes/${node}/config";
31 my $filename = config_file
($node);
32 my $raw = eval { PVE
::Tools
::file_get_contents
($filename); };
35 return parse_node_config
($raw);
39 my ($node, $conf) = @_;
41 my $filename = config_file
($node);
43 my $raw = write_node_config
($conf);
45 PVE
::Tools
::file_set_contents
($filename, $raw);
49 my ($node, $code, @param) = @_;
51 my $res = lock_file
($node_config_lock, 10, $code, @param);
61 description
=> 'Node description/comment.',
67 account
=> get_standard_option
('pve-acme-account-name'),
70 format
=> 'pve-acme-domain-list',
71 format_description
=> 'domain[;domain;...]',
72 description
=> 'List of domains for this node\'s ACME certificate',
75 PVE
::JSONSchema
::register_format
('pve-acme-node-conf', $acmedesc);
79 description
=> 'Node specific ACME settings.',
85 my ($key, $value) = @_;
87 die "unknown setting '$key'\n" if !$confdesc->{$key};
89 my $type = $confdesc->{$key}->{type
};
91 if (!defined($value)) {
92 die "got undefined value\n";
95 if ($value =~ m/[\n\r]/) {
96 die "property contains a line feed\n";
99 if ($type eq 'boolean') {
100 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
101 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
102 die "type check ('boolean') failed - got '$value'\n";
103 } elsif ($type eq 'integer') {
104 return int($1) if $value =~ m/^(\d+)$/;
105 die "type check ('integer') failed - got '$value'\n";
106 } elsif ($type eq 'number') {
107 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
108 die "type check ('number') failed - got '$value'\n";
109 } elsif ($type eq 'string') {
110 if (my $fmt = $confdesc->{$key}->{format
}) {
111 PVE
::JSONSchema
::check_format
($fmt, $value);
113 } elsif (my $pattern = $confdesc->{$key}->{pattern
}) {
114 if ($value !~ m/^$pattern$/) {
115 die "value does not match the regex pattern\n";
124 sub parse_node_config
{
127 return undef if !defined($content);
130 digest
=> Digest
::SHA
::sha1_hex
($content),
134 my @lines = split(/\n/, $content);
135 foreach my $line (@lines) {
136 if ($line =~ /^\#(.*)\s*$/ || $line =~ /^description:\s*(.*\S)\s*$/) {
137 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
140 if ($line =~ /^([a-z][a-z_]*\d*):\s*(\S.*)\s*$/) {
143 eval { $value = check_type
($key, $value); };
144 warn "cannot parse value of '$key' in node config: $@" if $@;
145 $conf->{$key} = $value;
147 warn "cannot parse line '$line' in node config\n";
151 $conf->{description
} = $descr if $descr;
156 sub write_node_config
{
160 # add description as comment to top of file
161 my $descr = $conf->{description
} || '';
162 foreach my $cl (split(/\n/, $descr)) {
163 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
166 for my $key (sort keys %$conf) {
167 next if ($key eq 'description');
168 next if ($key eq 'digest');
170 my $value = $conf->{$key};
171 die "detected invalid newline inside property '$key'\n"
173 $raw .= "$key: $value\n";
180 my ($data, $noerr) = @_;
184 my $res = eval { PVE
::JSONSchema
::parse_property_string
($acmedesc, $data); };
186 return undef if $noerr;
190 $res->{domains
} = [ PVE
::Tools
::split_list
($res->{domains
}) ];
198 $acme->{domains
} = join(';', $acme->{domains
}) if $acme->{domains
};
199 return PVE
::JSONSchema
::print_property_string
($acme, $acmedesc);
202 sub get_nodeconfig_schema
{