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(debug): Add debug mode for start and stop #131

Merged
merged 5 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions devservices/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
parser.add_argument(
"service_name", help="Name of the service to start", nargs="?", default=None
)
parser.add_argument(
"--debug",

Check warning on line 23 in devservices/commands/start.py

View check run for this annotation

Codecov / codecov/patch

devservices/commands/start.py#L22-L23

Added lines #L22 - L23 were not covered by tests
help="Enable debug mode",
action="store_true",
default=False,
)
parser.set_defaults(func=start)


Expand Down
7 changes: 7 additions & 0 deletions devservices/commands/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
parser.add_argument(
"service_name", help="Name of the service to stop", nargs="?", default=None
)
parser.add_argument(
"--debug",
help="Enable debug mode",
action="store_true",
default=False,
)
parser.set_defaults(func=stop)


Expand All @@ -37,6 +43,7 @@ def stop(args: Namespace) -> None:
# TODO: allow custom modes to be used
mode_to_stop = "default"
mode_dependencies = modes[mode_to_stop]

state = State()
started_services = state.get_started_services()
if service.name not in started_services:
Expand Down
1 change: 1 addition & 0 deletions devservices/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
DEVSERVICES_DOWNLOAD_URL = "https://github.com/getsentry/devservices/releases/download"
BINARY_PERMISSIONS = 0o755
MAX_LOG_LINES = "100"
LOGGER_NAME = "devservices"
8 changes: 8 additions & 0 deletions devservices/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import atexit
import logging
import os
from importlib import metadata

Expand All @@ -17,6 +18,7 @@
from devservices.commands import stop
from devservices.commands import update
from devservices.commands.check_for_update import check_for_update
from devservices.constants import LOGGER_NAME
from devservices.exceptions import DockerComposeInstallationError
from devservices.exceptions import DockerDaemonNotRunningError
from devservices.utils.console import Console
Expand All @@ -27,6 +29,7 @@
)

disable_sentry = os.environ.get("DISABLE_SENTRY", default=False)
logging.basicConfig(level=logging.INFO)

if not disable_sentry:
sentry_sdk.init(
Expand Down Expand Up @@ -77,6 +80,11 @@ def main() -> None:

args = parser.parse_args()

# If the command has a debug flag, set the logger to debug
if args.debug:
logger = logging.getLogger(LOGGER_NAME)
logger.setLevel(logging.DEBUG)

if args.command:
# Call the appropriate function based on the command
with sentry_sdk.start_transaction(op="command", name=args.command):
Expand Down
4 changes: 4 additions & 0 deletions devservices/utils/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import shutil
import subprocess
Expand All @@ -18,6 +19,7 @@
from devservices.constants import DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import LOGGER_NAME
from devservices.exceptions import ConfigNotFoundError
from devservices.exceptions import ConfigParseError
from devservices.exceptions import ConfigValidationError
Expand Down Expand Up @@ -453,4 +455,6 @@ def _has_remote_config(remote_config: RemoteConfig | None) -> TypeGuard[RemoteCo
def _run_command(
cmd: list[str], cwd: str, stdout: int | TextIO | None = subprocess.DEVNULL
) -> None:
logger = logging.getLogger(LOGGER_NAME)
logger.debug(f"Running command: {' '.join(cmd)}")
subprocess.run(cmd, cwd=cwd, check=True, stdout=stdout, stderr=subprocess.DEVNULL)
4 changes: 4 additions & 0 deletions devservices/utils/docker_compose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import platform
import re
Expand All @@ -17,6 +18,7 @@
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DOCKER_COMPOSE_DOWNLOAD_URL
from devservices.constants import DOCKER_USER_PLUGIN_DIR
from devservices.constants import LOGGER_NAME
from devservices.constants import MINIMUM_DOCKER_COMPOSE_VERSION
from devservices.exceptions import BinaryInstallError
from devservices.exceptions import DockerComposeError
Expand Down Expand Up @@ -252,6 +254,8 @@ def run_docker_compose_command(
cmd_outputs = []
for cmd in docker_compose_commands:
try:
logger = logging.getLogger(LOGGER_NAME)
logger.debug(f"Running command: {' '.join(cmd)}")
cmd_outputs.append(
subprocess.run(
cmd, check=True, capture_output=True, text=True, env=current_env
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_start_simple(
create_config_file(service_path, config)
os.chdir(service_path)

args = Namespace(service_name=None)
args = Namespace(service_name=None, debug=False)

start(args)

Expand Down Expand Up @@ -123,7 +123,7 @@ def test_start_dependency_error(
create_config_file(tmp_path, config)
os.chdir(tmp_path)

args = Namespace(service_name=None)
args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
start(args)
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_start_error(
create_config_file(tmp_path, config)
os.chdir(tmp_path)

args = Namespace(service_name=None)
args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
start(args)
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_stop_simple(
create_config_file(service_path, config)
os.chdir(service_path)

args = Namespace(service_name=None)
args = Namespace(service_name=None, debug=False)

with mock.patch(
"devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_stop_error(
create_config_file(tmp_path, config)
os.chdir(tmp_path)

args = Namespace(service_name=None)
args = Namespace(service_name=None, debug=False)

with mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")):
state = State()
Expand Down