Ver código fonte

bin/taud: load config file & add cli functions

ghassmo 4 anos atrás
pai
commit
72c1d516ad
4 arquivos alterados com 82 adições e 20 exclusões
  1. 1 0
      Cargo.lock
  2. 1 0
      bin/taud/Cargo.toml
  3. 61 20
      bin/taud/src/main.rs
  4. 19 0
      bin/taud/taud_config.toml

+ 1 - 0
Cargo.lock

@@ -5301,6 +5301,7 @@ dependencies = [
  "async-executor",
  "async-executor",
  "async-std",
  "async-std",
  "async-trait",
  "async-trait",
+ "clap 3.0.7",
  "darkfi",
  "darkfi",
  "futures",
  "futures",
  "log",
  "log",

+ 1 - 0
bin/taud/Cargo.toml

@@ -19,6 +19,7 @@ async-channel = "1.6.1"
 async-executor = "1.4.1"
 async-executor = "1.4.1"
 
 
 # Misc
 # Misc
+clap = {version = "3.0.7", features = ["derive"]}
 log = "0.4.14"
 log = "0.4.14"
 num_cpus = "1.13.1"
 num_cpus = "1.13.1"
 simplelog = "0.11.2"
 simplelog = "0.11.2"

+ 61 - 20
bin/taud/src/main.rs

@@ -1,24 +1,39 @@
-use std::{
-    net::{IpAddr, Ipv4Addr, SocketAddr},
-    path::PathBuf,
-    sync::Arc,
-};
+use std::{fs::create_dir_all, path::PathBuf, sync::Arc};
 
 
 use async_executor::Executor;
 use async_executor::Executor;
 use async_trait::async_trait;
 use async_trait::async_trait;
+use clap::{IntoApp, Parser};
 use log::debug;
 use log::debug;
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
 use serde_json::{json, Value};
 use serde_json::{json, Value};
-use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
+use simplelog::{ColorChoice, TermLogger, TerminalMode};
 
 
 use darkfi::{
 use darkfi::{
     rpc::{
     rpc::{
         jsonrpc::{error as jsonerr, response as jsonresp, ErrorCode::*, JsonRequest, JsonResult},
         jsonrpc::{error as jsonerr, response as jsonresp, ErrorCode::*, JsonRequest, JsonResult},
         rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig},
         rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig},
     },
     },
-    Result,
+    util::{
+        cli::{log_config, spawn_config, Config, UrlConfig},
+        expand_path, join_config_path,
+    },
+    Error, Result,
 };
 };
 
 
+const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../taud_config.toml");
+
+/// taud cli
+#[derive(Parser)]
+#[clap(name = "taud")]
+pub struct CliTaud {
+    /// Sets a custom config file
+    #[clap(short, long)]
+    pub config: Option<String>,
+    /// Increase verbosity
+    #[clap(short, parse(from_occurrences))]
+    pub verbose: u8,
+}
+
 #[derive(Clone, Debug, Serialize, Deserialize)]
 #[derive(Clone, Debug, Serialize, Deserialize)]
 struct Timestamp {
 struct Timestamp {
     //XXX change this
     //XXX change this
@@ -26,13 +41,18 @@ struct Timestamp {
 }
 }
 
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 #[derive(Clone, Debug, Serialize, Deserialize)]
-struct Config {
-    path: PathBuf,
+struct TauConfig {
+    /// path to dataset
+    pub dataset_path: String,
+    /// Path to DER-formatted PKCS#12 archive. (used only with tls listener url)
+    pub tls_identity_path: String,
+    /// The address where taud should bind its RPC socket
+    pub rpc_listener_url: UrlConfig,
 }
 }
 
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 #[derive(Clone, Debug, Serialize, Deserialize)]
 struct Settings {
 struct Settings {
-    config: Config,
+    dataset_path: PathBuf,
 }
 }
 
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 #[derive(Clone, Debug, Serialize, Deserialize)]
@@ -101,11 +121,19 @@ impl TaskInfo {
     }
     }
 }
 }
 
 
-async fn start(executor: Arc<Executor<'_>>) -> Result<()> {
-    let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7777);
+async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {
+    if config.dataset_path.is_empty() {
+        return Err(Error::ParseFailed("Failed to parse dataset_path"))
+    }
+
+    let dataset_path = expand_path(&config.dataset_path)?;
+
+    // mkdir dataset_path if not exists
+    create_dir_all(dataset_path.join("month"))?;
+    create_dir_all(dataset_path.join("task"))?;
 
 
     let server_config = RpcServerConfig {
     let server_config = RpcServerConfig {
-        socket_addr: rpc_addr,
+        socket_addr: config.rpc_listener_url.url.parse()?,
         use_tls: false,
         use_tls: false,
         // this is all random filler that is meaningless bc tls is disabled
         // this is all random filler that is meaningless bc tls is disabled
         identity_path: Default::default(),
         identity_path: Default::default(),
@@ -145,13 +173,26 @@ impl JsonRpcInterface {
 
 
 #[async_std::main]
 #[async_std::main]
 async fn main() -> Result<()> {
 async fn main() -> Result<()> {
-    TermLogger::init(
-        LevelFilter::Debug,
-        simplelog::Config::default(),
-        TerminalMode::Mixed,
-        ColorChoice::Auto,
-    )?;
+    let args = CliTaud::parse();
+    let matches = CliTaud::into_app().get_matches();
+
+    let config_path = if args.config.is_some() {
+        expand_path(&args.config.unwrap())?
+    } else {
+        join_config_path(&PathBuf::from("taud_config.toml"))?
+    };
+
+    // Spawn config file if it's not in place already.
+    spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
+    let verbosity_level = matches.occurrences_of("verbose");
+
+    let (lvl, conf) = log_config(verbosity_level)?;
+
+    TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
+
+    let config: TauConfig = Config::<TauConfig>::load(config_path)?;
 
 
     let ex = Arc::new(Executor::new());
     let ex = Arc::new(Executor::new());
-    smol::block_on(start(ex))
+    smol::block_on(start(config, ex))
 }
 }

+ 19 - 0
bin/taud/taud_config.toml

@@ -0,0 +1,19 @@
+## taud configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+
+# Path to the dataset 
+dataset_path = "~/.config/tau"
+
+# Path to DER-formatted PKCS#12 archive. (used only with tls url)
+# This can be created using openssl:
+# openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfile chain_certs.pem
+tls_identity_path = ""
+
+# The address where taud should bind its RPC socket
+[rpc_listener_url]
+url="127.0.0.1:8875"
+# Password for the created TLS identity or tor password
+password = "FOOBAR"
+