Skip to content

Commit 0985366

Browse files
committed
feat(config-migrate): couple to infra-agent type, simplify
1 parent ffc1b8f commit 0985366

File tree

9 files changed

+909
-161
lines changed

9 files changed

+909
-161
lines changed

agent-control/src/bin/main_config_migrate.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
use newrelic_agent_control::agent_control::config_repository::store::AgentControlConfigStore;
2-
use newrelic_agent_control::agent_control::defaults::{
3-
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR, SUB_AGENT_DIR,
4-
};
52
use newrelic_agent_control::config_migrate::cli::Cli;
63
use newrelic_agent_control::config_migrate::migration::agent_config_getter::AgentConfigGetter;
74
use newrelic_agent_control::config_migrate::migration::config::MigrationConfig;
85
use newrelic_agent_control::config_migrate::migration::converter::ConfigConverter;
9-
use newrelic_agent_control::config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING;
106
use newrelic_agent_control::config_migrate::migration::migrator::{ConfigMigrator, MigratorError};
117
use newrelic_agent_control::config_migrate::migration::persister::legacy_config_renamer::LegacyConfigRenamer;
128
use newrelic_agent_control::config_migrate::migration::persister::values_persister_file::ValuesPersisterFile;
139
use newrelic_agent_control::instrumentation::tracing::{TracingConfig, try_init_tracing};
1410
use newrelic_agent_control::values::file::ConfigRepositoryFile;
1511
use std::error::Error;
16-
use std::path::PathBuf;
1712
use std::sync::Arc;
1813
use tracing::{debug, info, warn};
1914

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

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

26-
let config: MigrationConfig = MigrationConfig::parse(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING)?;
22+
let config = MigrationConfig::parse(&cli.get_migration_config_str()?)?;
2723

28-
let cli = Cli::init_config_migrate_cli();
29-
let remote_dir = PathBuf::from(AGENT_CONTROL_DATA_DIR);
30-
let vr = ConfigRepositoryFile::new(cli.local_data_dir(), remote_dir);
24+
let vr = ConfigRepositoryFile::new(cli.local_data_dir(), cli.remote_data_dir());
3125
let sa_local_config_loader = AgentControlConfigStore::new(Arc::new(vr));
3226
let config_migrator = ConfigMigrator::new(
3327
ConfigConverter::default(),
3428
AgentConfigGetter::new(sa_local_config_loader),
35-
ValuesPersisterFile::new(PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR).join(SUB_AGENT_DIR)),
29+
ValuesPersisterFile::new(cli.local_sub_agent_data_dir()),
3630
);
3731

3832
let legacy_config_renamer = LegacyConfigRenamer::default();
Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,67 @@
1-
use crate::agent_control::defaults::AGENT_CONTROL_LOCAL_DATA_DIR;
1+
use crate::{
2+
agent_control::defaults::{
3+
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR, SUB_AGENT_DIR,
4+
},
5+
config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING,
6+
};
27
use clap::Parser;
3-
use std::path::PathBuf;
8+
use std::{error::Error, fs, path::PathBuf};
49

510
#[derive(Parser, Debug)]
611
#[command(author, version, about, long_about = None)] // Read from `Cargo.toml`
712
pub struct Cli {
8-
/// Overrides the default local configuration path `/etc/newrelic-agent-control/`.
9-
#[cfg(debug_assertions)]
13+
/// Local data path used by Agent Control.
14+
#[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR))]
15+
local_dir: PathBuf,
16+
17+
/// Remote data path used by Agent Control.
18+
#[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_DATA_DIR))]
19+
remote_dir: PathBuf,
20+
21+
/// Logs path used by Agent Control.
22+
#[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_LOG_DIR))]
23+
logs_dir: PathBuf,
24+
25+
/// Provides an external configuration mapping for the migration of agents to Agent Control.
1026
#[arg(long)]
11-
local_dir: Option<PathBuf>,
27+
migration_config_file: Option<PathBuf>,
1228
}
1329

1430
impl Cli {
1531
/// Parses command line arguments
16-
pub fn init_config_migrate_cli() -> Self {
32+
pub fn load() -> Self {
1733
// Get command line args
1834
Self::parse()
1935
}
2036

2137
pub fn local_data_dir(&self) -> PathBuf {
22-
#[cfg(debug_assertions)]
23-
if let Some(path) = &self.local_dir {
24-
return path.clone();
25-
}
38+
self.local_dir.to_path_buf()
39+
}
40+
41+
pub fn local_sub_agent_data_dir(&self) -> PathBuf {
42+
self.local_dir.join(SUB_AGENT_DIR)
43+
}
44+
45+
pub fn remote_data_dir(&self) -> PathBuf {
46+
self.remote_dir.to_path_buf()
47+
}
2648

27-
PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR)
49+
pub fn log_dir(&self) -> PathBuf {
50+
self.logs_dir.to_path_buf()
51+
}
52+
53+
pub fn get_migration_config_str(&self) -> Result<String, Box<dyn Error>> {
54+
if let Some(path) = &self.migration_config_file {
55+
fs::read_to_string(path).map_err(|e| {
56+
format!(
57+
"Could not read provided migration config file ({}): {}",
58+
path.display(),
59+
e
60+
)
61+
.into()
62+
})
63+
} else {
64+
Ok(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING.to_owned())
65+
}
2866
}
2967
}

agent-control/src/config_migrate/migration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod agent_config_getter;
2+
pub mod agent_configs;
23
pub mod agent_value_spec;
34
pub mod config;
45
pub mod converter;

0 commit comments

Comments
 (0)