Skip to content

Commit 5facfd7

Browse files
committed
feat: add new command to migrate onhost folders (#1815)
* feat: new command to migrate old folder to new ones * fix command name and check logic * simplify the check logic to check only for local paths * fix more comments and simplify * move from statics agents names to scrapping fleet/agents.d
1 parent e522fbe commit 5facfd7

File tree

14 files changed

+427
-37
lines changed

14 files changed

+427
-37
lines changed

agent-control/src/agent_control/defaults.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ cfg_if::cfg_if! {
4848
}
4949
}
5050

51-
/// - **On-host**: Used as the directory name (e.g., `.../local-data/`).
51+
/// - **On-host**: Used as the directory name (e.g., `.../fleet-data/` or `.../local-data/`).
5252
/// - **k8s**: Used as a ConfigMap prefix, followed by a hyphen (e.g., `local-data-agentid`).
5353
pub const FOLDER_NAME_LOCAL_DATA: &str = "local-data";
54-
55-
/// - **On-host**: Used as the directory name (e.g., `.../fleet-data/`).
56-
/// - **k8s**: Used as a ConfigMap prefix, followed by a hyphen (e.g., `fleet-data-agentid`).
5754
pub const FOLDER_NAME_FLEET_DATA: &str = "fleet-data";
5855

5956
/// - **On-host**: Used as the base filename, combined with ".yaml" (e.g., `local_config.yaml`).

agent-control/src/bin/main_agent_control_onhost_cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::process::ExitCode;
22

33
use clap::{CommandFactory, Parser, error::ErrorKind};
4+
use newrelic_agent_control::cli::on_host::migrate_folders;
45
use newrelic_agent_control::cli::on_host::{host_monitoring_gen, systemd_gen};
56
use newrelic_agent_control::cli::{logs, on_host::config_gen};
67
use tracing::{Level, error};
@@ -25,6 +26,8 @@ enum Commands {
2526
HostMonitoring(host_monitoring_gen::Args),
2627
// Generate systemd configuration according to the provided configuration data.
2728
SystemdConfig(systemd_gen::Args),
29+
/// Migrate from the older on host folders to the new ones. DON'T USE IT
30+
FilesBackwardsCompatibilityMigrationFromV120,
2831
}
2932

3033
fn main() -> ExitCode {
@@ -49,6 +52,7 @@ fn main() -> ExitCode {
4952
host_monitoring_gen::generate_host_monitoring_config(args)
5053
}
5154
Commands::SystemdConfig(args) => systemd_gen::generate_systemd_config(args),
55+
Commands::FilesBackwardsCompatibilityMigrationFromV120 => migrate_folders::migrate(),
5256
};
5357

5458
if let Err(err) = result {

agent-control/src/cli/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::process::ExitCode;
22

3-
use thiserror::Error;
4-
53
use crate::instrumentation::tracing::TracingError;
4+
use thiserror::Error;
65

76
#[derive(Debug, Error)]
87
pub enum CliError {
@@ -14,6 +13,9 @@ pub enum CliError {
1413

1514
#[error("{0}")]
1615
Command(String),
16+
17+
#[error("File system error: {0}")]
18+
FileSystemError(String),
1719
}
1820

1921
impl From<CliError> for ExitCode {
@@ -29,6 +31,7 @@ impl From<CliError> for ExitCode {
2931
CliError::Precondition(_) => Self::from(69),
3032
CliError::Tracing(_) => Self::from(70),
3133
CliError::Command(_) => Self::from(1),
34+
CliError::FileSystemError(_) => Self::from(1),
3235
}
3336
}
3437
}

agent-control/src/cli/on_host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pub mod config_gen;
2-
32
pub mod host_monitoring_gen;
3+
pub mod migrate_folders;
44
pub mod systemd_gen;

agent-control/src/cli/on_host/host_monitoring_gen/infra_config_gen.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::agent_control::config_repository::store::AgentControlConfigStore;
2-
use crate::agent_control::defaults::{
3-
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, SUB_AGENT_DIR,
4-
};
2+
use crate::agent_control::defaults::{AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR};
53
use crate::agent_type::agent_type_id::AgentTypeID;
64
use crate::cli::error::CliError;
75
use crate::cli::on_host::config_gen::region::Region;
@@ -97,8 +95,7 @@ impl InfraConfigGenerator {
9795
};
9896

9997
for (agent_id, _) in sub_agents_cfg.agents {
100-
let infra_values_persister =
101-
ValuesPersisterFile::new(self.local_dir.join(SUB_AGENT_DIR));
98+
let infra_values_persister = ValuesPersisterFile::new(self.local_dir.clone());
10299
infra_values_persister
103100
.persist_values_file(
104101
&agent_id,
@@ -126,7 +123,7 @@ impl InfraConfigGenerator {
126123
let config_migrator = ConfigMigrator::new(
127124
ConfigConverter::default(),
128125
AgentConfigGetter::new(sa_local_config_loader),
129-
ValuesPersisterFile::new(self.local_dir.join(SUB_AGENT_DIR)),
126+
ValuesPersisterFile::new(self.local_dir.clone()),
130127
);
131128

132129
let legacy_config_renamer = LegacyConfigRenamer::default();
@@ -169,7 +166,12 @@ impl InfraConfigGenerator {
169166
#[cfg(test)]
170167
mod tests {
171168
use super::*;
169+
use crate::agent_control::defaults::{
170+
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG,
171+
};
172+
use crate::opamp::instance_id::on_host::storer::build_config_name;
172173
use std::fs;
174+
use std::fs::create_dir_all;
173175
use tempfile::TempDir;
174176

175177
const INITIAL_INFRA_CONFIG: &str = r#"
@@ -187,20 +189,27 @@ agents:
187189
agent_type: "newrelic/com.newrelic.infrastructure:0.1.0"
188190
"#;
189191

190-
const INFRA_AGENT_VALUES: &str = "fleet/agents.d/infra-test/values/values.yaml";
192+
const INFRA_AGENT_VALUES: &str = "local-data/infra-test/local_config.yaml";
191193

192194
#[cfg(target_family = "unix")] //TODO This should be removed when Windows support is added (DirectoryManager unimplemented)
193195
#[test]
194196
fn test_migrate_old_infra_config() {
195197
// Create a temporary directory
196198
let temp_dir = TempDir::new().unwrap();
197199
let infra_file_path = temp_dir.path().join("newrelic-infra.yml");
198-
let agents_file_path = temp_dir.path().join("config.yaml");
199-
200+
let agents_file_path = temp_dir
201+
.path()
202+
.join(FOLDER_NAME_LOCAL_DATA)
203+
.join(AGENT_CONTROL_ID);
204+
create_dir_all(&agents_file_path).unwrap();
200205
// Emulate the existence of the file by creating it
201206
fs::write(&infra_file_path, INITIAL_INFRA_CONFIG).unwrap();
202207

203-
fs::write(&agents_file_path, AGENTS_CONFIG).unwrap();
208+
fs::write(
209+
agents_file_path.join(build_config_name(STORE_KEY_LOCAL_DATA_CONFIG)),
210+
AGENTS_CONFIG,
211+
)
212+
.unwrap();
204213

205214
// Format the string using dynamic file path
206215
let config_mapping = format!(
@@ -256,9 +265,17 @@ config_agent:
256265
#[test]
257266
fn test_generate_new_infra_config() {
258267
let temp_dir = TempDir::new().unwrap();
259-
let agents_file_path = temp_dir.path().join("config.yaml");
260-
261-
fs::write(&agents_file_path, AGENTS_CONFIG).unwrap();
268+
let agents_file_path = temp_dir
269+
.path()
270+
.join(FOLDER_NAME_LOCAL_DATA)
271+
.join(AGENT_CONTROL_ID);
272+
create_dir_all(&agents_file_path).unwrap();
273+
274+
fs::write(
275+
agents_file_path.join(build_config_name(STORE_KEY_LOCAL_DATA_CONFIG)),
276+
AGENTS_CONFIG,
277+
)
278+
.unwrap();
262279

263280
let infra_config_generator = InfraConfigGenerator::new(
264281
temp_dir.path().to_path_buf(),

agent-control/src/cli/on_host/host_monitoring_gen/otel_config_gen.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
use crate::agent_control::defaults::{
2+
AGENT_CONTROL_LOCAL_DATA_DIR, FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG,
3+
};
14
use crate::cli::error::CliError;
5+
use crate::opamp::instance_id::on_host::storer::build_config_name;
26
use std::path::PathBuf;
37
use tracing::info;
48

@@ -11,9 +15,9 @@ pub struct OtelConfigGen {
1115
impl Default for OtelConfigGen {
1216
fn default() -> Self {
1317
Self {
14-
otel_agent_values_path: PathBuf::from(
15-
"/etc/newrelic-agent-control/fleet/agents.d/nrdot/values",
16-
),
18+
otel_agent_values_path: PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR)
19+
.join(FOLDER_NAME_LOCAL_DATA)
20+
.join("nrdot"),
1721
otel_config_source_path: PathBuf::from(
1822
"/etc/newrelic-agent-control/examples/values-nr-otel-collector-agent-linux.yaml",
1923
),
@@ -42,7 +46,9 @@ impl OtelConfigGen {
4246

4347
fn modify_values_yaml(&self) -> Result<(), CliError> {
4448
let source_path = self.otel_config_source_path.clone();
45-
let file_path = self.otel_agent_values_path.join("values.yaml");
49+
let file_path = self
50+
.otel_agent_values_path
51+
.join(build_config_name(STORE_KEY_LOCAL_DATA_CONFIG));
4652
let content = std::fs::read_to_string(source_path)
4753
.map_err(|err| CliError::Command(format!("error reading otel values file: {err}")))?;
4854

@@ -67,6 +73,8 @@ impl OtelConfigGen {
6773
#[cfg(test)]
6874
mod tests {
6975
use super::*;
76+
use crate::agent_control::defaults::{FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG};
77+
use crate::opamp::instance_id::on_host::storer::build_config_name;
7078
use std::fs;
7179
use std::io::Write;
7280
use tempfile::tempdir;
@@ -83,7 +91,7 @@ mod tests {
8391
#[test]
8492
fn test_generate_otel_config_creates_directories_and_copies_file() {
8593
let temp_dir = tempdir().unwrap();
86-
let temp_values_dir = temp_dir.path().join("fleet/agents.d/nrdot/values");
94+
let temp_values_dir = temp_dir.path().join(FOLDER_NAME_LOCAL_DATA).join("nrdot");
8795
let temp_example_file = temp_dir
8896
.path()
8997
.join("values-nr-otel-collector-agent-linux.yaml");
@@ -100,7 +108,7 @@ mod tests {
100108
assert!(result.is_ok());
101109
assert!(temp_values_dir.exists());
102110

103-
let values_file = temp_values_dir.join("values.yaml");
111+
let values_file = temp_values_dir.join(build_config_name(STORE_KEY_LOCAL_DATA_CONFIG));
104112
let values_content = fs::read_to_string(&values_file).unwrap();
105113
assert!(values_content.contains("limit_mib: 100"));
106114
assert!(values_content.contains("OTHER_CONFIG: value"));

0 commit comments

Comments
 (0)