Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ impl Templateable for TemplateableValue<DirEntriesMap> {
"Could not parse templated directory items as YAML: {e}"
))
})?;
// Convert the serde_yaml::Value (i.e. the file contents) to String

// Convert the serde_yaml::Value (i.e. the file contents) to String
map_string_value
.into_iter()
.map(|(k, v)| Ok((k, output_string(v)?)))
Expand Down
32 changes: 15 additions & 17 deletions agent-control/src/bin/main_config_migrate.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
use newrelic_agent_control::agent_control::config_repository::store::AgentControlConfigStore;
use newrelic_agent_control::agent_control::defaults::{
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR, SUB_AGENT_DIR,
};
use newrelic_agent_control::config_migrate::cli::Cli;
use newrelic_agent_control::config_migrate::migration::agent_config_getter::AgentConfigGetter;
use newrelic_agent_control::config_migrate::migration::config::MigrationConfig;
use newrelic_agent_control::config_migrate::migration::config::{MappingType, MigrationConfig};
use newrelic_agent_control::config_migrate::migration::converter::ConfigConverter;
use newrelic_agent_control::config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING;
use newrelic_agent_control::config_migrate::migration::migrator::{ConfigMigrator, MigratorError};
use newrelic_agent_control::config_migrate::migration::persister::legacy_config_renamer::LegacyConfigRenamer;
use newrelic_agent_control::config_migrate::migration::persister::values_persister_file::ValuesPersisterFile;
use newrelic_agent_control::instrumentation::tracing::{TracingConfig, try_init_tracing};
use newrelic_agent_control::values::file::ConfigRepositoryFile;
use std::error::Error;
use std::path::PathBuf;
use std::sync::Arc;
use tracing::{debug, info, warn};

fn main() -> Result<(), Box<dyn Error>> {
let tracing_config = TracingConfig::from_logging_path(PathBuf::from(AGENT_CONTROL_LOG_DIR));
let cli = Cli::load();
let tracing_config = TracingConfig::from_logging_path(cli.log_dir());
let _tracer = try_init_tracing(tracing_config);

info!("Starting config conversion tool...");

let config: MigrationConfig = MigrationConfig::parse(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING)?;
let config = MigrationConfig::parse(&cli.get_migration_config_str()?)?;

let cli = Cli::init_config_migrate_cli();
let remote_dir = PathBuf::from(AGENT_CONTROL_DATA_DIR);
let vr = ConfigRepositoryFile::new(cli.local_data_dir(), remote_dir);
let vr = ConfigRepositoryFile::new(cli.local_data_dir(), cli.remote_data_dir());
let sa_local_config_loader = AgentControlConfigStore::new(Arc::new(vr));
let config_migrator = ConfigMigrator::new(
ConfigConverter::default(),
AgentConfigGetter::new(sa_local_config_loader),
ValuesPersisterFile::new(PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR).join(SUB_AGENT_DIR)),
ValuesPersisterFile::new(cli.local_sub_agent_data_dir()),
);

let legacy_config_renamer = LegacyConfigRenamer::default();
Expand All @@ -41,11 +35,15 @@ fn main() -> Result<(), Box<dyn Error>> {
debug!("Checking configurations for {}", cfg.agent_type_fqn);
match config_migrator.migrate(&cfg) {
Ok(_) => {
for (_, dir_path) in cfg.dirs_map {
legacy_config_renamer.rename_path(dir_path.path.as_path())?;
}
for (_, file_path) in cfg.files_map {
legacy_config_renamer.rename_path(file_path.as_path())?;
for (_, mapping_type) in cfg.filesystem_mappings {
match mapping_type {
MappingType::Dir(dir_path) => {
legacy_config_renamer.rename_path(dir_path.dir_path.as_path())?
}
MappingType::File(file_path) => {
legacy_config_renamer.rename_path(file_path.as_path())?
}
}
}
debug!("Classic config files and paths renamed");
}
Expand Down
60 changes: 49 additions & 11 deletions agent-control/src/config_migrate/cli.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
use crate::agent_control::defaults::AGENT_CONTROL_LOCAL_DATA_DIR;
use crate::{
agent_control::defaults::{
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR, SUB_AGENT_DIR,
},
config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING,
};
use clap::Parser;
use std::path::PathBuf;
use std::{error::Error, fs, path::PathBuf};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] // Read from `Cargo.toml`
pub struct Cli {
/// Overrides the default local configuration path `/etc/newrelic-agent-control/`.
#[cfg(debug_assertions)]
/// Local data path used by Agent Control.
#[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR))]
local_dir: PathBuf,

/// Remote data path used by Agent Control.
#[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_DATA_DIR))]
remote_dir: PathBuf,

/// Logs path used by Agent Control.
#[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_LOG_DIR))]
logs_dir: PathBuf,

/// Provides an external configuration mapping for the migration of agents to Agent Control.
#[arg(long)]
local_dir: Option<PathBuf>,
migration_config_file: Option<PathBuf>,
}

impl Cli {
/// Parses command line arguments
pub fn init_config_migrate_cli() -> Self {
pub fn load() -> Self {
// Get command line args
Self::parse()
}

pub fn local_data_dir(&self) -> PathBuf {
#[cfg(debug_assertions)]
if let Some(path) = &self.local_dir {
return path.clone();
}
self.local_dir.to_path_buf()
}

pub fn local_sub_agent_data_dir(&self) -> PathBuf {
self.local_dir.join(SUB_AGENT_DIR)
}

pub fn remote_data_dir(&self) -> PathBuf {
self.remote_dir.to_path_buf()
}

PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR)
pub fn log_dir(&self) -> PathBuf {
self.logs_dir.to_path_buf()
}

pub fn get_migration_config_str(&self) -> Result<String, Box<dyn Error>> {
if let Some(path) = &self.migration_config_file {
fs::read_to_string(path).map_err(|e| {
format!(
"Could not read provided migration config file ({}): {}",
path.display(),
e
)
.into()
})
} else {
Ok(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING.to_owned())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn from_fqn_and_value(
fqn: AgentTypeFieldFQN,
value: AgentValueSpec,
) -> HashMap<String, AgentValueSpec> {
let cloned_fqn = fqn.clone().as_string();
let cloned_fqn = fqn.to_string();
let mut parts: Vec<&str> = cloned_fqn.rsplit(FILE_SEPARATOR).collect();
let first = parts.last().unwrap().to_string();
parts.remove(parts.len() - 1);
Expand Down
Loading
Loading