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

Commit

Permalink
chore: manually parse environment variable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jacderida committed Jan 15, 2024
1 parent b9ee606 commit 48936a9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 133 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_json = "1.0"
service-manager = "0.5.1"
sn_node_rpc_client = "0.2.4"
sn_peers_acquisition = "0.2.1"
sn_protocol = "0.10.8"
sn-releases = "0.1.6"
sysinfo = "0.29.10"
tokio = { version = "1.26", features = ["full"] }
Expand Down
2 changes: 1 addition & 1 deletion src/add_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

use crate::config::create_owned_dir;
use crate::helpers::download_and_extract_release;
use crate::node::{Node, NodeRegistry, NodeStatus};
use crate::service::{ServiceConfig, ServiceControl};
use color_eyre::{eyre::eyre, Help, Result};
use colored::Colorize;
use libp2p::Multiaddr;
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
use sn_releases::{ReleaseType, SafeReleaseRepositoryInterface};
use std::path::PathBuf;

Expand Down
11 changes: 2 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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::{eyre::eyre, Result};
use color_eyre::Result;
use std::path::{Path, PathBuf};

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

pub fn get_local_node_registry_path() -> Result<PathBuf> {
let path = dirs_next::data_dir()
.ok_or_else(|| eyre!("Cannot obtain user's data directory"))?
.join("safe")
.join("local_node_registry.json");
Ok(path)
}

#[cfg(unix)]
pub fn get_service_data_dir_path(custom_path: Option<PathBuf>, owner: &str) -> Result<PathBuf> {
let path = match custom_path {
Expand Down Expand Up @@ -83,6 +75,7 @@ pub fn get_service_log_dir_path(custom_path: Option<PathBuf>, _owner: &str) -> R

#[cfg(unix)]
pub fn create_owned_dir(path: PathBuf, owner: &str) -> Result<()> {
use color_eyre::eyre::eyre;
use nix::unistd::{chown, Gid, Uid};
use std::os::unix::fs::PermissionsExt;
use users::get_user_by_name;
Expand Down
4 changes: 2 additions & 2 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// 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 crate::node::{Node, NodeRegistry, NodeStatus};
use crate::service::ServiceControl;
use color_eyre::{eyre::eyre, Help, Result};
use colored::Colorize;
use semver::Version;
use sn_node_rpc_client::{RpcActions, RpcClient};
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
use std::path::PathBuf;

pub enum UpgradeResult {
Expand Down Expand Up @@ -265,7 +265,6 @@ fn format_status(status: &NodeStatus) -> String {
#[cfg(test)]
mod tests {
use super::*;
use crate::node::{Node, NodeStatus};
use crate::service::MockServiceControl;
use assert_fs::prelude::*;
use assert_matches::assert_matches;
Expand All @@ -278,6 +277,7 @@ mod tests {
use sn_node_rpc_client::{
NetworkInfo, NodeInfo, RecordAddress, Result as RpcResult, RpcActions,
};
use sn_protocol::node_registry::{Node, NodeStatus};
use std::path::PathBuf;
use std::str::FromStr;

Expand Down
2 changes: 1 addition & 1 deletion src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
// 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 crate::node::{Node, NodeRegistry, NodeStatus};
use crate::service::ServiceControl;
use color_eyre::{eyre::eyre, Result};
use colored::Colorize;
use libp2p::{Multiaddr, PeerId};
#[cfg(test)]
use mockall::automock;
use sn_node_rpc_client::{RpcActions, RpcClient};
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
use std::io::Read;
use std::path::PathBuf;
use std::process::{Command, Stdio};
Expand Down
30 changes: 23 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ mod config;
mod control;
mod helpers;
mod local;
mod node;
mod service;

use crate::add_service::{add, AddServiceOptions};
use crate::config::*;
use crate::control::{remove, start, status, stop, upgrade, UpgradeResult};
use crate::helpers::download_and_extract_release;
use crate::local::{kill_network, run_network, LocalNetworkOptions};
use crate::node::NodeRegistry;
use crate::service::{NodeServiceManager, ServiceControl};
use clap::{Parser, Subcommand};
use color_eyre::{eyre::eyre, Help, Result};
use colored::Colorize;
use libp2p::Multiaddr;
use libp2p_identity::PeerId;
use semver::Version;
use sn_node_rpc_client::RpcClient;
use sn_peers_acquisition::{get_peers_from_args, PeersArgs};
use sn_peers_acquisition::{get_peers_from_args, parse_peer_addr, PeersArgs};
use sn_protocol::node_registry::{get_local_node_registry_path, NodeRegistry};
use sn_releases::{ReleaseType, SafeReleaseRepositoryInterface};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -359,10 +359,26 @@ async fn main() -> Result<()> {
)
.await?;

let peers = if let Some(peers) = peers {
Some(get_peers_from_args(peers).await?)
} else {
None
// Unfortunately because the peers argument is an option, clap does not handle parsing
// `SAFE_PEERS`.
//
// It needs to be an option, otherwise we wouldn't be able to use the `join` command
// without peers to automatically add new nodes to an existing local network.
let peers = match std::env::var("SAFE_PEERS") {
Ok(var) => {
let peers = var
.split(',')
.map(|addr| parse_peer_addr(addr.trim()).unwrap())
.collect::<Vec<Multiaddr>>();
Some(peers)
}
Err(_) => {
if let Some(peers) = peers {
Some(get_peers_from_args(peers).await?)
} else {
None
}
}
};

let options = LocalNetworkOptions {
Expand Down
113 changes: 0 additions & 113 deletions src/node.rs

This file was deleted.

0 comments on commit 48936a9

Please sign in to comment.