Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit e8d9f2b

Browse files
committed
chore: manually parse environment variable
On the `join` command, the peers argument is set as an option so we are not required to specify any peers when adding additional nodes to an existing local network. When the argument is set as an option, it doesn't parse the environment variable if it's set, so unfortunately we also have to provide some manual parsing. The commit also removes the node registry definition, which has been moved to `sn_protocol`. The churn tests in `safe_network` CI now need to be able to parse the node manager's deployment inventory so they can restart local nodes.
1 parent b9ee606 commit e8d9f2b

File tree

7 files changed

+30
-133
lines changed

7 files changed

+30
-133
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ serde_json = "1.0"
2525
service-manager = "0.5.1"
2626
sn_node_rpc_client = "0.2.4"
2727
sn_peers_acquisition = "0.2.1"
28+
sn_protocol = "0.10.8"
2829
sn-releases = "0.1.6"
2930
sysinfo = "0.29.10"
3031
tokio = { version = "1.26", features = ["full"] }

src/add_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
use crate::config::create_owned_dir;
1010
use crate::helpers::download_and_extract_release;
11-
use crate::node::{Node, NodeRegistry, NodeStatus};
1211
use crate::service::{ServiceConfig, ServiceControl};
1312
use color_eyre::{eyre::eyre, Help, Result};
1413
use colored::Colorize;
1514
use libp2p::Multiaddr;
15+
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
1616
use sn_releases::{ReleaseType, SafeReleaseRepositoryInterface};
1717
use std::path::PathBuf;
1818

src/config.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// KIND, either express or implied. Please review the Licences for the specific language governing
77
// permissions and limitations relating to use of the SAFE Network Software.
88

9-
use color_eyre::{eyre::eyre, Result};
9+
use color_eyre::Result;
1010
use std::path::{Path, PathBuf};
1111

1212
#[cfg(unix)]
@@ -33,14 +33,6 @@ pub fn get_node_registry_path() -> Result<PathBuf> {
3333
Ok(path.join("node_registry.json"))
3434
}
3535

36-
pub fn get_local_node_registry_path() -> Result<PathBuf> {
37-
let path = dirs_next::data_dir()
38-
.ok_or_else(|| eyre!("Cannot obtain user's data directory"))?
39-
.join("safe")
40-
.join("local_node_registry.json");
41-
Ok(path)
42-
}
43-
4436
#[cfg(unix)]
4537
pub fn get_service_data_dir_path(custom_path: Option<PathBuf>, owner: &str) -> Result<PathBuf> {
4638
let path = match custom_path {
@@ -83,6 +75,7 @@ pub fn get_service_log_dir_path(custom_path: Option<PathBuf>, _owner: &str) -> R
8375

8476
#[cfg(unix)]
8577
pub fn create_owned_dir(path: PathBuf, owner: &str) -> Result<()> {
78+
use color_eyre::eyre::eyre;
8679
use nix::unistd::{chown, Gid, Uid};
8780
use std::os::unix::fs::PermissionsExt;
8881
use users::get_user_by_name;

src/control.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
// KIND, either express or implied. Please review the Licences for the specific language governing
77
// permissions and limitations relating to use of the SAFE Network Software.
88

9-
use crate::node::{Node, NodeRegistry, NodeStatus};
109
use crate::service::ServiceControl;
1110
use color_eyre::{eyre::eyre, Help, Result};
1211
use colored::Colorize;
1312
use semver::Version;
1413
use sn_node_rpc_client::{RpcActions, RpcClient};
14+
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
1515
use std::path::PathBuf;
1616

1717
pub enum UpgradeResult {
@@ -265,7 +265,6 @@ fn format_status(status: &NodeStatus) -> String {
265265
#[cfg(test)]
266266
mod tests {
267267
use super::*;
268-
use crate::node::{Node, NodeStatus};
269268
use crate::service::MockServiceControl;
270269
use assert_fs::prelude::*;
271270
use assert_matches::assert_matches;
@@ -278,6 +277,7 @@ mod tests {
278277
use sn_node_rpc_client::{
279278
NetworkInfo, NodeInfo, RecordAddress, Result as RpcResult, RpcActions,
280279
};
280+
use sn_protocol::node_registry::{Node, NodeStatus};
281281
use std::path::PathBuf;
282282
use std::str::FromStr;
283283

src/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
// KIND, either express or implied. Please review the Licences for the specific language governing
77
// permissions and limitations relating to use of the SAFE Network Software.
88

9-
use crate::node::{Node, NodeRegistry, NodeStatus};
109
use crate::service::ServiceControl;
1110
use color_eyre::{eyre::eyre, Result};
1211
use colored::Colorize;
1312
use libp2p::{Multiaddr, PeerId};
1413
#[cfg(test)]
1514
use mockall::automock;
1615
use sn_node_rpc_client::{RpcActions, RpcClient};
16+
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
1717
use std::io::Read;
1818
use std::path::PathBuf;
1919
use std::process::{Command, Stdio};

src/main.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ mod config;
1111
mod control;
1212
mod helpers;
1313
mod local;
14-
mod node;
1514
mod service;
1615

1716
use crate::add_service::{add, AddServiceOptions};
1817
use crate::config::*;
1918
use crate::control::{remove, start, status, stop, upgrade, UpgradeResult};
2019
use crate::helpers::download_and_extract_release;
2120
use crate::local::{kill_network, run_network, LocalNetworkOptions};
22-
use crate::node::NodeRegistry;
2321
use crate::service::{NodeServiceManager, ServiceControl};
2422
use clap::{Parser, Subcommand};
2523
use color_eyre::{eyre::eyre, Help, Result};
2624
use colored::Colorize;
25+
use libp2p::Multiaddr;
2726
use libp2p_identity::PeerId;
2827
use semver::Version;
2928
use sn_node_rpc_client::RpcClient;
30-
use sn_peers_acquisition::{get_peers_from_args, PeersArgs};
29+
use sn_peers_acquisition::{get_peers_from_args, parse_peer_addr, PeersArgs};
30+
use sn_protocol::node_registry::{get_local_node_registry_path, NodeRegistry};
3131
use sn_releases::{ReleaseType, SafeReleaseRepositoryInterface};
3232
use std::path::PathBuf;
3333
use std::str::FromStr;
@@ -359,10 +359,26 @@ async fn main() -> Result<()> {
359359
)
360360
.await?;
361361

362-
let peers = if let Some(peers) = peers {
363-
Some(get_peers_from_args(peers).await?)
364-
} else {
365-
None
362+
// Unfortunately because the peers argument is an option, clap does not handle parsing
363+
// `SAFE_PEERS`.
364+
//
365+
// It needs to be an option, otherwise we wouldn't be able to use the `join` command
366+
// without peers to automatically add new nodes to an existing local network.
367+
let peers = match std::env::var("SAFE_PEERS") {
368+
Ok(var) => {
369+
let peers = var
370+
.split(',')
371+
.map(|addr| parse_peer_addr(addr.trim()).unwrap())
372+
.collect::<Vec<Multiaddr>>();
373+
Some(peers)
374+
}
375+
Err(_) => {
376+
if let Some(peers) = peers {
377+
Some(get_peers_from_args(peers).await?)
378+
} else {
379+
None
380+
}
381+
}
366382
};
367383

368384
let options = LocalNetworkOptions {

src/node.rs

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)