From e7308bd5c6d9ae0832bc3c5aaea4ea4a6f0b2a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Wed, 27 Nov 2024 17:55:45 +0100 Subject: [PATCH] feat: control default services via env vars Read default postgres compose service name and default django compose service name from env variables. AHAB_POSTGRES_CONTAINER - defaults to `appserver` AHAB_DJANGO_CONATINER - defaults to `postgresdb` Default values are there for 100% backwards compatibility. --- src/main.rs | 12 ++++++++---- src/scripts/django.rs | 3 ++- src/scripts/docker_compose.rs | 4 +++- src/scripts/postgres.rs | 20 ++++++++++---------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 716c981..9217b98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,9 +36,13 @@ fn main() -> Result<()> { cli::Django::Shell => scripts::django::shell(), cli::Django::Test => scripts::django::test(), }, - cli::Commands::Postgres { command } => match command { - cli::Postgres::Import { path } => scripts::postgres::import(&path), - cli::Postgres::Dump { path } => scripts::postgres::dump(&path), - }, + cli::Commands::Postgres { command } => { + let db_container = + std::env::var("AHAB_POSTGRES_CONTAINER").unwrap_or("postgresdb".to_string()); + match command { + cli::Postgres::Import { path } => scripts::postgres::import(&db_container, &path), + cli::Postgres::Dump { path } => scripts::postgres::dump(&db_container, &path), + } + } } } diff --git a/src/scripts/django.rs b/src/scripts/django.rs index 16cbe72..6468435 100644 --- a/src/scripts/django.rs +++ b/src/scripts/django.rs @@ -63,8 +63,9 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> { pub fn manage(rest: &[String]) -> Result<()> { let dsm = get_django_settings_module()?; + let container = env::var("AHAB_DJANGO_CONTAINER").unwrap_or("appserver".to_string()); let joined = rest.join(" "); - let command = format!("run --rm appserver python manage.py {joined} --settings={dsm}"); + let command = format!("run --rm {container} python manage.py {joined} --settings={dsm}"); CommandBuilder::docker_compose().args(&command).exec() } diff --git a/src/scripts/docker_compose.rs b/src/scripts/docker_compose.rs index d6db867..b94efe7 100644 --- a/src/scripts/docker_compose.rs +++ b/src/scripts/docker_compose.rs @@ -12,8 +12,10 @@ pub fn down() -> Result<()> { } pub fn exec(rest: &[String]) -> Result<()> { + let container = std::env::var("AHAB_DJANGO_CONTAINER").unwrap_or("appserver".to_string()); CommandBuilder::docker_compose() - .args("exec appserver") + .args("exec") + .args(&container) .args(rest) .exec() } diff --git a/src/scripts/postgres.rs b/src/scripts/postgres.rs index 0285da1..a2f6ba8 100644 --- a/src/scripts/postgres.rs +++ b/src/scripts/postgres.rs @@ -10,11 +10,11 @@ use std::{ use super::docker_compose; use crate::{command_builder::CommandBuilder, debug_eprintln}; -fn get_containers() -> Result<[String; 2]> { +fn get_containers(container: &str) -> Result<[String; 2]> { // get db container - // FIX: we assume we are running db in service named "postgresbd" let db_container = CommandBuilder::docker_compose() - .args("ps -q postgresdb") + .args("ps -q") + .args(container) .exec_get_stdout()? .trim() .to_string(); @@ -36,15 +36,15 @@ fn get_containers() -> Result<[String; 2]> { Ok([db_container, app_containers]) } -pub fn import(file: &Path) -> Result<()> { - let [db_container, app_containers] = get_containers()?; +pub fn import(container: &str, file: &Path) -> Result<()> { + let [db_container, app_containers] = get_containers(container)?; let dump_file = file.to_string_lossy(); eprintln!("stopping all containers"); docker_compose::stop()?; eprintln!("starting db container"); - docker_compose::start(Some("postgresdb"))?; + docker_compose::start(Some(container))?; eprintln!("restoring database"); let commands = [ @@ -58,7 +58,7 @@ pub fn import(file: &Path) -> Result<()> { debug_eprintln!("waiting until pg_isready"); while !CommandBuilder::docker() .args(&format!( - "exec {db_container} pg_isready -h postgresdb -p 5432 -U db", + "exec {db_container} pg_isready -h {container} -p 5432 -U db", )) .build()? .stdout(Stdio::null()) @@ -78,15 +78,15 @@ pub fn import(file: &Path) -> Result<()> { Ok(()) } -pub fn dump(file: &PathBuf) -> Result<()> { - let [db_container, _] = get_containers()?; +pub fn dump(container: &str, file: &PathBuf) -> Result<()> { + let [db_container, _] = get_containers(container)?; eprintln!("dumping to local file {}", file.to_string_lossy()); let file = File::create(file)?; let stdout = Stdio::from(file); - let command = format!("exec {db_container} pg_dump -U db --format=c db"); + let command = format!("exec {db_container} pg_dump -U db --format=c {container}"); CommandBuilder::docker() .args(&command) .exec_redirect_stdout(stdout)?;