2026-01-25 10:11:16 +02:00
|
|
|
use std::{env, fs, net::Ipv4Addr, process};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2026-01-19 08:48:05 +02:00
|
|
|
use cfgsync_tf::{
|
2025-12-01 12:48:39 +01:00
|
|
|
client::{FetchedConfig, get_config},
|
|
|
|
|
server::ClientIp,
|
|
|
|
|
};
|
2026-02-09 10:28:15 +02:00
|
|
|
use lb_node::UserConfig;
|
2025-12-01 12:48:39 +01:00
|
|
|
use serde::{Serialize, de::DeserializeOwned};
|
2026-01-19 08:48:05 +02:00
|
|
|
use testing_framework_config::constants::cfgsync_port as default_cfgsync_port;
|
|
|
|
|
use testing_framework_core::nodes::common::config::injection::{
|
|
|
|
|
inject_ibd_into_cryptarchia, normalize_ed25519_sigs,
|
2025-12-15 17:08:48 +01:00
|
|
|
};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
|
|
|
fn parse_ip(ip_str: &str) -> Ipv4Addr {
|
|
|
|
|
ip_str.parse().unwrap_or_else(|_| {
|
|
|
|
|
eprintln!("Invalid IP format, defaulting to 127.0.0.1");
|
|
|
|
|
Ipv4Addr::LOCALHOST
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 10:11:16 +02:00
|
|
|
async fn pull_to_file<Config>(payload: ClientIp, url: &str, config_file: &str) -> Result<(), String>
|
2025-12-01 12:48:39 +01:00
|
|
|
where
|
|
|
|
|
Config: Serialize + DeserializeOwned,
|
|
|
|
|
{
|
2026-01-25 10:11:16 +02:00
|
|
|
let FetchedConfig {
|
|
|
|
|
config,
|
|
|
|
|
raw: _unused,
|
|
|
|
|
} = get_config::<Config>(payload, url).await?;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2025-12-15 17:08:48 +01:00
|
|
|
let mut yaml_value = serde_yaml::to_value(&config)
|
|
|
|
|
.map_err(|err| format!("Failed to serialize config to YAML value: {err}"))?;
|
2025-12-18 20:13:23 +01:00
|
|
|
inject_ibd_into_cryptarchia(&mut yaml_value);
|
2025-12-15 17:08:48 +01:00
|
|
|
normalize_ed25519_sigs(&mut yaml_value);
|
|
|
|
|
let yaml = serde_yaml::to_string(&yaml_value)
|
2025-12-01 12:48:39 +01:00
|
|
|
.map_err(|err| format!("Failed to serialize config to YAML: {err}"))?;
|
|
|
|
|
|
|
|
|
|
fs::write(config_file, yaml).map_err(|err| format!("Failed to write config to file: {err}"))?;
|
|
|
|
|
|
|
|
|
|
println!("Config saved to {config_file}");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
let config_file_path = env::var("CFG_FILE_PATH").unwrap_or_else(|_| "config.yaml".to_owned());
|
2025-12-09 17:34:34 +01:00
|
|
|
let server_addr = env::var("CFG_SERVER_ADDR")
|
|
|
|
|
.unwrap_or_else(|_| format!("http://127.0.0.1:{}", default_cfgsync_port()));
|
2025-12-01 12:48:39 +01:00
|
|
|
let ip = parse_ip(&env::var("CFG_HOST_IP").unwrap_or_else(|_| "127.0.0.1".to_owned()));
|
|
|
|
|
let identifier =
|
|
|
|
|
env::var("CFG_HOST_IDENTIFIER").unwrap_or_else(|_| "unidentified-node".to_owned());
|
|
|
|
|
|
|
|
|
|
let network_port = env::var("CFG_NETWORK_PORT")
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|v| v.parse().ok());
|
|
|
|
|
let blend_port = env::var("CFG_BLEND_PORT").ok().and_then(|v| v.parse().ok());
|
|
|
|
|
let api_port = env::var("CFG_API_PORT").ok().and_then(|v| v.parse().ok());
|
|
|
|
|
let testing_http_port = env::var("CFG_TESTING_HTTP_PORT")
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|v| v.parse().ok());
|
|
|
|
|
|
|
|
|
|
let payload = ClientIp {
|
|
|
|
|
ip,
|
|
|
|
|
identifier,
|
|
|
|
|
network_port,
|
|
|
|
|
blend_port,
|
|
|
|
|
api_port,
|
|
|
|
|
testing_http_port,
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-26 08:26:15 +01:00
|
|
|
let node_config_endpoint = format!("{server_addr}/node");
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2026-01-25 10:11:16 +02:00
|
|
|
let config_result =
|
2026-01-29 09:33:25 +02:00
|
|
|
pull_to_file::<UserConfig>(payload, &node_config_endpoint, &config_file_path).await;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
|
|
|
// Handle error if the config request fails
|
|
|
|
|
if let Err(err) = config_result {
|
|
|
|
|
eprintln!("Error: {err}");
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|