From 7641f1846aeb24ef893320b4a4f88096ab2d0263 Mon Sep 17 00:00:00 2001 From: ldericher <40151420+ldericher@users.noreply.github.com> Date: Fri, 3 Dec 2021 15:04:03 +0100 Subject: [PATCH] remove legacy --- kiwi_scp/subcommands/__init__.py | 38 ----------- kiwi_scp/subcommands/build.py | 18 ------ kiwi_scp/subcommands/cmd.py | 40 ------------ kiwi_scp/subcommands/disable.py | 16 ----- kiwi_scp/subcommands/down.py | 56 ----------------- kiwi_scp/subcommands/enable.py | 16 ----- kiwi_scp/subcommands/logs.py | 40 ------------ kiwi_scp/subcommands/pull.py | 18 ------ kiwi_scp/subcommands/push.py | 18 ------ kiwi_scp/subcommands/restart.py | 26 -------- kiwi_scp/subcommands/shell.py | 104 ------------------------------- kiwi_scp/subcommands/show.py | 99 ----------------------------- kiwi_scp/subcommands/up.py | 26 -------- 13 files changed, 515 deletions(-) delete mode 100644 kiwi_scp/subcommands/__init__.py delete mode 100644 kiwi_scp/subcommands/build.py delete mode 100644 kiwi_scp/subcommands/cmd.py delete mode 100644 kiwi_scp/subcommands/disable.py delete mode 100644 kiwi_scp/subcommands/down.py delete mode 100644 kiwi_scp/subcommands/enable.py delete mode 100644 kiwi_scp/subcommands/logs.py delete mode 100644 kiwi_scp/subcommands/pull.py delete mode 100644 kiwi_scp/subcommands/push.py delete mode 100644 kiwi_scp/subcommands/restart.py delete mode 100644 kiwi_scp/subcommands/shell.py delete mode 100644 kiwi_scp/subcommands/show.py delete mode 100644 kiwi_scp/subcommands/up.py diff --git a/kiwi_scp/subcommands/__init__.py b/kiwi_scp/subcommands/__init__.py deleted file mode 100644 index cd108a6..0000000 --- a/kiwi_scp/subcommands/__init__.py +++ /dev/null @@ -1,38 +0,0 @@ -# local -from ._hidden import ConfCopyCommand, NetUpCommand - -from .build import BuildCommand -from .cmd import CmdCommand -from .disable import DisableCommand -from .down import DownCommand -from .enable import EnableCommand -from .logs import LogsCommand -from .new import NewCommand -from .pull import PullCommand -from .push import PushCommand -from .restart import RestartCommand -from .shell import ShellCommand -from .show import ShowCommand -from .up import UpCommand -from .update import UpdateCommand - -__all__ = [ - 'ConfCopyCommand', - 'NetUpCommand', - - 'BuildCommand', - 'CmdCommand', - 'DisableCommand', - 'DownCommand', - 'EnableCommand', - 'InitCommand', - 'LogsCommand', - 'NewCommand', - 'PullCommand', - 'PushCommand', - 'RestartCommand', - 'ShellCommand', - 'ShowCommand', - 'UpCommand', - 'UpdateCommand', -] diff --git a/kiwi_scp/subcommands/build.py b/kiwi_scp/subcommands/build.py deleted file mode 100644 index 35cb4e5..0000000 --- a/kiwi_scp/subcommands/build.py +++ /dev/null @@ -1,18 +0,0 @@ -# local -from ..subcommand import ServiceCommand - - -class BuildCommand(ServiceCommand): - """kiwi build""" - - def __init__(self): - super().__init__( - 'build', num_projects='?', num_services='*', - action="Building images for", - description="Build images for the whole instance, a project or service(s) inside a project" - ) - - def _run_services(self, runner, args, project, services): - project.compose_run(['build', '--pull', *services]) - - return True diff --git a/kiwi_scp/subcommands/cmd.py b/kiwi_scp/subcommands/cmd.py deleted file mode 100644 index c890edf..0000000 --- a/kiwi_scp/subcommands/cmd.py +++ /dev/null @@ -1,40 +0,0 @@ -# system -import logging - -# local -from ..subcommand import ProjectCommand - - -class CmdCommand(ProjectCommand): - """kiwi cmd""" - - def __init__(self): - super().__init__( - 'cmd', num_projects=1, - action="Running docker-compose in", - description="Run raw docker-compose command in a project" - ) - - # command for docker-compose - self._sub_parser.add_argument( - 'compose_cmd', metavar='cmd', type=str, - help="command for 'docker-compose'" - ) - - # arguments for docker-compose command - self._sub_parser.add_argument( - 'compose_args', metavar='arg', nargs='*', type=str, - help="arguments for 'docker-compose' commands" - ) - - def _run_project(self, runner, args, project): - if args.unknowns: - args.compose_args = [*args.compose_args, *args.unknowns] - args.unknowns = [] - - logging.debug(f"Updated args: {args}") - - # run with split compose_cmd argument - project.compose_run([args.compose_cmd, *args.compose_args]) - - return True diff --git a/kiwi_scp/subcommands/disable.py b/kiwi_scp/subcommands/disable.py deleted file mode 100644 index d4d762d..0000000 --- a/kiwi_scp/subcommands/disable.py +++ /dev/null @@ -1,16 +0,0 @@ -# local -from ..subcommand import ProjectCommand - - -class DisableCommand(ProjectCommand): - """kiwi disable""" - - def __init__(self): - super().__init__( - 'disable', num_projects='+', - action="Disabling", - description="Disable project(s) in this instance" - ) - - def _run_project(self, runner, args, project): - return project.disable() diff --git a/kiwi_scp/subcommands/down.py b/kiwi_scp/subcommands/down.py deleted file mode 100644 index b0a1147..0000000 --- a/kiwi_scp/subcommands/down.py +++ /dev/null @@ -1,56 +0,0 @@ -# system -import logging -import subprocess - -# local -from ._hidden import _find_net -from ..config import LoadedConfig -from ..executable import Executable -from ..misc import are_you_sure -from ..subcommand import ServiceCommand - - -class DownCommand(ServiceCommand): - """kiwi down""" - - def __init__(self): - super().__init__( - 'down', num_projects='?', num_services='*', - action="Bringing down", - description="Bring down the whole instance, a project or service(s) inside a project" - ) - - def _run_instance(self, runner, args): - net_name = LoadedConfig.get()['network:name'] - - if are_you_sure([ - "This will bring down the entire instance.", - "", - "This may not be what you intended, because:", - " - Bringing down the instance stops ALL services in here", - ]): - if super()._run_instance(runner, args): - # remove the hub network afterwards - if _find_net(net_name): - Executable('docker').run([ - 'network', 'rm', net_name - ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - - logging.info(f"Network '{net_name}' removed") - - else: - logging.info(f"Network '{net_name}' does not exist") - - return True - - return False - - def _run_project(self, runner, args, project): - project.compose_run(['down']) - return True - - def _run_services(self, runner, args, project, services): - project.compose_run(['stop', *services]) - project.compose_run(['rm', '-f', *services]) - - return True diff --git a/kiwi_scp/subcommands/enable.py b/kiwi_scp/subcommands/enable.py deleted file mode 100644 index 1262a7f..0000000 --- a/kiwi_scp/subcommands/enable.py +++ /dev/null @@ -1,16 +0,0 @@ -# local -from ..subcommand import ProjectCommand - - -class EnableCommand(ProjectCommand): - """kiwi enable""" - - def __init__(self): - super().__init__( - 'enable', num_projects='+', - action="Enabling", - description="Enable project(s) in this instance" - ) - - def _run_project(self, runner, args, project): - return project.enable() diff --git a/kiwi_scp/subcommands/logs.py b/kiwi_scp/subcommands/logs.py deleted file mode 100644 index 2407220..0000000 --- a/kiwi_scp/subcommands/logs.py +++ /dev/null @@ -1,40 +0,0 @@ -# local -from ..subcommand import ServiceCommand - - -class LogsCommand(ServiceCommand): - """kiwi logs""" - - def __init__(self): - super().__init__( - 'logs', num_projects=1, num_services='*', - action="Showing logs of", - description="Show logs of a project or service(s) inside a project" - ) - - # -f switch: Follow logs - self._sub_parser.add_argument( - '-f', '--follow', action='store_true', - help="output appended data as log grows" - ) - - def _run_services(self, runner, args, project, services): - # 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 services: - compose_cmd = [*compose_cmd, *args.services] - - if args.follow: - project.compose_run(compose_cmd) - - else: - # use 'less' viewer if output is static - project.compose_run_less(compose_cmd) - - return True diff --git a/kiwi_scp/subcommands/pull.py b/kiwi_scp/subcommands/pull.py deleted file mode 100644 index a1ed790..0000000 --- a/kiwi_scp/subcommands/pull.py +++ /dev/null @@ -1,18 +0,0 @@ -# local -from ..subcommand import ServiceCommand - - -class PullCommand(ServiceCommand): - """kiwi pull""" - - def __init__(self): - super().__init__( - 'pull', num_projects='?', num_services='*', - action="Pulling images for", - description="Pull images for the whole instance, a project or service(s) inside a project" - ) - - def _run_services(self, runner, args, project, services): - project.compose_run(['pull', '--ignore-pull-failures', *services]) - - return True diff --git a/kiwi_scp/subcommands/push.py b/kiwi_scp/subcommands/push.py deleted file mode 100644 index 0f83c52..0000000 --- a/kiwi_scp/subcommands/push.py +++ /dev/null @@ -1,18 +0,0 @@ -# local -from ..subcommand import ServiceCommand - - -class PushCommand(ServiceCommand): - """kiwi push""" - - def __init__(self): - super().__init__( - 'push', num_projects='?', num_services='*', - action="Pushing images for", - description="Push images for the whole instance, a project or service(s) inside a project" - ) - - def _run_services(self, runner, args, project, services): - project.compose_run(['push', *services]) - - return True diff --git a/kiwi_scp/subcommands/restart.py b/kiwi_scp/subcommands/restart.py deleted file mode 100644 index 86fce22..0000000 --- a/kiwi_scp/subcommands/restart.py +++ /dev/null @@ -1,26 +0,0 @@ -# local -from ..misc import are_you_sure -from ..subcommand import ServiceCommand - - -class RestartCommand(ServiceCommand): - """kiwi restart""" - - def __init__(self): - super().__init__( - 'restart', num_projects='?', num_services='*', - action="Restarting", - description="Restart the whole instance, a project or service(s) inside a project" - ) - - def _run_instance(self, runner, args): - if are_you_sure([ - "This will restart the entire instance." - ]): - return super()._run_instance(runner, args) - - return False - - def _run_services(self, runner, args, project, services): - project.compose_run(['restart', *services]) - return True diff --git a/kiwi_scp/subcommands/shell.py b/kiwi_scp/subcommands/shell.py deleted file mode 100644 index 7547ced..0000000 --- a/kiwi_scp/subcommands/shell.py +++ /dev/null @@ -1,104 +0,0 @@ -# system -import logging -import subprocess - -from ..config import LoadedConfig -# local -from ..subcommand import ServiceCommand - - -def _service_has_executable(project, service, exe_name): - """ - Test if service in project has an executable exe_name in its PATH. - Requires /bin/sh. - """ - - try: - # test if desired shell exists - project.compose_run( - ['exec', service, '/bin/sh', '-c', f"command -v {exe_name}"], - check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL - ) - return True - - except subprocess.CalledProcessError as e: - # fallback - return False - - -def _find_shell(args, project, service): - """find first working shell (provided by config and args) in service in project""" - - # builtin shells: as a last resort, fallback to '/bin/sh' and 'sh' - shells = ['/bin/sh', 'sh'] - - # load favorite shells from config - config = LoadedConfig.get() - 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_executable(project, service, shell): - # found working shell - logging.debug(f"Using shell '{shell}'") - return shell - - elif i + 1 < len(shells): - # try next in list - logging.info(f"Shell '{shell}' not found in container, trying '{shells[i + 1]}'") - - 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 - 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 ShellCommand(ServiceCommand): - """kiwi shell""" - - def __init__(self): - super().__init__( - 'shell', num_projects=1, num_services=1, - action="Spawning shell in", - description="Spawn shell inside a project's service" - ) - - # -s argument: Select shell - self._sub_parser.add_argument( - '-s', '--shell', type=str, - help="shell to spawn" - ) - - # -u argument: Run as user - self._sub_parser.add_argument( - '-u', '--user', type=str, - help="container user to run shell" - ) - - def _run_services(self, runner, args, project, services): - service = services[0] - shell = _find_shell(args, project, service) - - user_args = ['-u', args.user] if args.user else [] - - if shell is not None: - # spawn shell - project.compose_run(['exec', *user_args, service, shell]) - return True - - return False diff --git a/kiwi_scp/subcommands/show.py b/kiwi_scp/subcommands/show.py deleted file mode 100644 index cfb867a..0000000 --- a/kiwi_scp/subcommands/show.py +++ /dev/null @@ -1,99 +0,0 @@ -# system -import logging -import os - -import yaml - -from ..project import Project -from ..projects import Projects -# local -from ..subcommand import ServiceCommand - - -def _print_list(strings): - if isinstance(strings, str): - print(f" - {strings}") - - elif isinstance(strings, Project): - _print_list(strings.get_name()) - - elif isinstance(strings, list): - for string in strings: - _print_list(string) - - else: - _print_list(list(strings)) - - -class ShowCommand(ServiceCommand): - """kiwi show""" - - def __init__(self): - super().__init__( - 'show', num_projects='?', num_services='*', - action="Showing", - description="Show projects in this instance, services inside a project or service(s) inside a project" - ) - - def _run_instance(self, runner, args): - print(f"kiwi-scp instance at '{os.getcwd()}'") - print("#########") - projects = Projects.from_dir() - - enabled_projects = projects.filter_enabled() - if enabled_projects: - print(f"Enabled projects:") - _print_list(enabled_projects) - - disabled_projects = projects.filter_disabled() - if disabled_projects: - print(f"Disabled projects:") - _print_list(disabled_projects) - - return True - - def _run_project(self, runner, args, project): - if not project.exists(): - logging.warning(f"Project '{project.get_name()}' not found") - return False - - print(f"Services in project '{project.get_name()}':") - print("#########") - - with open(project.compose_file_name(), 'r') as stream: - try: - docker_compose_yml = yaml.safe_load(stream) - _print_list(docker_compose_yml['services'].keys()) - - except yaml.YAMLError as exc: - logging.error(exc) - - return True - - def _run_services(self, runner, args, project, services): - if not project.exists(): - logging.error(f"Project '{project.get_name()}' not found") - return False - - print(f"Configuration of services {services} in project '{project.get_name()}':") - print("#########") - - with open(project.compose_file_name(), 'r') as stream: - try: - docker_compose_yml = yaml.safe_load(stream) - - for service_name in services: - try: - print(yaml.dump( - {service_name: docker_compose_yml['services'][service_name]}, - default_flow_style=False, sort_keys=False - ).strip()) - except KeyError: - logging.error(f"Service '{service_name}' not found") - - return True - - except yaml.YAMLError as exc: - logging.error(exc) - - return False diff --git a/kiwi_scp/subcommands/up.py b/kiwi_scp/subcommands/up.py deleted file mode 100644 index b369545..0000000 --- a/kiwi_scp/subcommands/up.py +++ /dev/null @@ -1,26 +0,0 @@ -# local -from ..subcommand import ServiceCommand - - -class UpCommand(ServiceCommand): - """kiwi up""" - - def __init__(self): - super().__init__( - 'up', num_projects='?', num_services='*', - action="Bringing up", - description="Bring up the whole instance, a project or service(s) inside a project" - ) - - def _run_instance(self, runner, args): - if runner.run('conf-copy'): - return super()._run_instance(runner, args) - - return False - - def _run_services(self, runner, args, project, services): - if runner.run('net-up'): - project.compose_run(['up', '-d', *services]) - return True - - return False