mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 04:43:00 +00:00
doc + error handling
This commit is contained in:
parent
7aa257f26a
commit
a471ac4f51
6 changed files with 65 additions and 62 deletions
|
@ -42,7 +42,14 @@ class Runner:
|
|||
if str(cmd) == args.command:
|
||||
# command found
|
||||
logging.debug(f"Running '{cmd}' with args: {args}")
|
||||
|
||||
try:
|
||||
cmd.run(LoadedConfig.get(), args)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
logging.warning(f"'{cmd}' aborted, inputs may have been discarded.")
|
||||
|
||||
return True
|
||||
|
||||
# command not found
|
||||
|
|
|
@ -41,15 +41,20 @@ class ProjectCommand(SubCommand):
|
|||
|
||||
|
||||
class ServiceCommand(ProjectCommand):
|
||||
"""this command concerns services in a project"""
|
||||
"""this command concerns service(s) in a project"""
|
||||
|
||||
def __init__(self, name, nargs='*', **kwargs):
|
||||
def __init__(self, name, nargs=1, **kwargs):
|
||||
super().__init__(
|
||||
name,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
services = "service"
|
||||
|
||||
if not nargs == 1:
|
||||
services = "services"
|
||||
|
||||
self._sub_parser.add_argument(
|
||||
'services', metavar='service', nargs=nargs, type=str,
|
||||
help="select service(s) in a project"
|
||||
help=f"select {services} in a project"
|
||||
)
|
||||
|
|
|
@ -7,23 +7,22 @@ from .utils.dockercommand import DockerCommand
|
|||
|
||||
|
||||
class CmdCommand(ProjectCommand):
|
||||
"""kiwi cmd"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
'cmd',
|
||||
description="Run raw docker-compose command in a project"
|
||||
)
|
||||
|
||||
# arguments for docker-compose command
|
||||
# command string after docker-compose
|
||||
self._sub_parser.add_argument(
|
||||
'compose_cmd', metavar='cmd', type=str,
|
||||
help="runs `docker-compose <cmd>"
|
||||
help="runs `docker-compose <cmd>`"
|
||||
)
|
||||
|
||||
def run(self, config, args):
|
||||
try:
|
||||
import shlex
|
||||
DockerCommand('docker-compose').run(config, args, shlex.split(args.compose_cmd))
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.debug("Subprocess aborted.")
|
||||
print()
|
||||
# run with split compose_cmd argument
|
||||
DockerCommand('docker-compose').run(config, args, shlex.split(args.compose_cmd))
|
||||
|
|
|
@ -43,13 +43,13 @@ class InitCommand(SubCommand):
|
|||
def run(self, config, args):
|
||||
logging.info(f"Initializing '{KIWI_CONF_NAME}' in '{os.getcwd()}'")
|
||||
|
||||
# check force switch
|
||||
if args.force and os.path.isfile(KIWI_CONF_NAME):
|
||||
from ..config import DefaultConfig
|
||||
|
||||
logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!")
|
||||
config = DefaultConfig.get()
|
||||
|
||||
try:
|
||||
# version
|
||||
user_input(config, 'version', "Enter kiwi-config version for this instance")
|
||||
|
||||
|
@ -65,7 +65,3 @@ class InitCommand(SubCommand):
|
|||
user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
|
||||
|
||||
config.save()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
logging.warning(f"'{self}' aborted, input discarded.")
|
||||
|
|
|
@ -9,7 +9,7 @@ from .utils.dockercommand import DockerCommand
|
|||
class LogsCommand(ServiceCommand):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
'logs',
|
||||
'logs', nargs='*',
|
||||
description="Show logs of a project or service(s) of a project"
|
||||
)
|
||||
|
||||
|
@ -20,19 +20,19 @@ class LogsCommand(ServiceCommand):
|
|||
)
|
||||
|
||||
def run(self, config, args):
|
||||
compose_args = ['logs', '-t']
|
||||
if args.follow:
|
||||
compose_args = [*compose_args, '-f', '--tail=10']
|
||||
# include timestamps
|
||||
compose_cmd = ['logs', '-t']
|
||||
|
||||
# handle following the log output
|
||||
if args.follow:
|
||||
compose_cmd = [*compose_cmd, '-f', '--tail=10']
|
||||
|
||||
# append if one or more services are given
|
||||
if args.services:
|
||||
compose_args = [*compose_args, *args.services]
|
||||
compose_cmd = [*compose_cmd, *args.services]
|
||||
|
||||
try:
|
||||
# use 'less' viewer if output will be static
|
||||
if args.follow:
|
||||
DockerCommand('docker-compose').run(config, args, compose_args)
|
||||
DockerCommand('docker-compose').run(config, args, compose_cmd)
|
||||
else:
|
||||
DockerCommand('docker-compose').run_less(config, args, compose_args)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.debug("Subprocess aborted.")
|
||||
print()
|
||||
DockerCommand('docker-compose').run_less(config, args, compose_cmd)
|
||||
|
|
|
@ -7,26 +7,26 @@ from ._subcommand import ServiceCommand
|
|||
from .utils.dockercommand import DockerCommand
|
||||
|
||||
|
||||
def _service_has_shell(config, args, exec_service, try_shell):
|
||||
def _service_has_shell(config, args, compose_cmd, shell):
|
||||
try:
|
||||
# test if desired shell exists
|
||||
DockerCommand('docker-compose').run(
|
||||
config, args, [*exec_service, '/bin/sh', '-c', f"which {try_shell}"],
|
||||
config, args, [*compose_cmd, '/bin/sh', '-c', f"which {shell}"],
|
||||
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||
)
|
||||
return True
|
||||
|
||||
except subprocess.CalledProcessError:
|
||||
# fallback
|
||||
logging.info(f"Shell '{try_shell}' not found in container")
|
||||
logging.info(f"Shell '{shell}' not found in container")
|
||||
return False
|
||||
|
||||
|
||||
class ShellCommand(ServiceCommand):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
'sh', 1,
|
||||
description="Spawn shell inside project's service"
|
||||
'sh',
|
||||
description="Spawn shell inside a project's service"
|
||||
)
|
||||
|
||||
# -s switch: Select shell
|
||||
|
@ -36,22 +36,18 @@ class ShellCommand(ServiceCommand):
|
|||
)
|
||||
|
||||
def run(self, config, args):
|
||||
try:
|
||||
exec_service = ['exec', args.services[0]]
|
||||
exec_shell = args.shell
|
||||
compose_cmd = ['exec', args.services[0]]
|
||||
shell = args.shell
|
||||
|
||||
if not _service_has_shell(config, args, exec_service, exec_shell):
|
||||
if not _service_has_shell(config, args, compose_cmd, shell):
|
||||
# fallback
|
||||
exec_shell = '/bin/bash'
|
||||
shell = '/bin/bash'
|
||||
|
||||
if not _service_has_shell(config, args, exec_service, exec_shell):
|
||||
exec_shell = '/bin/sh'
|
||||
if not _service_has_shell(config, args, compose_cmd, shell):
|
||||
# safe fallback
|
||||
shell = '/bin/sh'
|
||||
|
||||
# spawn shell
|
||||
DockerCommand('docker-compose').run(
|
||||
config, args, [*exec_service, exec_shell]
|
||||
config, args, [*compose_cmd, shell]
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.debug("Subprocess aborted.")
|
||||
print()
|
||||
|
|
Loading…
Reference in a new issue