From 8adbb5b2a0e85a3115d193b3a6a2eff774a3d565 Mon Sep 17 00:00:00 2001 From: ldericher Date: Mon, 17 Aug 2020 12:20:38 +0200 Subject: [PATCH] subcommand "net-down" --- src/kiwi/subcommands/__init__.py | 3 +- src/kiwi/subcommands/net.py | 76 ++++++++++++++++++++++++++++++++ src/kiwi/subcommands/net_up.py | 46 ------------------- 3 files changed, 78 insertions(+), 47 deletions(-) create mode 100644 src/kiwi/subcommands/net.py delete mode 100644 src/kiwi/subcommands/net_up.py diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index 67861bd..e3b6ed4 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -2,7 +2,7 @@ from .cmd import CmdCommand from .init import InitCommand from .logs import LogsCommand -from .net_up import NetUpCommand +from .net import NetUpCommand, NetDownCommand from .sh import ShCommand from .show import ShowCommand @@ -11,6 +11,7 @@ __all__ = [ 'InitCommand', 'LogsCommand', 'NetUpCommand', + 'NetDownCommand', 'ShCommand', 'ShowCommand', ] diff --git a/src/kiwi/subcommands/net.py b/src/kiwi/subcommands/net.py new file mode 100644 index 0000000..6908285 --- /dev/null +++ b/src/kiwi/subcommands/net.py @@ -0,0 +1,76 @@ +# system +import logging +import subprocess + +# local +from ._subcommand import SubCommand +from .utils.dockercommand import DockerCommand + + +def _find_net(config, args): + ps = DockerCommand('docker').run( + config, args, ['network', 'ls', '--filter', f"name={config['network:name']}", '--format', '{{.Name}}'], + stdout=subprocess.PIPE + ) + + net_found = str(ps.stdout, 'utf-8').strip() + + return net_found == config['network:name'] + + +class NetUpCommand(SubCommand): + """kiwi net-up""" + + def __init__(self): + super().__init__( + 'net-up', + description="Create the local network hub for this instance" + ) + + def run(self, runner, config, args): + if _find_net(config, args): + logging.info(f"Network '{config['network:name']}' already exists") + return + + try: + DockerCommand('docker').run( + config, args, + [ + 'network', 'create', + '--driver', 'bridge', + '--internal', + '--subnet', config['network:cidr'], + config['network:name'] + ], + check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + logging.info(f"Network '{config['network:name']}' created") + + except subprocess.CalledProcessError: + logging.error(f"Error creating network '{config['network:name']}'") + + +class NetDownCommand(SubCommand): + """kiwi net-down""" + + def __init__(self): + super().__init__( + 'net-down', + description="Remove the local network hub for this instance" + ) + + def run(self, runner, config, args): + if not _find_net(config, args): + logging.info(f"Network '{config['network:name']}' already removed") + return + + try: + DockerCommand('docker').run( + config, args, + ['network', 'rm', config['network:name']], + check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + logging.info(f"Network '{config['network:name']}' removed") + + except subprocess.CalledProcessError: + logging.error(f"Error removing network '{config['network:name']}'") \ No newline at end of file diff --git a/src/kiwi/subcommands/net_up.py b/src/kiwi/subcommands/net_up.py deleted file mode 100644 index 339768e..0000000 --- a/src/kiwi/subcommands/net_up.py +++ /dev/null @@ -1,46 +0,0 @@ -# system -import logging -import subprocess - -# local -from ._subcommand import SubCommand -from .utils.dockercommand import DockerCommand - - -class NetUpCommand(SubCommand): - """kiwi net-up""" - - def __init__(self): - super().__init__( - 'net-up', - description="Create the local network hub for this instance" - ) - - def run(self, runner, config, args): - ps = DockerCommand('docker').run( - config, args, ['network', 'ls', '--filter', f"name={config['network:name']}", '--format', '{{.Name}}'], - stdout=subprocess.PIPE - ) - - net_found = str(ps.stdout, 'utf-8').strip() - - if net_found == config['network:name']: - logging.info(f"Network '{config['network:name']}' already exists") - return - - try: - DockerCommand('docker').run( - config, args, - [ - 'network', 'create', - '--driver', 'bridge', - '--internal', - '--subnet', config['network:cidr'], - config['network:name'] - ], - check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL - ) - logging.info(f"Network '{config['network:name']}' created") - - except subprocess.CalledProcessError: - logging.error(f"Error creating network '{config['network:name']}'")