Skip to content
Snippets Groups Projects
Commit 66730035 authored by Jonas SPRENGER's avatar Jonas SPRENGER
Browse files

WIP

parent c7537c3f
Branches jonas/wip
No related tags found
No related merge requests found
......@@ -397,6 +397,7 @@ dependencies = [
"dup-crypto 0.6.0",
"durs-common-tools 0.1.0",
"durs-module 0.1.0-a0.1",
"envy 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -619,6 +620,14 @@ name = "either"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "envy"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "failure"
version = "0.1.5"
......@@ -1627,6 +1636,7 @@ dependencies = [
"checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90"
"checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum envy 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "261b836bcf13f42a01c70351f56bd7b66db6e6fb58352bd214cb77e9269a34b4"
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
......
......@@ -22,5 +22,6 @@ serde = "1.0.*"
serde_derive = "1.0.*"
serde_json = "1.0.*"
rpassword = "1.0.0"
envy = "0.4.*"
[features]
......@@ -118,6 +118,20 @@ impl Default for DuRsConfV1 {
}
}
#[derive(Debug, Copy, Clone, Deserialize, PartialEq)]
#[serde(field_identifier)]
/// Ressource usage
pub enum ResourceUsageConf { //TODO: Hack the serializer!!!
/// Minimal use of the resource, to the detriment of performance
Minimal,
/// Trade-off between resource use and performance
Medium,
/// A performance-oriented trade-off, the use of the resource is slightly limited
Large,
/// No restrictions on the use of the resource, maximizes performance
Infinite,
}
#[derive(Debug, Copy, Clone, Deserialize, PartialEq, Serialize)]
/// Ressource usage
pub enum ResourceUsage {
......@@ -156,7 +170,7 @@ impl Default for ResourcesUsage {
#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)]
/// Duniter configuration v2
pub struct DuRsConfV2 {
pub struct DuRsConfV2 { //TODO: Only for V2, use ARG ENV
/// Currency name
pub currency: CurrencyName,
/// Duniter node unique identifier
......@@ -171,6 +185,32 @@ pub struct DuRsConfV2 {
pub enabled: HashSet<ModuleName>,
}
// TODO: implem from DursConfV2
#[derive(Debug, Clone, Deserialize, PartialEq)]
/// Optional configs loaded by the user (from config file or environment variables)
pub struct DursUserConfV3 {
/// Currency name
pub currency: Option<CurrencyName>,
/// Duniter node unique identifier
pub my_node_id: Option<u32>,
/// Name of the module used by default for synchronization
pub default_sync_module: Option<ModuleName>,
/// Cpu usage
pub cpu_usage: Option<ResourceUsageConf>,
/// Network usage
pub network_usage: Option<ResourceUsageConf>,
/// Memory usage
pub memory_usage: Option<ResourceUsageConf>,
/// Disk space usage
pub disk_space_usage: Option<ResourceUsageConf>,
/// Disabled modules
pub disabled: Option<HashSet<ModuleName>>,
/// Enabled modules
pub enabled: Option<HashSet<ModuleName>>,
/// Config modules in Json
pub modules_conf: Option<ModulesConf>
}
impl Default for DuRsConfV2 {
fn default() -> Self {
DuRsConfV2 {
......@@ -197,9 +237,21 @@ impl From<DuRsConfV1> for DuRsConfV2 {
}
}
#[derive(Clone, Debug, Deserialize)]
/// For the moment use less, check if needed.
pub enum DursUserConf {
///
V3 {
///
global_conf: DursUserConfV3,
///
modules_conf: ModulesConf
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
/// Durs node configuration
pub enum DuRsConf {
pub enum DuRsConf { //TODO: Duplicate to DursUserConf (versioning)
/// Durs node configuration v1
V1(DuRsConfV1),
/// Durs node configuration v2
......@@ -209,6 +261,7 @@ pub enum DuRsConf {
/// Modules configuration
modules_conf: ModulesConf,
},
//TODO: Add V3
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
......@@ -253,6 +306,8 @@ impl Default for DuRsConf {
}
}
// TODO: Add fn to (UserConfV3 from file , UserConfV3 from ENV) => DursConfV3 (computeRealConf)
impl DursConfTrait for DuRsConf {
type GlobalConf = DuRsGlobalConf;
......@@ -264,6 +319,7 @@ impl DursConfTrait for DuRsConf {
} => DuRsGlobalConf::V2(global_conf.clone()),
}
}
//TODO: Don't implem V1 => V3
fn upgrade(self) -> (Self, bool) {
if let DuRsConf::V1(conf_v1) = self {
let modules_conf = conf_v1.modules.clone();
......@@ -499,7 +555,7 @@ pub fn get_profile_path(profiles_path: &Option<PathBuf>, profile_name: &str) ->
}
/// Get keypairs file path
pub fn keypairs_filepath(profiles_path: &Option<PathBuf>, profile: &str) -> PathBuf {
pub fn keypairs_filepath(profiles_path: &Option<PathBuf>, profile: &str) -> PathBuf { //TODO: Is it used???
let profile_path = get_profile_path(profiles_path, profile);
let mut conf_keys_path = profile_path.clone();
conf_keys_path.push(constants::KEYPAIRS_FILENAME);
......@@ -631,20 +687,47 @@ pub fn load_conf_at_path(
keypairs
};
// #[derive(Serialize, Deserialize, Debug)]
// struct TestConf {
// name:String
// }
// Open conf file
let mut conf_path = profile_path;
conf_path.push(constants::CONF_FILENAME);
let conf = if conf_path.as_path().exists() {
match File::open(conf_path.as_path()) {
Ok(mut f) => {
let mut contents = String::new();
f.read_to_string(&mut contents)
.map_err(DursConfFileError::ReadError)?;
println!("CONF.JSON: \n {}",&contents);
// Parse conf file
let conf: DuRsConf =
let file_conf: DursUserConfV3 =
serde_json::from_str(&contents).map_err(DursConfFileError::ParseError)?;
println!("Conf from file: {:?}",file_conf);
let env_conf = envy::from_env::<DursUserConfV3>().unwrap_or_else(|e| {
panic!(dbg!(format!("Fatal error : fail to parse environment variable : {}", e)));
});
println!("Conf from env: {:?}",env_conf);
let conf = generate_from_user_conf(file_conf);
println!("Final conf: {:?}", conf);
// Upgrade conf to latest version
let (conf, upgraded) = conf.upgrade();
//TODO: gen ENV conf
//TODO: merge
//Then compute the real conf from both
// If conf is upgraded, rewrite conf file
if upgraded {
write_conf_file(conf_path.as_path(), &conf)
......@@ -666,6 +749,10 @@ pub fn load_conf_at_path(
Ok((conf, keypairs))
}
fn generate_from_user_conf(conf:DursUserConfV3) -> DuRsConf {
DuRsConf::default()
}
/// Save keypairs in profile folder
// Warning: This function cannot use the macro fatal_error! because the logger is not yet initialized, so it must use panic !
pub fn write_keypairs_file(
......
......@@ -45,6 +45,7 @@ pub struct DursCoreOptions {
pub profile_name: Option<String>,
/// Path where user profiles are persisted
pub profiles_path: Option<PathBuf>,
//TODO: add env prefix
}
/// Durs executable command
......
......@@ -56,6 +56,7 @@ use std::path::PathBuf;
use std::sync::mpsc;
use std::thread;
use unwrap::unwrap;
use std::process::exit;
#[macro_export]
/// Plug modules in durs core
......@@ -238,6 +239,7 @@ impl DursCore<DuRsConf> {
.map_err(DursCoreError::ConfFileError)?;
info!("Success to load global conf.");
exit(42);
// Instanciate durs core
Ok(DursCore {
keypairs,
......@@ -523,6 +525,7 @@ pub fn get_module_conf<M: DursModule<DuRsConf, DursMsg>>(
if let Some(module_conf_json) = module_conf_json {
let module_user_conf: Option<M::ModuleUserConf> =
serde_json::from_str(module_conf_json.to_string().as_str())?;
//TODO: Call fn merge of M::ModuleUserConf
M::generate_module_conf(global_conf, module_user_conf)
} else {
M::generate_module_conf(global_conf, None)
......
......@@ -400,7 +400,7 @@ pub trait DursModule<DC: DursConfTrait, M: ModuleMessage> {
+ Debug
+ Default
+ DeserializeOwned
+ Merge
+ Merge //Note: Added trait Merge
+ Send
+ Serialize
+ Sync;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment