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: provide --upnp flag for add command #1710

Merged
merged 3 commits into from
May 16, 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
1 change: 1 addition & 0 deletions node-launchpad/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ fn add_and_start_nodes(
None,
None,
None,
false,
None,
None,
None,
Expand Down
5 changes: 5 additions & 0 deletions sn_node_manager/src/add_services/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct InstallNodeServiceCtxBuilder {
pub rpc_socket_addr: SocketAddr,
pub safenode_path: PathBuf,
pub service_user: Option<String>,
pub upnp: bool,
}

impl InstallNodeServiceCtxBuilder {
Expand All @@ -77,6 +78,9 @@ impl InstallNodeServiceCtxBuilder {
if self.local {
args.push(OsString::from("--local"));
}
if self.upnp {
args.push(OsString::from("--upnp"));
}
if let Some(node_port) = self.node_port {
args.push(OsString::from("--port"));
args.push(OsString::from(node_port.to_string()));
Expand Down Expand Up @@ -125,6 +129,7 @@ pub struct AddNodeServiceOptions {
pub safenode_dir_path: PathBuf,
pub service_data_dir_path: PathBuf,
pub service_log_dir_path: PathBuf,
pub upnp: bool,
pub user: Option<String>,
pub user_mode: bool,
pub version: String,
Expand Down
45 changes: 45 additions & 0 deletions sn_node_manager/src/add_services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ pub async fn add_node(
}
}

if let Some(port_option) = &options.node_port {
check_port_availability(port_option, &node_registry.nodes)?;
}

if let Some(port_option) = &options.metrics_port {
check_port_availability(port_option, &node_registry.nodes)?;
}

if let Some(port_option) = &options.rpc_port {
check_port_availability(port_option, &node_registry.nodes)?;
}

let safenode_file_name = options
.safenode_src_path
.file_name()
Expand Down Expand Up @@ -180,6 +192,7 @@ pub async fn add_node(
rpc_socket_addr,
safenode_path: service_safenode_path.clone(),
service_user: options.user.clone(),
upnp: options.upnp,
}
.build()?;

Expand All @@ -201,6 +214,8 @@ pub async fn add_node(
listen_addr: None,
local: options.local,
log_dir_path: service_log_dir_path.clone(),
metrics_port,
node_port,
number: node_number,
reward_balance: None,
rpc_socket_addr,
Expand All @@ -209,6 +224,7 @@ pub async fn add_node(
safenode_path: service_safenode_path,
service_name,
status: ServiceStatus::Added,
upnp: options.upnp,
user: options.user.clone(),
user_mode: options.user_mode,
version: options.version.clone(),
Expand Down Expand Up @@ -413,3 +429,32 @@ fn increment_port_option(port: Option<u16>) -> Option<u16> {
}
None
}

fn check_port_availability(port_option: &PortRange, nodes: &[NodeServiceData]) -> Result<()> {
let mut all_ports = Vec::new();
for node in nodes {
if let Some(port) = node.metrics_port {
all_ports.push(port);
}
if let Some(port) = node.node_port {
all_ports.push(port);
}
all_ports.push(node.rpc_socket_addr.port());
}

match port_option {
PortRange::Single(port) => {
if all_ports.iter().any(|p| *p == *port) {
return Err(eyre!("Port {port} is being used by another service"));
}
}
PortRange::Range(start, end) => {
for i in *start..=*end {
if all_ports.iter().any(|p| *p == i) {
return Err(eyre!("Port {i} is being used by another service"));
}
}
}
}
Ok(())
}
Loading
Loading