-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcompose_utils.py
More file actions
49 lines (37 loc) · 1.17 KB
/
compose_utils.py
File metadata and controls
49 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from contextlib import contextmanager
from typing import List
import click
import derex # noqa # This is ugly, but makes mypy and flake8 happy and still performs type checks
import logging
import subprocess
import sys
logger = logging.getLogger(__name__)
def run_compose(compose_argv: List[str]):
subprocess.run(["docker", "compose"] + compose_argv)
def run_docker_compose(
compose_argv: List[str], dry_run: bool = False, exit_afterwards: bool = False
):
"""Run a docker-compose command with the specified arguments."""
if not dry_run:
click.echo(f'Running\ndocker compose {" ".join(compose_argv)}', err=True)
if exit_afterwards:
run_compose(compose_argv)
else:
with exit_cm():
run_compose(compose_argv)
else:
click.echo("Would have run:\n")
click.echo(click.style(" ".join(sys.argv), fg="blue"))
@contextmanager
def exit_cm():
# Context manager to monkey patch sys.exit calls
import sys
def myexit(result_code=0):
if result_code != 0:
raise RuntimeError
orig = sys.exit
sys.exit = myexit
try:
yield
finally:
sys.exit = orig