]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make --debug a boolean and add --debug-shell
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 24 Apr 2023 11:38:51 +0000 (13:38 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 24 Apr 2023 14:38:41 +0000 (16:38 +0200)
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.

mkosi.md
mkosi/__init__.py
mkosi/__main__.py
mkosi/config.py
mkosi/distributions/gentoo.py
mkosi/log.py
mkosi/run.py

index 0d0de76cbb14a0b722fb57687d0d8de744a41191..90037e91a042323ef07a28dac36de457fc53b6f5 100644 (file)
--- 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`
 
index f773ee978fbcb32ad7acc924178ec82ad290a023..929141bc5a7057c13dbdeb2bdcc69b4b721b7a44 100644 (file)
@@ -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.
index f80aa30f192684e36fedde349fcf00754b3cd815..91a4f159bf62728669401fe4e11bf55efb86ed9c 100644 (file)
@@ -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:
index 187ed2094a54bbc64278b4b662fe189c77ee0c92..4fcf16600b2ad5799e9c2d00529b5d2a06d88cc5 100644 (file)
@@ -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)
 
index 6786c241c8d76d89fa3d3b74a7704168bd499486..b672f063ea2a8ced4a29d1fa9f4193d8733c2938 100644 (file)
@@ -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"]
index d93c2dd21ede3072acad415c6ec6ecef4ccf3ece..7da713d4857dafc3f4bf3497bc72df2dc841c0a6 100644 (file)
@@ -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
 
 
index eca150f85a78add0b0118db547d59f3d240cb149..e9dc20262fb71ad4732f9aa09c236e96e90113bc 100644 (file)
@@ -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: