Config + Default directories (#1433)

* config file can now be generated by executable

* rustfmt

* remove now-unnecessary config defaults test

* set up paths and config file creation in user's home directory

* rustfmt

* remove default grin.toml

* add grin configuration command to spit out config file

* Split configuration into wallet and server

* rustfmt

* Restore logging to wallet configurations

* rustfmt
This commit is contained in:
Yeastplume
2018-08-30 10:10:40 +01:00
committed by GitHub
parent 471e80e69e
commit 1ded3f3972
19 changed files with 955 additions and 381 deletions
+71
View File
@@ -0,0 +1,71 @@
// Copyright 2018 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Grin configuation file output command
use config::{GlobalConfig, GlobalWalletConfig};
use std::env;
/// Create a config file in the current directory
pub fn config_command_server(file_name: &str) {
let mut default_config = GlobalConfig::default();
let current_dir = env::current_dir().unwrap_or_else(|e| {
panic!("Error creating config file: {}", e);
});
let mut config_file_name = current_dir.clone();
config_file_name.push(file_name);
if config_file_name.exists() {
panic!(
"{} already exists in the current directory. Please remove it first",
file_name
);
}
default_config.update_paths(&current_dir);
default_config
.write_to_file(config_file_name.to_str().unwrap())
.unwrap_or_else(|e| {
panic!("Error creating config file: {}", e);
});
println!(
"{} file configured and created in current directory",
file_name
);
}
/// Create a config file in the current directory
pub fn config_command_wallet(file_name: &str) {
let mut default_config = GlobalWalletConfig::default();
let current_dir = env::current_dir().unwrap_or_else(|e| {
panic!("Error creating config file: {}", e);
});
let mut config_file_name = current_dir.clone();
config_file_name.push(file_name);
if config_file_name.exists() {
panic!(
"{} already exists in the target directory. Please remove it first",
file_name
);
}
default_config.update_paths(&current_dir);
default_config
.write_to_file(config_file_name.to_str().unwrap())
.unwrap_or_else(|e| {
panic!("Error creating config file: {}", e);
});
println!(
"File {} configured and created",
config_file_name.to_str().unwrap(),
);
}
+3 -1
View File
@@ -13,9 +13,11 @@
// limitations under the License.
mod client;
mod config;
mod server;
mod wallet;
pub use self::client::client_command;
pub use self::config::{config_command_server, config_command_wallet};
pub use self::server::server_command;
pub use self::wallet::wallet_command;
pub use self::wallet::{seed_exists, wallet_command};
+2 -2
View File
@@ -123,7 +123,7 @@ pub fn server_command(server_args: Option<&ArgMatches>, mut global_config: Globa
}
}
if let Some(true) = server_config.run_wallet_listener {
/*if let Some(true) = server_config.run_wallet_listener {
let mut wallet_config = global_config.members.as_ref().unwrap().wallet.clone();
wallet::init_wallet_seed(wallet_config.clone());
let wallet = wallet::instantiate_wallet(wallet_config.clone(), "");
@@ -155,7 +155,7 @@ pub fn server_command(server_args: Option<&ArgMatches>, mut global_config: Globa
)
});
});
}
}*/
// start the server in the different run modes (interactive or daemon)
if let Some(a) = server_args {
+16 -5
View File
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::PathBuf;
/// Wallet commands processing
use std::process::exit;
use std::sync::{Arc, Mutex};
@@ -20,7 +21,7 @@ use std::time::Duration;
use clap::ArgMatches;
use config::GlobalConfig;
use config::GlobalWalletConfig;
use core::core;
use grin_wallet::{self, controller, display, libwallet};
use grin_wallet::{HTTPWalletClient, LMDBBackend, WalletConfig, WalletInst, WalletSeed};
@@ -28,12 +29,22 @@ use keychain;
use servers::start_webwallet_server;
use util::LOGGER;
pub fn init_wallet_seed(wallet_config: WalletConfig) {
pub fn _init_wallet_seed(wallet_config: WalletConfig) {
if let Err(_) = WalletSeed::from_file(&wallet_config) {
WalletSeed::init_file(&wallet_config).expect("Failed to create wallet seed file.");
};
}
pub fn seed_exists(wallet_config: WalletConfig) -> bool {
let mut data_file_dir = PathBuf::new();
data_file_dir.push(wallet_config.data_file_dir);
data_file_dir.push(grin_wallet::SEED_FILE);
if data_file_dir.exists() {
true
} else {
false
}
}
pub fn instantiate_wallet(
wallet_config: WalletConfig,
passphrase: &str,
@@ -49,7 +60,7 @@ pub fn instantiate_wallet(
warn!(LOGGER, "Migration successful. Using LMDB Wallet backend");
}
warn!(LOGGER, "Please check the results of the migration process using `grin wallet info` and `grin wallet outputs`");
warn!(LOGGER, "If anything went wrong, you can try again by deleting the `wallet_data` directory and running a wallet command");
warn!(LOGGER, "If anything went wrong, you can try again by deleting the `db` directory and running a wallet command");
warn!(LOGGER, "If all is okay, you can move/backup/delete all files in the wallet directory EXCEPT FOR wallet.seed");
}
let client = HTTPWalletClient::new(&wallet_config.check_node_api_http_addr);
@@ -63,9 +74,9 @@ pub fn instantiate_wallet(
Box::new(db_wallet)
}
pub fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
pub fn wallet_command(wallet_args: &ArgMatches, config: GlobalWalletConfig) {
// just get defaults from the global config
let mut wallet_config = global_config.members.unwrap().wallet;
let mut wallet_config = config.members.unwrap().wallet;
if wallet_args.is_present("external") {
wallet_config.api_listen_interface = "0.0.0.0".to_string();
+89 -54
View File
@@ -41,7 +41,7 @@ pub mod tui;
use clap::{App, Arg, SubCommand};
use config::GlobalConfig;
use config::config::{SERVER_CONFIG_FILE_NAME, WALLET_CONFIG_FILE_NAME};
use core::global;
use util::{init_logger, LOGGER};
@@ -81,10 +81,14 @@ fn main() {
.version(crate_version!())
.author("The Grin Team")
.about("Lightweight implementation of the MimbleWimble protocol.")
// specification of all the server commands and options
.subcommand(SubCommand::with_name("server")
.about("Control the Grin server")
.arg(Arg::with_name("config_file")
.short("c")
.long("config_file")
.help("Path to a grin-server.toml configuration file")
.takes_value(true))
.arg(Arg::with_name("port")
.short("p")
.long("port")
@@ -104,7 +108,9 @@ fn main() {
.short("w")
.long("wallet_url")
.help("The wallet listener to which mining rewards will be sent")
.takes_value(true))
.takes_value(true))
.subcommand(SubCommand::with_name("config")
.about("Generate a configuration grin-server.toml file in the current directory"))
.subcommand(SubCommand::with_name("start")
.about("Start the Grin server as a daemon"))
.subcommand(SubCommand::with_name("stop")
@@ -274,85 +280,114 @@ fn main() {
.about("basic wallet contents summary"))
.subcommand(SubCommand::with_name("init")
.about("Initialize a new wallet seed file and database."))
.about("Initialize a new wallet seed file and database.")
.arg(Arg::with_name("here")
.short("h")
.long("here")
.help("Create wallet files in the current directory instead of the default ~/.grin directory")
.takes_value(false)))
.subcommand(SubCommand::with_name("restore")
.about("Attempt to restore wallet contents from the chain using seed and password. \
NOTE: Backup wallet.* and run `wallet listen` before running restore.")))
.get_matches();
let mut wallet_config = None;
let mut node_config = None;
// load a global config object,
// then modify that object with any switches
// found so that the switches override the
// global config file
// This will return a global config object,
// which will either contain defaults for all // of the config structures or a
// configuration
// read from a config file
let mut global_config = GlobalConfig::new(None).unwrap_or_else(|e| {
panic!("Error parsing config file: {}", e);
});
// initialize the logger
let mut log_conf = global_config
.members
.as_mut()
.unwrap()
.logging
.clone()
.unwrap();
let run_tui = global_config.members.as_mut().unwrap().server.run_tui;
if run_tui.is_some() && run_tui.unwrap() && args.subcommand().0 != "wallet" {
log_conf.log_to_stdout = false;
log_conf.tui_running = Some(true);
// Deal with configuration file creation
match args.subcommand() {
("server", Some(server_args)) => {
// If it's just a server config command, do it and exit
if let ("config", Some(_)) = server_args.subcommand() {
cmd::config_command_server(SERVER_CONFIG_FILE_NAME);
return;
}
}
("wallet", Some(wallet_args)) => {
// wallet init command should spit out its config file then continue
// (if desired)
if let ("init", Some(init_args)) = wallet_args.subcommand() {
if init_args.is_present("here") {
cmd::config_command_wallet(WALLET_CONFIG_FILE_NAME);
}
}
}
_ => {}
}
match args.subcommand() {
// If it's a wallet command, try and load a wallet config file
("wallet", Some(wallet_args)) => {
let mut w = config::initial_setup_wallet().unwrap_or_else(|e| {
panic!("Error loading wallet configuration: {}", e);
});
if !cmd::seed_exists(w.members.as_ref().unwrap().wallet.clone()) {
if let ("init", Some(_)) = wallet_args.subcommand() {
} else {
println!("Wallet seed file doesn't exist. Run `grin wallet -p [password] init` first");
return;
}
}
let mut l = w.members.as_mut().unwrap().logging.clone().unwrap();
l.tui_running = Some(false);
init_logger(Some(l));
warn!(
LOGGER,
"Using wallet configuration file at {}",
w.config_file_path.as_ref().unwrap().to_str().unwrap()
);
wallet_config = Some(w);
}
// Otherwise load up the node config as usual
_ => {
let mut s = config::initial_setup_server().unwrap_or_else(|e| {
panic!("Error loading server configuration: {}", e);
});
let mut l = s.members.as_mut().unwrap().logging.clone().unwrap();
let run_tui = s.members.as_mut().unwrap().server.run_tui;
if let Some(true) = run_tui {
l.log_to_stdout = false;
l.tui_running = Some(true);
}
init_logger(Some(l));
global::set_mining_mode(s.members.as_mut().unwrap().server.clone().chain_type);
if let Some(file_path) = &s.config_file_path {
info!(
LOGGER,
"Using configuration file at {}",
file_path.to_str().unwrap()
);
} else {
info!(LOGGER, "Node configuration file not found, using default");
}
node_config = Some(s);
}
}
init_logger(Some(log_conf));
global::set_mining_mode(
global_config
.members
.as_mut()
.unwrap()
.server
.clone()
.chain_type,
);
log_build_info();
if let Some(file_path) = &global_config.config_file_path {
info!(
LOGGER,
"Found configuration file at {}",
file_path.to_str().unwrap()
);
} else {
info!(LOGGER, "configuration file not found, using default");
}
match args.subcommand() {
// server commands and options
("server", Some(server_args)) => {
cmd::server_command(Some(server_args), global_config);
cmd::server_command(Some(server_args), node_config.unwrap());
}
// client commands and options
("client", Some(client_args)) => {
cmd::client_command(client_args, global_config);
cmd::client_command(client_args, node_config.unwrap());
}
// client commands and options
("wallet", Some(wallet_args)) => {
cmd::wallet_command(wallet_args, global_config);
cmd::wallet_command(wallet_args, wallet_config.unwrap());
}
// If nothing is specified, try to just use the config file instead
// this could possibly become the way to configure most things
// with most command line options being phased out
_ => {
cmd::server_command(None, global_config);
cmd::server_command(None, node_config.unwrap());
}
}
}