Skip to content

Commit

Permalink
[FIX] Changing env to mountable path - PRIVATE_REGISTRY_CREDENTIAL_PA…
Browse files Browse the repository at this point in the history
…TH (#292)

* Changing env to mounting file

* Exception handling for missing files

* Env changes

* Context manager for file handling

* Exception handling
  • Loading branch information
harini-venkataraman authored Apr 30, 2024
1 parent 9d49cdb commit 42d784d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion worker/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ TOOL_CONTAINER_LABELS=""

WORKFLOW_DATA_DIR="${PWD}/workflow_data/execution"
TOOL_DATA_DIR="/data"
PRIVATE_REGISTRY_PASSWORD={}
PRIVATE_REGISTRY_CREDENTIAL_PATH=
PRIVATE_REGISTRY_USERNAME=
PRIVATE_REGISTRY_URL=
2 changes: 1 addition & 1 deletion worker/src/unstract/worker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class Env:
TOOL_CONTAINER_LABELS = "TOOL_CONTAINER_LABELS"
WORKFLOW_DATA_DIR = "WORKFLOW_DATA_DIR"
TOOL_DATA_DIR = "TOOL_DATA_DIR"
PRIVATE_REGISTRY_PASSWORD = "PRIVATE_REGISTRY_PASSWORD"
PRIVATE_REGISTRY_CREDENTIAL_PATH = "PRIVATE_REGISTRY_CREDENTIAL_PATH"
PRIVATE_REGISTRY_USERNAME = "PRIVATE_REGISTRY_USERNAME"
PRIVATE_REGISTRY_URL = "PRIVATE_REGISTRY_URL"
31 changes: 24 additions & 7 deletions worker/src/unstract/worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
from typing import Any, Optional

from docker.errors import APIError
from dotenv import load_dotenv
from unstract.worker.constants import Env, LogType, ToolKey

Expand All @@ -26,19 +27,35 @@ def __init__(self, image_name: str, image_tag: str) -> None:
# the Docker daemon in the host environment
self.client: DockerClient = docker.from_env() # type: ignore[attr-defined] # noqa: E501

private_registry_password = os.getenv(Env.PRIVATE_REGISTRY_PASSWORD)
private_registry_credential_path = os.getenv(
Env.PRIVATE_REGISTRY_CREDENTIAL_PATH
)
private_registry_username = os.getenv(Env.PRIVATE_REGISTRY_USERNAME)
private_registry_url = os.getenv(Env.PRIVATE_REGISTRY_URL)
if (
private_registry_password
private_registry_credential_path
and private_registry_username
and private_registry_url
):
self.client.login(
username=private_registry_username,
password=private_registry_password,
registry=private_registry_url,
)
try:
with open(private_registry_credential_path, encoding="utf-8") as file:
password = file.read()
self.client.login(
username=private_registry_username,
password=password,
registry=private_registry_url,
)
except FileNotFoundError as file_err:
logger.error(
f"Service account key file is not mounted "
f"in {private_registry_credential_path}: {file_err}"
"Logging to private registry might fail, if private tool is used."
)
except APIError as api_err:
logger.error(
f"Exception occured while invoking docker client : {api_err}."
f"Authentication to artifact registry failed."
)

self.image = self._get_image()

Expand Down

0 comments on commit 42d784d

Please sign in to comment.