2020-08-17 10:20:38 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# local
|
|
|
|
from ._subcommand import SubCommand
|
|
|
|
from .utils.dockercommand import DockerCommand
|
2020-08-18 12:04:10 +00:00
|
|
|
from .utils._misc import are_you_sure
|
2020-08-17 10:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2020-08-17 13:00:05 +00:00
|
|
|
return True
|
2020-08-17 10:20:38 +00:00
|
|
|
|
|
|
|
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']}'")
|
2020-08-17 13:00:05 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2020-08-17 10:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2020-08-17 13:21:42 +00:00
|
|
|
logging.info(f"Network '{config['network:name']}' does not exist")
|
2020-08-17 13:00:05 +00:00
|
|
|
return True
|
2020-08-17 10:20:38 +00:00
|
|
|
|
|
|
|
try:
|
2020-08-17 12:51:43 +00:00
|
|
|
if are_you_sure("This will bring down this instance's hub network!"):
|
2020-08-17 13:00:05 +00:00
|
|
|
if runner.run('down'):
|
|
|
|
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")
|
|
|
|
else:
|
|
|
|
return False
|
2020-08-17 10:20:38 +00:00
|
|
|
|
|
|
|
except subprocess.CalledProcessError:
|
2020-08-17 13:00:05 +00:00
|
|
|
logging.error(f"Error removing network '{config['network:name']}'")
|
|
|
|
return False
|
|
|
|
|
2020-08-17 13:21:42 +00:00
|
|
|
return True
|