2020-08-18 12:18:54 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
|
2020-08-18 11:50:21 +00:00
|
|
|
# local
|
2020-08-18 13:56:22 +00:00
|
|
|
from .utils.misc import get_first_project_name, get_services, list_projects
|
2020-08-18 11:50:21 +00:00
|
|
|
|
2020-08-12 15:46:50 +00:00
|
|
|
# parent
|
2020-08-12 14:43:13 +00:00
|
|
|
from ..parser import Parser
|
|
|
|
|
|
|
|
|
|
|
|
class SubCommand:
|
2020-08-13 08:48:01 +00:00
|
|
|
"""represents kiwi [anything] command"""
|
|
|
|
|
|
|
|
# actual command string
|
2020-08-12 14:43:13 +00:00
|
|
|
__name = None
|
2020-08-13 08:48:01 +00:00
|
|
|
# command parser
|
2020-08-12 14:43:13 +00:00
|
|
|
_sub_parser = None
|
|
|
|
|
|
|
|
def __init__(self, name, **kwargs):
|
|
|
|
self.__name = name
|
2020-08-13 08:48:01 +00:00
|
|
|
self._sub_parser = Parser().get_subparsers().add_parser(
|
|
|
|
name,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-08-12 14:43:13 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.__name
|
|
|
|
|
2020-08-17 08:57:45 +00:00
|
|
|
def run(self, runner, config, args):
|
2020-08-13 08:48:01 +00:00
|
|
|
"""actually run command with this dir's config and parsed CLI args"""
|
2020-08-12 14:43:13 +00:00
|
|
|
pass
|
2020-08-13 09:32:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProjectCommand(SubCommand):
|
|
|
|
"""this command concerns a project in current instance"""
|
|
|
|
|
2020-08-17 10:58:18 +00:00
|
|
|
def __init__(self, name, num_projects, **kwargs):
|
2020-08-13 09:32:54 +00:00
|
|
|
super().__init__(
|
|
|
|
name,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2020-08-17 10:58:18 +00:00
|
|
|
projects = "a project"
|
|
|
|
|
2020-08-18 13:56:22 +00:00
|
|
|
if not num_projects == 1:
|
|
|
|
projects = "project(s)"
|
2020-08-17 10:58:18 +00:00
|
|
|
|
2020-08-13 09:32:54 +00:00
|
|
|
self._sub_parser.add_argument(
|
2020-08-17 10:58:18 +00:00
|
|
|
'projects', metavar='project', nargs=num_projects, type=str,
|
|
|
|
help=f"select {projects} in this instance"
|
2020-08-13 09:32:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceCommand(ProjectCommand):
|
2020-08-13 12:26:49 +00:00
|
|
|
"""this command concerns service(s) in a project"""
|
2020-08-13 09:32:54 +00:00
|
|
|
|
2020-08-17 10:58:18 +00:00
|
|
|
def __init__(self, name, num_projects, num_services, **kwargs):
|
2020-08-13 09:32:54 +00:00
|
|
|
super().__init__(
|
2020-08-17 10:58:18 +00:00
|
|
|
name, num_projects=num_projects,
|
2020-08-13 09:32:54 +00:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2020-08-17 10:58:18 +00:00
|
|
|
services = "a service"
|
2020-08-13 12:26:49 +00:00
|
|
|
|
2020-08-18 13:56:22 +00:00
|
|
|
if not num_services == 1:
|
|
|
|
services = "service(s)"
|
2020-08-13 12:26:49 +00:00
|
|
|
|
2020-08-13 09:32:54 +00:00
|
|
|
self._sub_parser.add_argument(
|
2020-08-17 10:58:18 +00:00
|
|
|
'services', metavar='service', nargs=num_services, type=str,
|
2020-08-13 12:26:49 +00:00
|
|
|
help=f"select {services} in a project"
|
2020-08-13 09:32:54 +00:00
|
|
|
)
|
2020-08-18 11:50:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlexCommand(ServiceCommand):
|
2020-08-18 12:04:10 +00:00
|
|
|
"""this command concerns the entire instance, a whole project or just service(s) in a project"""
|
|
|
|
|
2020-08-18 12:18:54 +00:00
|
|
|
__action = None
|
|
|
|
|
|
|
|
def __init__(self, name, action='', **kwargs):
|
2020-08-18 11:50:21 +00:00
|
|
|
super().__init__(
|
|
|
|
name, num_projects='?', num_services='*',
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2020-08-18 12:18:54 +00:00
|
|
|
if not action:
|
|
|
|
# default action string
|
|
|
|
self.__action = f"Running '{str(self)}' for"
|
|
|
|
else:
|
|
|
|
self.__action = action
|
|
|
|
|
2020-08-18 11:50:21 +00:00
|
|
|
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
|
|
|
|
|
2020-08-18 12:18:54 +00:00
|
|
|
def _run_project(self, runner, config, args):
|
2020-08-18 12:28:03 +00:00
|
|
|
return self._run_services(runner, config, args, [])
|
2020-08-18 11:50:21 +00:00
|
|
|
|
2020-08-18 12:18:54 +00:00
|
|
|
def _run_services(self, runner, config, args, services):
|
2020-08-18 11:50:21 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def run(self, runner, config, args):
|
2020-08-18 13:56:22 +00:00
|
|
|
project_name = get_first_project_name(args)
|
2020-08-18 12:04:10 +00:00
|
|
|
services = get_services(args)
|
|
|
|
|
|
|
|
if project_name is None:
|
|
|
|
# no project given, run for entire instance
|
2020-08-18 12:18:54 +00:00
|
|
|
logging.info(f"{self.__action} this instance")
|
2020-08-18 11:50:21 +00:00
|
|
|
return self._run_instance(runner, config, args)
|
|
|
|
|
2020-08-18 12:18:54 +00:00
|
|
|
if not services:
|
2020-08-18 12:04:10 +00:00
|
|
|
# no services given, run for whole project
|
2020-08-18 12:18:54 +00:00
|
|
|
logging.info(f"{self.__action} project '{project_name}'")
|
|
|
|
return self._run_project(runner, config, args)
|
2020-08-18 11:50:21 +00:00
|
|
|
|
2020-08-18 12:04:10 +00:00
|
|
|
# run for service(s) inside project
|
2020-08-18 12:18:54 +00:00
|
|
|
logging.info(f"{self.__action} services {services} in project '{project_name}'")
|
|
|
|
return self._run_services(runner, config, args, services)
|