mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-12-25 18:42:59 +00:00
CLI commands cleanup
This commit is contained in:
parent
3a28af7f7e
commit
3ccdffe737
10 changed files with 175 additions and 173 deletions
|
@ -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
|
||||
|
|
|
@ -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',
|
||||
]
|
||||
|
|
|
@ -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
|
37
src/kiwi/subcommands/clean.py
Normal file
37
src/kiwi/subcommands/clean.py
Normal file
|
@ -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
|
|
@ -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):
|
||||
|
|
@ -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):
|
|
@ -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
|
45
src/kiwi/subcommands/purge.py
Normal file
45
src/kiwi/subcommands/purge.py
Normal file
|
@ -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
|
|
@ -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"
|
||||
)
|
|
@ -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
|
Loading…
Reference in a new issue