diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bcfcb95dc..2b595e04af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed +- Added centralized documentation for environment variables in use by Holochain [PR](https://github.com/holochain/holochain-rust/pull/990) - Added command `hc keygen` which creates a new key pair, asks for a passphrase and writes an encrypted key bundle file to `~/.holochain/keys`. - `hash` properties for `UiBundleConfiguration` and `DnaConfiguration` in Conductor config files is now optional - core now depends on `pretty_assertions` crate diff --git a/cli/src/cli/run.rs b/cli/src/cli/run.rs index 38bf6ecb81..69c09f1463 100644 --- a/cli/src/cli/run.rs +++ b/cli/src/cli/run.rs @@ -1,13 +1,14 @@ use cli::{self, package}; use colored::*; use error::DefaultResult; +use holochain_common::env_vars::EnvVar; use holochain_conductor_api::{ conductor::{mount_conductor_from_config, CONDUCTOR}, config::*, logger::LogRules, }; use holochain_core_types::agent::AgentId; -use std::{env, fs}; +use std::fs; const LOCAL_STORAGE_PATH: &str = ".hc"; @@ -16,7 +17,7 @@ const DNA_CONFIG_ID: &str = "hc-run-dna"; const INSTANCE_CONFIG_ID: &str = "test-instance"; const INTERFACE_CONFIG_ID: &str = "websocket-interface"; -/// Starts a small conductor with the current application running +/// Starts a minimal configuration Conductor with the current application running pub fn run( package: bool, port: u16, @@ -28,7 +29,10 @@ pub fn run( cli::package(true, Some(package::DEFAULT_BUNDLE_FILE_NAME.into()))?; } - let agent_name = env::var("HC_AGENT").ok(); + // note that this behaviour is documented within + // holochain_common::env_vars module and should be updated + // if this logic changes + let agent_name = EnvVar::value(&EnvVar::Agent).ok(); let agent = AgentId::generate_fake(&agent_name.unwrap_or_else(|| String::from("testAgent"))); let agent_config = AgentConfiguration { id: AGENT_CONFIG_ID.into(), @@ -60,7 +64,12 @@ pub fn run( storage, }; - let interface_type = env::var("HC_INTERFACE").ok().unwrap_or_else(|| interface); + // note that this behaviour is documented within + // holochain_common::env_vars module and should be updated + // if this logic changes + let interface_type = EnvVar::value(&EnvVar::Interface) + .ok() + .unwrap_or_else(|| interface); let driver = if interface_type == String::from("websocket") { InterfaceDriver::Websocket { port } } else if interface_type == String::from("http") { @@ -85,15 +94,21 @@ pub fn run( rules, }; - let n3h_path = env::var("HC_N3H_PATH").ok(); + // note that this behaviour is documented within + // holochain_common::env_vars module and should be updated + // if this logic changes + let n3h_path = EnvVar::value(&EnvVar::N3hPath).ok(); // create an n3h network config if the --networked flag is set // or if a value where to find n3h has been put into the // HC_N3H_PATH environment variable let network_config = if networked || n3h_path.is_some() { - let n3h_mode = env::var("HC_N3H_MODE").ok(); - let n3h_persistence_path = env::var("HC_N3H_WORK_DIR").ok(); - let n3h_bootstrap_node = env::var("HC_N3H_BOOTSTRAP_NODE").ok(); + // note that this behaviour is documented within + // holochain_common::env_vars module and should be updated + // if this logic changes + let n3h_mode = EnvVar::value(&EnvVar::N3hMode).ok(); + let n3h_persistence_path = EnvVar::value(&EnvVar::N3hWorkDir).ok(); + let n3h_bootstrap_node = EnvVar::value(&EnvVar::N3hBootstrapNode).ok(); let mut n3h_bootstrap = Vec::new(); if n3h_bootstrap_node.is_some() { @@ -101,7 +116,10 @@ pub fn run( } // Load end_user config file - let networking_config_filepath = env::var("NETWORKING_CONFIG_FILE").ok(); + // note that this behaviour is documented within + // holochain_common::env_vars module and should be updated + // if this logic changes + let networking_config_filepath = EnvVar::value(&EnvVar::NetworkingConfigFile).ok(); Some(NetworkConfig { bootstrap_nodes: n3h_bootstrap, diff --git a/common/src/env_vars.rs b/common/src/env_vars.rs new file mode 100644 index 0000000000..1c4dfbfaee --- /dev/null +++ b/common/src/env_vars.rs @@ -0,0 +1,57 @@ +//! Holochain uses a number of environment variables that can be set, for easy configuration for aspects of the system. +//! Below is the complete list of them, and what they are used for. +//! +//! ### `hc run` +//! Read more about the use of these environment variables [here](https://developer.holochain.org/guide/latest/hc_configuring_networking.html). +//! - **HC_AGENT** *string* Set an alternative name for the agent for the development instance. +//! Default value is `testAgent`. +//! Useful for changing the agent while running multiple instances. +//! - **HC_INTERFACE** *string* **websocket** OR **http** Set an interface type to use. Setting this as an environment variable will override the +//! value of the `--interface` option for `hc run`. The default interface if neither is set is `websocket`. +//! - **HC_N3H_PATH** *string* Path to the [n3h](https://github.com/holochain/n3h) networking module. If set, this will automatically trigger `hc run` to do live networking, +//! instead of mock networking which is the default. The following environment variables are irrelevant if `hc run` is not run with the `--networked` +//! flag, AND this is not set, because they are all configuration for live networking. Default is a subdirectory of your HOME folder, at the path `.hc/net/n3h`. +//! - **HC_N3H_WORK_DIR** *string* Eventually, there will be a directory needed by n3h for persisting data, such as remote node QoS metrics, peer lists, and non-core DHT data items such as peer discovery info. +//! Default is temporary directory which will get removed again once the Conductor process stops. Recommended not to use this at this time. +//! - **HC_N3H_BOOTSTRAP_NODE** *string* Set an external p2p bound ip4 address for another node, to bootstrap the networking discovery process. +//! Without this, a second node will of a network will be unable to find any others. See [configuring networking]([here](https://developer.holochain.org/guide/latest/hc_configuring_networking.html) +//! for details. +//! - **NETWORKING_CONFIG_FILE** *string* Path to a JSON file containing configuration for the n3h networking module. More on this soon. Recommended to +//! not use this as this time. +//! +//! ### Other +//! - **HC_SIMPLE_LOGGER_MUTE** *int* Setting this value to 1 will silence the log output of a SimpleLogger. Use with any Conductor. + +// TODO, add this back in once the only option isn't "HACK" +// - **HC_N3H_MODE** *string* **HACK** + +use std::env::VarError; + +pub enum EnvVar { + Agent, + Interface, + N3hPath, + N3hMode, + N3hWorkDir, + N3hBootstrapNode, + NetworkingConfigFile, + SimpleLoggerMute, +} + +impl EnvVar { + pub fn as_str(&self) -> &str { + match self { + EnvVar::Agent => "HC_AGENT", + EnvVar::Interface => "HC_INTERFACE", + EnvVar::N3hPath => "HC_N3H_PATH", + EnvVar::N3hMode => "HC_N3H_MODE", + EnvVar::N3hWorkDir => "HC_N3H_WORK_DIR", + EnvVar::N3hBootstrapNode => "HC_N3H_BOOTSTRAP_NODE", + EnvVar::NetworkingConfigFile => "NETWORKING_CONFIG_FILE", + EnvVar::SimpleLoggerMute => "HC_SIMPLE_LOGGER_MUTE", + } + } + pub fn value(&self) -> Result { + std::env::var(self.as_str()) + } +} diff --git a/common/src/lib.rs b/common/src/lib.rs index 8118b2968e..13affa7aa3 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1 +1,2 @@ +pub mod env_vars; pub mod paths; diff --git a/conductor_api/src/config.rs b/conductor_api/src/config.rs index d31615989d..6e60190686 100644 --- a/conductor_api/src/config.rs +++ b/conductor_api/src/config.rs @@ -466,10 +466,16 @@ pub struct NetworkConfig { pub networking_config_file: Option, } +// note that this behaviour is documented within +// holochain_common::env_vars module and should be updated +// if this logic changes pub fn default_n3h_mode() -> String { String::from("HACK") } +// note that this behaviour is documented within +// holochain_common::env_vars module and should be updated +// if this logic changes pub fn default_n3h_path() -> String { if let Some(user_dirs) = directories::UserDirs::new() { user_dirs @@ -484,6 +490,9 @@ pub fn default_n3h_path() -> String { } } +// note that this behaviour is documented within +// holochain_common::env_vars module and should be updated +// if this logic changes pub fn default_n3h_persistence_path() -> String { env::temp_dir().to_string_lossy().to_string() } diff --git a/core/Cargo.toml b/core/Cargo.toml index 66ade5d38b..f130de5576 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -31,6 +31,7 @@ num-derive = "0.2" config = "0.8" regex = "1" toml = "0.4.8" +holochain_common = { path = "../common" } holochain_core_types = { path = "../core_types" } holochain_core_types_derive = { path = "../core_types_derive" } holochain_cas_implementations = { path = "../cas_implementations" } diff --git a/core/src/lib.rs b/core/src/lib.rs index 73c04e8125..f5879099e9 100755 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -23,6 +23,7 @@ extern crate num_traits; extern crate regex; extern crate config; +extern crate holochain_common; extern crate holochain_net; #[macro_use] extern crate holochain_wasm_utils; diff --git a/core/src/logger.rs b/core/src/logger.rs index bee3056396..551df3e4ea 100644 --- a/core/src/logger.rs +++ b/core/src/logger.rs @@ -2,6 +2,7 @@ //! which is separate from standard logging via the log crate warn! info! debug! logging that //! gets emitted globaly from the conductor. use chrono::Local; +use holochain_common::env_vars::EnvVar; use std::sync::{mpsc, Arc, Mutex}; /// trait that defines the logging functionality that holochain_core requires @@ -21,7 +22,10 @@ pub struct SimpleLogger { #[cfg_attr(tarpaulin, skip)] impl Logger for SimpleLogger { fn log(&mut self, msg: String) { - if std::env::var("HC_SIMPLE_LOGGER_MUTE").is_err() { + // note that this behaviour is documented within + // holochain_common::env_vars module and should be updated + // if this logic changes + if EnvVar::value(&EnvVar::SimpleLoggerMute).is_err() { let date = Local::now(); println!("{}:{}", date.format("%Y-%m-%d %H:%M:%S"), msg); } diff --git a/wasm_utils/src/lib.rs b/wasm_utils/src/lib.rs index 81e1b66799..e6cf783bf6 100644 --- a/wasm_utils/src/lib.rs +++ b/wasm_utils/src/lib.rs @@ -18,6 +18,8 @@ pub mod macros; pub mod memory; pub fn wasm_target_dir(test_path: &str, wasm_path: &str) -> String { + // this env var checker can't use holochain_common + // crate because that uses `directories` crate which doesn't compile to WASM match std::env::var("HC_TARGET_PREFIX") { Ok(prefix) => format!("{}{}{}target", prefix, test_path, wasm_path), Err(_) => format!("{}target", wasm_path),