2020-08-13 11:00:32 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# local
|
|
|
|
from ._subcommand import ServiceCommand
|
|
|
|
from .utils.dockercommand import DockerCommand
|
|
|
|
|
|
|
|
|
2020-08-17 08:08:29 +00:00
|
|
|
def _service_has_executable(config, args, compose_cmd, exe_name):
|
|
|
|
"""
|
|
|
|
Test if container (as of compose_cmd array) has an executable exe_name in its PATH.
|
|
|
|
Requires /bin/sh and which.
|
|
|
|
"""
|
|
|
|
|
2020-08-13 11:00:32 +00:00
|
|
|
try:
|
|
|
|
# test if desired shell exists
|
|
|
|
DockerCommand('docker-compose').run(
|
2020-08-17 08:08:29 +00:00
|
|
|
config, args, [*compose_cmd, '/bin/sh', '-c', f"which {exe_name}"],
|
2020-08-13 11:00:32 +00:00
|
|
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
2020-08-17 08:08:29 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
2020-08-13 11:00:32 +00:00
|
|
|
# fallback
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-08-15 02:06:25 +00:00
|
|
|
def _find_shell(config, args, compose_cmd):
|
2020-08-17 08:08:29 +00:00
|
|
|
"""find first working shell (provided by config and args) in container (as of compose_cmd array)"""
|
|
|
|
|
|
|
|
# builtin shells: as a last resort, fallback to '/bin/sh' and 'sh'
|
2020-08-15 02:06:25 +00:00
|
|
|
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):
|
2020-08-17 08:08:29 +00:00
|
|
|
if _service_has_executable(config, args, compose_cmd, shell):
|
|
|
|
# found working shell
|
2020-08-15 02:06:25 +00:00
|
|
|
logging.debug(f"Using shell '{shell}'")
|
|
|
|
return shell
|
2020-08-17 08:08:29 +00:00
|
|
|
|
2020-08-15 02:06:25 +00:00
|
|
|
elif i + 1 < len(shells):
|
2020-08-17 08:08:29 +00:00
|
|
|
# try next in list
|
2020-08-15 02:06:25 +00:00
|
|
|
logging.info(f"Shell '{shell}' not found in container, trying '{shells[i+1]}'")
|
2020-08-17 08:08:29 +00:00
|
|
|
|
|
|
|
elif args.shell:
|
|
|
|
# not found, user suggestion provided
|
|
|
|
logging.warning(f"Could not find any working shell in this container. "
|
|
|
|
f"Launching provided '{args.shell}' nevertheless. "
|
|
|
|
f"Don't get mad if this fails!")
|
|
|
|
return args.shell
|
|
|
|
|
2020-08-15 02:06:25 +00:00
|
|
|
else:
|
|
|
|
# not found, search exhausted
|
2020-08-17 08:08:29 +00:00
|
|
|
logging.error(f"Could not find any working shell among '{shells}' in this container. "
|
|
|
|
f"Please suggest a shell using the '-s SHELL' command line option!")
|
2020-08-15 02:06:25 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-08-15 00:06:55 +00:00
|
|
|
class ShCommand(ServiceCommand):
|
2020-08-17 08:08:29 +00:00
|
|
|
"""kiwi sh"""
|
|
|
|
|
2020-08-13 11:00:32 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
2020-08-13 12:26:49 +00:00
|
|
|
'sh',
|
|
|
|
description="Spawn shell inside a project's service"
|
2020-08-13 11:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# -s switch: Select shell
|
|
|
|
self._sub_parser.add_argument(
|
2020-08-15 02:06:25 +00:00
|
|
|
'-s', '--shell', type=str,
|
2020-08-13 11:00:32 +00:00
|
|
|
help="shell to spawn"
|
|
|
|
)
|
|
|
|
|
|
|
|
def run(self, config, args):
|
2020-08-13 12:26:49 +00:00
|
|
|
compose_cmd = ['exec', args.services[0]]
|
2020-08-15 02:06:25 +00:00
|
|
|
shell = _find_shell(config, args, compose_cmd)
|
2020-08-13 11:00:32 +00:00
|
|
|
|
2020-08-17 08:08:29 +00:00
|
|
|
if shell is not None:
|
2020-08-15 02:06:25 +00:00
|
|
|
# spawn shell
|
|
|
|
DockerCommand('docker-compose').run(
|
|
|
|
config, args, [*compose_cmd, shell]
|
|
|
|
)
|