Skip to content

Commit

Permalink
Add message for ports config when container is stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
Dramelac committed Mar 21, 2024
1 parent 68542d2 commit fb695c1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions exegol/console/TUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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))
Expand Down
17 changes: 14 additions & 3 deletions exegol/model/ContainerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions exegol/model/ExegolContainerTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions exegol/model/ExegolNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fb695c1

Please sign in to comment.