From 79520c2a63e937bf4c745c337dff4da94738f432 Mon Sep 17 00:00:00 2001 From: ldericher Date: Mon, 17 Aug 2020 14:51:11 +0200 Subject: [PATCH] subcommand "down" --- src/kiwi/subcommands/__init__.py | 2 ++ src/kiwi/subcommands/down.py | 46 ++++++++++++++++++++++++ src/kiwi/subcommands/utils/user_input.py | 23 ++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/kiwi/subcommands/down.py create mode 100644 src/kiwi/subcommands/utils/user_input.py diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index b265b46..69d75d3 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -1,5 +1,6 @@ # local from .cmd import CmdCommand +from .down import DownCommand from .init import InitCommand from .logs import LogsCommand from .net import NetUpCommand, NetDownCommand @@ -9,6 +10,7 @@ from .up import UpCommand __all__ = [ 'CmdCommand', + 'DownCommand', 'InitCommand', 'LogsCommand', 'NetUpCommand', diff --git a/src/kiwi/subcommands/down.py b/src/kiwi/subcommands/down.py new file mode 100644 index 0000000..d931cc3 --- /dev/null +++ b/src/kiwi/subcommands/down.py @@ -0,0 +1,46 @@ +# system +import logging + +# local +from ._subcommand import ServiceCommand +from .utils.dockercommand import DockerCommand +from .utils.project import get_project_name, list_projects +from .utils.user_input import are_you_sure + + +class DownCommand(ServiceCommand): + """kiwi down""" + + def __init__(self): + super().__init__( + 'down', num_projects='?', num_services='*', + description="Bring down the whole instance, a project or service(s) inside a project" + ) + + def run(self, runner, config, args): + if 'projects' not in args or args.projects is None: + # "down" for all projects + if are_you_sure("This will bring down the entire instance."): + for project_name in list_projects(config): + args.projects = project_name + runner.run('down') + return + + if 'services' in args and args.services: + # "down" for service(s) inside project + logging.info(f"Bringing down services {args.services} in project '{get_project_name(args)}'") + + DockerCommand('docker-compose').run( + config, args, ['stop', *args.services] + ) + DockerCommand('docker-compose').run( + config, args, ['rm', '-f', *args.services] + ) + + else: + # "down" for project + logging.info(f"Bringing down project '{get_project_name(args)}'") + + DockerCommand('docker-compose').run( + config, args, ['down'] + ) diff --git a/src/kiwi/subcommands/utils/user_input.py b/src/kiwi/subcommands/utils/user_input.py new file mode 100644 index 0000000..39fff73 --- /dev/null +++ b/src/kiwi/subcommands/utils/user_input.py @@ -0,0 +1,23 @@ +def are_you_sure(prompt, default="no"): + if default.lower() == 'yes': + suffix = "[YES|no]" + else: + suffix = "[yes|NO]" + + answer = input( + "!!!!!!!!!!!!!!!!!!\n" + "!!! BE CAREFUL !!!\n" + "!!!!!!!!!!!!!!!!!!\n" + "\n" + f"{prompt}\n" + "\n" + f"Are you sure you want to proceed? {suffix} " + ).strip().lower() + + if answer == '': + answer = default + + while answer not in ['yes', 'no']: + answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower() + + return answer == 'yes'