Skip to content

Commit

Permalink
Managing hybrid environments: tool images can be uninstalled from rem…
Browse files Browse the repository at this point in the history
…ote hosts.
  • Loading branch information
janosmurai committed Feb 10, 2025
1 parent d9780d8 commit 140c57c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
13 changes: 7 additions & 6 deletions dem/core/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ def __init__(self, host_config: dict) -> None:
class Hosts(Core):
""" List of the available Hosts. """
def __init__(self) -> None:
""" Init the class with the host configurations.
Args:
config_file -- contains the host configurations
"""
""" Init the class with the host configurations. """
self.remotes: dict[str, Host] = {}

# The local host is always available.
local_host_config = {
"name": "local",
"address": "unix://var/run/docker.sock"
}
self.local = Host(local_host_config)
self.local.container_engine.start()

for host_config in self.config_file.hosts:
host = Host(host_config)
Expand Down Expand Up @@ -73,4 +71,7 @@ def get_host_by_name(self, host_name: str) -> Host | None:
Return with the host instance. If the host doesn't exist, return with None.
"""
return self.remotes.get(host_name, None)
if host_name == "local":
return self.local
else:
return self.remotes.get(host_name, None)
50 changes: 24 additions & 26 deletions dem/core/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,16 @@ def install_dev_env(self, dev_env_to_install: DevEnv) -> None:
Raises:
PlatformError -- if the install fails
"""
for task in dev_env_to_install.docker_task_descriptors:
tool_image_name: str = task["image"]
for task in dev_env_to_install.tasks:
tool_image_name: str = task.image
try:
tool_image = dev_env_to_install.assigned_tool_images[tool_image_name]
except KeyError:
raise PlatformError(f"The {tool_image_name} Tool Image is not assigned to the Development Environment.")

host_name = task["host_name"]
host = self.hosts.get_host_by_name(host_name)
host = self.hosts.get_host_by_name(task.host_name)
if host is None:
raise PlatformError(f"The {host_name} host is not available.")
raise PlatformError(f"The {task.host_name} host is not available.")

self.user_output.msg(f"\nPulling image {tool_image.name}", is_title=True)
try:
Expand Down Expand Up @@ -208,31 +207,30 @@ def uninstall_dev_env(self, dev_env_to_uninstall: DevEnv) -> Generator:
if not self.are_tool_images_assigned:
self.assign_tool_image_instances_to_all_dev_envs()

all_required_tool_images = set()
all_required_tool_images: dict[str, set] = {}

for dev_env in self.local_dev_envs:
if (dev_env is not dev_env_to_uninstall) and dev_env.is_installed:
for tool_image in dev_env.assigned_tool_images:
all_required_tool_images.add(tool_image.name)

tool_images_to_remove = set()
for tool_image in dev_env_to_uninstall.assigned_tool_images:
if tool_image.availability == ToolImage.NOT_AVAILABLE or tool_image.availability == ToolImage.REGISTRY_ONLY:
yield f"[yellow]Warning: The {tool_image.name} image could not be removed, because it is not available locally.[/]"
if dev_env is dev_env_to_uninstall or not dev_env.is_installed:
continue
for task in dev_env.tasks:
required_tool_images_per_host: set = all_required_tool_images.get(task.host.name, set())
required_tool_images_per_host.add(task.image)
all_required_tool_images[task.host.name] = required_tool_images_per_host

for task in dev_env_to_uninstall.tasks:
if task.host.name in all_required_tool_images.keys() and \
task.image in all_required_tool_images[task.host.name]:
continue

if tool_image.name not in all_required_tool_images:
tool_images_to_remove.add(tool_image.name)

# for tool_image_name in tool_images_to_remove:
# try:
# self.container_engine.remove(tool_image_name)
# except ContainerEngineError as e:
# raise PlatformError(f"Dev Env uninstall failed. --> {str(e)}")
# else:
# yield f"The {tool_image_name} image has been removed."
try:
task.host.container_engine.remove(task.image)
except ContainerEngineError as e:
raise PlatformError(f"Dev Env uninstall failed. --> {str(e)}")
else:
yield f"The {task.image} image has been removed."

# if dev_env_to_uninstall.enable_docker_network:
# self.container_engine.remove_network(dev_env_to_uninstall.name)
if dev_env_to_uninstall.enable_docker_network:
self.hosts.local.container_engine.remove_network(dev_env_to_uninstall.name)

dev_env_to_uninstall.is_installed = False
if self.default_dev_env_name == dev_env_to_uninstall.name:
Expand Down
6 changes: 4 additions & 2 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,10 @@ Development Environment.

Uninstall the selected Development Environment.

Sets the installed flag to False. DEM checks whether
a tool image is required or not by any of the remaining installed local Development Environments. In case the tool image is not required anymore, the DEM tries to delete it.
Sets the installed flag to False. DEM checks whether a tool image of the DevEnv is required or not
by any of the remaining installed Development Environments on the appropriate hosts. In case the
tool image is not required anymore by any other DevEnv, the DEM tries to delete it from the host
where it is installed.

**Arguments:**

Expand Down

0 comments on commit 140c57c

Please sign in to comment.