1
0
Fork 0
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:
Jörn-Michael Miehe 2020-08-13 14:26:49 +02:00
parent 7aa257f26a
commit a471ac4f51
6 changed files with 65 additions and 62 deletions

View file

@ -42,7 +42,14 @@ class Runner:
if str(cmd) == args.command: if str(cmd) == args.command:
# command found # command found
logging.debug(f"Running '{cmd}' with args: {args}") logging.debug(f"Running '{cmd}' with args: {args}")
cmd.run(LoadedConfig.get(), args)
try:
cmd.run(LoadedConfig.get(), args)
except KeyboardInterrupt:
print()
logging.warning(f"'{cmd}' aborted, inputs may have been discarded.")
return True return True
# command not found # command not found

View file

@ -41,15 +41,20 @@ class ProjectCommand(SubCommand):
class ServiceCommand(ProjectCommand): 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__( super().__init__(
name, name,
**kwargs **kwargs
) )
services = "service"
if not nargs == 1:
services = "services"
self._sub_parser.add_argument( self._sub_parser.add_argument(
'services', metavar='service', nargs=nargs, type=str, 'services', metavar='service', nargs=nargs, type=str,
help="select service(s) in a project" help=f"select {services} in a project"
) )

View file

@ -7,23 +7,22 @@ from .utils.dockercommand import DockerCommand
class CmdCommand(ProjectCommand): class CmdCommand(ProjectCommand):
"""kiwi cmd"""
def __init__(self): def __init__(self):
super().__init__( super().__init__(
'cmd', 'cmd',
description="Run raw docker-compose command in a project" description="Run raw docker-compose command in a project"
) )
# arguments for docker-compose command # command string after docker-compose
self._sub_parser.add_argument( self._sub_parser.add_argument(
'compose_cmd', metavar='cmd', type=str, 'compose_cmd', metavar='cmd', type=str,
help="runs `docker-compose <cmd>" help="runs `docker-compose <cmd>`"
) )
def run(self, config, args): def run(self, config, args):
try: import shlex
import shlex
DockerCommand('docker-compose').run(config, args, shlex.split(args.compose_cmd))
except KeyboardInterrupt: # run with split compose_cmd argument
logging.debug("Subprocess aborted.") DockerCommand('docker-compose').run(config, args, shlex.split(args.compose_cmd))
print()

View file

@ -43,29 +43,25 @@ class InitCommand(SubCommand):
def run(self, config, args): def run(self, config, args):
logging.info(f"Initializing '{KIWI_CONF_NAME}' in '{os.getcwd()}'") logging.info(f"Initializing '{KIWI_CONF_NAME}' in '{os.getcwd()}'")
# check force switch
if args.force and os.path.isfile(KIWI_CONF_NAME): if args.force and os.path.isfile(KIWI_CONF_NAME):
from ..config import DefaultConfig from ..config import DefaultConfig
logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!") logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!")
config = DefaultConfig.get() config = DefaultConfig.get()
try: # version
# version user_input(config, 'version', "Enter kiwi-config version for this instance")
user_input(config, 'version', "Enter kiwi-config version for this instance")
# runtime # runtime
user_input(config, 'runtime:storage', "Enter local directory for service data") user_input(config, 'runtime:storage', "Enter local directory for service data")
# markers # markers
user_input(config, 'markers:project', "Enter marker string for project directories") user_input(config, 'markers:project', "Enter marker string for project directories")
user_input(config, 'markers:down', "Enter marker string for disabled projects") user_input(config, 'markers:down', "Enter marker string for disabled projects")
# network # network
user_input(config, 'network:name', "Enter name for local docker network") user_input(config, 'network:name', "Enter name for local docker network")
user_input(config, 'network:cidr', "Enter CIDR block for local docker network") user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
config.save() config.save()
except KeyboardInterrupt:
print()
logging.warning(f"'{self}' aborted, input discarded.")

View file

@ -9,7 +9,7 @@ from .utils.dockercommand import DockerCommand
class LogsCommand(ServiceCommand): class LogsCommand(ServiceCommand):
def __init__(self): def __init__(self):
super().__init__( super().__init__(
'logs', 'logs', nargs='*',
description="Show logs of a project or service(s) of a project" description="Show logs of a project or service(s) of a project"
) )
@ -20,19 +20,19 @@ class LogsCommand(ServiceCommand):
) )
def run(self, config, args): def run(self, config, args):
compose_args = ['logs', '-t'] # include timestamps
compose_cmd = ['logs', '-t']
# handle following the log output
if args.follow: if args.follow:
compose_args = [*compose_args, '-f', '--tail=10'] compose_cmd = [*compose_cmd, '-f', '--tail=10']
# append if one or more services are given
if args.services: 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: if args.follow:
DockerCommand('docker-compose').run(config, args, compose_args) DockerCommand('docker-compose').run(config, args, compose_cmd)
else: else:
DockerCommand('docker-compose').run_less(config, args, compose_args) DockerCommand('docker-compose').run_less(config, args, compose_cmd)
except KeyboardInterrupt:
logging.debug("Subprocess aborted.")
print()

View file

@ -7,26 +7,26 @@ from ._subcommand import ServiceCommand
from .utils.dockercommand import DockerCommand 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: try:
# test if desired shell exists # test if desired shell exists
DockerCommand('docker-compose').run( 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 check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
) )
return True return True
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
# fallback # fallback
logging.info(f"Shell '{try_shell}' not found in container") logging.info(f"Shell '{shell}' not found in container")
return False return False
class ShellCommand(ServiceCommand): class ShellCommand(ServiceCommand):
def __init__(self): def __init__(self):
super().__init__( super().__init__(
'sh', 1, 'sh',
description="Spawn shell inside project's service" description="Spawn shell inside a project's service"
) )
# -s switch: Select shell # -s switch: Select shell
@ -36,22 +36,18 @@ class ShellCommand(ServiceCommand):
) )
def run(self, config, args): def run(self, config, args):
try: compose_cmd = ['exec', args.services[0]]
exec_service = ['exec', args.services[0]] shell = args.shell
exec_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 # fallback
exec_shell = '/bin/bash' shell = '/bin/bash'
if not _service_has_shell(config, args, exec_service, exec_shell): if not _service_has_shell(config, args, compose_cmd, shell):
exec_shell = '/bin/sh' # safe fallback
shell = '/bin/sh'
# spawn shell # spawn shell
DockerCommand('docker-compose').run( DockerCommand('docker-compose').run(
config, args, [*exec_service, exec_shell] config, args, [*compose_cmd, shell]
) )
except KeyboardInterrupt:
logging.debug("Subprocess aborted.")
print()