Skip to content

Commit 11e6840

Browse files
committed
feat: Add param "host_temp_dir" to get_source() and get_destination() methods (docker in docker compatibility)
1 parent ee5cb2d commit 11e6840

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

airbyte/_executors/docker.py

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from airbyte import exceptions as exc
1010
from airbyte._executors.base import Executor
11+
from airbyte.constants import TEMP_DIR
1112

1213

1314
logger = logging.getLogger("airbyte")
@@ -76,6 +77,11 @@ def map_cli_args(self, args: list[str]) -> list[str]:
7677
# This is a file path and we need to map it to the same file within the
7778
# relative path of the file within the container's volume.
7879
for local_volume, container_path in self.volumes.items():
80+
# If the container path corresponds to the container's temporary directory,
81+
# then the local temporary directory path is used (as the local volume
82+
# path can be overridden).
83+
if container_path == DEFAULT_AIRBYTE_CONTAINER_TEMP_DIR:
84+
local_volume = TEMP_DIR
7985
if Path(arg).is_relative_to(local_volume):
8086
logger.debug(
8187
f"Found file input path `{arg}` "

airbyte/_executors/util.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
22
from __future__ import annotations
33

4-
import tempfile
54
from pathlib import Path
65
from typing import TYPE_CHECKING, Literal, cast
76

@@ -16,7 +15,7 @@
1615
from airbyte._executors.python import VenvExecutor
1716
from airbyte._util.meta import which
1817
from airbyte._util.telemetry import EventState, log_install_state # Non-public API
19-
from airbyte.constants import AIRBYTE_OFFLINE_MODE, TEMP_DIR_OVERRIDE
18+
from airbyte.constants import AIRBYTE_OFFLINE_MODE, TEMP_DIR
2019
from airbyte.sources.registry import ConnectorMetadata, InstallType, get_connector_metadata
2120
from airbyte.version import get_version
2221

@@ -132,6 +131,7 @@ def get_connector_executor( # noqa: PLR0912, PLR0913, PLR0915 # Too many branch
132131
local_executable: Path | str | None = None,
133132
docker_image: bool | str | None = None,
134133
use_host_network: bool = False,
134+
host_temp_dir: Path | str | None = None,
135135
source_manifest: bool | dict | Path | str | None = None,
136136
install_if_missing: bool = True,
137137
install_root: Path | None = None,
@@ -226,7 +226,9 @@ def get_connector_executor( # noqa: PLR0912, PLR0913, PLR0915 # Too many branch
226226
if ":" not in docker_image:
227227
docker_image = f"{docker_image}:{version or 'latest'}"
228228

229-
host_temp_dir = TEMP_DIR_OVERRIDE or Path(tempfile.gettempdir())
229+
if not host_temp_dir:
230+
host_temp_dir = TEMP_DIR
231+
230232
container_temp_dir = DEFAULT_AIRBYTE_CONTAINER_TEMP_DIR
231233

232234
local_mount_dir = Path().absolute() / name

airbyte/_util/temp_files.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pathlib import Path
1313
from typing import TYPE_CHECKING, Any
1414

15-
from airbyte.constants import TEMP_DIR_OVERRIDE
15+
from airbyte.constants import TEMP_DIR
1616

1717

1818
if TYPE_CHECKING:
@@ -30,7 +30,7 @@ def as_temp_files(files_contents: list[dict | str]) -> Generator[list[str], Any,
3030
mode="w+t",
3131
delete=False,
3232
encoding="utf-8",
33-
dir=TEMP_DIR_OVERRIDE or None,
33+
dir=TEMP_DIR,
3434
suffix=".json" if use_json else ".txt",
3535
)
3636
temp_file.write(

airbyte/constants.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import annotations
55

66
import os
7+
import tempfile
78
from pathlib import Path
89

910

@@ -64,9 +65,8 @@ def _str_to_bool(value: str) -> bool:
6465
"""Convert a string value of an environment values to a boolean value."""
6566
return bool(value) and value.lower() not in {"", "0", "false", "f", "no", "n", "off"}
6667

67-
68-
TEMP_DIR_OVERRIDE: Path | None = (
69-
Path(os.environ["AIRBYTE_TEMP_DIR"]) if os.getenv("AIRBYTE_TEMP_DIR") else None
68+
TEMP_DIR: Path = Path(
69+
os.environ["AIRBYTE_TEMP_DIR"] if os.getenv("AIRBYTE_TEMP_DIR") else tempfile.gettempdir()
7070
)
7171
"""The directory to use for temporary files.
7272

airbyte/destinations/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def get_destination( # noqa: PLR0913 # Too many arguments
2828
local_executable: Path | str | None = None,
2929
docker_image: str | bool | None = None,
3030
use_host_network: bool = False,
31+
host_temp_dir: Path | str | None = None,
3132
install_if_missing: bool = True,
3233
) -> Destination:
3334
"""Get a connector by name and version.
@@ -56,6 +57,9 @@ def get_destination( # noqa: PLR0913 # Too many arguments
5657
the host network. This is useful for connectors that need to access resources on
5758
the host machine, such as a local database. This parameter is ignored when
5859
`docker_image` is not set.
60+
host_temp_dir: If set, along with docker_image, this replaces the volume exposing the
61+
temporary files directory, ensuring compatibility when the Docker engine runs on a
62+
different host (e.g., Docker in Docker), where paths may differ.
5963
install_if_missing: Whether to install the connector if it is not available locally. This
6064
parameter is ignored when local_executable is set.
6165
"""
@@ -70,6 +74,7 @@ def get_destination( # noqa: PLR0913 # Too many arguments
7074
local_executable=local_executable,
7175
docker_image=docker_image,
7276
use_host_network=use_host_network,
77+
host_temp_dir=host_temp_dir,
7378
install_if_missing=install_if_missing,
7479
),
7580
)

airbyte/sources/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def get_source( # noqa: PLR0913 # Too many arguments
5555
local_executable: Path | str | None = None,
5656
docker_image: bool | str | None = None,
5757
use_host_network: bool = False,
58+
host_temp_dir: Path | str | None = None,
5859
source_manifest: bool | dict | Path | str | None = None,
5960
install_if_missing: bool = True,
6061
install_root: Path | None = None,
@@ -95,6 +96,9 @@ def get_source( # noqa: PLR0913 # Too many arguments
9596
the host network. This is useful for connectors that need to access resources on
9697
the host machine, such as a local database. This parameter is ignored when
9798
`docker_image` is not set.
99+
host_temp_dir: If set, along with docker_image, this replaces the volume exposing the
100+
temporary files directory, ensuring compatibility when the Docker engine runs on a
101+
different host (e.g., Docker in Docker), where paths may differ.
98102
source_manifest: If set, the connector will be executed based on a declarative YAML
99103
source definition. This input can be `True` to attempt to auto-download a YAML spec,
100104
`dict` to accept a Python dictionary as the manifest, `Path` to pull a manifest from
@@ -116,6 +120,7 @@ def get_source( # noqa: PLR0913 # Too many arguments
116120
local_executable=local_executable,
117121
docker_image=docker_image,
118122
use_host_network=use_host_network,
123+
host_temp_dir=host_temp_dir,
119124
source_manifest=source_manifest,
120125
install_if_missing=install_if_missing,
121126
install_root=install_root,

0 commit comments

Comments
 (0)