Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker): Avoid rebuilding images available #160

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions odtp/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import json
import subprocess
import docker
import odtp.helpers.settings as config
import odtp.helpers.git as git_helpers
import odtp.helpers.environment as env_helpers
Expand Down Expand Up @@ -38,8 +39,9 @@ def __init__(self, repo_url="", commit_hash="", image_name="", project_folder=""
def prepare_component(self):
self._checks_for_prepare()
self._create_project_folder_structure()
self._download_repo()
self._build_image()
if not self._check_if_image_exists():
self._download_repo()
self._build_image()

def _create_project_folder_structure(self):
"""Create all the folder structure in project_folder"""
Expand Down Expand Up @@ -83,6 +85,20 @@ def _checks_for_run(self, parameters, ports, image_name):
db_utils.check_port_mappings_for_component_runs(ports)
self._check_image_exists()

def _check_if_image_exists(self):
"""
Check whether a docker image exists
"""
logging.info(f"VALIDATION: Checking if Docker image exists: {self.docker_image_name}")
client = docker.from_env()
images = client.images.list(name=self.docker_image_name)
logging.info(f"Images found: {images}")

if len(images) > 0:
return True
else:
return False

def _download_repo(self):
"""
Download a GitHub repository to the specified destination.
Expand Down Expand Up @@ -149,14 +165,14 @@ def _create_volume(self, volume_name):
log.info(f"RUN: Creating Docker volume {volume_name}")
subprocess.run(["docker", "volume", "create", volume_name])

def run_component(self, parameters, secrets, ports, instance_name, step_id=None, debug=False):
def run_component(self, parameters, secrets, ports, container_name, step_id=None, debug=False):
"""
Run a Docker component with the specified parameters.

Args:
secrets (dict): The secrets variables to pass to the Docker component.
parameters (dict): The environment variables to pass to the Docker component.
instance_name (str, optional): The name of the Docker container. Defaults to "odtp_component".
container_name (str, optional): The name of the Docker container. Defaults to "odtp_component".

Returns:
str: The ID of the Docker run.
Expand All @@ -183,7 +199,7 @@ def run_component(self, parameters, secrets, ports, instance_name, step_id=None,
else:
secrets_args = [""]

docker_run_command = ["docker", "run", "--rm", "-it", "--name", instance_name,
docker_run_command = ["docker", "run", "--rm", "-it", "--name", container_name,
"--network", "odtp_odtp-network",
"--volume", f"{os.path.abspath(self.input_volume)}:/odtp/odtp-input",
"--volume", f"{os.path.abspath(self.output_volume)}:/odtp/odtp-output"] + env_args + ports_args + secrets_args + [self.docker_image_name]
Expand Down
13 changes: 8 additions & 5 deletions odtp/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, execution_data, working_path, secrets):
self.image_names = []
self.repo_urls = []
self.commits = []
self.instance_names = []
self.container_names = []
self.steps_folder_paths = []
self.secrets = secrets

Expand Down Expand Up @@ -48,12 +48,15 @@ def __init__(self, execution_data, working_path, secrets):
step_folder_path = os.path.join(self.working_path, step_name)
self.steps_folder_paths.append(step_folder_path)

image_name = step_name
image_name = odtp_utils.get_execution_step_name(
sabinem marked this conversation as resolved.
Show resolved Hide resolved
component_name=component_name,
component_version=component_version
)

self.image_names.append(image_name)
self.repo_urls.append(repo_link)
self.commits.append(commit_hash)
self.instance_names.append(image_name)
self.container_names.append(step_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix: I think here it should be image_name:

self.container_names.append(image_name)

except Exception as e:
raise OdtpRunSetupException(
f"Workflowmanager could not be intialized: Exception occured: {e}"
Expand Down Expand Up @@ -159,7 +162,7 @@ def run_workflow(self):
# By now the image_name is just the name of the component and the version
componentManager = DockerManager(
repo_url=self.repo_urls[step_index],
image_name=self.image_names[step_index],
image_name=self.image_names[step_index],
project_folder=self.steps_folder_paths[step_index]
)

Expand All @@ -170,7 +173,7 @@ def run_workflow(self):
parameters,
secrets,
ports=ports,
instance_name=self.instance_names[step_index],
container_name=self.container_names[step_index],
step_id=self.execution["steps"][step_index]
)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pygwalker = "^0.3.17"
streamlit-aggrid = "^0.3.4.post3"
nicegui = "1.4.24"
directory-tree = "^0.0.4"
docker = "7.1.0"
python-slugify = "^8.0.4"

[tool.poetry.group.dev.dependencies]
Expand Down