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: runtime:
storage: /tmp/kiwi storage: /tmp/kiwi
shells:
- /bin/bash
env: null env: null
markers: markers:

View file

@ -18,10 +18,38 @@ def _service_has_shell(config, args, compose_cmd, shell):
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
# fallback # fallback
logging.info(f"Shell '{shell}' not found in container")
return False 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): class ShCommand(ServiceCommand):
def __init__(self): def __init__(self):
super().__init__( super().__init__(
@ -31,23 +59,16 @@ class ShCommand(ServiceCommand):
# -s switch: Select shell # -s switch: Select shell
self._sub_parser.add_argument( self._sub_parser.add_argument(
'-s', '--shell', type=str, default="/bin/bash", '-s', '--shell', type=str,
help="shell to spawn" help="shell to spawn"
) )
def run(self, config, args): def run(self, config, args):
compose_cmd = ['exec', args.services[0]] 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): if shell:
# fallback # spawn shell
shell = '/bin/bash' DockerCommand('docker-compose').run(
config, args, [*compose_cmd, shell]
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]
)

View file

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