1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 21:03:00 +00:00
kiwi-scp/src/kiwi/subcommands/sh.py

94 lines
2.8 KiB
Python
Raw Normal View History

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
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'
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
logging.debug(f"Using shell '{shell}'")
return shell
2020-08-17 08:08:29 +00:00
elif i + 1 < len(shells):
2020-08-17 08:08:29 +00:00
# try next in list
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
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!")
return None
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(
'-s', '--shell', type=str,
2020-08-13 11:00:32 +00:00
help="shell to spawn"
)
2020-08-17 08:57:45 +00:00
def run(self, runner, config, args):
2020-08-13 12:26:49 +00:00
compose_cmd = ['exec', args.services[0]]
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:
# spawn shell
DockerCommand('docker-compose').run(
config, args, [*compose_cmd, shell]
)