42 lines
962 B
Python
42 lines
962 B
Python
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def compose(*args, check=True, input_bytes=None, timeout=180):
|
|
proc = subprocess.run(
|
|
["docker", "compose", "--env-file", ".env", *args],
|
|
cwd=ROOT,
|
|
input=input_bytes,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
timeout=timeout,
|
|
check=False,
|
|
)
|
|
if check and proc.returncode != 0:
|
|
err = proc.stderr.decode("utf-8", "replace")[-800:]
|
|
raise RuntimeError(err or "docker compose failed")
|
|
return proc
|
|
|
|
|
|
def psql(sql, timeout=180):
|
|
proc = compose(
|
|
"exec",
|
|
"-T",
|
|
"postgres",
|
|
"psql",
|
|
"-U",
|
|
"messenger",
|
|
"-d",
|
|
"messenger",
|
|
"-v",
|
|
"ON_ERROR_STOP=1",
|
|
"-q",
|
|
"-t",
|
|
"-A",
|
|
input_bytes=sql.encode("utf-8"),
|
|
timeout=timeout,
|
|
)
|
|
return proc.stdout.decode("utf-8").strip()
|