diff --git a/src/database.rs b/src/database.rs index 0bc31d7..7f943bd 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,3 +1,4 @@ +use crate::config::HazeConfig; use crate::exec::{exec, exec_tty, ExitCode}; use crate::image::pull_image; use bollard::config::ContainerCreateBody; @@ -7,7 +8,7 @@ use bollard::Docker; use maplit::hashmap; use miette::{IntoDiagnostic, Report, Result, WrapErr}; use std::io::{stdout, Stdout}; -use std::net::IpAddr; +use std::net::{IpAddr, Ipv4Addr}; use std::str::FromStr; use std::time::Duration; use strum::{Display, EnumIter, EnumProperty, IntoStaticStr}; @@ -414,6 +415,37 @@ impl Database { } } + pub async fn location( + &self, + docker: &Docker, + cloud_id: &str, + config: &HazeConfig, + ) -> Result { + let ip = match self.family() { + DatabaseFamily::Mysql + | DatabaseFamily::MariaDB + | DatabaseFamily::Postgres + | DatabaseFamily::Oracle => self + .ip(docker, cloud_id) + .await + .ok_or_else(|| Report::msg("Failed to get the IP of the database container"))?, + DatabaseFamily::Sqlite => IpAddr::V4(Ipv4Addr::LOCALHOST), + }; + + match self.family() { + DatabaseFamily::Mysql | DatabaseFamily::MariaDB => { + Ok(format!("mysql://haze:haze@{ip}/haze")) + } + DatabaseFamily::Postgres => Ok(format!("postgresql://haze:haze@{ip}/haze")), + DatabaseFamily::Oracle => Ok(format!("oracle://system:haze@{ip}:1521/XE")), + DatabaseFamily::Sqlite => Ok(config + .work_dir + .join(cloud_id) + .join("data/haze.db") + .to_string()), + } + } + pub async fn is_healthy(&self, docker: &Docker, cloud_id: &str, postfix: &str) -> Result { match self.family() { DatabaseFamily::Sqlite => Ok(true), diff --git a/src/main.rs b/src/main.rs index bcfd7e5..049b56b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -422,21 +422,15 @@ async fn main() -> Result { let ip = cloud .ip .ok_or_else(|| Report::msg(format!("{} is not running", cloud.id)))?; - let db_type = match cloud.db().family() { + match cloud.db().family() { DatabaseFamily::Sqlite => { return Err(Report::msg("sqlite is not supported with `haze env`")); } DatabaseFamily::Oracle => { return Err(Report::msg("oracle is not supported with `haze env`")); } - DatabaseFamily::Mysql | DatabaseFamily::MariaDB => "mysql", - DatabaseFamily::Postgres => "postgresql", - }; - let db_ip = cloud - .db() - .ip(&docker, &cloud.id) - .await - .ok_or_else(|| Report::msg(format!("{}-db is not running", cloud.id)))?; + _ => {} + } let mut command = Command::new(command); command @@ -445,7 +439,7 @@ async fn main() -> Result { .env("NEXTCLOUD_URL", &cloud.address) .env( "DATABASE_URL", - format!("{}://haze:haze@{}/haze", db_type, db_ip), + cloud.db().location(&docker, &cloud.id, &config).await?, ); if cloud.services().contains(&Service::RedisTls(RedisTls)) { @@ -552,6 +546,16 @@ async fn setup( let cloud = Cloud::create(docker, options, config).await?; println!("{}", cloud.address); let host = cloud.address.split_once("://").expect("no address?").1; + + match cloud.db().location(docker, &cloud.id, config).await { + Ok(value) => { + println!("Database: {}", value); + } + Err(e) => { + println!("Failed to print DB location: {}", e); + } + } + if always_setup || config.auto_setup.enabled { println!("Waiting for servers to start"); cloud.wait_for_start(docker).await?;