diff --git a/exegol/console/TUI.py b/exegol/console/TUI.py index e1469b82..11822e4d 100644 --- a/exegol/console/TUI.py +++ b/exegol/console/TUI.py @@ -242,7 +242,7 @@ def __buildContainerTable(table: Table, data: Sequence[ExegolContainer], safe_ke container.config.getTextFeatures(verbose_mode), container.config.getTextMounts(debug_mode), container.config.getTextDevices(debug_mode), - container.config.getTextPorts(), + container.config.getTextPorts(is_running=container.isRunning()), container.config.getTextEnvs(debug_mode)) else: table.add_row(container.getDisplayName(), container.getTextStatus(), container.image.getDisplayName(), @@ -420,7 +420,7 @@ def __buildContainerRecapTable(container: ExegolContainerTemplate): # Fetch data devices = container.config.getTextDevices(logger.isEnabledFor(ExeLog.VERBOSE)) envs = container.config.getTextEnvs(logger.isEnabledFor(ExeLog.VERBOSE)) - ports = container.config.getTextPorts() + ports = container.config.getTextPorts(is_running=container.isRunning() if type(container) is ExegolContainer else True) sysctls = container.config.getSysctls() capabilities = container.config.getCapabilities() volumes = container.config.getTextMounts(logger.isEnabledFor(ExeLog.VERBOSE)) diff --git a/exegol/model/ContainerConfig.py b/exegol/model/ContainerConfig.py index bb864f9d..4e0a8b70 100644 --- a/exegol/model/ContainerConfig.py +++ b/exegol/model/ContainerConfig.py @@ -23,7 +23,7 @@ from exegol.console.cli.ParametersManager import ParametersManager from exegol.exceptions.ExegolExceptions import ProtocolNotSupported, CancelOperation from exegol.model.ExegolModules import ExegolModules -from exegol.model.ExegolNetwork import ExegolNetwork, ExegolNetworkMode +from exegol.model.ExegolNetwork import ExegolNetwork, ExegolNetworkMode, DockerDrivers from exegol.utils import FsUtils from exegol.utils.ExeLog import logger, ExeLog from exegol.utils.GuiUtils import GuiUtils @@ -1034,6 +1034,13 @@ def isNetworkHost(self) -> bool: return True return False + def isNetworkBridge(self) -> bool: + """Return True if the container is attached to the host network""" + for net in self.__networks: + if net.getNetworkDriver() == DockerDrivers.Bridge: + return True + return False + def isNetworkDisabled(self) -> bool: """Return True if the container is not connected to any network""" return len(self.__networks) == 0 @@ -1497,7 +1504,7 @@ def getTextEnvs(self, verbose: bool = False) -> str: result += f"{k}={v}{os.linesep}" return result - def getTextPorts(self) -> str: + def getTextPorts(self, is_running: bool = True) -> str: """Text formatter for Ports configuration. Dict Port key = container port/protocol Dict Port Values: @@ -1506,8 +1513,12 @@ def getTextPorts(self) -> str: tuple = (host_ip, port) list of int = open multiple host port list of dict = open one or more ports on host, key ('HostIp' / 'HostPort') and value ip or port""" + + # Port configuration cannot be fetched from docker until container startup + if self.isNetworkBridge() and len(self.__ports) == 0 and not is_running: + return "[bright_black]Container must be started first[/bright_black]" + result = '' - # TODO if network bridge and container not started, ports config cannot be printed: add a user warning message start_host_ip = None start_host_port = None diff --git a/exegol/model/ExegolContainerTemplate.py b/exegol/model/ExegolContainerTemplate.py index 6c36cb37..06bac061 100644 --- a/exegol/model/ExegolContainerTemplate.py +++ b/exegol/model/ExegolContainerTemplate.py @@ -43,3 +43,7 @@ def getDisplayName(self) -> str: if self.getContainerName() != self.config.hostname: return f"{self.name} [bright_black]({self.config.hostname})[/bright_black]" return self.name + + def isRunning(self) -> bool: + """Interface for running status getter""" + raise NotImplementedError diff --git a/exegol/model/ExegolNetwork.py b/exegol/model/ExegolNetwork.py index 3b6aa066..c2364e07 100644 --- a/exegol/model/ExegolNetwork.py +++ b/exegol/model/ExegolNetwork.py @@ -68,6 +68,9 @@ def getNetworkConfig(self) -> Tuple[str, str]: def getNetworkMode(self) -> ExegolNetworkMode: return self.__net_mode + def getNetworkDriver(self) -> DockerDrivers: + return self.__docker_net_mode + def getNetworkName(self): return self.__net_name