From 2fae77fdf37757f8b06f866334eddbe4dcfdafa7 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Thu, 21 Mar 2024 20:05:15 +0530 Subject: [PATCH] fix(manager): store exclusive reference to service data instead of cloning --- sn_node_manager/src/cmd/daemon.rs | 10 ++-- sn_node_manager/src/cmd/faucet.rs | 15 +++--- sn_node_manager/src/cmd/node.rs | 72 ++++++++++++------------- sn_node_manager/src/lib.rs | 64 +++++++++++------------ sn_node_manager/src/rpc.rs | 81 ++++++++++++++--------------- sn_service_management/src/daemon.rs | 12 ++--- sn_service_management/src/faucet.rs | 12 ++--- sn_service_management/src/lib.rs | 12 ----- sn_service_management/src/node.rs | 12 ++--- 9 files changed, 135 insertions(+), 155 deletions(-) diff --git a/sn_node_manager/src/cmd/daemon.rs b/sn_node_manager/src/cmd/daemon.rs index f660f9d720..8c0119d4f7 100644 --- a/sn_node_manager/src/cmd/daemon.rs +++ b/sn_node_manager/src/cmd/daemon.rs @@ -79,14 +79,14 @@ pub async fn start(verbosity: VerbosityLevel) -> Result<()> { } let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?; - if let Some(daemon) = node_registry.daemon.clone() { + if let Some(daemon) = &mut node_registry.daemon { if verbosity != VerbosityLevel::Minimal { println!("================================================="); println!(" Start Daemon Service "); println!("================================================="); } - let service = DaemonService::new(daemon.clone(), Box::new(ServiceController {})); + let service = DaemonService::new(daemon, Box::new(ServiceController {})); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.start().await?; @@ -100,7 +100,6 @@ pub async fn start(verbosity: VerbosityLevel) -> Result<()> { .map_or("-".to_string(), |e| e.to_string()) ); - node_registry.daemon = Some(service_manager.service.service_data); node_registry.save()?; return Ok(()); } @@ -114,19 +113,18 @@ pub async fn stop(verbosity: VerbosityLevel) -> Result<()> { } let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?; - if let Some(daemon) = node_registry.daemon.clone() { + if let Some(daemon) = &mut node_registry.daemon { if verbosity != VerbosityLevel::Minimal { println!("================================================="); println!(" Stop Daemon Service "); println!("================================================="); } - let service = DaemonService::new(daemon.clone(), Box::new(ServiceController {})); + let service = DaemonService::new(daemon, Box::new(ServiceController {})); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.stop().await?; - node_registry.daemon = Some(service_manager.service.service_data); node_registry.save()?; return Ok(()); diff --git a/sn_node_manager/src/cmd/faucet.rs b/sn_node_manager/src/cmd/faucet.rs index abdb21cc04..bbccc3a2b6 100644 --- a/sn_node_manager/src/cmd/faucet.rs +++ b/sn_node_manager/src/cmd/faucet.rs @@ -88,14 +88,14 @@ pub async fn start(verbosity: VerbosityLevel) -> Result<()> { } let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?; - if let Some(faucet) = node_registry.faucet.clone() { + if let Some(faucet) = &mut node_registry.faucet { if verbosity != VerbosityLevel::Minimal { println!("================================================="); println!(" Start Faucet Service "); println!("================================================="); } - let service = FaucetService::new(faucet.clone(), Box::new(ServiceController {})); + let service = FaucetService::new(faucet, Box::new(ServiceController {})); let mut service_manager = ServiceManager::new( service, Box::new(ServiceController {}), @@ -103,7 +103,6 @@ pub async fn start(verbosity: VerbosityLevel) -> Result<()> { ); service_manager.start().await?; - node_registry.faucet = Some(service_manager.service.service_data); node_registry.save()?; return Ok(()); } @@ -117,19 +116,18 @@ pub async fn stop(verbosity: VerbosityLevel) -> Result<()> { } let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?; - if let Some(faucet) = node_registry.faucet.clone() { + if let Some(faucet) = &mut node_registry.faucet { if verbosity != VerbosityLevel::Minimal { println!("================================================="); println!(" Stop Faucet Service "); println!("================================================="); } - let service = FaucetService::new(faucet.clone(), Box::new(ServiceController {})); + let service = FaucetService::new(faucet, Box::new(ServiceController {})); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.stop().await?; - node_registry.faucet = Some(service_manager.service.service_data); node_registry.save()?; return Ok(()); @@ -164,7 +162,7 @@ pub async fn upgrade( let (upgrade_bin_path, target_version) = download_and_get_upgrade_bin_path(ReleaseType::Faucet, url, version).await?; - let faucet = node_registry.faucet.clone().unwrap(); + let faucet = node_registry.faucet.as_mut().unwrap(); if !force { let current_version = Version::parse(&faucet.version)?; @@ -190,13 +188,12 @@ pub async fn upgrade( target_bin_path: upgrade_bin_path.clone(), target_version: target_version.clone(), }; - let service = FaucetService::new(faucet.clone(), Box::new(ServiceController {})); + let service = FaucetService::new(faucet, Box::new(ServiceController {})); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); match service_manager.upgrade(options).await { Ok(upgrade_result) => { - node_registry.faucet = Some(service_manager.service.service_data); print_upgrade_summary(vec![("faucet".to_string(), upgrade_result)]); node_registry.save()?; Ok(()) diff --git a/sn_node_manager/src/cmd/node.rs b/sn_node_manager/src/cmd/node.rs index 321b6613d3..8ee2aae842 100644 --- a/sn_node_manager/src/cmd/node.rs +++ b/sn_node_manager/src/cmd/node.rs @@ -126,11 +126,10 @@ pub async fn remove( .ok_or_else(|| eyre!("No service named '{name}'"))?; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.remove(keep_directories).await?; - node_registry.update_node(service_manager.service.service_data)?; } else if let Some(ref peer_id) = peer_id { let peer_id = PeerId::from_str(peer_id)?; let node = node_registry @@ -144,11 +143,10 @@ pub async fn remove( )) })?; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.remove(keep_directories).await?; - node_registry.update_node(service_manager.service.service_data)?; } node_registry.save()?; @@ -180,12 +178,11 @@ pub async fn start( .ok_or_else(|| eyre!("No service named '{name}'"))?; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.start().await?; - node_registry.update_node(service_manager.service.service_data)?; node_registry.save()?; } else if let Some(ref peer_id) = peer_id { let peer_id = PeerId::from_str(peer_id)?; @@ -201,24 +198,22 @@ pub async fn start( })?; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.start().await?; - node_registry.update_node(service_manager.service.service_data)?; node_registry.save()?; } else { let mut failed_services = Vec::new(); let node_count = node_registry.nodes.len(); for i in 0..node_count { let rpc_client = RpcClient::from_socket_addr(node_registry.nodes[i].rpc_socket_addr); - let service = NodeService::new(node_registry.nodes[i].clone(), Box::new(rpc_client)); + let service = NodeService::new(&mut node_registry.nodes[i], Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity.clone()); match service_manager.start().await { Ok(()) => { - node_registry.update_node(service_manager.service.service_data)?; node_registry.save()?; } Err(e) => { @@ -305,12 +300,11 @@ pub async fn stop( .ok_or_else(|| eyre!("No service named '{name}'"))?; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.stop().await?; - node_registry.update_node(service_manager.service.service_data)?; node_registry.save()?; } else if let Some(ref peer_id) = peer_id { let peer_id = PeerId::from_str(peer_id)?; @@ -326,23 +320,21 @@ pub async fn stop( })?; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); service_manager.stop().await?; - node_registry.update_node(service_manager.service.service_data)?; node_registry.save()?; } else { let node_count = node_registry.nodes.len(); for i in 0..node_count { let rpc_client = RpcClient::from_socket_addr(node_registry.nodes[i].rpc_socket_addr); - let service = NodeService::new(node_registry.nodes[i].clone(), Box::new(rpc_client)); + let service = NodeService::new(&mut node_registry.nodes[i], Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity.clone()); service_manager.stop().await?; - node_registry.update_node(service_manager.service.service_data)?; node_registry.save()?; } } @@ -412,16 +404,22 @@ pub async fn upgrade( }; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); match service_manager.upgrade(options).await { Ok(upgrade_result) => { - upgrade_summary.push((service_manager.service.service_data, upgrade_result)); + upgrade_summary.push(( + service_manager.service.service_data.service_name.clone(), + upgrade_result, + )); } Err(e) => { - upgrade_summary.push((node.clone(), UpgradeResult::Error(format!("Error: {}", e)))); + upgrade_summary.push(( + node.service_name.clone(), + UpgradeResult::Error(format!("Error: {}", e)), + )); } } } else if let Some(ref peer_id) = peer_id { @@ -452,16 +450,22 @@ pub async fn upgrade( }; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity); match service_manager.upgrade(options).await { Ok(upgrade_result) => { - upgrade_summary.push((service_manager.service.service_data, upgrade_result)); + upgrade_summary.push(( + service_manager.service.service_data.service_name.clone(), + upgrade_result, + )); } Err(e) => { - upgrade_summary.push((node.clone(), UpgradeResult::Error(format!("Error: {}", e)))); + upgrade_summary.push(( + node.service_name.clone(), + UpgradeResult::Error(format!("Error: {}", e)), + )); } } } else { @@ -481,32 +485,28 @@ pub async fn upgrade( }; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new(service, Box::new(ServiceController {}), verbosity.clone()); match service_manager.upgrade(options).await { Ok(upgrade_result) => { - upgrade_summary.push((service_manager.service.service_data, upgrade_result)); + upgrade_summary.push(( + service_manager.service.service_data.service_name.clone(), + upgrade_result, + )); } Err(e) => { - upgrade_summary - .push((node.clone(), UpgradeResult::Error(format!("Error: {}", e)))); + upgrade_summary.push(( + node.service_name.clone(), + UpgradeResult::Error(format!("Error: {}", e)), + )); } } } } - print_upgrade_summary( - upgrade_summary - .iter() - .map(|x| (x.0.service_name.clone(), x.1.clone())) - .collect::>(), - ); - - for (node, _) in upgrade_summary { - node_registry.update_node(node.clone())?; - } + print_upgrade_summary(upgrade_summary); node_registry.save()?; Ok(()) diff --git a/sn_node_manager/src/lib.rs b/sn_node_manager/src/lib.rs index 5d151f33d9..8128affb12 100644 --- a/sn_node_manager/src/lib.rs +++ b/sn_node_manager/src/lib.rs @@ -491,7 +491,7 @@ mod tests { }) }); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -508,7 +508,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -567,7 +567,7 @@ mod tests { }) }); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -586,7 +586,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -621,7 +621,7 @@ mod tests { .times(1) .returning(|_| true); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -640,7 +640,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -704,7 +704,7 @@ mod tests { }) }); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -723,7 +723,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -762,7 +762,7 @@ mod tests { .times(1) .returning(|_| true); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -781,7 +781,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -801,7 +801,7 @@ mod tests { #[tokio::test] async fn stop_should_not_return_error_for_attempt_to_stop_installed_service() -> Result<()> { - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -818,7 +818,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(MockServiceControl::new()), @@ -838,7 +838,7 @@ mod tests { #[tokio::test] async fn stop_should_return_ok_when_attempting_to_stop_service_that_was_already_stopped( ) -> Result<()> { - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -857,7 +857,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(MockServiceControl::new()), @@ -878,7 +878,7 @@ mod tests { #[tokio::test] async fn stop_should_return_ok_when_attempting_to_stop_a_removed_service() -> Result<()> { - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -895,7 +895,7 @@ mod tests { user: "safe".to_string(), version: "0.98.1".to_string(), }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(MockServiceControl::new()), @@ -986,7 +986,7 @@ mod tests { }) }); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -1005,7 +1005,7 @@ mod tests { user: "safe".to_string(), version: current_version.to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1063,7 +1063,7 @@ mod tests { let mock_service_control = MockServiceControl::new(); let mock_rpc_client = MockRpcClient::new(); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -1082,7 +1082,7 @@ mod tests { user: "safe".to_string(), version: current_version.to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1177,7 +1177,7 @@ mod tests { }) }); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -1196,7 +1196,7 @@ mod tests { user: "safe".to_string(), version: current_version.to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1309,7 +1309,7 @@ mod tests { }) }); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { connected_peers: None, data_dir_path: PathBuf::from("/var/safenode-manager/services/safenode1"), genesis: false, @@ -1328,7 +1328,7 @@ mod tests { user: "safe".to_string(), version: current_version.to_string(), }; - let service = NodeService::new(service_data, Box::new(mock_rpc_client)); + let service = NodeService::new(&mut service_data, Box::new(mock_rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1390,7 +1390,7 @@ mod tests { .times(1) .returning(|_| Ok(())); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { genesis: false, local: false, version: "0.98.1".to_string(), @@ -1407,7 +1407,7 @@ mod tests { safenode_path: safenode_bin.to_path_buf(), connected_peers: None, }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1435,7 +1435,7 @@ mod tests { .times(1) .returning(|_| true); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { genesis: false, local: false, version: "0.98.1".to_string(), @@ -1454,7 +1454,7 @@ mod tests { safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"), connected_peers: None, }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1488,7 +1488,7 @@ mod tests { .times(1) .returning(|_| false); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { genesis: false, local: false, version: "0.98.1".to_string(), @@ -1507,7 +1507,7 @@ mod tests { safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"), connected_peers: None, }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), @@ -1543,7 +1543,7 @@ mod tests { .times(1) .returning(|_| Ok(())); - let service_data = NodeServiceData { + let mut service_data = NodeServiceData { genesis: false, local: false, version: "0.98.1".to_string(), @@ -1560,7 +1560,7 @@ mod tests { safenode_path: safenode_bin.to_path_buf(), connected_peers: None, }; - let service = NodeService::new(service_data, Box::new(MockRpcClient::new())); + let service = NodeService::new(&mut service_data, Box::new(MockRpcClient::new())); let mut service_manager = ServiceManager::new( service, Box::new(mock_service_control), diff --git a/sn_node_manager/src/rpc.rs b/sn_node_manager/src/rpc.rs index 2a2466583b..01d0c5c28b 100644 --- a/sn_node_manager/src/rpc.rs +++ b/sn_node_manager/src/rpc.rs @@ -27,14 +27,15 @@ pub async fn restart_node_service( retain_peer_id: bool, ) -> Result<()> { let nodes_len = node_registry.nodes.len(); - let current_node = node_registry + let current_node_mut = node_registry .nodes .iter_mut() .find(|node| node.peer_id.is_some_and(|id| id == peer_id)) .ok_or_eyre(format!("Could not find the provided PeerId: {peer_id:?}"))?; + let current_node_clone = current_node_mut.clone(); - let rpc_client = RpcClient::from_socket_addr(current_node.rpc_socket_addr); - let service = NodeService::new(current_node.clone(), Box::new(rpc_client)); + let rpc_client = RpcClient::from_socket_addr(current_node_mut.rpc_socket_addr); + let service = NodeService::new(current_node_mut, Box::new(rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(ServiceController {}), @@ -46,31 +47,31 @@ pub async fn restart_node_service( if retain_peer_id { // reuse the same port and root dir to retain peer id. service_control - .uninstall(¤t_node.service_name.clone()) + .uninstall(¤t_node_clone.service_name) .map_err(|err| { eyre!( "Error while uninstalling node {:?} with: {err:?}", - current_node.service_name + current_node_clone.service_name ) })?; let install_ctx = InstallNodeServiceCtxBuilder { - local: current_node.local, - data_dir_path: current_node.data_dir_path.clone(), - genesis: current_node.genesis, - name: current_node.service_name.clone(), - node_port: current_node.get_safenode_port(), + local: current_node_clone.local, + data_dir_path: current_node_clone.data_dir_path.clone(), + genesis: current_node_clone.genesis, + name: current_node_clone.service_name.clone(), + node_port: current_node_clone.get_safenode_port(), bootstrap_peers: node_registry.bootstrap_peers.clone(), - rpc_socket_addr: current_node.rpc_socket_addr, - log_dir_path: current_node.log_dir_path.clone(), - safenode_path: current_node.safenode_path.clone(), - service_user: current_node.user.clone(), + rpc_socket_addr: current_node_clone.rpc_socket_addr, + log_dir_path: current_node_clone.log_dir_path.clone(), + safenode_path: current_node_clone.safenode_path.clone(), + service_user: current_node_clone.user.clone(), env_variables: node_registry.environment_variables.clone(), } .build()?; service_control.install(install_ctx).map_err(|err| { eyre!( "Error while installing node {:?} with: {err:?}", - current_node.service_name + current_node_clone.service_name ) })?; service_manager.start().await?; @@ -82,31 +83,31 @@ pub async fn restart_node_service( // modify the paths & copy safenode binary // example path "log_dir_path":"/var/log/safenode/safenode18" let log_dir_path = { - let mut log_dir_path = current_node.log_dir_path.clone(); + let mut log_dir_path = current_node_clone.log_dir_path.clone(); log_dir_path.pop(); log_dir_path.join(&new_service_name) }; // example path "data_dir_path":"/var/safenode-manager/services/safenode18" let data_dir_path = { - let mut data_dir_path = current_node.data_dir_path.clone(); + let mut data_dir_path = current_node_clone.data_dir_path.clone(); data_dir_path.pop(); data_dir_path.join(&new_service_name) }; - create_owned_dir(log_dir_path.clone(), ¤t_node.user).map_err(|err| { + create_owned_dir(log_dir_path.clone(), ¤t_node_clone.user).map_err(|err| { eyre!( "Error while creating owned dir for {:?}: {err:?}", - current_node.user + current_node_clone.user ) })?; - create_owned_dir(data_dir_path.clone(), ¤t_node.user).map_err(|err| { + create_owned_dir(data_dir_path.clone(), ¤t_node_clone.user).map_err(|err| { eyre!( "Error while creating owned dir for {:?}: {err:?}", - current_node.user + current_node_clone.user ) })?; // example path "safenode_path":"/var/safenode-manager/services/safenode18/safenode" let safenode_path = { - let mut safenode_path = current_node.safenode_path.clone(); + let mut safenode_path = current_node_clone.safenode_path.clone(); let safenode_file_name = safenode_path .file_name() .ok_or_eyre("Could not get filename from the current node's safenode path")? @@ -116,36 +117,36 @@ pub async fn restart_node_service( safenode_path.pop(); let safenode_path = safenode_path.join(&new_service_name); - create_owned_dir(data_dir_path.clone(), ¤t_node.user).map_err(|err| { + create_owned_dir(data_dir_path.clone(), ¤t_node_clone.user).map_err(|err| { eyre!( "Error while creating owned dir for {:?}: {err:?}", - current_node.user + current_node_clone.user ) })?; let safenode_path = safenode_path.join(safenode_file_name); - std::fs::copy(¤t_node.safenode_path, &safenode_path).map_err(|err| { + std::fs::copy(¤t_node_clone.safenode_path, &safenode_path).map_err(|err| { eyre!( "Failed to copy safenode bin from {:?} to {safenode_path:?} with err: {err}", - current_node.safenode_path + current_node_clone.safenode_path ) })?; safenode_path }; let install_ctx = InstallNodeServiceCtxBuilder { - local: current_node.local, - genesis: current_node.genesis, + local: current_node_clone.local, + genesis: current_node_clone.genesis, name: new_service_name.clone(), // don't re-use port node_port: None, bootstrap_peers: node_registry.bootstrap_peers.clone(), - rpc_socket_addr: current_node.rpc_socket_addr, + rpc_socket_addr: current_node_clone.rpc_socket_addr, // set new paths data_dir_path: data_dir_path.clone(), log_dir_path: log_dir_path.clone(), safenode_path: safenode_path.clone(), - service_user: current_node.user.clone(), + service_user: current_node_clone.user.clone(), env_variables: node_registry.environment_variables.clone(), } .build()?; @@ -153,14 +154,14 @@ pub async fn restart_node_service( eyre!("Error while installing node {new_service_name:?} with: {err:?}",) })?; - let node = NodeServiceData { - genesis: current_node.genesis, - local: current_node.local, + let mut node = NodeServiceData { + genesis: current_node_clone.genesis, + local: current_node_clone.local, service_name: new_service_name.clone(), - user: current_node.user.clone(), + user: current_node_clone.user.clone(), number: new_node_number as u16, - rpc_socket_addr: current_node.rpc_socket_addr, - version: current_node.version.clone(), + rpc_socket_addr: current_node_clone.rpc_socket_addr, + version: current_node_clone.version.clone(), status: ServiceStatus::Added, listen_addr: None, pid: None, @@ -172,7 +173,7 @@ pub async fn restart_node_service( }; let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr); - let service = NodeService::new(node.clone(), Box::new(rpc_client)); + let service = NodeService::new(&mut node, Box::new(rpc_client)); let mut service_manager = ServiceManager::new( service, Box::new(ServiceController {}), @@ -181,12 +182,8 @@ pub async fn restart_node_service( service_manager.start().await?; node_registry .nodes - .push(service_manager.service.service_data); + .push(service_manager.service.service_data.clone()); }; - node_registry - .save() - .map_err(|err| eyre!("Error while saving node registry with: {err:?}"))?; - Ok(()) } diff --git a/sn_service_management/src/daemon.rs b/sn_service_management/src/daemon.rs index f9c1b4c96f..72c8da6135 100644 --- a/sn_service_management/src/daemon.rs +++ b/sn_service_management/src/daemon.rs @@ -26,16 +26,16 @@ pub struct DaemonServiceData { pub version: String, } -pub struct DaemonService { - pub service_data: DaemonServiceData, +pub struct DaemonService<'a> { + pub service_data: &'a mut DaemonServiceData, pub service_control: Box, } -impl DaemonService { +impl<'a> DaemonService<'a> { pub fn new( - service_data: DaemonServiceData, + service_data: &'a mut DaemonServiceData, service_control: Box, - ) -> DaemonService { + ) -> DaemonService<'a> { DaemonService { service_data, service_control, @@ -44,7 +44,7 @@ impl DaemonService { } #[async_trait] -impl ServiceStateActions for DaemonService { +impl<'a> ServiceStateActions for DaemonService<'a> { fn bin_path(&self) -> PathBuf { self.service_data.daemon_path.clone() } diff --git a/sn_service_management/src/faucet.rs b/sn_service_management/src/faucet.rs index 01b3edbfb9..7607278bef 100644 --- a/sn_service_management/src/faucet.rs +++ b/sn_service_management/src/faucet.rs @@ -26,16 +26,16 @@ pub struct FaucetServiceData { pub version: String, } -pub struct FaucetService { - pub service_data: FaucetServiceData, +pub struct FaucetService<'a> { + pub service_data: &'a mut FaucetServiceData, pub service_control: Box, } -impl FaucetService { +impl<'a> FaucetService<'a> { pub fn new( - service_data: FaucetServiceData, + service_data: &'a mut FaucetServiceData, service_control: Box, - ) -> FaucetService { + ) -> FaucetService<'a> { FaucetService { service_data, service_control, @@ -44,7 +44,7 @@ impl FaucetService { } #[async_trait] -impl ServiceStateActions for FaucetService { +impl<'a> ServiceStateActions for FaucetService<'a> { fn bin_path(&self) -> PathBuf { self.service_data.faucet_path.clone() } diff --git a/sn_service_management/src/lib.rs b/sn_service_management/src/lib.rs index d0620586ad..457e0926bd 100644 --- a/sn_service_management/src/lib.rs +++ b/sn_service_management/src/lib.rs @@ -141,18 +141,6 @@ impl NodeRegistry { Ok(registry) } - pub fn update_node(&mut self, new_data: NodeServiceData) -> Result<()> { - if let Some(node) = self - .nodes - .iter_mut() - .find(|n| n.service_name == new_data.service_name) - { - *node = new_data; - return Ok(()); - } - Err(Error::NodeNotFound(new_data.service_name)) - } - pub fn to_status_summary(&self) -> StatusSummary { StatusSummary { nodes: self.nodes.clone(), diff --git a/sn_service_management/src/node.rs b/sn_service_management/src/node.rs index daadad1059..77a9a61d32 100644 --- a/sn_service_management/src/node.rs +++ b/sn_service_management/src/node.rs @@ -14,16 +14,16 @@ use service_manager::{ServiceInstallCtx, ServiceLabel}; use sn_protocol::get_port_from_multiaddr; use std::{ffi::OsString, net::SocketAddr, path::PathBuf, str::FromStr}; -pub struct NodeService { - pub service_data: NodeServiceData, +pub struct NodeService<'a> { + pub service_data: &'a mut NodeServiceData, pub rpc_actions: Box, } -impl NodeService { +impl<'a> NodeService<'a> { pub fn new( - service_data: NodeServiceData, + service_data: &'a mut NodeServiceData, rpc_actions: Box, - ) -> NodeService { + ) -> NodeService<'a> { NodeService { rpc_actions, service_data, @@ -32,7 +32,7 @@ impl NodeService { } #[async_trait] -impl ServiceStateActions for NodeService { +impl<'a> ServiceStateActions for NodeService<'a> { fn bin_path(&self) -> PathBuf { self.service_data.safenode_path.clone() }