Skip to content

Commit 6778f0d

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 6778f0d

File tree

13 files changed

+413
-33
lines changed

13 files changed

+413
-33
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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ pub struct OtelConfigGen {
1111
impl Default for OtelConfigGen {
1212
fn default() -> Self {
1313
Self {
14-
otel_agent_values_path: PathBuf::from(
15-
"/etc/newrelic-agent-control/fleet/agents.d/nrdot/values",
16-
),
14+
otel_agent_values_path: PathBuf::from("/etc/newrelic-agent-control/local-data/nrdot"),
1715
otel_config_source_path: PathBuf::from(
1816
"/etc/newrelic-agent-control/examples/values-nr-otel-collector-agent-linux.yaml",
1917
),

0 commit comments

Comments
 (0)