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:
parent
3077420db2
commit
e7308bd5c6
12
src/main.rs
12
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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)?;
|
||||
|
|
Loading…
Reference in New Issue