default configs

This commit is contained in:
lda
2026-05-27 02:28:05 +07:00 Verified
parent 69b5313005
commit 71be2b7ccc
6 changed files with 180 additions and 79 deletions
+19 -1
View File
@@ -2,6 +2,7 @@ use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
pub const DEFAULT_CONFIG_PATH: &str = "/etc/wakey-agent/config.toml";
pub const DEFAULT_PID_FILE: &str = "/var/run/wakey-agent.pid";
@@ -36,6 +37,20 @@ pub struct AgentConfig {
pub observation_store_path: PathBuf,
}
pub static DEFAULT_CONFIG: LazyLock<AgentConfig> = LazyLock::new(|| AgentConfig {
server_url: "https://wakey.ldlda.com".to_string(),
agent_id: "REPLACE_ME_AGENT_ID".to_string(),
agent_token: "REPLACE_ME_AGENT_TOKEN".to_string(),
reconnect_base_ms: default_reconnect_base_ms(),
reconnect_max_ms: default_reconnect_max_ms(),
observation_sync_interval_seconds: default_observation_sync_interval_seconds(),
observation_retention_days: default_observation_retention_days(),
pid_file: default_pid_file(),
dhcp_leases_path: default_dhcp_leases_path(),
mac_name_cache_path: default_mac_name_cache_path(),
observation_store_path: default_observation_store_path(),
});
impl fmt::Debug for AgentConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AgentConfig")
@@ -226,6 +241,9 @@ agent_token = "secret"
)
.expect("config should parse");
assert_eq!(config.observation_retention_days, 7);
assert_eq!(
config.observation_retention_days,
DEFAULT_OBSERVATION_RETENTION_DAYS
);
}
}
+7 -30
View File
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tracing::{info, warn};
use crate::config::{AgentConfig, save_config_with_backup};
use crate::config::{AgentConfig, DEFAULT_CONFIG, save_config_with_backup};
pub struct EnrollOutcome {
pub config: AgentConfig,
@@ -55,35 +55,12 @@ pub async fn enroll(
.await
.context("failed to decode enrollment response")?;
let config = AgentConfig {
server_url: payload.server_url.unwrap_or(server_url),
agent_id: payload.agent_id,
agent_token: payload.agent_token,
reconnect_base_ms: base_config
.map(|config| config.reconnect_base_ms)
.unwrap_or(1_000),
reconnect_max_ms: base_config
.map(|config| config.reconnect_max_ms)
.unwrap_or(30_000),
observation_sync_interval_seconds: base_config
.map(|config| config.observation_sync_interval_seconds)
.unwrap_or(60),
observation_retention_days: base_config
.map(|config| config.observation_retention_days)
.unwrap_or(crate::config::DEFAULT_OBSERVATION_RETENTION_DAYS),
pid_file: base_config
.map(|config| config.pid_file.clone())
.unwrap_or_else(|| crate::config::DEFAULT_PID_FILE.into()),
dhcp_leases_path: base_config
.map(|config| config.dhcp_leases_path.clone())
.unwrap_or_else(|| "/tmp/dhcp.leases".into()),
mac_name_cache_path: base_config
.map(|config| config.mac_name_cache_path.clone())
.unwrap_or_else(|| "/tmp/wakey_mac_names.json".into()),
observation_store_path: base_config
.map(|config| config.observation_store_path.clone())
.unwrap_or_else(|| "/tmp/wakey_observations.json".into()),
};
let mut config = base_config
.cloned()
.unwrap_or_else(|| (*DEFAULT_CONFIG).clone());
config.server_url = payload.server_url.unwrap_or(server_url);
config.agent_id = payload.agent_id;
config.agent_token = payload.agent_token;
let backup_path = save_config_with_backup(config_path, &config)?;
info!(agent_id = %config.agent_id, config_path = %config_path.display(), "agent enrollment succeeded and config was written");
Ok(EnrollOutcome {
+1 -13
View File
@@ -248,19 +248,7 @@ fn init_config(args: InitConfigArgs) -> Result<()> {
let mut cfg = if let Some(from_config) = &args.from_config {
config::load_config(from_config)?
} else {
config::AgentConfig {
server_url: "https://wakey.ldlda.com".to_string(),
agent_id: "REPLACE_ME_AGENT_ID".to_string(),
agent_token: "REPLACE_ME_AGENT_TOKEN".to_string(),
reconnect_base_ms: 1_000,
reconnect_max_ms: 30_000,
observation_sync_interval_seconds: 60,
observation_retention_days: config::DEFAULT_OBSERVATION_RETENTION_DAYS,
pid_file: config::DEFAULT_PID_FILE.into(),
dhcp_leases_path: "/tmp/dhcp.leases".into(),
mac_name_cache_path: "/tmp/wakey_mac_names.json".into(),
observation_store_path: "/tmp/wakey_observations.json".into(),
}
(*config::DEFAULT_CONFIG).clone()
};
if let Some(server_url) = args.server_url {