|
| 1 | +use log::{error, info}; |
| 2 | +use newrelic_super_agent::config::store::{SuperAgentConfigLoader, SuperAgentConfigStoreFile}; |
| 3 | +use newrelic_super_agent::config::super_agent_configs::{AgentID, AgentTypeFQN}; |
| 4 | +use newrelic_super_agent::config_migrate::config::MigrationConfig; |
| 5 | +use newrelic_super_agent::config_migrate::converter::ConfigConverter; |
| 6 | +use newrelic_super_agent::config_migrate::defaults::{ |
| 7 | + DEFAULT_AGENT_ID, NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING, |
| 8 | +}; |
| 9 | +use newrelic_super_agent::config_migrate::legacy_config_renamer::LegacyConfigRenamer; |
| 10 | +use newrelic_super_agent::config_migrate::values_persister_file::ValuesPersisterFile; |
| 11 | +use newrelic_super_agent::logging::Logging; |
| 12 | +use std::error::Error; |
| 13 | +use std::path::PathBuf; |
| 14 | +const DEFAULT_CFG_PATH: &str = "/etc/newrelic-super-agent/config.yaml"; |
| 15 | + |
| 16 | +fn main() -> Result<(), Box<dyn Error>> { |
| 17 | + // init logging singleton |
| 18 | + Logging::try_init()?; |
| 19 | + |
| 20 | + info!("Starting conversion tool..."); |
| 21 | + |
| 22 | + let config: MigrationConfig = serde_yaml::from_str(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING)?; |
| 23 | + |
| 24 | + let config_converter = ConfigConverter::default(); |
| 25 | + let values_persister = ValuesPersisterFile::default(); |
| 26 | + let legacy_config_renamer = LegacyConfigRenamer::default(); |
| 27 | + |
| 28 | + for cfg in config.configs { |
| 29 | + let Ok(agent_type) = get_current_agent_type().map_err(|e| { |
| 30 | + error!("Error finding newrelic-super-agent config: {}", e); |
| 31 | + }) else { |
| 32 | + return Ok(()); |
| 33 | + }; |
| 34 | + |
| 35 | + if cfg.agent_type_fqn != agent_type { |
| 36 | + error!("This script requires a config file and nr_infra_agent agent_type 0.0.2"); |
| 37 | + return Ok(()); |
| 38 | + } |
| 39 | + |
| 40 | + match config_converter.convert(&cfg) { |
| 41 | + Ok(agent_variables) => { |
| 42 | + let values_content = serde_yaml::to_string(&agent_variables)?; |
| 43 | + values_persister.persist_values_file( |
| 44 | + &AgentID::new(DEFAULT_AGENT_ID)?, |
| 45 | + values_content.as_str(), |
| 46 | + )?; |
| 47 | + for (_, dir_path) in cfg.dirs_map { |
| 48 | + legacy_config_renamer.rename_path(dir_path.as_str())?; |
| 49 | + } |
| 50 | + for (_, file_path) in cfg.files_map { |
| 51 | + legacy_config_renamer.rename_path(file_path.as_str())?; |
| 52 | + } |
| 53 | + info!("Config files successfully converted"); |
| 54 | + } |
| 55 | + Err(e) => { |
| 56 | + error!( |
| 57 | + "Conversion failed, old files or paths are renamed or not present: {}", |
| 58 | + e |
| 59 | + ); |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +// TODO : Add tests for getting this agent_type and check the agent_type by namespace instead of name DEFAULT_AGENT_ID |
| 68 | +fn get_current_agent_type() -> Result<AgentTypeFQN, Box<dyn Error>> { |
| 69 | + let local_config_path = PathBuf::from(DEFAULT_CFG_PATH.to_string()); |
| 70 | + let super_agent_config_storer = |
| 71 | + SuperAgentConfigStoreFile::new(&local_config_path).with_remote()?; |
| 72 | + let agents = super_agent_config_storer.load()?.agents; |
| 73 | + Ok(agents |
| 74 | + .get(&AgentID::new(DEFAULT_AGENT_ID)?) |
| 75 | + .cloned()? |
| 76 | + .agent_type) |
| 77 | +} |
0 commit comments