diff --git a/src/kiwi/subcommands/_subcommand.py b/src/kiwi/subcommands/_subcommand.py index e7c0274..2ff6d20 100644 --- a/src/kiwi/subcommands/_subcommand.py +++ b/src/kiwi/subcommands/_subcommand.py @@ -1,3 +1,6 @@ +# local +from .utils.project import get_project_name, list_projects + # parent from ..parser import Parser @@ -63,3 +66,39 @@ class ServiceCommand(ProjectCommand): 'services', metavar='service', nargs=num_services, type=str, help=f"select {services} in a project" ) + + +class FlexCommand(ServiceCommand): + def __init__(self, name, **kwargs): + super().__init__( + name, num_projects='?', num_services='*', + **kwargs + ) + + def _run_instance(self, runner, config, args): + result = True + + for project_name in list_projects(config): + args.projects = project_name + result &= runner.run(str(self)) + + return result + + def _run_project(self, runner, config, args): + pass + + def _run_services(self, runner, config, args): + pass + + def run(self, runner, config, args): + if 'projects' not in args or args.projects is None: + # command for entire instance + return self._run_instance(runner, config, args) + + elif 'services' not in args or not args.services: + # command for whole project + return self._run_project(runner, config, args) + + else: + # command for service(s) inside project + return self._run_services(runner, config, args) diff --git a/src/kiwi/subcommands/down.py b/src/kiwi/subcommands/down.py index 790f3bb..34c8681 100644 --- a/src/kiwi/subcommands/down.py +++ b/src/kiwi/subcommands/down.py @@ -2,48 +2,41 @@ import logging # local -from ._subcommand import ServiceCommand +from ._subcommand import FlexCommand 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): +class DownCommand(FlexCommand): """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" + 'down', 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') - else: - return False + def _run_instance(self, runner, config, args): + if are_you_sure("This will bring down the entire instance."): + super()._run_instance(runner, config, args) - elif '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)}'") + return False - 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'] - ) + def _run_project(self, runner, config, args): + logging.info(f"Bringing down project '{get_project_name(args)}'") + DockerCommand('docker-compose').run( + config, args, ['down'] + ) + return True + + def _run_services(self, runner, config, args): + 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] + ) return True diff --git a/src/kiwi/subcommands/up.py b/src/kiwi/subcommands/up.py index 0681d83..1a9b61a 100644 --- a/src/kiwi/subcommands/up.py +++ b/src/kiwi/subcommands/up.py @@ -2,38 +2,31 @@ import logging # local -from ._subcommand import ServiceCommand +from ._subcommand import FlexCommand from .utils.dockercommand import DockerCommand from .utils.project import get_project_name, list_projects -class UpCommand(ServiceCommand): +class UpCommand(FlexCommand): """kiwi up""" def __init__(self): super().__init__( - 'up', num_projects='?', num_services='*', - description="Bring up the whole instance, a project or service(s) inside a project" + 'up', description="Bring up 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: - # "up" for all projects - for project_name in list_projects(config): - args.projects = project_name - runner.run('up') + def _run_project(self, runner, config, args): + logging.info(f"Bringing up project '{get_project_name(args)}'") - return + DockerCommand('docker-compose').run( + config, args, ['up', '-d'] + ) + return True - if 'services' in args and args.services: - logging.info(f"Bringing up services {args.services} in project '{get_project_name(args)}'") - else: - logging.info(f"Bringing up project '{get_project_name(args)}'") + def _run_services(self, runner, config, args): + logging.info(f"Bringing up services {args.services} in project '{get_project_name(args)}'") - if runner.run('net-up'): - DockerCommand('docker-compose').run( - config, args, ['up', '-d', *args.services] - ) - return True - - return False + DockerCommand('docker-compose').run( + config, args, ['up', '-d', *args.services] + ) + return True