From 1a98137c716d08d43d91cd322249e68ce606a875 Mon Sep 17 00:00:00 2001 From: ldericher Date: Tue, 18 Aug 2020 15:56:58 +0200 Subject: [PATCH] subcommands "new", "enable", "disable" --- src/etc/docker-compose_default.yml | 19 +++++++++++++++ src/kiwi/_constants.py | 1 + src/kiwi/subcommands/__init__.py | 6 +++++ src/kiwi/subcommands/disable.py | 38 +++++++++++++++++++++++++++++ src/kiwi/subcommands/enable.py | 38 +++++++++++++++++++++++++++++ src/kiwi/subcommands/new.py | 39 ++++++++++++++++++++++++++++++ 6 files changed, 141 insertions(+) create mode 100644 src/etc/docker-compose_default.yml create mode 100644 src/kiwi/subcommands/disable.py create mode 100644 src/kiwi/subcommands/enable.py create mode 100644 src/kiwi/subcommands/new.py diff --git a/src/etc/docker-compose_default.yml b/src/etc/docker-compose_default.yml new file mode 100644 index 0000000..e6b96b6 --- /dev/null +++ b/src/etc/docker-compose_default.yml @@ -0,0 +1,19 @@ +version: "2" + +networks: + # reachable from outside + default: + driver: bridge + # interconnects projects + kiwi_hub: + external: + name: $KIWI_HUB_NAME + +services: + something: + image: maintainer/repo:tag + restart: unless-stopped + networks: + - default + - kiwi_hub + [...] diff --git a/src/kiwi/_constants.py b/src/kiwi/_constants.py index ef2e6b5..715372e 100644 --- a/src/kiwi/_constants.py +++ b/src/kiwi/_constants.py @@ -18,6 +18,7 @@ KIWI_CONF_NAME = os.getenv('KIWI_CONF_NAME', "kiwi.yml") HEADER_KIWI_CONF_NAME = f"{KIWI_ROOT}/etc/kiwi_header.yml" DEFAULT_KIWI_CONF_NAME = f"{KIWI_ROOT}/etc/kiwi_default.yml" VERSION_TAG_NAME = f"{KIWI_ROOT}/etc/version_tag" +DEFAULT_DOCKER_COMPOSE_NAME = f"{KIWI_ROOT}/etc/docker-compose_default.yml" # special config directory in projects CONF_DIRECTORY_NAME = 'conf' diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index 864cea2..ce70da2 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -2,10 +2,13 @@ from .build import BuildCommand from .cmd import CmdCommand from .conf import ConfCopyCommand, ConfPurgeCommand +from .disable import DisableCommand from .down import DownCommand +from .enable import EnableCommand from .init import InitCommand from .logs import LogsCommand from .net import NetUpCommand, NetDownCommand +from .new import NewCommand from .pull import PullCommand from .push import PushCommand from .sh import ShCommand @@ -18,11 +21,14 @@ __all__ = [ 'CmdCommand', 'ConfCopyCommand', 'ConfPurgeCommand', + 'DisableCommand', 'DownCommand', + 'EnableCommand', 'InitCommand', 'LogsCommand', 'NetUpCommand', 'NetDownCommand', + 'NewCommand', 'PullCommand', 'PushCommand', 'ShCommand', diff --git a/src/kiwi/subcommands/disable.py b/src/kiwi/subcommands/disable.py new file mode 100644 index 0000000..ccdd477 --- /dev/null +++ b/src/kiwi/subcommands/disable.py @@ -0,0 +1,38 @@ +# system +import logging +import os + +# local +from .utils.misc import get_project_names, get_project_dir, get_project_down_dir +from ._subcommand import ProjectCommand + + +class DisableCommand(ProjectCommand): + """kiwi disable""" + + def __init__(self): + super().__init__( + 'disable', num_projects='+', + description="Disable whole project(s) in this instance" + ) + + def run(self, runner, config, args): + result = True + + for project_name in get_project_names(args): + project_dir = get_project_dir(config, project_name) + project_down_dir = get_project_down_dir(config, project_name) + + if os.path.isdir(project_dir): + logging.info(f"Disabling project '{project_name}'") + os.rename(project_dir, project_down_dir) + + elif os.path.isdir(project_down_dir): + logging.warning(f"Project '{project_name}' is already disabled!") + result = False + + else: + logging.warning(f"Project '{project_name}' not found in instance!") + result = False + + return result diff --git a/src/kiwi/subcommands/enable.py b/src/kiwi/subcommands/enable.py new file mode 100644 index 0000000..1d67124 --- /dev/null +++ b/src/kiwi/subcommands/enable.py @@ -0,0 +1,38 @@ +# system +import logging +import os + +# local +from .utils.misc import get_project_names, get_project_dir, get_project_down_dir +from ._subcommand import ProjectCommand + + +class EnableCommand(ProjectCommand): + """kiwi enable""" + + def __init__(self): + super().__init__( + 'enable', num_projects='+', + description="Enable whole project(s) in this instance" + ) + + def run(self, runner, config, args): + result = True + + for project_name in get_project_names(args): + project_dir = get_project_dir(config, project_name) + project_down_dir = get_project_down_dir(config, project_name) + + if os.path.isdir(project_down_dir): + logging.info(f"Enabling project '{project_name}'") + os.rename(project_down_dir, project_dir) + + elif os.path.isdir(project_dir): + logging.warning(f"Project '{project_name}' is already enabled!") + result = False + + else: + logging.warning(f"Project '{project_name}' not found in instance!") + result = False + + return result diff --git a/src/kiwi/subcommands/new.py b/src/kiwi/subcommands/new.py new file mode 100644 index 0000000..e30a915 --- /dev/null +++ b/src/kiwi/subcommands/new.py @@ -0,0 +1,39 @@ +# system +import logging +import os +import shutil + +# local +from .utils.misc import get_project_names, get_project_dir, get_project_down_dir +from ._subcommand import ProjectCommand + +# parent +from .._constants import DEFAULT_DOCKER_COMPOSE_NAME + + +class NewCommand(ProjectCommand): + """kiwi new""" + + def __init__(self): + super().__init__( + 'new', num_projects='+', + description="Create new empty project(s) in this instance" + ) + + def run(self, runner, config, args): + result = True + + for project_name in get_project_names(args): + project_dir = get_project_dir(config, project_name) + project_down_dir = get_project_down_dir(config, project_name) + + if os.path.isdir(project_dir) or os.path.isdir(project_down_dir): + logging.error(f"Project '{project_name}' exists in this instance!") + result = False + + else: + logging.info(f"Creating project '{project_name}'") + os.mkdir(project_dir) + shutil.copy(DEFAULT_DOCKER_COMPOSE_NAME, os.path.join(project_dir, "docker-compose.yml")) + + return result