From 2dd524fbad61d700d36fca7e8d2013895dc68e90 Mon Sep 17 00:00:00 2001 From: ldericher Date: Sat, 15 Aug 2020 04:06:25 +0200 Subject: [PATCH] better logging for "sh", 'runtime:shells' config key --- example/kiwi.yml | 2 ++ src/kiwi/subcommands/sh.py | 51 +++++++++++++++++++++++++++----------- src/kiwi_default.yml | 2 ++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/example/kiwi.yml b/example/kiwi.yml index a78bc97..323d023 100644 --- a/example/kiwi.yml +++ b/example/kiwi.yml @@ -6,6 +6,8 @@ version: '0.1' runtime: storage: /tmp/kiwi + shells: + - /bin/bash env: null markers: diff --git a/src/kiwi/subcommands/sh.py b/src/kiwi/subcommands/sh.py index 00f5d48..19ba909 100644 --- a/src/kiwi/subcommands/sh.py +++ b/src/kiwi/subcommands/sh.py @@ -18,10 +18,38 @@ def _service_has_shell(config, args, compose_cmd, shell): except subprocess.CalledProcessError: # fallback - logging.info(f"Shell '{shell}' not found in container") return False +def _find_shell(config, args, compose_cmd): + # as a last resort, fallback to "sh" + shells = ['/bin/sh', 'sh'] + + # load favorite shells from config + if config['runtime:shells']: + shells = [*config['runtime:shells'], *shells] + + # consider shell from args + if args.shell: + shells = [args.shell, *shells] + + logging.debug(f"Shells priority: {shells}") + + # actually try shells + for i, shell in enumerate(shells): + if _service_has_shell(config, args, compose_cmd, shell): + # shell found + logging.debug(f"Using shell '{shell}'") + return shell + elif i + 1 < len(shells): + # not found, try next + logging.info(f"Shell '{shell}' not found in container, trying '{shells[i+1]}'") + else: + # not found, search exhausted + logging.error(f"None of the shells {shells} found in container, please provide -s SHELL!") + return None + + class ShCommand(ServiceCommand): def __init__(self): super().__init__( @@ -31,23 +59,16 @@ class ShCommand(ServiceCommand): # -s switch: Select shell self._sub_parser.add_argument( - '-s', '--shell', type=str, default="/bin/bash", + '-s', '--shell', type=str, help="shell to spawn" ) def run(self, config, args): compose_cmd = ['exec', args.services[0]] - shell = args.shell + shell = _find_shell(config, args, compose_cmd) - if not _service_has_shell(config, args, compose_cmd, shell): - # fallback - shell = '/bin/bash' - - if not _service_has_shell(config, args, compose_cmd, shell): - # safe fallback - shell = '/bin/sh' - - # spawn shell - DockerCommand('docker-compose').run( - config, args, [*compose_cmd, shell] - ) + if shell: + # spawn shell + DockerCommand('docker-compose').run( + config, args, [*compose_cmd, shell] + ) diff --git a/src/kiwi_default.yml b/src/kiwi_default.yml index 2e5cd0c..8f477ee 100644 --- a/src/kiwi_default.yml +++ b/src/kiwi_default.yml @@ -1,6 +1,8 @@ version: runtime: storage: /var/kiwi + shells: + - /bin/bash env: null markers: project: .project