Skip to content

Commit

Permalink
Merge pull request runziggurat#85 from eqlabs/feat/improve_config
Browse files Browse the repository at this point in the history
feat: automated node config path args
  • Loading branch information
niklaslong authored Jul 5, 2021
2 parents 7cbe057 + 5f8503f commit 41d394d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Ziggurat is configured with a `config.toml` file in the root.
```toml
kind = "zebra"
path = "path/to/zebra/repo"
start_command = "cargo +stable r -- --config zebra.toml --verbose start"
start_command = "cargo +stable r -- --verbose start"

# kind = "zcashd"
# path = "path/to/zcash/repo"
# start_command = "./src/zcashd -debug=1 -dnsseed=0 -printtoconsole -logips=1 -listenonion=0 -dns=0 -conf=/path/to/zcash/repo/zcash.conf"
# start_command = "./src/zcashd -debug=1 -dnsseed=0 -printtoconsole -logips=1 -listenonion=0 -dns=0"
```

Information about the node to be tested can be set under the `[node]` table:
Expand Down
34 changes: 34 additions & 0 deletions src/setup/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use std::{

use crate::setup::node::Action;

// The names of the files the node configurations will be written to.
const ZEBRA_CONFIG: &str = "zebra.toml";
const ZCASHD_CONFIG: &str = "zcash.conf";

const CONFIG: &str = "config.toml";
const DEFAULT_PORT: u16 = 8080;

Expand Down Expand Up @@ -65,6 +69,16 @@ pub(super) enum NodeKind {
Zcashd,
}

impl NodeKind {
/// Path to the configuration file for this [NodeKind]
pub(super) fn config_filepath(&self) -> std::path::PathBuf {
match self {
NodeKind::Zebra => std::env::current_dir().unwrap().join(ZEBRA_CONFIG),
NodeKind::Zcashd => std::env::current_dir().unwrap().join(ZCASHD_CONFIG),
}
}
}

/// Node configuration read from the `config.toml` file.
#[derive(Clone)]
pub(super) struct NodeMetaData {
Expand All @@ -91,6 +105,26 @@ impl NodeMetaData {
let mut start_args = args_from(&config_file.start_command);
let start_command = start_args.remove(0);

// insert the config file path into start args
let config_path = config_file.kind.config_filepath();
match config_file.kind {
NodeKind::Zebra => {
// Zebra's final arg must be `start`, so we insert the actual args before it.
let n_args = start_args.len();
if n_args < 1 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Expected at least one start_command arg for Zebra (`start`)",
));
}
start_args.insert(n_args - 1, "--config".into());
start_args.insert(n_args, config_path.into_os_string());
}
NodeKind::Zcashd => {
start_args.push(format!("-conf={}", config_path.to_str().unwrap()).into());
}
}

Ok(Self {
kind: config_file.kind,
path: config_file.path,
Expand Down
15 changes: 2 additions & 13 deletions src/setup/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ use tokio::process::{Child, Command};

use std::{fs, io, net::SocketAddr, process::Stdio, time::Duration};

// The names of the files the node configurations will be written to.
const ZEBRA_CONFIG: &str = "zebra.toml";
const ZCASHD_CONFIG: &str = "zcash.conf";

/// Actions to prepare node state on start.
pub enum Action {
/// Performs no action
Expand Down Expand Up @@ -272,7 +268,7 @@ impl Node {
}

fn generate_config_file(&self) -> io::Result<()> {
let path = self.config_filepath();
let path = self.meta.kind.config_filepath();
let content = match self.meta.kind {
NodeKind::Zebra => ZebraConfigFile::generate(&self.config)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?,
Expand All @@ -282,20 +278,13 @@ impl Node {
fs::write(path, content)
}

fn config_filepath(&self) -> std::path::PathBuf {
match self.meta.kind {
NodeKind::Zebra => self.meta.path.join(ZEBRA_CONFIG),
NodeKind::Zcashd => self.meta.path.join(ZCASHD_CONFIG),
}
}

fn cleanup(&self) -> io::Result<()> {
self.cleanup_config_file()?;
self.cleanup_cache()
}

fn cleanup_config_file(&self) -> io::Result<()> {
let path = self.config_filepath();
let path = self.meta.kind.config_filepath();
match fs::remove_file(path) {
// File may not exist, so we surpress the error.
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e),
Expand Down

0 comments on commit 41d394d

Please sign in to comment.