113 lines
3 KiB
Rust
113 lines
3 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use figment::{
|
|
providers::{Env, Format, Json, Toml},
|
|
Figment, Metadata, Provider,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use trust_dns_proto::rr::Name;
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct StorageConfig {
|
|
pub primary_pool: String,
|
|
pub secondary_pool: String,
|
|
pub ci_image_pool: String,
|
|
pub base_image_pool: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct SOAConfig {
|
|
pub nzr_domain: Name,
|
|
pub contact: Name,
|
|
pub refresh: i32,
|
|
pub retry: i32,
|
|
pub expire: i32,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct DNSConfig {
|
|
pub listen_addr: String,
|
|
pub default_zone: Name,
|
|
pub soa: SOAConfig,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct RPCConfig {
|
|
pub socket_path: PathBuf,
|
|
pub admin_group: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Config {
|
|
pub rpc: RPCConfig,
|
|
pub log_level: String,
|
|
pub db_path: PathBuf,
|
|
pub libvirt_uri: String,
|
|
pub storage: StorageConfig,
|
|
pub dns: DNSConfig,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Self {
|
|
log_level: "WARN".to_owned(),
|
|
rpc: RPCConfig {
|
|
socket_path: PathBuf::from("/var/run/nazrin/nzrd.sock"),
|
|
admin_group: None,
|
|
},
|
|
db_path: PathBuf::from("/var/run/nazrin/nzr.db"),
|
|
libvirt_uri: match std::env::var("LIBVIRT_URI") {
|
|
Ok(v) => v,
|
|
Err(_) => String::from("qemu:///system"),
|
|
},
|
|
storage: StorageConfig {
|
|
primary_pool: "pri".to_owned(),
|
|
secondary_pool: "data".to_owned(),
|
|
ci_image_pool: "cidata".to_owned(),
|
|
base_image_pool: "images".to_owned(),
|
|
},
|
|
dns: DNSConfig {
|
|
listen_addr: "127.0.0.1:5353".to_owned(),
|
|
default_zone: Name::from_utf8("servers.local").unwrap(),
|
|
soa: SOAConfig {
|
|
nzr_domain: Name::from_utf8("nzr.local").unwrap(),
|
|
contact: Name::from_utf8("admin.nzr.local").unwrap(),
|
|
refresh: 86400,
|
|
retry: 7200,
|
|
expire: 3_600_000,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Provider for Config {
|
|
fn metadata(&self) -> figment::Metadata {
|
|
Metadata::named("Nazrin config")
|
|
}
|
|
|
|
fn data(
|
|
&self,
|
|
) -> Result<figment::value::Map<figment::Profile, figment::value::Dict>, figment::Error> {
|
|
figment::providers::Serialized::defaults(Config::default()).data()
|
|
}
|
|
|
|
fn profile(&self) -> Option<figment::Profile> {
|
|
None
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn figment() -> Figment {
|
|
let mut fig = Figment::from(Config::default()).merge(Toml::file("/etc/nazrin.conf"));
|
|
|
|
#[allow(deprecated)]
|
|
if let Some(mut home) = std::env::home_dir() {
|
|
home.push(".nazrin.conf");
|
|
fig = fig.merge(Json::file(home));
|
|
}
|
|
|
|
fig.merge(Env::prefixed("NZR_"))
|
|
}
|
|
}
|