FlexCommand class

This commit is contained in:
Jörn-Michael Miehe 2020-08-18 13:50:21 +02:00
parent 79ecca2322
commit 6615982e95
3 changed files with 77 additions and 52 deletions

View file

@ -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)

View file

@ -2,33 +2,35 @@
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
def _run_instance(self, runner, config, args):
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:
super()._run_instance(runner, config, args)
return False
elif 'services' in args and args.services:
# "down" for service(s) inside project
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(
@ -37,13 +39,4 @@ class DownCommand(ServiceCommand):
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']
)
return True

View file

@ -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')
return
if 'services' in args and args.services:
logging.info(f"Bringing up services {args.services} in project '{get_project_name(args)}'")
else:
def _run_project(self, runner, config, args):
logging.info(f"Bringing up project '{get_project_name(args)}'")
if runner.run('net-up'):
DockerCommand('docker-compose').run(
config, args, ['up', '-d']
)
return True
def _run_services(self, runner, config, args):
logging.info(f"Bringing up services {args.services} in project '{get_project_name(args)}'")
DockerCommand('docker-compose').run(
config, args, ['up', '-d', *args.services]
)
return True
return False