Skip to content

Commit 79a2ea5

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

File tree

8 files changed

+872
-160
lines changed

8 files changed

+872
-160
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
@@ -5,3 +5,4 @@ pub mod converter;
55
pub mod defaults;
66
pub mod migrator;
77
pub mod persister;
8+
pub mod supported_config_value;

agent-control/src/config_migrate/migration/config.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use regex::Regex;
21
use serde::Deserialize;
32
use serde_yaml::Error;
43
use std::collections::HashMap;
4+
use std::ffi::OsString;
55
use std::fmt::{Display, Formatter};
66
use std::hash::{Hash, Hasher};
7-
use std::path::PathBuf;
7+
use std::path::{Path, PathBuf};
88
use thiserror::Error;
99
use tracing::error;
1010

@@ -101,19 +101,15 @@ pub struct DirMap {
101101
#[derive(Clone, Debug, PartialEq, Deserialize)]
102102
pub struct DirInfo {
103103
pub path: FilePath,
104-
pub filename_patterns: Vec<String>,
104+
pub extensions: Vec<String>,
105105
}
106106

107107
impl DirInfo {
108-
pub fn valid_filename(&self, filename: &str) -> bool {
109-
for filename_pattern in &self.filename_patterns {
110-
let re = Regex::new(filename_pattern)
111-
.unwrap_or_else(|_| panic!("invalid filename_pattern: {filename_pattern}"));
112-
if re.is_match(filename) {
113-
return true;
114-
}
115-
}
116-
false
108+
pub fn valid_filename(&self, filename: impl AsRef<Path>) -> bool {
109+
self.extensions
110+
.iter()
111+
.map(OsString::from)
112+
.any(|ext| filename.as_ref().extension().is_some_and(|e| e == ext))
117113
}
118114
}
119115

@@ -163,12 +159,6 @@ pub struct MigrationAgentConfig {
163159
pub next: Option<AgentTypeID>,
164160
}
165161

166-
impl MigrationAgentConfig {
167-
pub(crate) fn get_agent_type_fqn(&self) -> AgentTypeID {
168-
self.agent_type_fqn.clone()
169-
}
170-
}
171-
172162
impl MigrationAgentConfig {
173163
pub fn get_file(&self, fqn_to_check: AgentTypeFieldFQN) -> Option<FilePath> {
174164
for (fqn, path) in self.files_map.iter() {
@@ -207,12 +197,14 @@ configs:
207197
dirs_map:
208198
config_ohis:
209199
path: /etc/newrelic-infra/integrations.d
210-
filename_patterns:
211-
- ".*\\.ya?ml$"
200+
extensions:
201+
- "yaml"
202+
- "yml"
212203
logging:
213204
path: /etc/newrelic-infra/logging.d
214-
filename_patterns:
215-
- ".*\\.ya?ml$"
205+
extensions:
206+
- "yaml"
207+
- "yml"
216208
-
217209
agent_type_fqn: newrelic/com.newrelic.another:1.0.0
218210
files_map:
@@ -225,13 +217,15 @@ configs:
225217
dirs_map:
226218
config_integrations:
227219
path: /etc/newrelic-infra/integrations.d
228-
filename_patterns:
229-
- ".*\\.ya?ml$"
220+
extensions:
221+
- "yaml"
222+
- "yml"
230223
231224
config_logging:
232225
path: /etc/newrelic-infra/logging.d
233-
filename_patterns:
234-
- ".*\\.ya?ml$"
226+
extensions:
227+
- "yaml"
228+
- "yml"
235229
236230
-
237231
agent_type_fqn: francisco-partners/com.newrelic.another:0.0.2
@@ -245,13 +239,15 @@ configs:
245239
dirs_map:
246240
config_integrations:
247241
path: /etc/newrelic-infra/integrations.d
248-
filename_patterns:
249-
- ".*\\.ya?ml$"
250-
242+
extensions:
243+
- "yaml"
244+
- "yml"
245+
251246
config_logging:
252247
path: /etc/newrelic-infra/logging.d
253-
filename_patterns:
254-
- ".*\\.ya?ml$"
248+
extensions:
249+
- "yaml"
250+
- "yml"
255251
256252
-
257253
agent_type_fqn: newrelic/com.newrelic.another:0.0.1
@@ -311,7 +307,11 @@ configs: []
311307
#[test]
312308
fn test_dir_info() {
313309
let dir_info = DirInfo {
314-
filename_patterns: vec![String::from(".*\\.ya?ml$"), String::from(".*\\.otro$")],
310+
extensions: vec![
311+
String::from("yaml"),
312+
String::from("yml"),
313+
String::from("otro"),
314+
],
315315
path: FilePath::from("some/path"),
316316
};
317317

0 commit comments

Comments
 (0)