2025-10-18 19:08:00 -03:00
|
|
|
use crate::config::AppConfig;
|
|
|
|
|
use crate::package_manager::{InstallMode, PackageManager};
|
2025-10-19 11:08:23 -03:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use log::{debug, info, trace, warn};
|
2025-10-18 19:08:00 -03:00
|
|
|
|
|
|
|
|
pub struct BootstrapManager {
|
2025-10-19 11:08:23 -03:00
|
|
|
pub install_mode: InstallMode,
|
|
|
|
|
pub tenant: Option<String>,
|
2025-10-18 19:08:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BootstrapManager {
|
2025-10-19 11:08:23 -03:00
|
|
|
pub fn new(install_mode: InstallMode, tenant: Option<String>) -> Self {
|
|
|
|
|
info!(
|
|
|
|
|
"Initializing BootstrapManager with mode {:?} and tenant {:?}",
|
|
|
|
|
install_mode, tenant
|
|
|
|
|
);
|
2025-10-18 19:08:00 -03:00
|
|
|
Self {
|
2025-10-19 11:08:23 -03:00
|
|
|
install_mode,
|
2025-10-18 19:08:00 -03:00
|
|
|
tenant,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bootstrap(&mut self) -> Result<AppConfig> {
|
2025-10-19 11:08:23 -03:00
|
|
|
info!("Starting bootstrap process");
|
2025-10-18 19:08:00 -03:00
|
|
|
|
2025-10-19 11:08:23 -03:00
|
|
|
let pm = PackageManager::new(self.install_mode.clone(), self.tenant.clone())?;
|
2025-10-18 19:08:00 -03:00
|
|
|
|
2025-10-19 14:02:47 -03:00
|
|
|
let required_components = vec!["tables", "cache", "drive", "llm"];
|
2025-10-18 19:08:00 -03:00
|
|
|
|
2025-10-19 11:08:23 -03:00
|
|
|
for component in required_components {
|
2025-10-19 14:02:47 -03:00
|
|
|
info!("Checking component: {}", component);
|
2025-10-19 11:08:23 -03:00
|
|
|
if !pm.is_installed(component) {
|
|
|
|
|
info!("Installing required component: {}", component);
|
|
|
|
|
futures::executor::block_on(pm.install(component))?;
|
|
|
|
|
trace!("Successfully installed component: {}", component);
|
|
|
|
|
} else {
|
|
|
|
|
debug!("Component {} already installed", component);
|
2025-10-18 19:08:00 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 11:08:23 -03:00
|
|
|
info!("Bootstrap completed successfully");
|
2025-10-18 19:08:00 -03:00
|
|
|
|
2025-10-19 11:08:23 -03:00
|
|
|
let config = match diesel::Connection::establish(
|
|
|
|
|
"postgres://botserver:botserver@localhost:5432/botserver",
|
|
|
|
|
) {
|
|
|
|
|
Ok(mut conn) => {
|
|
|
|
|
trace!("Connected to database for config loading");
|
|
|
|
|
AppConfig::from_database(&mut conn)
|
2025-10-18 19:08:00 -03:00
|
|
|
}
|
2025-10-19 11:08:23 -03:00
|
|
|
Err(e) => {
|
|
|
|
|
warn!("Failed to connect to database for config: {}", e);
|
|
|
|
|
trace!("Falling back to environment configuration");
|
|
|
|
|
AppConfig::from_env()
|
2025-10-18 19:08:00 -03:00
|
|
|
}
|
2025-10-19 11:08:23 -03:00
|
|
|
};
|
2025-10-18 19:08:00 -03:00
|
|
|
|
2025-10-19 11:08:23 -03:00
|
|
|
Ok(config)
|
2025-10-18 19:08:00 -03:00
|
|
|
}
|
|
|
|
|
}
|