better logging for "sh", 'runtime:shells' config key

This commit is contained in:
Jörn-Michael Miehe 2020-08-15 04:06:25 +02:00
parent 156318295f
commit 2dd524fbad
3 changed files with 40 additions and 15 deletions

View file

@ -6,6 +6,8 @@ version: '0.1'
runtime:
storage: /tmp/kiwi
shells:
- /bin/bash
env: null
markers:

View file

@ -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,22 +59,15 @@ 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
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'
shell = _find_shell(config, args, compose_cmd)
if shell:
# spawn shell
DockerCommand('docker-compose').run(
config, args, [*compose_cmd, shell]

View file

@ -1,6 +1,8 @@
version:
runtime:
storage: /var/kiwi
shells:
- /bin/bash
env: null
markers:
project: .project