Skip to content

Commit 5b6efaf

Browse files
pavelzwbeckermr
andauthored
Add proxy mode to run_container_operation (#34)
* Add proxy mode to `run_container_operation` * add proxy_in_container * format --------- Co-authored-by: Matthew R. Becker <[email protected]>
1 parent ce28c90 commit 5b6efaf

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

conda_forge_feedstock_ops/container_utils.py

+43
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212

1313
DEFAULT_CONTAINER_TMPFS_SIZE_MB = 6000
1414

15+
CONTAINER_PROXY_MODE = os.environ.get(
16+
"CF_FEEDSTOCK_OPS_CONTAINER_PROXY_MODE", "false"
17+
).lower() in ("yes", "true", "t", "1")
18+
"""
19+
Whether to use a proxy that is locally configured for all requests inside the container.
20+
Set the environment variable `CF_FEEDSTOCK_OPS_CONTAINER_PROXY_MODE` to 'true' to enable this feature.
21+
"""
22+
23+
PROXY_IN_CONTAINER = os.environ.get(
24+
"CF_FEEDSTOCK_OPS_PROXY_IN_CONTAINER", "http://host.docker.internal:8080"
25+
)
26+
"""
27+
The hostname of the proxy to use in the container.
28+
The default value of 'http://host.docker.internal:8080' is the default value for Docker Desktop on Windows and macOS.
29+
It also works for OrbStack.
30+
31+
For podman, use http://host.containers.internal:8080.
32+
For GitHub Actions, use http://172.17.0.1:8080, see https://stackoverflow.com/a/65505308
33+
"""
34+
1535

1636
def get_default_container_name():
1737
"""Get the default container name for feedstock ops.
@@ -97,6 +117,28 @@ def get_default_log_level_args(logger):
97117
]
98118

99119

120+
def _get_proxy_mode_container_args():
121+
if not CONTAINER_PROXY_MODE:
122+
return []
123+
assert os.environ["SSL_CERT_FILE"] == os.environ["REQUESTS_CA_BUNDLE"]
124+
return [
125+
"-e",
126+
f"http_proxy={PROXY_IN_CONTAINER}",
127+
"-e",
128+
f"https_proxy={PROXY_IN_CONTAINER}",
129+
"-e",
130+
f"no_proxy={os.environ.get('no_proxy', '')}",
131+
"-e",
132+
"SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt",
133+
"-e",
134+
"REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt",
135+
"--network",
136+
"host",
137+
"-v",
138+
f"{os.environ['SSL_CERT_FILE']}:/etc/ssl/certs/ca-certificates.crt:ro",
139+
]
140+
141+
100142
def run_container_operation(
101143
args: Iterable[str],
102144
json_loads: Callable = json.loads,
@@ -146,6 +188,7 @@ def run_container_operation(
146188
cmd = [
147189
*get_default_container_run_args(tmpfs_size_mb=tmpfs_size_mb),
148190
*mnt_args,
191+
*_get_proxy_mode_container_args(),
149192
*extra_container_args,
150193
get_default_container_name(),
151194
*args,

0 commit comments

Comments
 (0)