Skip to content

Commit

Permalink
Support docker desktop beta Host network
Browse files Browse the repository at this point in the history
Signed-off-by: Dramelac <[email protected]>
  • Loading branch information
Dramelac committed May 14, 2024
1 parent fb695c1 commit 5a92afc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
3 changes: 2 additions & 1 deletion exegol/config/ConstantConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ConstantConfig:
exegol_config_path: Path = Path().home() / ".exegol"
# Docker Desktop for mac config file
docker_desktop_mac_config_path = Path().home() / "Library/Group Containers/group.com.docker/settings.json"
docker_desktop_windows_config_path = Path().home() / "AppData/Roaming/Docker/settings.json"
docker_desktop_windows_config_short_path = "AppData/Roaming/Docker/settings.json"
docker_desktop_windows_config_path = Path().home() / docker_desktop_windows_config_short_path
# Install mode, check if Exegol has been git cloned or installed using pip package
git_source_installation: bool = (src_root_path_obj / '.git').is_dir()
pip_installed: bool = src_root_path_obj.name == "site-packages"
Expand Down
40 changes: 30 additions & 10 deletions exegol/config/EnvInfo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import platform
from enum import Enum
from typing import Optional, Any, List
from pathlib import Path
from typing import Optional, List, Dict

from exegol.config.ConstantConfig import ConstantConfig
from exegol.utils.ExeLog import logger
Expand Down Expand Up @@ -128,6 +129,11 @@ def isMacHost(cls) -> bool:
"""Return true if macOS is detected on the host"""
return cls.getHostOs() == cls.HostOs.MAC

@classmethod
def isLinuxHost(cls) -> bool:
"""Return true if Linux is detected on the host"""
return cls.getHostOs() == cls.HostOs.LINUX

@classmethod
def isDockerDesktop(cls) -> bool:
"""Return true if docker desktop is used on the host"""
Expand Down Expand Up @@ -159,7 +165,7 @@ def getShellType(cls):
return "Unknown"

@classmethod
def getDockerDesktopSettings(cls) -> Optional[Any]:
def getDockerDesktopSettings(cls) -> Dict:
"""Applicable only for docker desktop on macos"""
if cls.isDockerDesktop():
if cls.__docker_desktop_resource_config is None:
Expand All @@ -168,20 +174,34 @@ def getDockerDesktopSettings(cls) -> Optional[Any]:
elif cls.is_windows_shell:
path = ConstantConfig.docker_desktop_windows_config_path
else:
return None
# TODO support from WSL shell
# Find docker desktop config
config_file = list(Path("/mnt/c/Users").glob(f"*/{ConstantConfig.docker_desktop_windows_config_short_path}"))
if len(config_file) == 0:
return {}
else:
path = config_file[0]
logger.debug(f"Docker desktop config found at {path}")
try:
with open(path, 'r') as docker_desktop_config:
cls.__docker_desktop_resource_config = json.load(docker_desktop_config)
except FileNotFoundError:
logger.warning(f"Docker Desktop configuration file not found: '{path}'")
return None
return {}
return cls.__docker_desktop_resource_config
return None
return {}

@classmethod
def getDockerDesktopResources(cls) -> List[str]:
config = cls.getDockerDesktopSettings()
if config:
return config.get('filesharingDirectories', [])
return []
return cls.getDockerDesktopSettings().get('filesharingDirectories', [])

@classmethod
def isHostNetworkAvailable(cls) -> bool:
if cls.isLinuxHost():
return True
elif cls.isOrbstack():
return True
elif cls.isDockerDesktop():
res = cls.getDockerDesktopSettings().get('hostNetworkingEnabled', False)
return res if res is not None else False
logger.warning("Unknown or not supported environment for host network mode.")
return False
13 changes: 8 additions & 5 deletions exegol/model/ContainerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
from docker.types import Mount
from rich.prompt import Prompt

from exegol.console.cli.SyntaxFormat import SyntaxFormat
from exegol.config.ConstantConfig import ConstantConfig
from exegol.config.EnvInfo import EnvInfo
from exegol.config.UserConfig import UserConfig
from exegol.console.ConsoleFormat import boolFormatter, getColor
from exegol.console.ExegolPrompt import Confirm
from exegol.console.cli.ParametersManager import ParametersManager
from exegol.console.cli.SyntaxFormat import SyntaxFormat
from exegol.exceptions.ExegolExceptions import ProtocolNotSupported, CancelOperation
from exegol.model.ExegolModules import ExegolModules
from exegol.model.ExegolNetwork import ExegolNetwork, ExegolNetworkMode, DockerDrivers
Expand Down Expand Up @@ -889,10 +889,13 @@ def setNetworkMode(self, network: Union[ExegolNetworkMode, str] = ExegolNetworkM
logger.warning("Host mode cannot be set with NAT ports configured. Disabling the host network mode.")
net_mode = self.__fallback_network_mode
if EnvInfo.isDockerDesktop():
logger.warning("Docker desktop (Windows & macOS) does not support sharing of host network interfaces.")
logger.verbose("Official doc: https://docs.docker.com/network/host/")
logger.info("To share network ports between the host and exegol, use the [bright_blue]--port[/bright_blue] parameter.")
net_mode = self.__fallback_network_mode
if not EnvInfo.isHostNetworkAvailable():
logger.warning("Host network mode for Docker desktop (Windows & macOS) is not available.")
logger.verbose("Official doc: https://docs.docker.com/network/drivers/host/#docker-desktop")
logger.info("To share network ports between the host and exegol, use the [bright_blue]--port[/bright_blue] parameter.")
net_mode = self.__fallback_network_mode
else:
logger.warning("Docker desktop host network mode is enabled but in beta. Everything might not work as you expect.")

self.__networks.clear()
if type(net_mode) is str or net_mode != ExegolNetworkMode.disable:
Expand Down
1 change: 1 addition & 0 deletions exegol/utils/GuiUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __checkDockerDesktopResourcesConfig() -> bool:
mount /tmp/.X11-unix for display sharing.
Return True if the configuration is correct and /tmp is part of the whitelisted resources
"""
# Function not used for now because the X11 socket cannot be used for now with Docker Desktop
docker_config = EnvInfo.getDockerDesktopResources()
logger.debug(f"Docker Desktop configuration filesharingDirectories: {docker_config}")
return '/tmp' in docker_config
Expand Down

0 comments on commit 5a92afc

Please sign in to comment.