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 1FFFE706BE for ; Fri, 3 Jun 2022 12:17:01 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1E1C465A4 for ; Fri, 3 Jun 2022 12:17:01 +0200 (CEST) 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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 7C642659B for ; Fri, 3 Jun 2022 12:17:00 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5870343A09 for ; Fri, 3 Jun 2022 12:17:00 +0200 (CEST) Date: Fri, 3 Jun 2022 12:16:59 +0200 From: Wolfgang Bumiller To: Dominik Csapak Cc: pbs-devel@lists.proxmox.com Message-ID: <20220603101659.xokej3cppb362oi5@wobu-vie.proxmox.com> References: <20220602121811.3472729-1-d.csapak@proxmox.com> <20220602121811.3472729-4-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220602121811.3472729-4-d.csapak@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.325 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. [prune.rs] Subject: Re: [pbs-devel] [PATCH proxmox-backup v7 3/8] pbs-config: add metrics config class X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Jun 2022 10:17:01 -0000 On Thu, Jun 02, 2022 at 02:18:06PM +0200, Dominik Csapak wrote: (...) > @@ -0,0 +1,115 @@ > +use std::collections::HashMap; > + > +use anyhow::Error; > +use lazy_static::lazy_static; > + > +use proxmox_metrics::{influxdb_http, influxdb_udp, Metrics}; > +use proxmox_schema::*; > +use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; > + > +use pbs_api_types::{InfluxDbHttp, InfluxDbUdp, METRIC_SERVER_ID_SCHEMA}; > + > +use crate::{open_backup_lockfile, BackupLockGuard}; > + > +lazy_static! { > + pub static ref CONFIG: SectionConfig = init(); > +} > + > +fn init() -> SectionConfig { > + let mut config = SectionConfig::new(&METRIC_SERVER_ID_SCHEMA); > + > + let udp_schema = match InfluxDbUdp::API_SCHEMA { > + Schema::Object(ref object_schema) => object_schema, > + _ => unreachable!(), > + }; New code should prefer using the `unwrap__schema()` const fns, defining a const for the schema, in order to get some compile time checks there. const UDP_SCHEMA: &ObjectSchema = InfluxDbUdp::API_SCHEMA.unwrap_object_schema(); Eg. if you ever `#[serde(flatten)]` some struct into the type the schema type will change to `AllOfSchema`. See prune.rs as an example for this. (We should also switch the remaining files probably.) > + > + let udp_plugin = SectionConfigPlugin::new( > + "influxdb-udp".to_string(), > + Some("name".to_string()), > + udp_schema, > + ); > + config.register_plugin(udp_plugin); > + > + let http_schema = match InfluxDbHttp::API_SCHEMA { > + Schema::Object(ref object_schema) => object_schema, > + _ => unreachable!(), > + }; > + > + let http_plugin = SectionConfigPlugin::new( > + "influxdb-http".to_string(), > + Some("name".to_string()), > + http_schema, > + ); > + > + config.register_plugin(http_plugin); > + > + config > +} > + > +pub const METRIC_SERVER_CFG_FILENAME: &str = "/etc/proxmox-backup/metricserver.cfg"; > +pub const METRIC_SERVER_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.metricserver.lck"; > + > +/// Get exclusive lock > +pub fn lock_config() -> Result { > + open_backup_lockfile(METRIC_SERVER_CFG_LOCKFILE, None, true) > +} > + > +pub fn config() -> Result<(SectionConfigData, [u8; 32]), Error> { > + let content = proxmox_sys::fs::file_read_optional_string(METRIC_SERVER_CFG_FILENAME)? > + .unwrap_or_else(|| "".to_string()); Easier on the compiler (and a `const fn`): .unwrap_or_else(String::new) or even .unwrap_or_default() > + > + let digest = openssl::sha::sha256(content.as_bytes()); > + let data = CONFIG.parse(METRIC_SERVER_CFG_FILENAME, &content)?; > + Ok((data, digest)) > +} > + > +pub fn save_config(config: &SectionConfigData) -> Result<(), Error> { > + let raw = CONFIG.write(METRIC_SERVER_CFG_FILENAME, config)?; > + crate::replace_backup_config(METRIC_SERVER_CFG_FILENAME, raw.as_bytes()) > +} > + > +// shell completion helper > +pub fn complete_remote_name(_arg: &str, _param: &HashMap) -> Vec { > + match config() { > + Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(), I think you could simplify this to `.keys().cloned().collect()`