Skip to content

Commit

Permalink
fix(manager): update node registry with new service data once it is m…
Browse files Browse the repository at this point in the history
…odified
  • Loading branch information
RolandSherwin committed Mar 21, 2024
1 parent 882c0a2 commit 60a7ac0
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions sn_node_manager/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,58 @@ 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_clone.rpc_socket_addr);
let service = NodeService::new(current_node_clone.clone(), Box::new(rpc_client));
let mut service_manager = ServiceManager::new(
service,
Box::new(ServiceController {}),
VerbosityLevel::Normal,
);
// since current_node is cloned into ServiceManager. We need to manually update the actual current_node form node_registry, before saving.
service_manager.stop().await?;
*current_node_mut = service_manager.service.service_data.clone();

let service_control = ServiceController {};
if retain_peer_id {
// reuse the same port and root dir to retain peer id.
service_control
.uninstall(&current_node.service_name.clone())
.uninstall(&current_node_clone.service_name.clone())
.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?;
// update node service data again
*current_node_mut = service_manager.service.service_data.clone();
} else {
// else start a new node instance.
let new_node_number = nodes_len + 1;
Expand All @@ -82,31 +87,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(), &current_node.user).map_err(|err| {
create_owned_dir(log_dir_path.clone(), &current_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(), &current_node.user).map_err(|err| {
create_owned_dir(data_dir_path.clone(), &current_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")?
Expand All @@ -116,36 +121,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(), &current_node.user).map_err(|err| {
create_owned_dir(data_dir_path.clone(), &current_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(&current_node.safenode_path, &safenode_path).map_err(|err| {
std::fs::copy(&current_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()?;
Expand All @@ -154,13 +159,13 @@ pub async fn restart_node_service(
})?;

let node = NodeServiceData {
genesis: current_node.genesis,
local: current_node.local,
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,
Expand Down

0 comments on commit 60a7ac0

Please sign in to comment.