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

Add proxy mode to run_container_operation #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions conda_forge_feedstock_ops/container_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@

DEFAULT_CONTAINER_TMPFS_SIZE_MB = 6000

CONTAINER_PROXY_MODE = os.environ.get(
"CF_FEEDSTOCK_OPS_CONTAINER_PROXY_MODE", "false"
).lower() in ("yes", "true", "t", "1")
"""
Whether to use a proxy that is locally configured for all requests inside the container.
Set the environment variable `CF_FEEDSTOCK_OPS_CONTAINER_PROXY_MODE` to 'true' to enable this feature.
"""

PROXY_IN_CONTAINER = os.environ.get(
"CF_FEEDSTOCK_OPS_PROXY_IN_CONTAINER", "http://host.docker.internal:8080"
)
"""
The hostname of the proxy to use in the container.
The default value of 'http://host.docker.internal:8080' is the default value for Docker Desktop on Windows and macOS.
It also works for OrbStack.

For podman, use http://host.containers.internal:8080.
For GitHub Actions, use http://172.17.0.1:8080, see https://stackoverflow.com/a/65505308
"""


def get_default_container_name():
"""Get the default container name for feedstock ops.
Expand Down Expand Up @@ -97,6 +117,28 @@ def get_default_log_level_args(logger):
]


def _get_proxy_mode_container_args():
if not CONTAINER_PROXY_MODE:
return []
assert os.environ["SSL_CERT_FILE"] == os.environ["REQUESTS_CA_BUNDLE"]
return [
"-e",
f"http_proxy={PROXY_IN_CONTAINER}",
"-e",
f"https_proxy={PROXY_IN_CONTAINER}",
"-e",
f"no_proxy={os.environ.get('no_proxy', '')}",
"-e",
"SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt",
"-e",
"REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt",
"--network",
"host",
"-v",
f"{os.environ['SSL_CERT_FILE']}:/etc/ssl/certs/ca-certificates.crt:ro",
]


def run_container_operation(
args: Iterable[str],
json_loads: Callable = json.loads,
Expand Down Expand Up @@ -146,6 +188,7 @@ def run_container_operation(
cmd = [
*get_default_container_run_args(tmpfs_size_mb=tmpfs_size_mb),
*mnt_args,
*_get_proxy_mode_container_args(),
*extra_container_args,
get_default_container_name(),
*args,
Expand Down
Loading