1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-24 21:52:59 +00:00

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 # parent
from ..parser import Parser from ..parser import Parser
@ -63,3 +66,39 @@ class ServiceCommand(ProjectCommand):
'services', metavar='service', nargs=num_services, type=str, 'services', metavar='service', nargs=num_services, type=str,
help=f"select {services} in a project" 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 import logging
# local # local
from ._subcommand import ServiceCommand from ._subcommand import FlexCommand
from .utils.dockercommand import DockerCommand from .utils.dockercommand import DockerCommand
from .utils.project import get_project_name, list_projects from .utils.project import get_project_name, list_projects
from .utils.user_input import are_you_sure from .utils.user_input import are_you_sure
class DownCommand(ServiceCommand): class DownCommand(FlexCommand):
"""kiwi down""" """kiwi down"""
def __init__(self): def __init__(self):
super().__init__( super().__init__(
'down', num_projects='?', num_services='*', 'down', description="Bring down the whole instance, a project or service(s) inside a project"
description="Bring down the whole instance, a project or service(s) inside a project"
) )
def run(self, runner, config, args): def _run_instance(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."): if are_you_sure("This will bring down the entire instance."):
for project_name in list_projects(config): super()._run_instance(runner, config, args)
args.projects = project_name
runner.run('down')
else:
return False return False
elif 'services' in args and args.services: def _run_project(self, runner, config, args):
# "down" for service(s) inside project 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)}'") logging.info(f"Bringing down services {args.services} in project '{get_project_name(args)}'")
DockerCommand('docker-compose').run( DockerCommand('docker-compose').run(
@ -37,13 +39,4 @@ class DownCommand(ServiceCommand):
DockerCommand('docker-compose').run( DockerCommand('docker-compose').run(
config, args, ['rm', '-f', *args.services] 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 return True

View file

@ -2,38 +2,31 @@
import logging import logging
# local # local
from ._subcommand import ServiceCommand from ._subcommand import FlexCommand
from .utils.dockercommand import DockerCommand from .utils.dockercommand import DockerCommand
from .utils.project import get_project_name, list_projects from .utils.project import get_project_name, list_projects
class UpCommand(ServiceCommand): class UpCommand(FlexCommand):
"""kiwi up""" """kiwi up"""
def __init__(self): def __init__(self):
super().__init__( super().__init__(
'up', num_projects='?', num_services='*', 'up', description="Bring up the whole instance, a project or service(s) inside a project"
description="Bring up the whole instance, a project or service(s) inside a project"
) )
def run(self, runner, config, args): def _run_project(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:
logging.info(f"Bringing up project '{get_project_name(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( DockerCommand('docker-compose').run(
config, args, ['up', '-d', *args.services] config, args, ['up', '-d', *args.services]
) )
return True return True
return False