Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uniform behaviour for all add commands #1455

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 87 additions & 101 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sn_node_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ service-manager = "0.6.0"
sn_peers_acquisition = { path = "../sn_peers_acquisition", version = "0.2.8" }
sn_protocol = { path = "../sn_protocol", version = "0.15.4" }
sn_service_management = { path = "../sn_service_management", version = "0.1.1" }
sn-releases = "0.1.7"
sn-releases = "0.2.0"
sn_transfers = { path = "../sn_transfers", version = "0.16.4" }
sysinfo = "0.29.10"
tokio = { version = "1.26", features = ["full"] }
Expand Down
12 changes: 6 additions & 6 deletions sn_node_manager/src/add_services/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ pub struct AddNodeServiceOptions {
pub local: bool,
pub node_port: Option<PortRange>,
pub rpc_address: Option<Ipv4Addr>,
pub safenode_bin_path: PathBuf,
pub safenode_src_path: PathBuf,
pub safenode_dir_path: PathBuf,
pub service_data_dir_path: PathBuf,
pub service_log_dir_path: PathBuf,
pub url: Option<String>,
pub user: String,
pub version: String,
}
Expand Down Expand Up @@ -163,20 +162,21 @@ impl InstallFaucetServiceCtxBuilder {
pub struct AddFaucetServiceOptions {
pub bootstrap_peers: Vec<Multiaddr>,
pub env_variables: Option<Vec<(String, String)>>,
pub faucet_download_bin_path: PathBuf,
pub faucet_install_bin_path: PathBuf,
pub faucet_src_bin_path: PathBuf,
pub local: bool,
pub service_data_dir_path: PathBuf,
pub service_log_dir_path: PathBuf,
pub url: Option<String>,
pub user: String,
pub version: String,
}

pub struct AddDaemonServiceOptions {
pub address: Ipv4Addr,
pub port: u16,
pub daemon_download_bin_path: PathBuf,
pub env_variables: Option<Vec<(String, String)>>,
pub daemon_install_bin_path: PathBuf,
pub daemon_src_bin_path: PathBuf,
pub port: u16,
pub user: String,
pub version: String,
}
22 changes: 11 additions & 11 deletions sn_node_manager/src/add_services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn add_node(
}

let safenode_file_name = options
.safenode_bin_path
.safenode_src_path
.file_name()
.ok_or_else(|| eyre!("Could not get filename from the safenode download path"))?
.to_string_lossy()
Expand Down Expand Up @@ -138,7 +138,7 @@ pub async fn add_node(
create_owned_dir(service_log_dir_path.clone(), &options.user)?;

std::fs::copy(
options.safenode_bin_path.clone(),
options.safenode_src_path.clone(),
service_safenode_path.clone(),
)?;
let install_ctx = InstallNodeServiceCtxBuilder {
Expand Down Expand Up @@ -201,7 +201,7 @@ pub async fn add_node(
};
}

std::fs::remove_file(options.safenode_bin_path)?;
std::fs::remove_file(options.safenode_src_path)?;

if !added_service_data.is_empty() {
println!("Services Added:");
Expand Down Expand Up @@ -242,23 +242,23 @@ pub fn add_daemon(
}

std::fs::copy(
options.daemon_download_bin_path.clone(),
options.daemon_src_bin_path.clone(),
options.daemon_install_bin_path.clone(),
)?;

let install_ctx = ServiceInstallCtx {
label: DAEMON_SERVICE_NAME.parse()?,
program: options.daemon_install_bin_path.clone(),
args: vec![
OsString::from("--port"),
OsString::from(options.port.to_string()),
OsString::from("--address"),
OsString::from(options.address.to_string()),
],
contents: None,
username: None,
environment: options.env_variables,
label: DAEMON_SERVICE_NAME.parse()?,
program: options.daemon_install_bin_path.clone(),
username: Some(options.user),
working_directory: None,
environment: None,
};

match service_control.install(install_ctx) {
Expand All @@ -275,7 +275,7 @@ pub fn add_daemon(
println!("Daemon service added {}", "✓".green());
println!("[!] Note: the service has not been started");
node_registry.save()?;
std::fs::remove_file(options.daemon_download_bin_path)?;
std::fs::remove_file(options.daemon_src_bin_path)?;
Ok(())
}
Err(e) => {
Expand Down Expand Up @@ -307,7 +307,7 @@ pub fn add_faucet(
)?;

std::fs::copy(
install_options.faucet_download_bin_path.clone(),
install_options.faucet_src_bin_path.clone(),
install_options.faucet_install_bin_path.clone(),
)?;

Expand Down Expand Up @@ -350,7 +350,7 @@ pub fn add_faucet(
);
}
println!("[!] Note: the service has not been started");
std::fs::remove_file(install_options.faucet_download_bin_path)?;
std::fs::remove_file(install_options.faucet_src_bin_path)?;
node_registry.save()?;
Ok(())
}
Expand Down
55 changes: 23 additions & 32 deletions sn_node_manager/src/add_services/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,13 @@ async fn add_genesis_node_should_use_latest_version_and_add_one_service() -> Res
local: true,
genesis: true,
count: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
node_port: None,
bootstrap_peers: vec![],
rpc_address: None,
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -228,14 +227,13 @@ async fn add_genesis_node_should_return_an_error_if_there_is_already_a_genesis_n
local: true,
genesis: true,
count: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
node_port: None,
bootstrap_peers: vec![],
rpc_address: Some(custom_rpc_address),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -286,14 +284,13 @@ async fn add_genesis_node_should_return_an_error_if_count_is_greater_than_1() ->
local: true,
genesis: true,
count: Some(3),
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
node_port: None,
bootstrap_peers: vec![],
rpc_address: Some(custom_rpc_address),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -441,11 +438,10 @@ async fn add_node_should_use_latest_version_and_add_three_services() -> Result<(
bootstrap_peers: vec![],
node_port: None,
rpc_address: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -576,14 +572,13 @@ async fn add_node_should_update_the_bootstrap_peers_inside_node_registry() -> Re
local: false,
genesis: false,
count: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
bootstrap_peers: new_peers.clone(),
node_port: None,
rpc_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -688,14 +683,13 @@ async fn add_node_should_update_the_environment_variables_inside_node_registry()
local: false,
genesis: false,
count: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
bootstrap_peers: vec![],
node_port: None,
rpc_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: env_variables.clone(),
Expand Down Expand Up @@ -813,11 +807,10 @@ async fn add_new_node_should_add_another_service() -> Result<()> {
bootstrap_peers: vec![],
node_port: None,
rpc_address: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -913,14 +906,13 @@ async fn add_node_should_use_custom_ports_for_one_service() -> Result<()> {
local: false,
genesis: false,
count: None,
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
bootstrap_peers: vec![],
node_port: Some(PortRange::Single(custom_port)),
rpc_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -1123,14 +1115,13 @@ async fn add_node_should_use_a_custom_port_range() -> Result<()> {
local: false,
genesis: false,
count: Some(3),
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
bootstrap_peers: vec![],
node_port: Some(PortRange::Range(12000, 12002)),
rpc_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -1176,14 +1167,13 @@ async fn add_node_should_return_an_error_if_port_and_node_count_do_not_match() -
local: false,
genesis: false,
count: Some(2),
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
bootstrap_peers: vec![],
node_port: Some(PortRange::Range(12000, 12002)),
rpc_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -1235,14 +1225,13 @@ async fn add_node_should_return_an_error_if_multiple_services_are_specified_with
local: false,
genesis: false,
count: Some(2),
safenode_bin_path: safenode_download_path.to_path_buf(),
safenode_src_path: safenode_download_path.to_path_buf(),
safenode_dir_path: temp_dir.to_path_buf(),
service_data_dir_path: node_data_dir.to_path_buf(),
service_log_dir_path: node_logs_dir.to_path_buf(),
bootstrap_peers: vec![],
node_port: Some(PortRange::Single(12000)),
rpc_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
url: None,
user: get_username(),
version: latest_version.to_string(),
env_variables: None,
Expand Down Expand Up @@ -1316,12 +1305,11 @@ async fn add_faucet_should_add_a_faucet_service() -> Result<()> {
AddFaucetServiceOptions {
bootstrap_peers: vec![],
env_variables: Some(vec![("SN_LOG".to_string(), "all".to_string())]),
faucet_download_bin_path: faucet_download_path.to_path_buf(),
faucet_src_bin_path: faucet_download_path.to_path_buf(),
faucet_install_bin_path: faucet_install_path.to_path_buf(),
local: false,
service_data_dir_path: faucet_data_dir.to_path_buf(),
service_log_dir_path: faucet_logs_dir.to_path_buf(),
url: None,
user: get_username(),
version: latest_version.to_string(),
},
Expand Down Expand Up @@ -1387,12 +1375,11 @@ async fn add_faucet_should_return_an_error_if_a_faucet_service_was_already_creat
AddFaucetServiceOptions {
bootstrap_peers: vec![],
env_variables: Some(vec![("SN_LOG".to_string(), "all".to_string())]),
faucet_download_bin_path: faucet_download_path.to_path_buf(),
faucet_src_bin_path: faucet_download_path.to_path_buf(),
faucet_install_bin_path: faucet_install_path.to_path_buf(),
local: false,
service_data_dir_path: faucet_data_dir.to_path_buf(),
service_log_dir_path: faucet_logs_dir.to_path_buf(),
url: None,
user: get_username(),
version: latest_version.to_string(),
},
Expand Down Expand Up @@ -1449,20 +1436,22 @@ async fn add_daemon_should_add_a_daemon_service() -> Result<()> {
OsString::from("127.0.0.1"),
],
contents: None,
environment: None,
environment: Some(vec![("SN_LOG".to_string(), "ALL".to_string())]),
label: "safenodemand".parse()?,
program: daemon_install_path.to_path_buf(),
username: None,
username: Some(get_username()),
working_directory: None,
}))
.returning(|_| Ok(()));

add_daemon(
AddDaemonServiceOptions {
address: Ipv4Addr::new(127, 0, 0, 1),
port: 8080,
daemon_download_bin_path: daemon_download_path.to_path_buf(),
daemon_install_bin_path: daemon_install_path.to_path_buf(),
daemon_src_bin_path: daemon_download_path.to_path_buf(),
env_variables: Some(vec![("SN_LOG".to_string(), "ALL".to_string())]),
port: 8080,
user: get_username(),
version: latest_version.to_string(),
},
&mut node_registry,
Expand Down Expand Up @@ -1519,9 +1508,11 @@ async fn add_daemon_should_return_an_error_if_a_daemon_service_was_already_creat
let result = add_daemon(
AddDaemonServiceOptions {
address: Ipv4Addr::new(127, 0, 0, 1),
port: 8080,
daemon_download_bin_path: daemon_download_path.to_path_buf(),
daemon_install_bin_path: daemon_install_path.to_path_buf(),
daemon_src_bin_path: daemon_download_path.to_path_buf(),
env_variables: Some(Vec::new()),
port: 8080,
user: get_username(),
version: latest_version.to_string(),
},
&mut node_registry,
Expand Down
Loading
Loading