From: Daan De Meyer Date: Mon, 24 Apr 2023 11:38:51 +0000 (+0200) Subject: Make --debug a boolean and add --debug-shell X-Git-Tag: v15~205 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a13e4b0e7056cca96b922f441a48a2885a326019;p=thirdparty%2Fmkosi.git Make --debug a boolean and add --debug-shell Let's drop the support for multiple string arguments since we only have "run" anyway and make --boolean a simple yes/no option. To get an interactive shell on failure, a new option --debug-shell is added. --- diff --git a/mkosi.md b/mkosi.md index 0d0de76cb..90037e91a 100644 --- a/mkosi.md +++ b/mkosi.md @@ -933,9 +933,12 @@ Those settings cannot be configured in the configuration files. `--debug=` -: Enable additional debugging output. Takes a comma-separated list of - arguments specifying the area of interest. Pass any invalid value - (e.g. empty) to list currently accepted values. +: Enable additional debugging output. + +`--debug-shell=` + +: When executing a command in the image fails, mkosi will start an interactive + shell in the image allowing further debugging. `--version` diff --git a/mkosi/__init__.py b/mkosi/__init__.py index f773ee978..929141bc5 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -1328,7 +1328,7 @@ def run_kernel_install(state: MkosiState) -> None: for kver, kimg in gen_kernel_images(state): cmd: list[PathString] = ["kernel-install", "add", kver, Path("/") / kimg] - if ARG_DEBUG: + if ARG_DEBUG.get(): cmd.insert(1, "--verbose") # Make dracut think --no-host-only was passed via the CLI. diff --git a/mkosi/__main__.py b/mkosi/__main__.py index f80aa30f1..91a4f159b 100644 --- a/mkosi/__main__.py +++ b/mkosi/__main__.py @@ -21,24 +21,24 @@ def propagate_failed_return() -> Iterator[None]: try: yield except SystemExit as e: - if ARG_DEBUG: + if ARG_DEBUG.get(): raise e sys.exit(e.code) except KeyboardInterrupt as e: - if ARG_DEBUG: + if ARG_DEBUG.get(): raise e logging.error("Interrupted") sys.exit(1) except subprocess.CalledProcessError as e: - if ARG_DEBUG: + if ARG_DEBUG.get(): raise e # We always log when subprocess.CalledProcessError is raised, so we don't log again here. sys.exit(e.returncode) except Exception as e: - if ARG_DEBUG: + if ARG_DEBUG.get(): raise e elif not isinstance(e, RuntimeError): # RuntimeError is used to wrap generic errors, and the message that was printed should be enough. @@ -52,7 +52,7 @@ def main() -> None: log_setup() args = MkosiConfigParser().parse() - if ARG_DEBUG: + if ARG_DEBUG.get(): logging.getLogger().setLevel(logging.DEBUG) if args.directory: diff --git a/mkosi/config.py b/mkosi/config.py index 187ed2094..4fcf16600 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -16,7 +16,7 @@ from collections.abc import Sequence from pathlib import Path from typing import Any, Callable, Optional, Type, Union, cast -from mkosi.log import ARG_DEBUG, Style, die +from mkosi.log import ARG_DEBUG, ARG_DEBUG_SHELL, Style, die from mkosi.pager import page from mkosi.run import run from mkosi.util import ( @@ -927,8 +927,14 @@ class MkosiConfigParser: parser.add_argument( "--debug", help="Turn on debugging output", - action="append", - default=[], + action="store_true", + default=False, + ) + parser.add_argument( + "--debug-shell", + help="Spawn an interactive shell in the image if a chroot command fails", + action="store_true", + default=False, ) parser.add_argument( "--no-pager", @@ -1554,7 +1560,8 @@ class MkosiConfig: ssh: bool credentials: dict[str, str] directory: Optional[Path] - debug: list[str] + debug: bool + debug_shell: bool auto_bump: bool workspace_dir: Optional[Path] initrds: list[Path] @@ -1754,7 +1761,8 @@ def load_kernel_command_line_extra(args: argparse.Namespace) -> list[str]: def load_args(args: argparse.Namespace) -> MkosiConfig: - ARG_DEBUG.update(args.debug) + ARG_DEBUG.set(args.debug) + ARG_DEBUG_SHELL.set(args.debug_shell) find_image_version(args) diff --git a/mkosi/distributions/gentoo.py b/mkosi/distributions/gentoo.py index 6786c241c..b672f063e 100644 --- a/mkosi/distributions/gentoo.py +++ b/mkosi/distributions/gentoo.py @@ -42,7 +42,7 @@ def invoke_emerge( f"--load-average={jobs+1}", "--nospinner", ] - if "build-script" in ARG_DEBUG: + if ARG_DEBUG.get(): emerge_default_opts += ["--verbose", "--quiet=n", "--quiet-fail=n"] else: emerge_default_opts += ["--quiet-build", "--quiet"] diff --git a/mkosi/log.py b/mkosi/log.py index d93c2dd21..7da713d48 100644 --- a/mkosi/log.py +++ b/mkosi/log.py @@ -1,11 +1,13 @@ import contextlib +import contextvars import logging import os import sys from typing import Any, Iterator, NoReturn, Optional # This global should be initialized after parsing arguments -ARG_DEBUG: set[str] = set() +ARG_DEBUG = contextvars.ContextVar("debug", default=False) +ARG_DEBUG_SHELL = contextvars.ContextVar("debug-shell", default=False) LEVEL = 0 diff --git a/mkosi/run.py b/mkosi/run.py index eca150f85..e9dc20262 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -15,7 +15,7 @@ from pathlib import Path from types import TracebackType from typing import Any, Callable, Mapping, Optional, Sequence, Type, TypeVar -from mkosi.log import ARG_DEBUG, die +from mkosi.log import ARG_DEBUG, ARG_DEBUG_SHELL, die from mkosi.types import _FILE, CompletedProcess, PathString, Popen from mkosi.util import current_user @@ -203,7 +203,7 @@ def run( log: bool = True, **kwargs: Any, ) -> CompletedProcess: - if "run" in ARG_DEBUG: + if ARG_DEBUG.get(): logging.info(f"+ {shlex.join(str(s) for s in cmdline)}") cmdline = [os.fspath(x) for x in cmdline] @@ -220,7 +220,7 @@ def run( LANG="C.UTF-8", ) | env - if "run" in ARG_DEBUG: + if ARG_DEBUG.get(): env["SYSTEMD_LOG_LEVEL"] = "debug" if "input" in kwargs: @@ -251,7 +251,7 @@ def spawn( stderr: _FILE = None, **kwargs: Any, ) -> Popen: - if "run" in ARG_DEBUG: + if ARG_DEBUG.get(): logging.info(f"+ {shlex.join(str(s) for s in cmdline)}") if not stdout and not stderr: @@ -309,7 +309,7 @@ def bwrap( try: return run([*cmdline, *cmd], text=True, stdout=stdout, env=env, log=False) except subprocess.CalledProcessError as e: - if "run" in ARG_DEBUG: + if ARG_DEBUG_SHELL.get(): run([*cmdline, "sh"], stdin=sys.stdin, check=False, env=env, log=False) die(f'"{shlex.join(str(s) for s in cmd)}" returned non-zero exit code {e.returncode}.') @@ -364,7 +364,7 @@ def run_workspace_command( try: return run([*cmdline, *cmd], text=True, stdout=stdout, env=env, log=False) except subprocess.CalledProcessError as e: - if "run" in ARG_DEBUG: + if ARG_DEBUG_SHELL.get(): run([*cmdline, "sh"], stdin=sys.stdin, check=False, env=env, log=False) die(f'"{shlex.join(str(s) for s in cmd)}" returned non-zero exit code {e.returncode}.') finally: