@@ -12,9 +12,7 @@ const LOCAL_DATA_DIR: &str = "/etc/newrelic-agent-control";
1212const REMOTE_DATA_DIR : & str = "/var/lib/newrelic-agent-control" ;
1313const OLD_ENV_FILE_NAME : & str = "newrelic-agent-control.conf" ;
1414const NEW_ENV_FILE_NAME : & str = "systemd-env.conf" ;
15- const OTEL_AGENT_ID : & str = "nrdot" ;
1615const VALUES_FOLDER : & str = "values" ;
17- const INFRA_AGENT_ID : & str = "nr-infra" ;
1816// old folder and file names
1917const OLD_CONFIG_AGENT_CONTROL_FILE_NAME : & str = "config.yaml" ;
2018const OLD_IDENTIFIERS_YAML : & str = "identifiers.yaml" ;
@@ -83,36 +81,33 @@ fn copy_and_rename_item(old_path: &Path, new_path: &Path) -> Result<(), CliError
8381 }
8482 Ok ( ( ) )
8583}
84+
8685fn add_agent_control_files (
8786 migration_pairs : & mut Vec < ( PathBuf , PathBuf ) > ,
8887 local_base : & Path ,
8988 remote_base : & Path ,
9089) {
9190 // --- LOCAL ---
92- // agent control config.yaml -> local-data/agent-control/local_config.yaml
9391 migration_pairs. push ( (
9492 local_base. join ( OLD_CONFIG_AGENT_CONTROL_FILE_NAME ) ,
9593 local_base
9694 . join ( FOLDER_NAME_LOCAL_DATA )
9795 . join ( AGENT_CONTROL_ID )
9896 . join ( build_config_name ( STORE_KEY_LOCAL_DATA_CONFIG ) ) ,
9997 ) ) ;
100- // agent-control-config.conf -> systemd-env.conf
10198 migration_pairs. push ( (
10299 local_base. join ( OLD_ENV_FILE_NAME ) ,
103100 local_base. join ( NEW_ENV_FILE_NAME ) ,
104101 ) ) ;
105102
106103 // --- REMOTE ---
107- // agent control config.yaml -> fleet-data/agent-control/remote_config.yaml
108104 migration_pairs. push ( (
109105 remote_base. join ( OLD_CONFIG_AGENT_CONTROL_FILE_NAME ) ,
110106 remote_base
111107 . join ( FOLDER_NAME_FLEET_DATA )
112108 . join ( AGENT_CONTROL_ID )
113109 . join ( build_config_name ( STORE_KEY_OPAMP_DATA_CONFIG ) ) ,
114110 ) ) ;
115- // agent control identifiers.yaml -> fleet-data/agent-control/instance_id.yaml
116111 migration_pairs. push ( (
117112 remote_base. join ( OLD_IDENTIFIERS_YAML ) ,
118113 remote_base
@@ -121,119 +116,79 @@ fn add_agent_control_files(
121116 . join ( INSTANCE_ID_FILENAME ) ,
122117 ) ) ;
123118}
124-
125- fn add_infra_agent_files (
119+ fn discover_and_add_sub_agents (
126120 migration_pairs : & mut Vec < ( PathBuf , PathBuf ) > ,
127- local_base : & Path ,
128- remote_base : & Path ,
121+ old_agents_dir : & Path ,
122+ new_base_dir : & Path ,
123+ new_data_folder : & str ,
124+ config_key : & str ,
125+ is_remote : bool ,
129126) {
130- // --- LOCAL ---
131- let old_local_infra_dir = local_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( INFRA_AGENT_ID ) ;
132- if old_local_infra_dir. exists ( ) && old_local_infra_dir. is_dir ( ) {
133- debug ! (
134- "Found old local nr-infra directory, adding to migration: {}" ,
135- old_local_infra_dir. display( )
136- ) ;
137- // nf-infra values.yaml -> local-data/nr-infra/local_config.yaml
138- migration_pairs. push ( (
139- old_local_infra_dir
140- . join ( VALUES_FOLDER )
141- . join ( OLD_CONFIG_SUB_AGENT_FILE_NAME ) ,
142- local_base
143- . join ( FOLDER_NAME_LOCAL_DATA )
144- . join ( INFRA_AGENT_ID )
145- . join ( build_config_name ( STORE_KEY_LOCAL_DATA_CONFIG ) ) ,
146- ) ) ;
127+ if !old_agents_dir. is_dir ( ) {
128+ return ;
147129 }
148-
149- // --- REMOTE ---
150- let old_remote_infra_dir = remote_base
151- . join ( OLD_SUB_AGENT_DATA_DIR )
152- . join ( INFRA_AGENT_ID ) ;
153- if old_remote_infra_dir. exists ( ) && old_remote_infra_dir. is_dir ( ) {
130+ let entries = if let Ok ( entries) = fs:: read_dir ( old_agents_dir) {
131+ entries
132+ } else {
154133 debug ! (
155- "Found old remote nr-infra directory, adding to migration: {} " ,
156- old_remote_infra_dir . display( )
134+ "Could not read old agent directory '{}' " ,
135+ old_agents_dir . display( ) ,
157136 ) ;
158- // nr-infra values.yaml -> fleet-data/nr-infra/remote_config.yaml
159- migration_pairs. push ( (
160- old_remote_infra_dir
161- . join ( VALUES_FOLDER )
162- . join ( OLD_CONFIG_SUB_AGENT_FILE_NAME ) ,
163- remote_base
164- . join ( FOLDER_NAME_FLEET_DATA )
165- . join ( INFRA_AGENT_ID )
166- . join ( build_config_name ( STORE_KEY_OPAMP_DATA_CONFIG ) ) ,
167- ) ) ;
168- // nr-infra identifiers.yaml -> fleet-data/nr-infra/instance_id.yaml
169- migration_pairs. push ( (
170- old_remote_infra_dir. join ( OLD_IDENTIFIERS_YAML ) ,
171- remote_base
172- . join ( FOLDER_NAME_FLEET_DATA )
173- . join ( INFRA_AGENT_ID )
174- . join ( INSTANCE_ID_FILENAME ) ,
175- ) ) ;
176- }
177- }
137+ return ;
138+ } ;
178139
179- fn add_otel_agent_files (
180- migration_pairs : & mut Vec < ( PathBuf , PathBuf ) > ,
181- local_base : & Path ,
182- remote_base : & Path ,
183- ) {
184- // --- LOCAL ---
185- let old_local_otel_dir = local_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( OTEL_AGENT_ID ) ;
186- if old_local_otel_dir. exists ( ) && old_local_otel_dir. is_dir ( ) {
187- debug ! (
188- "Found old local nrdot directory, adding to migration: {}" ,
189- old_local_otel_dir. display( )
190- ) ;
191- // nrdot values.yaml -> local-data/nrdot/local_config.yaml
192- migration_pairs. push ( (
193- old_local_otel_dir
194- . join ( VALUES_FOLDER )
195- . join ( OLD_CONFIG_SUB_AGENT_FILE_NAME ) ,
196- local_base
197- . join ( FOLDER_NAME_LOCAL_DATA )
198- . join ( OTEL_AGENT_ID )
199- . join ( build_config_name ( STORE_KEY_LOCAL_DATA_CONFIG ) ) ,
200- ) ) ;
201- }
140+ let agent_iter = entries
141+ . filter_map ( Result :: ok)
142+ . map ( |entry| entry. path ( ) )
143+ . filter ( |path| path. is_dir ( ) )
144+ . filter_map ( |p| p. file_name ( ) . map ( |f| f. to_owned ( ) ) . map ( |f| ( p, f) ) ) ;
202145
203- // --- REMOTE ---
204- let old_remote_otel_dir = remote_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( OTEL_AGENT_ID ) ;
205- if old_remote_otel_dir. exists ( ) && old_remote_otel_dir. is_dir ( ) {
146+ for ( old_agent_dir, agent_id) in agent_iter {
206147 debug ! (
207- "Found old remote nrdot directory , adding to migration: {} " ,
208- old_remote_otel_dir . display( )
148+ "Discovered old agent '{}' , adding to migration. " ,
149+ old_agent_dir . display( )
209150 ) ;
210- // nrdot values.yaml -> fleet-data/nrdot/remote_config.yaml
151+
152+ let new_agent_dir = new_base_dir. join ( new_data_folder) . join ( agent_id) ;
153+
211154 migration_pairs. push ( (
212- old_remote_otel_dir
155+ old_agent_dir
213156 . join ( VALUES_FOLDER )
214157 . join ( OLD_CONFIG_SUB_AGENT_FILE_NAME ) ,
215- remote_base
216- . join ( FOLDER_NAME_FLEET_DATA )
217- . join ( OTEL_AGENT_ID )
218- . join ( build_config_name ( STORE_KEY_OPAMP_DATA_CONFIG ) ) ,
219- ) ) ;
220- // nrdot identifiers.yaml -> fleet-data/nrdot/instance_id.yaml
221- migration_pairs. push ( (
222- old_remote_otel_dir. join ( OLD_IDENTIFIERS_YAML ) ,
223- remote_base
224- . join ( FOLDER_NAME_FLEET_DATA )
225- . join ( OTEL_AGENT_ID )
226- . join ( INSTANCE_ID_FILENAME ) ,
158+ new_agent_dir. join ( build_config_name ( config_key) ) ,
227159 ) ) ;
160+
161+ if is_remote {
162+ migration_pairs. push ( (
163+ old_agent_dir. join ( OLD_IDENTIFIERS_YAML ) ,
164+ new_agent_dir. join ( INSTANCE_ID_FILENAME ) ,
165+ ) ) ;
166+ }
228167 }
229168}
230169
231170fn get_migration_list ( local_base : & Path , remote_base : & Path ) -> Vec < ( PathBuf , PathBuf ) > {
232171 let mut migration_pairs = Vec :: new ( ) ;
233172
234173 add_agent_control_files ( & mut migration_pairs, local_base, remote_base) ;
235- add_infra_agent_files ( & mut migration_pairs, local_base, remote_base) ;
236- add_otel_agent_files ( & mut migration_pairs, local_base, remote_base) ;
174+
175+ discover_and_add_sub_agents (
176+ & mut migration_pairs,
177+ & local_base. join ( OLD_SUB_AGENT_DATA_DIR ) ,
178+ local_base,
179+ FOLDER_NAME_LOCAL_DATA ,
180+ STORE_KEY_LOCAL_DATA_CONFIG ,
181+ false ,
182+ ) ;
183+
184+ discover_and_add_sub_agents (
185+ & mut migration_pairs,
186+ & remote_base. join ( OLD_SUB_AGENT_DATA_DIR ) ,
187+ remote_base,
188+ FOLDER_NAME_FLEET_DATA ,
189+ STORE_KEY_OPAMP_DATA_CONFIG ,
190+ true ,
191+ ) ;
237192
238193 migration_pairs
239194}
@@ -251,7 +206,6 @@ mod tests {
251206 let new_path = temp_dir. path ( ) . join ( "new.txt" ) ;
252207
253208 File :: create ( & old_path) . unwrap ( ) ;
254-
255209 assert ! ( old_path. exists( ) ) ;
256210 assert ! ( !new_path. exists( ) ) ;
257211
@@ -272,18 +226,16 @@ mod tests {
272226 let new_path = temp_dir. path ( ) . join ( "new.txt" ) ;
273227
274228 assert ! ( !old_path. exists( ) ) ;
275-
276229 let result = copy_and_rename_item ( & old_path, & new_path) ;
277230 assert ! ( result. is_ok( ) ) ;
278-
279231 assert ! (
280232 !new_path. exists( ) ,
281233 "The new file should not have been created"
282234 ) ;
283235 }
284236
285237 #[ test]
286- fn test_full_migration_logic_with_all_agents ( ) {
238+ fn test_full_migration_logic_with_dynamic_agents ( ) {
287239 let temp_dir = tempdir ( ) . unwrap ( ) ;
288240 let root = temp_dir. path ( ) ;
289241
@@ -292,21 +244,28 @@ mod tests {
292244 fs:: create_dir_all ( & local_base) . unwrap ( ) ;
293245 fs:: create_dir_all ( & remote_base) . unwrap ( ) ;
294246
295- fs:: create_dir_all ( local_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( INFRA_AGENT_ID ) ) . unwrap ( ) ;
296- fs:: create_dir_all ( local_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( OTEL_AGENT_ID ) ) . unwrap ( ) ;
297- fs:: create_dir_all (
298- remote_base
299- . join ( OLD_SUB_AGENT_DATA_DIR )
300- . join ( INFRA_AGENT_ID ) ,
301- )
302- . unwrap ( ) ;
303- fs:: create_dir_all ( remote_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( OTEL_AGENT_ID ) ) . unwrap ( ) ;
247+ let agent1_id = "nr-infra" ;
248+ let agent2_id = "nrdot" ;
249+ let agent3_id = "my-custom-agent" ;
250+
251+ fs:: create_dir_all ( local_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( agent1_id) ) . unwrap ( ) ;
252+ fs:: create_dir_all ( remote_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( agent1_id) ) . unwrap ( ) ;
253+ fs:: create_dir_all ( local_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( agent2_id) ) . unwrap ( ) ;
254+ fs:: create_dir_all ( remote_base. join ( OLD_SUB_AGENT_DATA_DIR ) . join ( agent3_id) ) . unwrap ( ) ;
304255
305256 let migration_pairs = get_migration_list ( & local_base, & remote_base) ;
306257 assert_eq ! (
307258 migration_pairs. len( ) ,
308259 10 ,
309- "Migration list should contain all 10 items when old agent dirs exist"
260+ "Migration list should contain all dynamically found items"
261+ ) ;
262+
263+ let has_custom_agent = migration_pairs
264+ . iter ( )
265+ . any ( |( _old, new) | new. to_str ( ) . unwrap_or_default ( ) . contains ( agent3_id) ) ;
266+ assert ! (
267+ has_custom_agent,
268+ "Dynamically discovered agent 'my-custom-agent' was not found in migration pairs"
310269 ) ;
311270
312271 for ( old_path, _) in migration_pairs. iter ( ) {
@@ -338,6 +297,12 @@ mod tests {
338297 new_path. display( )
339298 ) ;
340299 }
300+
301+ let migration_pairs_2 = get_migration_list ( & local_base, & remote_base) ;
302+ assert_eq ! ( migration_pairs_2. len( ) , 10 ) ;
303+
304+ let result_2 = migrate ( ) ;
305+ assert ! ( result_2. is_ok( ) ) ;
341306 }
342307
343308 #[ test]
@@ -371,43 +336,4 @@ mod tests {
371336 assert ! ( new_path. exists( ) ) ;
372337 }
373338 }
374-
375- #[ test]
376- fn test_migration_logic_only_remote_infra ( ) {
377- let temp_dir = tempdir ( ) . unwrap ( ) ;
378- let root = temp_dir. path ( ) ;
379-
380- let local_base = root. join ( "etc" ) ;
381- let remote_base = root. join ( "var" ) ;
382- fs:: create_dir_all ( & local_base) . unwrap ( ) ;
383- fs:: create_dir_all ( & remote_base) . unwrap ( ) ;
384-
385- fs:: create_dir_all (
386- remote_base
387- . join ( OLD_SUB_AGENT_DATA_DIR )
388- . join ( INFRA_AGENT_ID ) ,
389- )
390- . unwrap ( ) ;
391-
392- let migration_pairs = get_migration_list ( & local_base, & remote_base) ;
393- assert_eq ! (
394- migration_pairs. len( ) ,
395- 6 ,
396- "Migration list should contain 6 items (4 ac + 2 remote infra)"
397- ) ;
398-
399- for ( old_path, _) in migration_pairs. iter ( ) {
400- let parent = old_path. parent ( ) . unwrap ( ) ;
401- fs:: create_dir_all ( parent) . unwrap ( ) ;
402- File :: create ( old_path) . unwrap ( ) ;
403- }
404-
405- let result = move_and_rename ( & local_base, & remote_base) ;
406- assert ! ( result. is_ok( ) ) ;
407-
408- for ( old_path, new_path) in migration_pairs. iter ( ) {
409- assert ! ( old_path. exists( ) ) ;
410- assert ! ( new_path. exists( ) ) ;
411- }
412- }
413339}
0 commit comments