diff --git a/src/etc/command_help.txt b/src/etc/command_help.txt index 07163bd..ea1ecf6 100644 --- a/src/etc/command_help.txt +++ b/src/etc/command_help.txt @@ -1,27 +1,30 @@ COMMANDS ======== -init -> config -show -> config --show +# operation +config up down update +clean + +# management + new enable disable - -list -> show -logs -sh cmd +purge + +# inspection + +inspect +logs +shell + +# imaging build pull push - -net-up -net-down -conf-copy (hide) -conf-purge (hide) -conf-clean -> clean diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index 637f9be..76ff7ab 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -1,41 +1,42 @@ # local +from ._hidden import ConfCopyCommand, ConfPurgeCommand, NetUpCommand + from .build import BuildCommand +from .clean import CleanCommand from .cmd import CmdCommand -from .conf import ConfCopyCommand, ConfPurgeCommand, ConfCleanCommand +from .config import ConfigCommand from .disable import DisableCommand from .down import DownCommand from .enable import EnableCommand -from .init import InitCommand -from .list import ListCommand +from .inspect import InspectCommand from .logs import LogsCommand -from .net import NetUpCommand, NetDownCommand from .new import NewCommand from .pull import PullCommand +from .purge import PurgeCommand from .push import PushCommand -from .sh import ShCommand -from .show import ShowCommand +from .shell import ShellCommand from .up import UpCommand from .update import UpdateCommand __all__ = [ - 'BuildCommand', - 'CmdCommand', 'ConfCopyCommand', 'ConfPurgeCommand', - 'ConfCleanCommand', + 'NetUpCommand', + + 'BuildCommand', + 'CleanCommand', + 'CmdCommand', + 'ConfigCommand', 'DisableCommand', 'DownCommand', 'EnableCommand', - 'InitCommand', - 'ListCommand', + 'InspectCommand', 'LogsCommand', - 'NetUpCommand', - 'NetDownCommand', 'NewCommand', 'PullCommand', + 'PurgeCommand', 'PushCommand', - 'ShCommand', - 'ShowCommand', + 'ShellCommand', 'UpCommand', 'UpdateCommand', ] diff --git a/src/kiwi/subcommands/conf.py b/src/kiwi/subcommands/_hidden.py similarity index 54% rename from src/kiwi/subcommands/conf.py rename to src/kiwi/subcommands/_hidden.py index bfbd933..bd7bbc8 100644 --- a/src/kiwi/subcommands/conf.py +++ b/src/kiwi/subcommands/_hidden.py @@ -4,6 +4,7 @@ import subprocess # local from .._constants import CONF_DIRECTORY_NAME +from ..executable import Executable from ..subcommand import SubCommand from ..config import LoadedConfig from ..projects import Projects @@ -16,7 +17,7 @@ class ConfCopyCommand(SubCommand): def __init__(self): super().__init__( 'conf-copy', - action="Syncing all configs for", + action="Syncing all configs for", add_parser=False, description="Synchronize all config files to target directory" ) @@ -44,7 +45,7 @@ class ConfPurgeCommand(SubCommand): def __init__(self): super().__init__( 'conf-purge', - action="Removing all configs for", + action="Removing all configs for", add_parser=False, description="Remove all config files in target directory" ) @@ -59,36 +60,47 @@ class ConfPurgeCommand(SubCommand): return True -class ConfCleanCommand(SubCommand): - """kiwi conf-clean""" +def _find_net(net_name): + ps = Executable('docker').run([ + 'network', 'ls', '--filter', f"name={net_name}", '--format', '{{.Name}}' + ], stdout=subprocess.PIPE) + + net_found = str(ps.stdout, 'utf-8').strip() + + return net_found == net_name + + +class NetUpCommand(SubCommand): + """kiwi net-up""" def __init__(self): super().__init__( - 'conf-clean', - action="Cleaning all configs for", - description="Cleanly sync all configs to target folder, relaunch affected projects" + 'net-up', + action="Creating the local network hub for", add_parser=False, + description="Create the local network hub for this instance" ) def _run_instance(self, runner, args): - result = True + config = LoadedConfig.get() + net_name = config['network:name'] + net_cidr = config['network:cidr'] - affected_projects = [ - project.get_name() - for project in Projects.from_dir() - if project.has_configs() - ] + if _find_net(net_name): + logging.info(f"Network '{net_name}' already exists") + return True - for project_name in affected_projects: - args.projects = project_name - result &= runner.run('down') + try: + Executable('docker').run([ + 'network', 'create', + '--driver', 'bridge', + '--internal', + '--subnet', net_cidr, + net_name + ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + logging.info(f"Network '{net_name}' created") - # cleanly sync configs - result &= runner.run('conf-purge') - result &= runner.run('conf-copy') + except subprocess.CalledProcessError: + logging.error(f"Error creating network '{net_name}'") + return False - # bring projects back up - for project_name in affected_projects: - args.projects = project_name - result &= runner.run('up') - - return result + return True diff --git a/src/kiwi/subcommands/clean.py b/src/kiwi/subcommands/clean.py new file mode 100644 index 0000000..22422e5 --- /dev/null +++ b/src/kiwi/subcommands/clean.py @@ -0,0 +1,37 @@ +from ..projects import Projects +from ..subcommand import SubCommand + + +class CleanCommand(SubCommand): + """kiwi clean""" + + def __init__(self): + super().__init__( + 'clean', + action="Cleaning all configs for", + description="Cleanly sync all configs to target folder, then relaunch affected projects" + ) + + def _run_instance(self, runner, args): + result = True + + affected_projects = [ + project.get_name() + for project in Projects.from_dir() + if project.has_configs() + ] + + for project_name in affected_projects: + args.projects = project_name + result &= runner.run('down') + + # cleanly sync configs + result &= runner.run('conf-purge') + result &= runner.run('conf-copy') + + # bring projects back up + for project_name in affected_projects: + args.projects = project_name + result &= runner.run('up') + + return result diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/config.py similarity index 68% rename from src/kiwi/subcommands/init.py rename to src/kiwi/subcommands/config.py index 05012c0..1174a3b 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/config.py @@ -8,14 +8,14 @@ from ..subcommand import SubCommand from ..config import DefaultConfig, LoadedConfig -class InitCommand(SubCommand): - """kiwi init""" +class ConfigCommand(SubCommand): + """kiwi config""" def __init__(self): super().__init__( - 'init', - action=f"Initializing '{KIWI_CONF_NAME}' in", - description="Create a new kiwi-config instance" + 'config', + action=f"Configuring '{KIWI_CONF_NAME}' in", + description="Configure kiwi-config instance" ) # -f switch: Initialize with default config @@ -25,9 +25,21 @@ class InitCommand(SubCommand): help=f"use default values even if {KIWI_CONF_NAME} is present" ) + # -s switch: Show current config instead + self._sub_parser.add_argument( + '-s', '--show', + action='store_true', + help=f"show effective {KIWI_CONF_NAME} contents instead" + ) + def _run_instance(self, runner, args): config = LoadedConfig.get() + # check show switch + if args.show: + print(config) + return True + # check force switch if args.force and os.path.isfile(KIWI_CONF_NAME): diff --git a/src/kiwi/subcommands/list.py b/src/kiwi/subcommands/inspect.py similarity index 90% rename from src/kiwi/subcommands/list.py rename to src/kiwi/subcommands/inspect.py index cd75af8..ae679f1 100644 --- a/src/kiwi/subcommands/list.py +++ b/src/kiwi/subcommands/inspect.py @@ -24,14 +24,14 @@ def _print_list(strings): _print_list(list(strings)) -class ListCommand(ServiceCommand): - """kiwi list""" +class InspectCommand(ServiceCommand): + """kiwi inspect""" def __init__(self): super().__init__( - 'list', num_projects='?', num_services='*', - action="Listing", - description="List projects in this instance, services inside a project or service(s) inside a project" + 'inspect', num_projects='?', num_services='*', + action="Inspecting", + description="Inspect projects in this instance, services inside a project or service(s) inside a project" ) def _run_instance(self, runner, args): diff --git a/src/kiwi/subcommands/net.py b/src/kiwi/subcommands/net.py deleted file mode 100644 index 9db49a5..0000000 --- a/src/kiwi/subcommands/net.py +++ /dev/null @@ -1,90 +0,0 @@ -# system -import logging -import subprocess - -# local -from ..subcommand import SubCommand -from ..config import LoadedConfig -from ..executable import Executable -from ..misc import are_you_sure - - -def _find_net(net_name): - ps = Executable('docker').run([ - 'network', 'ls', '--filter', f"name={net_name}", '--format', '{{.Name}}' - ], stdout=subprocess.PIPE) - - net_found = str(ps.stdout, 'utf-8').strip() - - return net_found == net_name - - -class NetUpCommand(SubCommand): - """kiwi net-up""" - - def __init__(self): - super().__init__( - 'net-up', - action="Creating the local network hub for", - description="Create the local network hub for this instance" - ) - - def _run_instance(self, runner, args): - config = LoadedConfig.get() - net_name = config['network:name'] - net_cidr = config['network:cidr'] - - if _find_net(net_name): - logging.info(f"Network '{net_name}' already exists") - return True - - try: - Executable('docker').run([ - 'network', 'create', - '--driver', 'bridge', - '--internal', - '--subnet', net_cidr, - net_name - ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - logging.info(f"Network '{net_name}' created") - - except subprocess.CalledProcessError: - logging.error(f"Error creating network '{net_name}'") - return False - - return True - - -class NetDownCommand(SubCommand): - """kiwi net-down""" - - def __init__(self): - super().__init__( - 'net-down', - action="Removing the local network hub for", - description="Remove the local network hub for this instance" - ) - - def _run_instance(self, runner, args): - net_name = LoadedConfig.get()['network:name'] - - if not _find_net(net_name): - logging.info(f"Network '{net_name}' does not exist") - return True - - try: - if are_you_sure("This will bring down this instance's hub network!"): - if runner.run('down'): - Executable('docker').run([ - 'network', 'rm', net_name - ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - - logging.info(f"Network '{net_name}' removed") - else: - return False - - except subprocess.CalledProcessError: - logging.error(f"Error removing network '{net_name}'") - return False - - return True diff --git a/src/kiwi/subcommands/purge.py b/src/kiwi/subcommands/purge.py new file mode 100644 index 0000000..fbf771b --- /dev/null +++ b/src/kiwi/subcommands/purge.py @@ -0,0 +1,45 @@ +# system +import logging +import subprocess + +# local +from ._hidden import _find_net +from ..subcommand import SubCommand +from ..config import LoadedConfig +from ..executable import Executable +from ..misc import are_you_sure + + +class PurgeCommand(SubCommand): + """kiwi purge""" + + def __init__(self): + super().__init__( + 'purge', + action="Tearing down", + description="Remove all ephemeral artifacts of this instance" + ) + + def _run_instance(self, runner, args): + net_name = LoadedConfig.get()['network:name'] + + if not _find_net(net_name): + logging.info(f"Network '{net_name}' does not exist") + return True + + try: + if are_you_sure("This will bring down this instance's hub network!"): + if runner.run('down'): + Executable('docker').run([ + 'network', 'rm', net_name + ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + logging.info(f"Network '{net_name}' removed") + else: + return False + + except subprocess.CalledProcessError: + logging.error(f"Error removing network '{net_name}'") + return False + + return True diff --git a/src/kiwi/subcommands/sh.py b/src/kiwi/subcommands/shell.py similarity index 96% rename from src/kiwi/subcommands/sh.py rename to src/kiwi/subcommands/shell.py index c8ef46e..3a3ed00 100644 --- a/src/kiwi/subcommands/sh.py +++ b/src/kiwi/subcommands/shell.py @@ -68,12 +68,12 @@ def _find_shell(args, project, service): return None -class ShCommand(ServiceCommand): - """kiwi sh""" +class ShellCommand(ServiceCommand): + """kiwi shell""" def __init__(self): super().__init__( - 'sh', num_projects=1, num_services=1, + 'shell', num_projects=1, num_services=1, action="Spawning shell in", description="Spawn shell inside a project's service" ) diff --git a/src/kiwi/subcommands/show.py b/src/kiwi/subcommands/show.py deleted file mode 100644 index 8b47e75..0000000 --- a/src/kiwi/subcommands/show.py +++ /dev/null @@ -1,18 +0,0 @@ -# local -from ..subcommand import SubCommand -from ..config import LoadedConfig - - -class ShowCommand(SubCommand): - """kiwi show""" - - def __init__(self): - super().__init__( - 'show', - action="Printing", - description="Show effective kiwi.yml" - ) - - def _run_instance(self, runner, args): - print(LoadedConfig.get()) - return True