2021-11-03 00:12:32 +00:00
|
|
|
import logging
|
2021-11-02 16:21:01 +00:00
|
|
|
from typing import Callable, Type, Optional, Tuple
|
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
from .cli import KiwiCommandType, KiwiCommand
|
|
|
|
from ..instance import Instance
|
|
|
|
|
|
|
|
_pass_instance = click.make_pass_decorator(
|
|
|
|
Instance,
|
|
|
|
ensure=True,
|
|
|
|
)
|
2021-11-03 00:12:32 +00:00
|
|
|
|
2021-11-02 16:21:01 +00:00
|
|
|
_project_arg = click.argument(
|
2021-11-06 02:45:27 +00:00
|
|
|
"project_name",
|
|
|
|
metavar="[PROJECT]",
|
2021-11-02 16:21:01 +00:00
|
|
|
required=False,
|
|
|
|
type=str,
|
|
|
|
)
|
2021-11-03 00:12:32 +00:00
|
|
|
|
2021-11-02 16:21:01 +00:00
|
|
|
_services_arg = click.argument(
|
2021-11-06 02:45:27 +00:00
|
|
|
"service_names",
|
2021-11-02 16:21:01 +00:00
|
|
|
metavar="[SERVICE]...",
|
|
|
|
nargs=-1,
|
|
|
|
type=str,
|
|
|
|
)
|
|
|
|
|
2021-11-03 00:12:32 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2021-11-02 16:21:01 +00:00
|
|
|
|
|
|
|
def kiwi_command(
|
|
|
|
name: str,
|
|
|
|
command_type: KiwiCommandType,
|
2021-11-03 00:12:32 +00:00
|
|
|
**decorator_kwargs,
|
2021-11-02 16:21:01 +00:00
|
|
|
) -> Callable:
|
|
|
|
def decorator(command_cls: Type[KiwiCommand]) -> Callable:
|
|
|
|
|
2021-11-03 00:12:32 +00:00
|
|
|
@click.command(
|
|
|
|
name,
|
|
|
|
help=command_cls.__doc__,
|
|
|
|
**decorator_kwargs,
|
|
|
|
)
|
2021-11-02 16:21:01 +00:00
|
|
|
@_pass_instance
|
2021-11-06 02:45:27 +00:00
|
|
|
def cmd(ctx: Instance, project_name: Optional[str] = None, service_names: Optional[Tuple[str]] = None,
|
2021-11-03 00:12:32 +00:00
|
|
|
**kwargs) -> None:
|
|
|
|
|
2021-11-06 02:45:27 +00:00
|
|
|
_logger.debug(f"{ctx.directory!r}: {project_name!r}, {service_names!r}")
|
|
|
|
if project_name is None:
|
2021-11-02 16:21:01 +00:00
|
|
|
# run for whole instance
|
2021-11-03 00:12:32 +00:00
|
|
|
_logger.debug(f"running for instance, kwargs={kwargs}")
|
|
|
|
command_cls.run_for_instance(ctx, **kwargs)
|
2021-11-02 16:21:01 +00:00
|
|
|
|
2021-11-06 02:45:27 +00:00
|
|
|
elif not service_names:
|
2021-11-02 16:21:01 +00:00
|
|
|
# run for one entire project
|
2021-11-06 02:45:27 +00:00
|
|
|
project = ctx.get_project(project_name)
|
|
|
|
if project is not None:
|
|
|
|
_logger.debug(f"running for existing project {project}, kwargs={kwargs}")
|
|
|
|
command_cls.run_for_existing_project(ctx, project, **kwargs)
|
|
|
|
|
|
|
|
else:
|
|
|
|
_logger.debug(f"running for new project {project_name}, kwargs={kwargs}")
|
|
|
|
command_cls.run_for_new_project(ctx, project_name, **kwargs)
|
2021-11-02 16:21:01 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
# run for some services
|
2021-11-06 02:45:27 +00:00
|
|
|
project = ctx.get_project(project_name)
|
|
|
|
if project is not None:
|
|
|
|
_logger.debug(f"running for services {service_names} in project {project}, kwargs={kwargs}")
|
|
|
|
command_cls.run_for_services(ctx, project, list(service_names), **kwargs)
|
|
|
|
|
|
|
|
else:
|
|
|
|
KiwiCommand.print_error(f"Project '{project_name}' not in kiwi-scp instance at '{ctx.directory}'!")
|
2021-11-02 16:21:01 +00:00
|
|
|
|
|
|
|
if command_type is KiwiCommandType.PROJECT:
|
|
|
|
cmd = _project_arg(cmd)
|
|
|
|
|
|
|
|
elif command_type is KiwiCommandType.SERVICE:
|
|
|
|
cmd = _project_arg(cmd)
|
|
|
|
cmd = _services_arg(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
2021-11-02 16:21:31 +00:00
|
|
|
return decorator
|