Skip to content

Commit

Permalink
feat: support a port range on the add command
Browse files Browse the repository at this point in the history
Previously we would not allow a custom port to be specified if more than one service was being
added. However, without autonat, people still need to open ports manually on their router, so the
node services they add will need to match this port range. Without the ability to specify a range,
they'd need to add the services one by one.
  • Loading branch information
jacderida authored and joshuef committed Mar 18, 2024
1 parent 397e7ca commit 75e1256
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 28 deletions.
28 changes: 26 additions & 2 deletions sn_node_manager/src/add_services/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,39 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use color_eyre::Result;
use color_eyre::{eyre::eyre, Result};
use libp2p::Multiaddr;
use service_manager::{ServiceInstallCtx, ServiceLabel};
use std::{
ffi::OsString,
net::{Ipv4Addr, SocketAddr},
path::PathBuf,
str::FromStr,
};

#[derive(Clone, Debug)]
pub enum PortRange {
Single(u16),
Range(u16, u16),
}

pub fn parse_port_range(s: &str) -> Result<PortRange> {
if let Ok(port) = u16::from_str(s) {
Ok(PortRange::Single(port))
} else {
let parts: Vec<&str> = s.split('-').collect();
if parts.len() != 2 {
return Err(eyre!("Port range must be in the format 'start-end'"));
}
let start = parts[0].parse::<u16>()?;
let end = parts[1].parse::<u16>()?;
if start >= end {
return Err(eyre!("End port must be greater than start port"));
}
Ok(PortRange::Range(start, end))
}
}

#[derive(Debug, PartialEq)]
pub struct InstallNodeServiceCtxBuilder {
pub data_dir_path: PathBuf,
Expand Down Expand Up @@ -82,7 +106,7 @@ pub struct AddNodeServiceOptions {
pub env_variables: Option<Vec<(String, String)>>,
pub genesis: bool,
pub local: bool,
pub node_port: Option<u16>,
pub node_port: Option<PortRange>,
pub rpc_address: Option<Ipv4Addr>,
pub safenode_bin_path: PathBuf,
pub safenode_dir_path: PathBuf,
Expand Down
44 changes: 36 additions & 8 deletions sn_node_manager/src/add_services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod tests;

use self::config::{
AddDaemonServiceOptions, AddFaucetServiceOptions, AddNodeServiceOptions,
InstallFaucetServiceCtxBuilder, InstallNodeServiceCtxBuilder,
InstallFaucetServiceCtxBuilder, InstallNodeServiceCtxBuilder, PortRange,
};
use crate::{config::create_owned_dir, VerbosityLevel, DAEMON_SERVICE_NAME};
use color_eyre::{eyre::eyre, Help, Result};
Expand Down Expand Up @@ -51,12 +51,25 @@ pub async fn add_node(
}
}

if options.count.is_some() && options.node_port.is_some() {
let count = options.count.unwrap();
if count > 1 {
return Err(eyre!(
"Custom node port can only be used when adding a single service"
));
if let Some(ref port_range) = options.node_port {
match port_range {
PortRange::Single(_) => {
let count = options.count.unwrap_or(1);
if count != 1 {
return Err(eyre!(
"The number of services to add ({count}) does not match the number of ports (1)"
));
}
}
PortRange::Range(start, end) => {
let port_count = end - start + 1;
let service_count = options.count.unwrap_or(1);
if port_count != service_count {
return Err(eyre!(
"The number of services to add ({service_count}) does not match the number of ports ({port_count})"
));
}
}
}
}

Expand Down Expand Up @@ -99,6 +112,15 @@ pub async fn add_node(
let target_node_count = current_node_count + options.count.unwrap_or(1);

let mut node_number = current_node_count + 1;
let mut port_number = if let Some(port) = options.node_port {
match port {
PortRange::Single(val) => Some(val),
PortRange::Range(start, _) => Some(start),
}
} else {
None
};

while node_number <= target_node_count {
let rpc_free_port = service_control.get_available_port()?;
let rpc_socket_addr = if let Some(addr) = options.rpc_address {
Expand Down Expand Up @@ -127,7 +149,7 @@ pub async fn add_node(
local: options.local,
log_dir_path: service_log_dir_path.clone(),
name: service_name.clone(),
node_port: options.node_port,
node_port: port_number,
rpc_socket_addr,
safenode_path: service_safenode_path.clone(),
service_user: options.user.clone(),
Expand Down Expand Up @@ -171,6 +193,12 @@ pub async fn add_node(
}

node_number += 1;
port_number = if let Some(port) = port_number {
let incremented_port = port + 1;
Some(incremented_port)
} else {
None
};
}

std::fs::remove_file(options.safenode_bin_path)?;
Expand Down
Loading

0 comments on commit 75e1256

Please sign in to comment.