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.
This commit is contained in:
Matej Janezic 2024-11-27 17:55:45 +01:00
parent 3077420db2
commit e7308bd5c6
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
4 changed files with 23 additions and 16 deletions

View File

@ -36,9 +36,13 @@ fn main() -> Result<()> {
cli::Django::Shell => scripts::django::shell(), cli::Django::Shell => scripts::django::shell(),
cli::Django::Test => scripts::django::test(), cli::Django::Test => scripts::django::test(),
}, },
cli::Commands::Postgres { command } => match command { cli::Commands::Postgres { command } => {
cli::Postgres::Import { path } => scripts::postgres::import(&path), let db_container =
cli::Postgres::Dump { path } => scripts::postgres::dump(&path), 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),
}
}
} }
} }

View File

@ -63,8 +63,9 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
pub fn manage(rest: &[String]) -> Result<()> { pub fn manage(rest: &[String]) -> Result<()> {
let dsm = get_django_settings_module()?; let dsm = get_django_settings_module()?;
let container = env::var("AHAB_DJANGO_CONTAINER").unwrap_or("appserver".to_string());
let joined = rest.join(" "); 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() CommandBuilder::docker_compose().args(&command).exec()
} }

View File

@ -12,8 +12,10 @@ pub fn down() -> Result<()> {
} }
pub fn exec(rest: &[String]) -> Result<()> { pub fn exec(rest: &[String]) -> Result<()> {
let container = std::env::var("AHAB_DJANGO_CONTAINER").unwrap_or("appserver".to_string());
CommandBuilder::docker_compose() CommandBuilder::docker_compose()
.args("exec appserver") .args("exec")
.args(&container)
.args(rest) .args(rest)
.exec() .exec()
} }

View File

@ -10,11 +10,11 @@ use std::{
use super::docker_compose; use super::docker_compose;
use crate::{command_builder::CommandBuilder, debug_eprintln}; use crate::{command_builder::CommandBuilder, debug_eprintln};
fn get_containers() -> Result<[String; 2]> { fn get_containers(container: &str) -> Result<[String; 2]> {
// get db container // get db container
// FIX: we assume we are running db in service named "postgresbd"
let db_container = CommandBuilder::docker_compose() let db_container = CommandBuilder::docker_compose()
.args("ps -q postgresdb") .args("ps -q")
.args(container)
.exec_get_stdout()? .exec_get_stdout()?
.trim() .trim()
.to_string(); .to_string();
@ -36,15 +36,15 @@ fn get_containers() -> Result<[String; 2]> {
Ok([db_container, app_containers]) Ok([db_container, app_containers])
} }
pub fn import(file: &Path) -> Result<()> { pub fn import(container: &str, file: &Path) -> Result<()> {
let [db_container, app_containers] = get_containers()?; let [db_container, app_containers] = get_containers(container)?;
let dump_file = file.to_string_lossy(); let dump_file = file.to_string_lossy();
eprintln!("stopping all containers"); eprintln!("stopping all containers");
docker_compose::stop()?; docker_compose::stop()?;
eprintln!("starting db container"); eprintln!("starting db container");
docker_compose::start(Some("postgresdb"))?; docker_compose::start(Some(container))?;
eprintln!("restoring database"); eprintln!("restoring database");
let commands = [ let commands = [
@ -58,7 +58,7 @@ pub fn import(file: &Path) -> Result<()> {
debug_eprintln!("waiting until pg_isready"); debug_eprintln!("waiting until pg_isready");
while !CommandBuilder::docker() while !CommandBuilder::docker()
.args(&format!( .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()? .build()?
.stdout(Stdio::null()) .stdout(Stdio::null())
@ -78,15 +78,15 @@ pub fn import(file: &Path) -> Result<()> {
Ok(()) Ok(())
} }
pub fn dump(file: &PathBuf) -> Result<()> { pub fn dump(container: &str, file: &PathBuf) -> Result<()> {
let [db_container, _] = get_containers()?; let [db_container, _] = get_containers(container)?;
eprintln!("dumping to local file {}", file.to_string_lossy()); eprintln!("dumping to local file {}", file.to_string_lossy());
let file = File::create(file)?; let file = File::create(file)?;
let stdout = Stdio::from(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() CommandBuilder::docker()
.args(&command) .args(&command)
.exec_redirect_stdout(stdout)?; .exec_redirect_stdout(stdout)?;