Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dependency): Adding dependency management to commands #56

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions devservices/commands/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def logs(args: Namespace) -> None:
except Exception as e:
print(e)
exit(1)

modes = service.config.modes
# TODO: allow custom modes to be used
mode_to_use = "default"
mode_dependencies = " ".join(modes[mode_to_use])

try:
logs = run_docker_compose_command(service, f"logs {mode_dependencies}")
except DockerComposeError as dce:
Expand Down
6 changes: 5 additions & 1 deletion devservices/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ def start(args: Namespace) -> None:
except Exception as e:
print(e)
exit(1)

modes = service.config.modes
# TODO: allow custom modes to be used
mode_to_start = "default"
mode_dependencies = " ".join(modes[mode_to_start])

with Status(f"Starting {service.name}", f"{service.name} started") as status:
try:
run_docker_compose_command(service, f"up -d {mode_dependencies}")
run_docker_compose_command(
service, f"up -d {mode_dependencies}", force_update_dependencies=True
)
except DockerComposeError as dce:
status.print(f"Failed to start {service.name}: {dce.stderr}")
exit(1)
2 changes: 2 additions & 0 deletions devservices/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ def status(args: Namespace) -> None:
except Exception as e:
print(e)
exit(1)

modes = service.config.modes
# TODO: allow custom modes to be used
mode_to_view = "default"
mode_dependencies = " ".join(modes[mode_to_view])

try:
status_json = run_docker_compose_command(
service, f"ps {mode_dependencies} --format json"
Expand Down
2 changes: 2 additions & 0 deletions devservices/commands/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def stop(args: Namespace) -> None:
except Exception as e:
print(e)
exit(1)

modes = service.config.modes
# TODO: allow custom modes to be used
mode_to_stop = "default"
mode_dependencies = " ".join(modes[mode_to_stop])

with Status(f"Stopping {service.name}", f"{service.name} stopped") as status:
try:
run_docker_compose_command(service, f"down {mode_dependencies}")
Expand Down
21 changes: 17 additions & 4 deletions devservices/configs/service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
from dataclasses import dataclass
from dataclasses import fields

import yaml

Expand Down Expand Up @@ -80,11 +81,23 @@ def load_service_config_from_file(repo_path: str) -> ServiceConfig:
)
service_config_data = config.get("x-sentry-service-config")

valid_dependency_keys = {field.name for field in fields(Dependency)}

dependencies = {}

try:
dependencies = {
key: Dependency(**value)
for key, value in service_config_data.get("dependencies", {}).items()
}
for key, value in service_config_data.get("dependencies", {}).items():
unexpected_keys = set(value.keys()) - valid_dependency_keys
if unexpected_keys:
raise ConfigParseError(
f"Unexpected key(s) in dependency '{key}': {unexpected_keys}"
)
dependencies[key] = Dependency(
description=value.get("description"),
remote=RemoteConfig(**value.get("remote"))
if "remote" in value
else None,
)
except TypeError as type_error:
raise ConfigParseError(
f"Error parsing service dependencies: {type_error}"
Expand Down
1 change: 1 addition & 0 deletions devservices/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

DEVSERVICES_LOCAL_DIR = os.path.expanduser("~/.local/share/sentry-devservices")
DEVSERVICES_LOCAL_DEPENDENCIES_DIR = os.path.join(DEVSERVICES_LOCAL_DIR, "dependencies")
DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY = "DEVSERVICES_LOCAL_DEPENDENCY_DIR"
38 changes: 33 additions & 5 deletions devservices/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,37 @@

from devservices.configs.service_config import Dependency
from devservices.configs.service_config import RemoteConfig
from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DEVSERVICES_LOCAL_DEPENDENCIES_DIR
from devservices.exceptions import DependencyError


def verify_local_dependencies(dependencies: list[Dependency]) -> bool:
remote_configs = _get_remote_configs(dependencies)

# Short circuit to avoid doing unnecessary work
if len(remote_configs) == 0:
return True

if not os.path.exists(DEVSERVICES_LOCAL_DEPENDENCIES_DIR):
return False

return all(
IanWoodard marked this conversation as resolved.
Show resolved Hide resolved
os.path.exists(
os.path.join(
DEVSERVICES_LOCAL_DEPENDENCIES_DIR,
remote_config.repo_name,
DEVSERVICES_DIR_NAME,
CONFIG_FILE_NAME,
)
)
for remote_config in remote_configs
)


def install_dependencies(dependencies: list[Dependency]) -> None:
remote_configs = [
dependency.remote
for dependency in dependencies
if _has_remote_config(dependency.remote)
]
remote_configs = _get_remote_configs(dependencies)

# Short circuit to avoid doing unnecessary work
if len(remote_configs) == 0:
Expand Down Expand Up @@ -141,6 +161,14 @@ def _is_valid_repo(path: str) -> bool:
return False


def _get_remote_configs(dependencies: list[Dependency]) -> list[RemoteConfig]:
return [
dependency.remote
for dependency in dependencies
if _has_remote_config(dependency.remote)
]


def _has_remote_config(remote_config: RemoteConfig | None) -> TypeGuard[RemoteConfig]:
return remote_config is not None

Expand Down
28 changes: 27 additions & 1 deletion devservices/utils/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,38 @@

import os
import subprocess
import tempfile

from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DEVSERVICES_LOCAL_DEPENDENCIES_DIR
from devservices.constants import DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY
from devservices.exceptions import DockerComposeError
from devservices.utils.dependencies import install_dependencies
from devservices.utils.dependencies import verify_local_dependencies
from devservices.utils.services import Service


def run_docker_compose_command(
service: Service, command: str
service: Service, command: str, force_update_dependencies: bool = False
) -> subprocess.CompletedProcess[str]:
dependencies = list(service.config.dependencies.values())
if force_update_dependencies:
install_dependencies(dependencies)
else:
are_dependencies_valid = verify_local_dependencies(dependencies)
if not are_dependencies_valid:
# TODO: Figure out how to handle this case as installing dependencies may not be the right thing to do
# since the dependencies may have changed since the service was started.
install_dependencies(dependencies)

Check warning on line 28 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L28

Added line #L28 was not covered by tests
relative_local_dependency_directory = os.path.relpath(
DEVSERVICES_LOCAL_DEPENDENCIES_DIR, service.repo_path
)
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_env_file:
IanWoodard marked this conversation as resolved.
Show resolved Hide resolved
temp_env_file.write(
f"{DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY}={relative_local_dependency_directory}\n"
)
temp_env_file_path = temp_env_file.name
service_config_file_path = os.path.join(
service.repo_path, DEVSERVICES_DIR_NAME, CONFIG_FILE_NAME
)
Expand All @@ -22,6 +44,8 @@
service.name,
"-f",
service_config_file_path,
"--env-file",
temp_env_file_path,
] + command.split()
try:
return subprocess.run(cmd, check=True, capture_output=True, text=True)
Expand All @@ -32,3 +56,5 @@
stdout=e.stdout,
stderr=e.stderr,
)
finally:
os.remove(temp_env_file_path)
81 changes: 55 additions & 26 deletions tests/commands/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import subprocess
import tempfile
from argparse import Namespace
from pathlib import Path
from unittest import mock
Expand Down Expand Up @@ -37,25 +38,40 @@
os.chdir(tmp_path)

args = Namespace(service_name=None)
start(args)

mock_run.assert_called_once_with(
[
"docker",
"compose",
"-p",
"example-service",
"-f",
f"{tmp_path}/{DEVSERVICES_DIR_NAME}/{CONFIG_FILE_NAME}",
"up",
"-d",
"redis",
"clickhouse",
],
check=True,
capture_output=True,
text=True,
)

with tempfile.NamedTemporaryFile(delete=False) as temp_env_file:
temp_env_file_path = temp_env_file.name

try:
with mock.patch("tempfile.NamedTemporaryFile") as mock_tempfile:
mock_tempfile.return_value.__enter__.return_value.name = temp_env_file_path

start(args)

mock_run.assert_called_once_with(
[
"docker",
"compose",
"-p",
"example-service",
"-f",
f"{tmp_path}/{DEVSERVICES_DIR_NAME}/{CONFIG_FILE_NAME}",
"--env-file",
temp_env_file_path,
"up",
"-d",
"redis",
"clickhouse",
],
check=True,
capture_output=True,
text=True,
)
finally:
# Ensure the temporary file is removed (even if the test fails)
if os.path.exists(temp_env_file_path):
os.remove(temp_env_file_path)
assert False, f"Failed to remove temporary file {temp_env_file_path}"

Check warning on line 74 in tests/commands/test_start.py

View check run for this annotation

Codecov / codecov/patch

tests/commands/test_start.py#L73-L74

Added lines #L73 - L74 were not covered by tests


@mock.patch("devservices.utils.docker_compose.subprocess.run")
Expand Down Expand Up @@ -88,12 +104,25 @@

args = Namespace(service_name=None)

with pytest.raises(SystemExit):
start(args)
with tempfile.NamedTemporaryFile(delete=False) as temp_env_file:
temp_env_file_path = temp_env_file.name

# Capture the printed output
captured = capsys.readouterr()
try:
with mock.patch("tempfile.NamedTemporaryFile") as mock_tempfile:
mock_tempfile.return_value.__enter__.return_value.name = temp_env_file_path

assert (
"Failed to start example-service: Docker Compose error" in captured.out.strip()
)
with pytest.raises(SystemExit):
start(args)

# Capture the printed output
captured = capsys.readouterr()

assert (
"Failed to start example-service: Docker Compose error"
in captured.out.strip()
)
finally:
# Ensure the temporary file is removed (even if the test fails)
if os.path.exists(temp_env_file_path):
os.remove(temp_env_file_path)
assert False, f"Failed to remove temporary file {temp_env_file_path}"

Check warning on line 128 in tests/commands/test_start.py

View check run for this annotation

Codecov / codecov/patch

tests/commands/test_start.py#L127-L128

Added lines #L127 - L128 were not covered by tests
Loading