Skip to content

Commit d856dc8

Browse files
committed
simplify the check logic to check only for local paths
1 parent 29763c9 commit d856dc8

File tree

1 file changed

+12
-58
lines changed

1 file changed

+12
-58
lines changed

agent-control/src/cli/on_host/migrate_folders.rs

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const REMOTE_DATA_DIR: &str = "/var/lib/newrelic-agent-control";
1313
const OLD_ENV_FILE_NAME: &str = "newrelic-agent-control.conf";
1414
const NEW_ENV_FILE_NAME: &str = "systemd-env.conf";
1515
const OTEL_AGENT_ID: &str = "nrdot";
16+
const VALUES_FOLDER: &str = "values";
1617
const INFRA_AGENT_ID: &str = "nr-infra";
1718
// old folder and file names
1819
const OLD_CONFIG_AGENT_CONTROL_FILE_NAME: &str = "config.yaml";
@@ -26,20 +27,14 @@ pub fn migrate() -> Result<(), CliError> {
2627
let remote_base = PathBuf::from(REMOTE_DATA_DIR);
2728

2829
let new_local_data_path = local_base.join(FOLDER_NAME_LOCAL_DATA);
29-
let new_fleet_data_path = remote_base.join(FOLDER_NAME_FLEET_DATA);
30-
31-
if !check_new_folders(&new_local_data_path, &new_fleet_data_path) {
30+
31+
// Check if the new folder already exists - local-data
32+
if new_local_data_path.exists() && new_local_data_path.is_dir() {
3233
move_and_rename(&local_base, &remote_base)?;
3334
}
3435
Ok(())
3536
}
36-
// Check if the new folders already exists - local-data or fleet-data
37-
fn check_new_folders(local_new_path: &Path, remote_new_path: &Path) -> bool {
38-
let local_exists = local_new_path.exists() && local_new_path.is_dir();
39-
let fleet_exists = remote_new_path.exists() && remote_new_path.is_dir();
4037

41-
local_exists || fleet_exists
42-
}
4338
// Copy the old files in the new paths but leaving the old ones in place
4439
fn move_and_rename(local_base: &Path, remote_base: &Path) -> Result<(), CliError> {
4540
debug!("Starting migration: moving files from old structure to new structure...");
@@ -156,15 +151,15 @@ fn add_infra_agent_files(
156151
) {
157152
// --- LOCAL ---
158153
let old_local_infra_dir = local_base.join(OLD_SUB_AGENT_DATA_DIR).join(INFRA_AGENT_ID);
159-
if old_local_infra_dir.exists() {
154+
if old_local_infra_dir.exists() && old_local_infra_dir.is_dir() {
160155
debug!(
161156
"Found old local nr-infra directory, adding to migration: {}",
162157
old_local_infra_dir.display()
163158
);
164159
// nf-infra values.yaml -> local-data/nr-infra/local_config.yaml
165160
migration_pairs.push((
166161
old_local_infra_dir
167-
.join("values")
162+
.join(VALUES_FOLDER)
168163
.join(OLD_CONFIG_SUB_AGENT_FILE_NAME),
169164
local_base
170165
.join(FOLDER_NAME_LOCAL_DATA)
@@ -177,15 +172,15 @@ fn add_infra_agent_files(
177172
let old_remote_infra_dir = remote_base
178173
.join(OLD_SUB_AGENT_DATA_DIR)
179174
.join(INFRA_AGENT_ID);
180-
if old_remote_infra_dir.exists() {
175+
if old_remote_infra_dir.exists() && old_remote_infra_dir.is_dir() {
181176
debug!(
182177
"Found old remote nr-infra directory, adding to migration: {}",
183178
old_remote_infra_dir.display()
184179
);
185180
// nr-infra values.yaml -> fleet-data/nr-infra/remote_config.yaml
186181
migration_pairs.push((
187182
old_remote_infra_dir
188-
.join("values")
183+
.join(VALUES_FOLDER)
189184
.join(OLD_CONFIG_SUB_AGENT_FILE_NAME),
190185
remote_base
191186
.join(FOLDER_NAME_FLEET_DATA)
@@ -210,15 +205,15 @@ fn add_otel_agent_files(
210205
) {
211206
// --- LOCAL ---
212207
let old_local_otel_dir = local_base.join(OLD_SUB_AGENT_DATA_DIR).join(OTEL_AGENT_ID);
213-
if old_local_otel_dir.exists() {
208+
if old_local_otel_dir.exists() && old_local_otel_dir.is_dir() {
214209
debug!(
215210
"Found old local nrdot directory, adding to migration: {}",
216211
old_local_otel_dir.display()
217212
);
218213
// nrdot values.yaml -> local-data/nrdot/local_config.yaml
219214
migration_pairs.push((
220215
old_local_otel_dir
221-
.join("values")
216+
.join(VALUES_FOLDER)
222217
.join(OLD_CONFIG_SUB_AGENT_FILE_NAME),
223218
local_base
224219
.join(FOLDER_NAME_LOCAL_DATA)
@@ -229,15 +224,15 @@ fn add_otel_agent_files(
229224

230225
// --- REMOTE ---
231226
let old_remote_otel_dir = remote_base.join(OLD_SUB_AGENT_DATA_DIR).join(OTEL_AGENT_ID);
232-
if old_remote_otel_dir.exists() {
227+
if old_remote_otel_dir.exists() && old_remote_otel_dir.is_dir() {
233228
debug!(
234229
"Found old remote nrdot directory, adding to migration: {}",
235230
old_remote_otel_dir.display()
236231
);
237232
// nrdot values.yaml -> fleet-data/nrdot/remote_config.yaml
238233
migration_pairs.push((
239234
old_remote_otel_dir
240-
.join("values")
235+
.join(VALUES_FOLDER)
241236
.join(OLD_CONFIG_SUB_AGENT_FILE_NAME),
242237
remote_base
243238
.join(FOLDER_NAME_FLEET_DATA)
@@ -271,35 +266,6 @@ mod tests {
271266
use std::fs::{self, File};
272267
use tempfile::tempdir;
273268

274-
#[test]
275-
fn test_check_new_folders_false_when_missing() {
276-
let temp_dir = tempdir().unwrap();
277-
let local_base = temp_dir.path().join("local");
278-
let data_base = temp_dir.path().join("data");
279-
280-
let local_new_path = local_base.join(FOLDER_NAME_LOCAL_DATA);
281-
let remote_new_path = data_base.join(FOLDER_NAME_FLEET_DATA);
282-
283-
let exists = check_new_folders(&local_new_path, &remote_new_path);
284-
assert!(!exists);
285-
}
286-
287-
#[test]
288-
fn test_check_new_folders_true_when_present() {
289-
let temp_dir = tempdir().unwrap();
290-
let local_base = temp_dir.path().join("local");
291-
let data_base = temp_dir.path().join("data");
292-
293-
let local_new_path = local_base.join(FOLDER_NAME_LOCAL_DATA);
294-
let remote_new_path = data_base.join(FOLDER_NAME_FLEET_DATA);
295-
296-
fs::create_dir_all(&local_new_path).unwrap();
297-
fs::create_dir_all(&remote_new_path).unwrap();
298-
299-
let exists = check_new_folders(&local_new_path, &remote_new_path);
300-
assert!(exists);
301-
}
302-
303269
#[test]
304270
fn test_move_item_success() {
305271
let temp_dir = tempdir().unwrap();
@@ -365,13 +331,6 @@ mod tests {
365331
"Migration list should contain all 10 items when old agent dirs exist"
366332
);
367333

368-
let new_local_data_path = local_base.join(FOLDER_NAME_LOCAL_DATA);
369-
let new_fleet_data_path = remote_base.join(FOLDER_NAME_FLEET_DATA);
370-
assert!(
371-
!check_new_folders(&new_local_data_path, &new_fleet_data_path),
372-
"New folders should not exist yet"
373-
);
374-
375334
for (old_path, _) in migration_pairs.iter() {
376335
let parent = old_path.parent().unwrap();
377336
fs::create_dir_all(parent).unwrap_or_else(|e| {
@@ -401,11 +360,6 @@ mod tests {
401360
new_path.display()
402361
);
403362
}
404-
405-
assert!(
406-
check_new_folders(&new_local_data_path, &new_fleet_data_path),
407-
"New folders should be detected after migration"
408-
);
409363
}
410364

411365
#[test]

0 commit comments

Comments
 (0)