[pdm-devel] [PATCH proxmox-datacenter-manager 04/25] pdm-config: add functions for reading/writing metric collection settings

Lukas Wagner l.wagner at proxmox.com
Tue Feb 11 13:05:20 CET 2025


This commit adds support for reading and writing a new configuration
file at /etc/proxmox-datacenter-manager/metric-collection.cfg.

It is a regular SectionConfig file holding CollectionSettings entries
for now. For starters, there will be only a single entry with the key
'default', e.g.

collection-settings: default
    max-concurrency 10
    ...

It might seem a bit odd to use a section config file for something like
this, but this gives us the ability to expand it more easily in the
future, e.g. different settings for different groups of remotes, other
section types for logical structuring, etc.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 lib/pdm-config/src/lib.rs               |  1 +
 lib/pdm-config/src/metric_collection.rs | 69 +++++++++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 lib/pdm-config/src/metric_collection.rs

diff --git a/lib/pdm-config/src/lib.rs b/lib/pdm-config/src/lib.rs
index ac398ca..d80ee40 100644
--- a/lib/pdm-config/src/lib.rs
+++ b/lib/pdm-config/src/lib.rs
@@ -5,6 +5,7 @@ pub use pdm_buildcfg::{BACKUP_GROUP_NAME, BACKUP_USER_NAME};
 
 pub mod certificate_config;
 pub mod domains;
+pub mod metric_collection;
 pub mod node;
 pub mod remotes;
 pub mod setup;
diff --git a/lib/pdm-config/src/metric_collection.rs b/lib/pdm-config/src/metric_collection.rs
new file mode 100644
index 0000000..613bb0e
--- /dev/null
+++ b/lib/pdm-config/src/metric_collection.rs
@@ -0,0 +1,69 @@
+//! Read/write metric collection configuration
+
+use std::sync::OnceLock;
+
+use anyhow::Error;
+
+use proxmox_config_digest::ConfigDigest;
+use proxmox_product_config::{open_api_lockfile, replace_config, ApiLockGuard};
+
+use pdm_api_types::{CollectionSettings, PROXMOX_SAFE_ID_FORMAT};
+
+use pdm_buildcfg::configdir;
+use proxmox_schema::{ApiType, ObjectSchema, Schema, StringSchema};
+use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
+
+const METRIC_COLLECTION_CFG_FILENAME: &str = configdir!("/metric-collection.cfg");
+const METRIC_COLLECTION_CFG_LOCKFILE: &str = configdir!("/.metric-collection.lock");
+
+/// The section-config type name for metric collection settings.
+pub const COLLECTION_SETTINGS_TYPE: &str = "collection-settings";
+
+/// Section config schema for the public config file.
+fn config_parser() -> &'static SectionConfig {
+    static CONFIG: OnceLock<SectionConfig> = OnceLock::new();
+    CONFIG.get_or_init(config_init)
+}
+
+const ENTITY_NAME_SCHEMA: Schema = StringSchema::new("collection-settings key.")
+    .format(&PROXMOX_SAFE_ID_FORMAT)
+    .min_length(2)
+    .max_length(32)
+    .schema();
+
+fn config_init() -> SectionConfig {
+    let mut config = SectionConfig::new(&ENTITY_NAME_SCHEMA);
+
+    const MATCHER_SCHEMA: &ObjectSchema = CollectionSettings::API_SCHEMA.unwrap_object_schema();
+    config.register_plugin(SectionConfigPlugin::new(
+        COLLECTION_SETTINGS_TYPE.into(),
+        Some(String::from("id")),
+        MATCHER_SCHEMA,
+    ));
+
+    config
+}
+
+/// Lock the metric-collection config
+pub fn lock_config() -> Result<ApiLockGuard, Error> {
+    open_api_lockfile(METRIC_COLLECTION_CFG_LOCKFILE, None, true)
+}
+
+/// Return contents of the metric-collection config
+pub fn config() -> Result<(SectionConfigData, ConfigDigest), Error> {
+    let content = proxmox_sys::fs::file_read_optional_string(METRIC_COLLECTION_CFG_FILENAME)?
+        .unwrap_or_default();
+    //
+    let digest = openssl::sha::sha256(content.as_bytes());
+    let data = config_parser().parse(METRIC_COLLECTION_CFG_FILENAME, &content)?;
+
+    Ok((data, digest.into()))
+}
+
+/// Replace the currently persisted metric-collection config
+pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
+    let content = config_parser().write(METRIC_COLLECTION_CFG_FILENAME, config)?;
+    replace_config(METRIC_COLLECTION_CFG_FILENAME, content.as_bytes())?;
+
+    Ok(())
+}
-- 
2.39.5





More information about the pdm-devel mailing list