mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 04:43:00 +00:00
subcommand "net-down"
This commit is contained in:
parent
fe094fbecb
commit
8adbb5b2a0
3 changed files with 78 additions and 47 deletions
|
@ -2,7 +2,7 @@
|
||||||
from .cmd import CmdCommand
|
from .cmd import CmdCommand
|
||||||
from .init import InitCommand
|
from .init import InitCommand
|
||||||
from .logs import LogsCommand
|
from .logs import LogsCommand
|
||||||
from .net_up import NetUpCommand
|
from .net import NetUpCommand, NetDownCommand
|
||||||
from .sh import ShCommand
|
from .sh import ShCommand
|
||||||
from .show import ShowCommand
|
from .show import ShowCommand
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ __all__ = [
|
||||||
'InitCommand',
|
'InitCommand',
|
||||||
'LogsCommand',
|
'LogsCommand',
|
||||||
'NetUpCommand',
|
'NetUpCommand',
|
||||||
|
'NetDownCommand',
|
||||||
'ShCommand',
|
'ShCommand',
|
||||||
'ShowCommand',
|
'ShowCommand',
|
||||||
]
|
]
|
||||||
|
|
76
src/kiwi/subcommands/net.py
Normal file
76
src/kiwi/subcommands/net.py
Normal file
|
@ -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']}'")
|
|
@ -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']}'")
|
|
Loading…
Reference in a new issue