Skip to content

Commit 6e41010

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 6e41010

File tree

13 files changed

+416
-33
lines changed

13 files changed

+416
-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: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::agent_control::config_repository::store::AgentControlConfigStore;
22
use crate::agent_control::defaults::{
3-
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, SUB_AGENT_DIR,
3+
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, FOLDER_NAME_FLEET_DATA,
4+
FOLDER_NAME_LOCAL_DATA,
45
};
56
use crate::agent_type::agent_type_id::AgentTypeID;
67
use crate::cli::error::CliError;
@@ -34,8 +35,8 @@ const INFRA_CONFIG_PATH: &str = "/etc/newrelic-infra.yml";
3435
impl Default for InfraConfigGenerator {
3536
fn default() -> Self {
3637
Self {
37-
local_dir: PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR),
38-
remote_dir: PathBuf::from(AGENT_CONTROL_DATA_DIR),
38+
local_dir: PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR).join(FOLDER_NAME_LOCAL_DATA),
39+
remote_dir: PathBuf::from(AGENT_CONTROL_DATA_DIR).join(FOLDER_NAME_FLEET_DATA),
3940
config_mapping: NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING.to_string(),
4041
infra_config_path: Path::new(INFRA_CONFIG_PATH).to_path_buf(),
4142
}
@@ -97,8 +98,7 @@ impl InfraConfigGenerator {
9798
};
9899

99100
for (agent_id, _) in sub_agents_cfg.agents {
100-
let infra_values_persister =
101-
ValuesPersisterFile::new(self.local_dir.join(SUB_AGENT_DIR));
101+
let infra_values_persister = ValuesPersisterFile::new(self.local_dir.clone());
102102
infra_values_persister
103103
.persist_values_file(
104104
&agent_id,
@@ -126,7 +126,7 @@ impl InfraConfigGenerator {
126126
let config_migrator = ConfigMigrator::new(
127127
ConfigConverter::default(),
128128
AgentConfigGetter::new(sa_local_config_loader),
129-
ValuesPersisterFile::new(self.local_dir.join(SUB_AGENT_DIR)),
129+
ValuesPersisterFile::new(self.local_dir.clone()),
130130
);
131131

132132
let legacy_config_renamer = LegacyConfigRenamer::default();
@@ -169,7 +169,12 @@ impl InfraConfigGenerator {
169169
#[cfg(test)]
170170
mod tests {
171171
use super::*;
172+
use crate::agent_control::defaults::{
173+
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG,
174+
};
175+
use crate::opamp::instance_id::on_host::storer::build_config_name;
172176
use std::fs;
177+
use std::fs::create_dir_all;
173178
use tempfile::TempDir;
174179

175180
const INITIAL_INFRA_CONFIG: &str = r#"
@@ -187,20 +192,27 @@ agents:
187192
agent_type: "newrelic/com.newrelic.infrastructure:0.1.0"
188193
"#;
189194

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

192197
#[cfg(target_family = "unix")] //TODO This should be removed when Windows support is added (DirectoryManager unimplemented)
193198
#[test]
194199
fn test_migrate_old_infra_config() {
195200
// Create a temporary directory
196201
let temp_dir = TempDir::new().unwrap();
197202
let infra_file_path = temp_dir.path().join("newrelic-infra.yml");
198-
let agents_file_path = temp_dir.path().join("config.yaml");
199-
203+
let agents_file_path = temp_dir
204+
.path()
205+
.join(FOLDER_NAME_LOCAL_DATA)
206+
.join(AGENT_CONTROL_ID);
207+
create_dir_all(&agents_file_path).unwrap();
200208
// Emulate the existence of the file by creating it
201209
fs::write(&infra_file_path, INITIAL_INFRA_CONFIG).unwrap();
202210

203-
fs::write(&agents_file_path, AGENTS_CONFIG).unwrap();
211+
fs::write(
212+
agents_file_path.join(build_config_name(STORE_KEY_LOCAL_DATA_CONFIG)),
213+
AGENTS_CONFIG,
214+
)
215+
.unwrap();
204216

205217
// Format the string using dynamic file path
206218
let config_mapping = format!(
@@ -256,9 +268,17 @@ config_agent:
256268
#[test]
257269
fn test_generate_new_infra_config() {
258270
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();
271+
let agents_file_path = temp_dir
272+
.path()
273+
.join(FOLDER_NAME_LOCAL_DATA)
274+
.join(AGENT_CONTROL_ID);
275+
create_dir_all(&agents_file_path).unwrap();
276+
277+
fs::write(
278+
agents_file_path.join(build_config_name(STORE_KEY_LOCAL_DATA_CONFIG)),
279+
AGENTS_CONFIG,
280+
)
281+
.unwrap();
262282

263283
let infra_config_generator = InfraConfigGenerator::new(
264284
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)