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

Commit

Permalink
dev: First pass on a bootstrap CLI in noosphere-ns to spin up DHT
Browse files Browse the repository at this point in the history
bootstrap nodes for the name system.
  • Loading branch information
jsantell committed Nov 15, 2022
1 parent 20c5a04 commit cf7a8cb
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 2 deletions.
59 changes: 59 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions rust/noosphere-ns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ readme = "README.md"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
anyhow = "^1"
tracing = "0.1"
lazy_static = "^1"
cid = "~0.8"
serde = "^1"
serde_json = "^1"
Expand All @@ -37,9 +38,27 @@ tokio = { version = "1.15", features = ["io-util", "io-std", "sync", "macros", "
libp2p = { version = "0.49.0", default-features = false, features = [ "identify", "dns", "tcp", "tokio", "noise", "mplex", "yamux", "kad" ] }
noosphere-storage = { version = "0.1.0", path = "../noosphere-storage" }
noosphere-core = { version = "0.2.0", path = "../noosphere-core" }
noosphere = { version = "0.2.0", path = "../noosphere", optional = true }
clap = { version = "^4", features = ["derive"], optional = true }
home = { version = "~0.5", optional = true }
toml = { version = "~0.5", optional = true }

[dev-dependencies]

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
rand = { version = "0.8.5" }
test-log = { version = "0.2.11", default-features = false, features = ["trace"] }
tracing-subscriber = { version = "~0.3", features = ["env-filter"] }
libipld-cbor = "~0.14"
tempdir = { version = "~0.3" }

[features]
default = []
bootstrap = ["clap", "noosphere", "home", "toml"]

[target.'cfg(not(target_arch = "wasm32"))'.features]
default = ["bootstrap"]

[[bin]]
name = "bootstrap_nns"
required-features = ["bootstrap"]
9 changes: 9 additions & 0 deletions rust/noosphere-ns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
# noosphere-ns

Noosphere's P2P name system.

## Bootstrap Node

The `bootstrap_nns` binary target is an executable that runs one or many bootstrap
nodes, based on configuration.

```
cargo run --bin bootstrap_nns -- run --key my-key-name --port 6666
```
50 changes: 50 additions & 0 deletions rust/noosphere-ns/src/bin/bootstrap_nns/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![cfg(not(target_arch = "wasm32"))]

use clap::{Parser, Subcommand};
use serde::Deserialize;
use std::path::PathBuf;

#[derive(Parser)]
#[clap(name = "bootstrap_nns")]
pub struct CLI {
#[clap(subcommand)]
pub command: CLICommand,
}

#[derive(Subcommand)]
pub enum CLICommand {
Install,
Uninstall,
/// Run DHT nodes in the Noosphere Name System.
Run {
/// The path to the bootstrap configuration, a TOML file, containing
/// entries for keyname/port pairs.
#[clap(short, long)]
config: Option<PathBuf>,

/// If no configuration path provided, the name of the Noosphere keypair to use
/// stored in `~/.noosphere/keys/`.
#[clap(short, long)]
key: Option<String>,

/// If no configuration path provided, the listening port of this DHT node.
#[clap(short, long)]
port: Option<u16>,

/// If no configuration path provided, a flag to create key with name `--key`
/// if one does not already exist.
#[clap(long)]
create_key_if_needed: bool,
},
}

#[derive(Debug, Deserialize)]
pub struct CLIConfigNode {
pub key: String,
pub port: Option<u16>,
}

#[derive(Debug, Deserialize)]
pub struct CLIConfig {
pub nodes: Vec<CLIConfigNode>,
}
38 changes: 38 additions & 0 deletions rust/noosphere-ns/src/bin/bootstrap_nns/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![cfg(not(target_arch = "wasm32"))]

mod cli;
mod runner;
mod utils;
use anyhow::{anyhow, Result};
use clap::Parser;
use cli::{CLICommand, CLI};
use noosphere::key::InsecureKeyStorage;
use runner::{Runner, RunnerConfig};
use tokio;

#[tokio::main]
async fn main() -> Result<()> {
let key_storage = InsecureKeyStorage::new(&utils::get_keys_dir()?)?;

let mut runner = match CLI::parse().command {
command @ CLICommand::Run { .. } => {
Runner::from(RunnerConfig::try_from_command(&key_storage, command).await?)
}
_ => {
return Err(anyhow!("Unimplemented."));
}
};

// Allow aborting (ctrl+c) during the initial run,
// and after (when we want to wait exclusively for ctrl+c signal)
let mut aborted = false;
tokio::select! {
_ = tokio::signal::ctrl_c() => { aborted = true; },
_ = runner.run() => {}
}
if !aborted {
tokio::signal::ctrl_c().await?;
}

Ok(())
}
Loading

0 comments on commit cf7a8cb

Please sign in to comment.