Skip to content

Commit 52f2aab

Browse files
authored
feat(config-migrate): couple to infra-agent agent type, simplify (#1640)
* feat: add map[string]yaml var type * feat: extend filesystem (#1699) * fix: use `relative_path` for file entry info * docs: type * feat: extend filesystem to fully support directories * feat: a specific dir for each filesystem entity (files, dirs) * test: templating * test: move test-only impls to test module * test: templating * test: proper yaml rendering * docs: detail how this module works * feat: SafePath -> PathBuf * feat(rendering): generate writable file list * feat: rendering filesystem entry list * style: rename path field * test: rendering expected file entries * test(integration): fix filesystem ops * style: trace level logs to paths written * refactor: migrate host agent types to filesystem * style: use capital letters * docs: clarify * chore: remove unused From impl * chore: remove leftover * feat: merge files and directories * refactor: make writes more safe by hiding rendering function The function `FileSystem::rendered` was public before, which could cause access to the internal `HashMap<PathBuf, String>`. If bound as mutable, it could be modified to write to unintended locations. Now the creation of `RenderedFileSystemEntries` is safer and doesn't provide visibility to the paths, only to write. * test: fix rendered paths comparison * fix: correctly handle yaml templating * feat(config-migrate): couple to infra-agent type, simplify * style: error quoting * wip: redefining * feat: merge file and directory mappings * test: fix * docs: clarify * style: use only the filename for map keys * chore: remove unused code * docs: remove misleading comment * feat: handle env var syntax for infra configs * docs: remove comment related to previous implementation * style: compile Regex only once * test: duplicate keys error * docs: wording * refactor: remove the possibility of duplicate mapping keys
1 parent c1e5c88 commit 52f2aab

File tree

10 files changed

+718
-237
lines changed

10 files changed

+718
-237
lines changed

agent-control/src/agent_type/runtime_config/on_host/filesystem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ impl Templateable for TemplateableValue<DirEntriesMap> {
242242
"Could not parse templated directory items as YAML: {e}"
243243
))
244244
})?;
245-
// Convert the serde_yaml::Value (i.e. the file contents) to String
246245

246+
// Convert the serde_yaml::Value (i.e. the file contents) to String
247247
map_string_value
248248
.into_iter()
249249
.map(|(k, v)| Ok((k, output_string(v)?)))

agent-control/src/bin/main_config_migrate.rs

Lines changed: 15 additions & 17 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;
7-
use newrelic_agent_control::config_migrate::migration::config::MigrationConfig;
4+
use newrelic_agent_control::config_migrate::migration::config::{MappingType, 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();
@@ -41,11 +35,15 @@ fn main() -> Result<(), Box<dyn Error>> {
4135
debug!("Checking configurations for {}", cfg.agent_type_fqn);
4236
match config_migrator.migrate(&cfg) {
4337
Ok(_) => {
44-
for (_, dir_path) in cfg.dirs_map {
45-
legacy_config_renamer.rename_path(dir_path.path.as_path())?;
46-
}
47-
for (_, file_path) in cfg.files_map {
48-
legacy_config_renamer.rename_path(file_path.as_path())?;
38+
for (_, mapping_type) in cfg.filesystem_mappings {
39+
match mapping_type {
40+
MappingType::Dir(dir_path) => {
41+
legacy_config_renamer.rename_path(dir_path.dir_path.as_path())?
42+
}
43+
MappingType::File(file_path) => {
44+
legacy_config_renamer.rename_path(file_path.as_path())?
45+
}
46+
}
4947
}
5048
debug!("Classic config files and paths renamed");
5149
}
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/agent_value_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn from_fqn_and_value(
3737
fqn: AgentTypeFieldFQN,
3838
value: AgentValueSpec,
3939
) -> HashMap<String, AgentValueSpec> {
40-
let cloned_fqn = fqn.clone().as_string();
40+
let cloned_fqn = fqn.to_string();
4141
let mut parts: Vec<&str> = cloned_fqn.rsplit(FILE_SEPARATOR).collect();
4242
let first = parts.last().unwrap().to_string();
4343
parts.remove(parts.len() - 1);

0 commit comments

Comments
 (0)