From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id DD80B6AC4D for ; Thu, 17 Mar 2022 11:26:49 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C71781BE9 for ; Thu, 17 Mar 2022 11:26:19 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4BB5D1BC1 for ; Thu, 17 Mar 2022 11:26:18 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1FB9D46EBE for ; Thu, 17 Mar 2022 11:26:18 +0100 (CET) From: Wolfgang Bumiller To: pve-devel@lists.proxmox.com Date: Thu, 17 Mar 2022 11:26:01 +0100 Message-Id: <20220317102603.49426-2-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220317102603.49426-1-w.bumiller@proxmox.com> References: <20220317102603.49426-1-w.bumiller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.357 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [nodeconfig.pm] Subject: [pve-devel] [PATCH manager 1/2] nodeconfig: use common config parser X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Mar 2022 10:26:50 -0000 Signed-off-by: Wolfgang Bumiller --- PVE/NodeConfig.pm | 75 +++++++---------------------------------------- 1 file changed, 10 insertions(+), 65 deletions(-) diff --git a/PVE/NodeConfig.pm b/PVE/NodeConfig.pm index df44410f..941e6009 100644 --- a/PVE/NodeConfig.pm +++ b/PVE/NodeConfig.pm @@ -48,7 +48,7 @@ sub load_config { my $raw = eval { PVE::Tools::file_get_contents($filename); }; return {} if !$raw; - return parse_node_config($raw); + return parse_node_config($raw, $filename); } sub write_config { @@ -153,74 +153,19 @@ for my $i (0..$MAXDOMAINS) { }; }; -sub check_type { - my ($key, $value) = @_; - - die "unknown setting '$key'\n" if !$confdesc->{$key}; - - my $type = $confdesc->{$key}->{type}; - - if (!defined($value)) { - die "got undefined value\n"; - } - - if ($value =~ m/[\n\r]/) { - die "property contains a line feed\n"; - } - - if ($type eq 'boolean') { - return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i); - return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i); - die "type check ('boolean') failed - got '$value'\n"; - } elsif ($type eq 'integer') { - return int($1) if $value =~ m/^(\d+)$/; - die "type check ('integer') failed - got '$value'\n"; - } elsif ($type eq 'number') { - return $value if $value =~ m/^(\d+)(\.\d+)?$/; - die "type check ('number') failed - got '$value'\n"; - } elsif ($type eq 'string') { - if (my $fmt = $confdesc->{$key}->{format}) { - PVE::JSONSchema::check_format($fmt, $value); - return $value; - } elsif (my $pattern = $confdesc->{$key}->{pattern}) { - if ($value !~ m/^$pattern$/) { - die "value does not match the regex pattern\n"; - } - } - return $value; - } else { - die "internal error" - } -} +my $conf_schema = { + type => 'object', + properties => $confdesc, +}; -sub parse_node_config { - my ($content) = @_; +sub parse_node_config : prototype($$) { + my ($content, $filename) = @_; return undef if !defined($content); + my $digest = Digest::SHA::sha1_hex($content); - my $conf = { - digest => Digest::SHA::sha1_hex($content), - }; - my $descr = ''; - - my @lines = split(/\n/, $content); - foreach my $line (@lines) { - if ($line =~ /^\#(.*)\s*$/ || $line =~ /^description:\s*(.*\S)\s*$/) { - $descr .= PVE::Tools::decode_text($1) . "\n"; - next; - } - if ($line =~ /^([a-z][a-z-_]*\d*):\s*(\S.*)\s*$/) { - my $key = $1; - my $value = $2; - $value = eval { check_type($key, $value) }; - die "cannot parse value of '$key' in node config: $@" if $@; - $conf->{$key} = $value; - } else { - warn "cannot parse line '$line' in node config\n"; - } - } - - $conf->{description} = $descr if $descr; + my $conf = PVE::JSONSchema::parse_config($conf_schema, $filename, $content, 'description'); + $conf->{digest} = $digest; return $conf; } -- 2.30.2