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

dev: First pass on a bootstrap CLI #143

Merged
merged 1 commit into from
Nov 16, 2022
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
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.

17 changes: 17 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,25 @@ 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"]
jsantell marked this conversation as resolved.
Show resolved Hide resolved
bootstrap = ["clap", "noosphere", "home", "toml"]


[[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
jsantell marked this conversation as resolved.
Show resolved Hide resolved
```
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 {
/// 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>,
},

/// Utility to create keys compatible with Noosphere.
KeyGen {
/// The name of the key to be stored in `~/.noosphere/keys/`.
#[clap(short, long)]
key: String,
},
}

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

#[derive(Debug, Deserialize)]
pub struct CLIConfig {
pub nodes: Vec<CLIConfigNode>,
}
48 changes: 48 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,48 @@
#[cfg(target_arch = "wasm32")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
mod cli;
#[cfg(not(target_arch = "wasm32"))]
mod runner;
#[cfg(not(target_arch = "wasm32"))]
mod utils;

#[cfg(not(target_arch = "wasm32"))]
mod inner {
pub use crate::cli::{CLICommand, CLI};
pub use crate::runner::{Runner, RunnerConfig};
pub use anyhow::{anyhow, Result};
pub use clap::Parser;
pub use noosphere::key::{InsecureKeyStorage, KeyStorage};
pub use tokio;
}

#[cfg(not(target_arch = "wasm32"))]
use inner::*;

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

match CLI::parse().command {
command @ CLICommand::Run { .. } => {
let mut runner =
Runner::from(RunnerConfig::try_from_command(&key_storage, command).await?);
utils::run_until_abort(runner.run()).await?;
Ok(())
}
CLICommand::KeyGen { key } => {
if key_storage.require_key(&key).await.is_ok() {
println!("Key \"{}\" already exists in `~/.noosphere/keys/`.", &key);
} else {
key_storage.create_key(&key).await?;
println!("Key \"{}\" created in `~/.noosphere/keys/`.", &key);
}
Ok(())
}
}
}
Loading