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 56fd8ea commit 5f980bf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 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
Expand Up @@ -3,7 +3,7 @@
import platform
from enum import Enum
from pathlib import Path
from typing import Optional, Any, List
from typing import Optional, List, Dict

from exegol.config.ConstantConfig import ConstantConfig
from exegol.utils.ExeLog import logger
Expand Down Expand Up @@ -149,6 +149,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 isWaylandAvailable(cls) -> bool:
"""Return true if wayland is detected on the host"""
Expand Down Expand Up @@ -185,7 +190,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 @@ -194,20 +199,35 @@ 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
path = None
for i in Path("/mnt/c/Users").glob(f"*/{ConstantConfig.docker_desktop_windows_config_short_path}"):
path = i
logger.debug(f"Docker desktop config found at {path}")
break
if path is None:
return {}
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
11 changes: 7 additions & 4 deletions exegol/model/ContainerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,10 +838,13 @@ def setNetworkMode(self, host_mode: Optional[bool]):
logger.warning("Host mode cannot be set with NAT ports configured. Disabling the shared network mode.")
host_mode = False
if EnvInfo.isDockerDesktop() and host_mode:
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.")
host_mode = False
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.")
host_mode = False
else:
logger.warning("Docker desktop host network mode is enabled but in beta. Everything might not work as you expect.")
self.__network_host = host_mode

def setPrivileged(self, status: bool = True):
Expand Down
1 change: 1 addition & 0 deletions exegol/utils/GuiUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,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 5f980bf

Please sign in to comment.