Skip to content

Commit

Permalink
Warehouse now manages configuration files properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reboot-Codes committed Nov 14, 2024
1 parent 45f86aa commit d60316b
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 7 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions clover-hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ taurus = { git = "https://github.com/Reboot-Codes/taurus" }
bollard = "0.17.1"
simple-error = "0.3.1"
git2 = "0.19.0"
os_path = "0.8.0"
serde_jsonc = "1.0.108"
1 change: 1 addition & 0 deletions clover-hub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ async fn wait_for_signal_impl() {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// TODO:: Create a logger that will send logs to a FIFO buffer to send over WS via EvtBuzz
env_logger::Builder::new()
.parse_filters(&env::var("CLOVER_LOG").unwrap_or("info".to_string()))
.init();
Expand Down
3 changes: 2 additions & 1 deletion clover-hub/src/server/appd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub async fn appd_main(
) {
info!("Starting AppDaemon...");

let docker_path = store.config.lock().await.docker_daemon.clone();
// TODO: Move to parameterized config from store!
match Docker::connect_with_unix(&store.config.docker_daemon.clone(), 120, API_DEFAULT_VERSION) {
match Docker::connect_with_unix(&docker_path, 120, API_DEFAULT_VERSION) {
Ok(docker_conn) => {
let docker = Arc::new(docker_conn);

Expand Down
4 changes: 2 additions & 2 deletions clover-hub/src/server/evtbuzz/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct UserConfig {
// TODO: Add options for making certain models ephemeral or persistent.
#[derive(Debug, Clone)]
pub struct Store {
pub config: Arc<Config>,
pub config: Arc<Mutex<Config>>,
pub users: Arc<Mutex<HashMap<String, User>>>,
pub api_keys: Arc<Mutex<HashMap<String, ApiKey>>>,
pub clients: Arc<Mutex<HashMap<String, Client>>>,
Expand All @@ -103,7 +103,7 @@ pub struct CoreUserConfigs {
impl Store {
pub fn new() -> Self {
Store {
config: Arc::new(Config { docker_daemon: "/run/user/1000/podman/podman.sock".to_string() }),
config: Arc::new(Mutex::new(Default::default())),
users: Arc::new(Mutex::new(HashMap::new())),
api_keys: Arc::new(Mutex::new(HashMap::new())),
clients: Arc::new(Mutex::new(HashMap::new())),
Expand Down
18 changes: 18 additions & 0 deletions clover-hub/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ pub async fn server_main(data_dir: &String, port: u16, cancellation_token: Cance
},
warehouse::Error::FailedToCreateDataDir { error } => {
error!("Failed to create data directory! Please create `{}` and set the proper permissions manually, then re-run the server. Failed due to:\n{}", data_dir.clone(), error);
},
warehouse::Error::FailedToCheckConfigFile { error } => {
error!("Failed to check existence of config file, due to:\n{}", error);
},
warehouse::Error::FailedToCreateConfigFile { error } => {
error!("Failed to create the configuration file, due to:\n{}", error);
},
warehouse::Error::FailedToOpenConfigFile { error } => {
error!("Failed to open config file, due to:\n{}", error);
},
warehouse::Error::FailedToParseConfigFile { error } => {
error!("Failed to parse configuration file, due to:\n{}", error);
},
warehouse::Error::FailedToReadConfigFile { error } => {
error!("Failed to read configuration file, due to:\n{}", error);
},
warehouse::Error::FailedToWriteToConfigFile { error } => {
error!("Failed to write to the configuration file, due to:\n{}", error);
}
}

Expand Down
12 changes: 11 additions & 1 deletion clover-hub/src/server/warehouse/config/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
#[derive(Debug, Clone)]
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub docker_daemon: String,
}

impl Default for Config {
fn default() -> Self {
Config {
docker_daemon: "/run/user/1000/podman/podman.sock".to_string()
}
}
}
87 changes: 85 additions & 2 deletions clover-hub/src/server/warehouse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
pub mod config;
pub mod manifest;

use config::models::Config;
use os_path::OsPath;
use std::io::{Read, Write};
use std::sync::Arc;
use std::fs;
use log::debug;
use log::{debug, info};
use simple_error::SimpleError;

use crate::server::evtbuzz::models::Store;

// TODO: Move to snafu crate.
#[derive(Debug, Clone)]
pub enum Error {
FailedToCheckDataDir {
error: SimpleError
},
FailedToCreateDataDir {
error: SimpleError
},
FailedToCheckConfigFile {
error: SimpleError
},
FailedToOpenConfigFile {
error: SimpleError
},
FailedToCreateConfigFile {
error: SimpleError
},
FailedToWriteToConfigFile {
error: SimpleError
},
FailedToParseConfigFile {
error: SimpleError
},
FailedToReadConfigFile {
error: SimpleError
}
}

Expand Down Expand Up @@ -51,8 +73,69 @@ pub async fn setup_warehouse(data_dir: String, store: Arc<Store>) -> Result<(),
}
}

let warehouse_path = OsPath::from(data_dir.clone());

// Read configuration and load defaults otherwise

let config_file_path = warehouse_path.join("/config.jsonc");
match err {
Some(_) => {},
None => {
match fs::exists(config_file_path.clone()) {
Ok(config_file_exists) => {
if !config_file_exists {
match fs::File::create(config_file_path.clone()) {
Ok(mut file) => {
match file.write_all(serde_jsonc::to_string::<Config>(&Default::default()).unwrap().as_bytes()) {
Ok(_) => {
info!("Wrote default config!");
},
Err(e) => {
err = Some(Err(Error::FailedToWriteToConfigFile { error: SimpleError::from(e) }))
}
}
},
Err(e) => {
err = Some(Err(Error::FailedToCreateConfigFile { error: SimpleError::from(e) }))
}
}
}
},
Err(e) => {
err = Some(Err(Error::FailedToCheckConfigFile { error: SimpleError::from(e) }));
}
}
}
}

match err {
Some(_) => {},
None => {
match fs::File::open(config_file_path) {
Ok(mut config_file) => {
let mut contents = String::new();
match config_file.read_to_string(&mut contents) {
Ok(_) => {
match serde_jsonc::from_str::<Config>(&contents) {
Ok(config_values) => {
*store.config.lock().await = config_values;
debug!("Loaded config!");
},
Err(e) => {
err = Some(Err(Error::FailedToParseConfigFile { error: SimpleError::from(e) }))
}
}
},
Err(e) => {
err = Some(Err(Error::FailedToReadConfigFile { error: SimpleError::from(e) }))
}
}
},
Err(e) => {
err = Some(Err(Error::FailedToOpenConfigFile { error: SimpleError::from(e) }))
}
}
}
}

// Read repo data and load into applicable areas in the store.

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/components/00-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Rust-based nerve centre for all communication between modules, their compone

## CORE

Sensible default applications, lovingly designed parts, and dynamic expression packs to get you started. All parts are build using best-practices, and are considered to be fully supported and used as an immutable fallback when it comes to software.
Sensible default applications, lovingly designed parts, and dynamic expression packs to get you started. All parts are built using best-practices, and are considered to be fully supported and used as an immutable fallback when it comes to software.

## Spanner

Expand Down

0 comments on commit d60316b

Please sign in to comment.