Skip to content

Commit

Permalink
Revert check for docker-compose version, prioritizing v2 (#31446)
Browse files Browse the repository at this point in the history
The docker-compose version v2 is now the "primary" version used
when you install docker, so it is more likely that it will be
available. We should check if v2 is available and use it. Also
checking the list of containers via json is only available when
v2 is used so we should only do it then.
  • Loading branch information
potiuk authored May 21, 2023
1 parent 58aab11 commit 9a0f6b8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
10 changes: 5 additions & 5 deletions dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def check_remote_ghcr_io_commands():
sys.exit(1)


DOCKER_COMPOSE_COMMAND = ["docker-compose"]
DOCKER_COMPOSE_COMMAND = ["docker", "compose"]


def check_docker_compose_version():
Expand All @@ -313,7 +313,7 @@ def check_docker_compose_version():
warning for the user.
"""
version_pattern = re.compile(r"(\d+)\.(\d+)\.(\d+)")
docker_compose_version_command = ["docker-compose", "--version"]
docker_compose_version_command = ["docker", "compose", "version"]
try:
docker_compose_version_result = run_command(
docker_compose_version_command,
Expand All @@ -322,8 +322,8 @@ def check_docker_compose_version():
text=True,
dry_run_override=False,
)
except FileNotFoundError:
docker_compose_version_command = ["docker", "compose", "version"]
except Exception:
docker_compose_version_command = ["docker-compose", "--version"]
docker_compose_version_result = run_command(
docker_compose_version_command,
no_output_dump_on_exception=True,
Expand All @@ -332,7 +332,7 @@ def check_docker_compose_version():
dry_run_override=False,
)
DOCKER_COMPOSE_COMMAND.clear()
DOCKER_COMPOSE_COMMAND.extend(["docker", "compose"])
DOCKER_COMPOSE_COMMAND.append("docker-compose")
if docker_compose_version_result.returncode == 0:
docker_compose_version = docker_compose_version_result.stdout
version_extracted = version_pattern.search(docker_compose_version)
Expand Down
8 changes: 4 additions & 4 deletions dev/breeze/tests/test_docker_command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_check_docker_compose_version_unknown(mock_get_console, mock_run_command
check_docker_compose_version()
expected_run_command_calls = [
call(
["docker-compose", "--version"],
["docker", "compose", "version"],
no_output_dump_on_exception=True,
capture_output=True,
text=True,
Expand All @@ -150,7 +150,7 @@ def test_check_docker_compose_version_low(mock_get_console, mock_run_command):
mock_run_command.return_value.stdout = "1.28.5"
check_docker_compose_version()
mock_run_command.assert_called_with(
["docker-compose", "--version"],
["docker", "compose", "version"],
no_output_dump_on_exception=True,
capture_output=True,
text=True,
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_check_docker_compose_version_ok(mock_get_console, mock_run_command):
mock_run_command.return_value.stdout = "1.29.0"
check_docker_compose_version()
mock_run_command.assert_called_with(
["docker-compose", "--version"],
["docker", "compose", "version"],
no_output_dump_on_exception=True,
capture_output=True,
text=True,
Expand All @@ -197,7 +197,7 @@ def test_check_docker_compose_version_higher(mock_get_console, mock_run_command)
mock_run_command.return_value.stdout = "1.29.2"
check_docker_compose_version()
mock_run_command.assert_called_with(
["docker-compose", "--version"],
["docker", "compose", "version"],
no_output_dump_on_exception=True,
capture_output=True,
text=True,
Expand Down
4 changes: 2 additions & 2 deletions docker_tests/command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def run_command(
return subprocess.check_output(cmd, **kwargs).decode()
else:
try:
subprocess.run(cmd, check=check, **kwargs)
return True
result = subprocess.run(cmd, check=check, **kwargs)
return result.returncode == 0
except FileNotFoundError:
if check:
raise
Expand Down
24 changes: 13 additions & 11 deletions docker_tests/test_docker_compose_quick_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ def test_trigger_dag_and_wait_for_result(tmp_path_factory, monkeypatch):
print(dot_env_file.read_text())

# check if docker-compose is available
compose_command = ["docker-compose"]
compose_command = ["docker", "compose"]
success = run_command([*compose_command, "version"], check=False)
if not success:
compose_command = ["docker", "compose"]
success = run_command([*compose_command, "version"], check=False)
compose_command = ["docker-compose"]
success = run_command([*compose_command, "--version"], check=False)
if not success:
print("ERROR: Neither `docker compose` nor `docker-compose` is available")
sys.exit(1)
Expand Down Expand Up @@ -178,14 +178,16 @@ def test_trigger_dag_and_wait_for_result(tmp_path_factory, monkeypatch):
run_command(["docker", "ps"])
run_command([*compose_command, "logs"])

ps_output = run_command([*compose_command, "ps", "--format", "json"], return_output=True)
container_names = [container["Name"] for container in json.loads(ps_output)]
for container in container_names:
print(f"Health check for {container}")
result = run_command(
["docker", "inspect", "--format", "{{json .State}}", container], return_output=True
)
pprint(json.loads(result))
if compose_command == ["docker", "compose"]:
# JSON output is only available for docker compose v2
ps_output = run_command([*compose_command, "ps", "--format", "json"], return_output=True)
container_names = [container["Name"] for container in json.loads(ps_output)]
for container in container_names:
print(f"Health check for {container}")
result = run_command(
["docker", "inspect", "--format", "{{json .State}}", container], return_output=True
)
pprint(json.loads(result))

raise
finally:
Expand Down

0 comments on commit 9a0f6b8

Please sign in to comment.