diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index e582594..67861bd 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -2,6 +2,7 @@ from .cmd import CmdCommand from .init import InitCommand from .logs import LogsCommand +from .net_up import NetUpCommand from .sh import ShCommand from .show import ShowCommand @@ -9,6 +10,7 @@ __all__ = [ 'CmdCommand', 'InitCommand', 'LogsCommand', + 'NetUpCommand', 'ShCommand', 'ShowCommand', ] diff --git a/src/kiwi/subcommands/net_up.py b/src/kiwi/subcommands/net_up.py new file mode 100644 index 0000000..339768e --- /dev/null +++ b/src/kiwi/subcommands/net_up.py @@ -0,0 +1,46 @@ +# 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']}'")