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:
@@ -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)?;
|
||||
|
||||
Reference in New Issue
Block a user