mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-27 14:43:00 +00:00
net-up and net-down functionality
This commit is contained in:
parent
67f495d072
commit
52d54eddad
5 changed files with 57 additions and 60 deletions
|
@ -36,8 +36,7 @@ class DownCommand(KiwiCommand):
|
|||
return
|
||||
|
||||
super().run_for_instance(instance)
|
||||
|
||||
# TODO net-down
|
||||
instance.remove_net()
|
||||
|
||||
@classmethod
|
||||
def run_for_project(cls, instance: Instance, project: Project, **kwargs) -> None:
|
||||
|
|
|
@ -20,9 +20,6 @@ class UpCommand(KiwiCommand):
|
|||
@classmethod
|
||||
def run_for_filtered_services(cls, instance: Instance, project: Project, services: Services,
|
||||
new_service_names: List[str], **kwargs) -> None:
|
||||
# TODO conf-copy
|
||||
# TODO net-up
|
||||
|
||||
if not services:
|
||||
if not click.confirm(
|
||||
"Did not find any of those services. \n"
|
||||
|
@ -31,4 +28,7 @@ class UpCommand(KiwiCommand):
|
|||
):
|
||||
return
|
||||
|
||||
instance.create_net()
|
||||
services.copy_configs()
|
||||
|
||||
COMPOSE_EXE.run(["up", "-d", *services.names], **project.process_kwargs)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import logging
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Generator, Dict, Sequence
|
||||
|
||||
|
@ -5,8 +7,11 @@ import attr
|
|||
|
||||
from ._constants import KIWI_CONF_NAME, CONFIG_DIRECTORY_NAME
|
||||
from .config import KiwiConfig
|
||||
from .executable import DOCKER_EXE
|
||||
from .project import Project
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@attr.s
|
||||
class Instance:
|
||||
|
@ -30,6 +35,54 @@ class Instance:
|
|||
def storage_config_directory(self):
|
||||
return self.config.storage.directory.joinpath(CONFIG_DIRECTORY_NAME)
|
||||
|
||||
@staticmethod
|
||||
def __find_net(net_name):
|
||||
ps = DOCKER_EXE.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
|
||||
|
||||
def create_net(self):
|
||||
net_name = self.config.network.name
|
||||
net_cidr = str(self.config.network.cidr)
|
||||
|
||||
if self.__find_net(net_name):
|
||||
_logger.info(f"Network '{net_name}' already exists")
|
||||
return
|
||||
|
||||
try:
|
||||
DOCKER_EXE.run([
|
||||
"network", "create",
|
||||
"--driver", "bridge",
|
||||
"--internal",
|
||||
"--subnet", net_cidr,
|
||||
net_name
|
||||
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
_logger.info(f"Network '{net_name}' created")
|
||||
|
||||
except subprocess.CalledProcessError:
|
||||
_logger.error(f"Error creating network '{net_name}'")
|
||||
|
||||
def remove_net(self):
|
||||
net_name = self.config.network.name
|
||||
|
||||
if not self.__find_net(net_name):
|
||||
_logger.info(f"Network '{net_name}' does not exist")
|
||||
return
|
||||
|
||||
try:
|
||||
DOCKER_EXE.run([
|
||||
"network", "rm",
|
||||
net_name
|
||||
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
_logger.info(f"Network '{net_name}' removed")
|
||||
|
||||
except subprocess.CalledProcessError:
|
||||
_logger.error(f"Error removing network '{net_name}'")
|
||||
|
||||
@property
|
||||
def projects(self) -> Generator[Project, None, None]:
|
||||
for project in self.config.projects:
|
||||
|
|
|
@ -11,7 +11,6 @@ from ruamel.yaml import CommentedMap
|
|||
from .executable import COMPOSE_EXE
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .instance import Instance
|
||||
from .project import Project
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
# system
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
# local
|
||||
from ..config import LoadedConfig
|
||||
from ..executable import Executable
|
||||
from ..subcommand import SubCommand
|
||||
|
||||
|
||||
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", add_parser=False,
|
||||
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
|
Loading…
Reference in a new issue