Skip to content

Commit 976c340

Browse files
committed
Improve proxy handling for builds
Signed-off-by: Jonas Wood <jw@ti.com>
1 parent 0ce3fd5 commit 976c340

4 files changed

Lines changed: 57 additions & 12 deletions

File tree

lib/functions/configuration/main-config.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,23 @@ function do_extra_configuration() {
455455
APT_MIRROR=$UBUNTU_MIRROR
456456
fi
457457

458-
[[ -n "${APT_PROXY_ADDR}" ]] && display_alert "Using custom apt proxy address" "APT_PROXY_ADDR=${APT_PROXY_ADDR}" "info"
458+
# Derive APT_PROXY_ADDR from proxy env vars if unset, which runners.sh uses inside chroot.
459+
# Skip if MANAGE_ACNG is active to prevent conflicting behavior.
460+
if [[ -z "${APT_PROXY_ADDR}" && -n "${http_proxy:-${https_proxy:-${HTTP_PROXY:-${HTTPS_PROXY:-}}}}" && ( -z "${MANAGE_ACNG}" || "${MANAGE_ACNG}" == "no" ) ]]; then
461+
APT_PROXY_ADDR="$(echo "${http_proxy:-${https_proxy:-${HTTP_PROXY:-${HTTPS_PROXY:-}}}}" | sed -E 's|https?://([^/]+).*|\1|')"
462+
display_alert "Derived APT proxy address from proxy env vars" "${APT_PROXY_ADDR##*@}" "info"
463+
fi
464+
[[ -n "${APT_PROXY_ADDR}" ]] && display_alert "Using custom apt proxy address" "APT_PROXY_ADDR=${APT_PROXY_ADDR##*@}" "info"
459465

460466
# @TODO: allow to run aggregation, for CONFIG_DEFS_ONLY? rootfs_aggregate_packages
461467

462-
# Give the option to configure DNS server used in the chroot during the build process
463-
[[ -z $NAMESERVER ]] && NAMESERVER="1.0.0.1" # default is cloudflare alternate
468+
# Derive host DNS server so chroot can resolve hostnames on proxy; else, use cloudflare
469+
if [[ -z "${NAMESERVER}" ]]; then
470+
declare _dns_resolv_file="/etc/resolv.conf"
471+
[[ -f "/run/systemd/resolve/resolv.conf" ]] && _dns_resolv_file="/run/systemd/resolve/resolv.conf"
472+
NAMESERVER="$(awk '(/^nameserver/) && ($2 !~ /^127\./) && ($2 != "::1") && ($2 !~ /^fe80:/) {print $2; exit}' "${_dns_resolv_file}" 2>/dev/null)"
473+
NAMESERVER="${NAMESERVER:-1.0.0.1}"
474+
fi
464475

465476
# Consolidate the extra image suffix. loop and add.
466477
declare EXTRA_IMAGE_SUFFIX=""

lib/functions/general/oci-oras.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,21 @@ function run_tool_oras() {
8787

8888
# Run oras, possibly with retries...
8989
declare ORAS_HOME="${HOME:-"${TMPDIR}"}" # oras _requires_ a HOME to work atleast in 1.2+
90+
declare -a oras_proxy_env=(
91+
"http_proxy=${http_proxy:-${HTTP_PROXY:-}}"
92+
"https_proxy=${https_proxy:-${HTTPS_PROXY:-}}"
93+
"HTTP_PROXY=${HTTP_PROXY:-${http_proxy:-}}"
94+
"HTTPS_PROXY=${HTTPS_PROXY:-${https_proxy:-}}"
95+
"ftp_proxy=${ftp_proxy:-${FTP_PROXY:-}}"
96+
"FTP_PROXY=${FTP_PROXY:-${ftp_proxy:-}}"
97+
"no_proxy=${no_proxy:-${NO_PROXY:-}}"
98+
"NO_PROXY=${NO_PROXY:-${no_proxy:-}}"
99+
"APT_PROXY_ADDR=${APT_PROXY_ADDR:-}"
100+
)
90101
display_alert "Running ORAS ${ACTUAL_VERSION}" "HOME='${ORAS_HOME}'; retries='${retries:-1}'; cmdline: $*" "debug"
91102
if [[ "${retries:-1}" -gt 1 ]]; then
92103
display_alert "Calling ORAS with retries ${retries}" "$*" "debug"
93-
sleep_seconds="30" do_with_retries "${retries}" env -i "HOME=${ORAS_HOME}" "HTTPS_PROXY=${HTTPS_PROXY}" "${ORAS_BIN}" "$@"
104+
sleep_seconds="30" do_with_retries "${retries}" env -i "HOME=${ORAS_HOME}" "${oras_proxy_env[@]}" "${ORAS_BIN}" "$@"
94105
else
95106
# If any parameters passed, call ORAS, otherwise exit. We call it this way (sans-parameters) early to prepare ORAS tooling.
96107
if [[ $# -eq 0 ]]; then
@@ -99,7 +110,7 @@ function run_tool_oras() {
99110
fi
100111

101112
display_alert "Calling ORAS" "$*" "debug"
102-
env -i "HOME=${ORAS_HOME}" "${ORAS_BIN}" "$@"
113+
env -i "HOME=${ORAS_HOME}" "${oras_proxy_env[@]}" "${ORAS_BIN}" "$@"
103114
fi
104115
}
105116

lib/functions/general/python-tools.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,22 @@ function prepare_python_and_pip() {
126126

127127
# Install pip, using get-pip.py; that bootstraps pip using an embedded, temporary, pip contained in get-pip.py
128128
display_alert "Installing pip using get-pip.py" "${pip3_version_number}" "info"
129-
run_host_command_logged env -i "${PYTHON3_VARS[@]@Q}" "${PYTHON3_INFO[BIN]}" "${PYTHON3_INFO[GET_PIP_BIN]}" "${pip3_extra_args[@]}" "pip==${pip3_version_number}"
129+
declare -a python_proxy_env=(
130+
"http_proxy=${http_proxy:-${HTTP_PROXY:-}}"
131+
"https_proxy=${https_proxy:-${HTTPS_PROXY:-}}"
132+
"HTTP_PROXY=${HTTP_PROXY:-${http_proxy:-}}"
133+
"HTTPS_PROXY=${HTTPS_PROXY:-${https_proxy:-}}"
134+
"ftp_proxy=${ftp_proxy:-${FTP_PROXY:-}}"
135+
"FTP_PROXY=${FTP_PROXY:-${ftp_proxy:-}}"
136+
"no_proxy=${no_proxy:-${NO_PROXY:-}}"
137+
"NO_PROXY=${NO_PROXY:-${no_proxy:-}}"
138+
"APT_PROXY_ADDR=${APT_PROXY_ADDR:-}"
139+
)
140+
run_host_command_logged env -i "${python_proxy_env[@]@Q}" "${PYTHON3_VARS[@]@Q}" "${PYTHON3_INFO[BIN]}" "${PYTHON3_INFO[GET_PIP_BIN]}" "${pip3_extra_args[@]}" "pip==${pip3_version_number}"
130141

131142
# Install the dependencies
132143
display_alert "Installing Python dependencies" "from ${python3_pip_dependencies_path}" "info"
133-
run_host_command_logged env -i "${PYTHON3_VARS[@]@Q}" "${PYTHON3_INFO[BIN]}" -m pip install "${pip3_extra_args[@]}" -r "${python3_pip_dependencies_path}"
144+
run_host_command_logged env -i "${python_proxy_env[@]@Q}" "${PYTHON3_VARS[@]@Q}" "${PYTHON3_INFO[BIN]}" -m pip install "${pip3_extra_args[@]}" -r "${python3_pip_dependencies_path}"
134145

135146
# Create the hash file
136147
run_host_command_logged touch "${python_hash_file}"

lib/functions/host/docker.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,25 @@ function docker_cli_prepare_launch() {
442442
"--env" "GITHUB_WORKSPACE=${GITHUB_WORKSPACE}"
443443

444444
# Pass proxy args
445-
"--env" "http_proxy=${http_proxy:-${HTTP_PROXY}}"
446-
"--env" "https_proxy=${https_proxy:-${HTTPS_PROXY}}"
447-
"--env" "HTTP_PROXY=${HTTP_PROXY}"
448-
"--env" "HTTPS_PROXY=${HTTPS_PROXY}"
449-
"--env" "APT_PROXY_ADDR=${APT_PROXY_ADDR}"
445+
"--env" "http_proxy=${http_proxy:-${HTTP_PROXY:-}}"
446+
"--env" "https_proxy=${https_proxy:-${HTTPS_PROXY:-}}"
447+
"--env" "HTTP_PROXY=${HTTP_PROXY:-${http_proxy:-}}"
448+
"--env" "HTTPS_PROXY=${HTTPS_PROXY:-${https_proxy:-}}"
449+
"--env" "ftp_proxy=${ftp_proxy:-${FTP_PROXY:-}}"
450+
"--env" "FTP_PROXY=${FTP_PROXY:-${ftp_proxy:-}}"
451+
"--env" "no_proxy=${no_proxy:-${NO_PROXY:-}}"
452+
"--env" "NO_PROXY=${NO_PROXY:-${no_proxy:-}}"
453+
"--env" "APT_PROXY_ADDR=${APT_PROXY_ADDR:-}"
450454
)
451455

456+
# Pass in host DNS server so container can resolve hostnames on proxy
457+
declare _dns_resolv_file="/etc/resolv.conf"
458+
[[ -f "/run/systemd/resolve/resolv.conf" ]] && _dns_resolv_file="/run/systemd/resolve/resolv.conf"
459+
while IFS= read -r _dns_server; do
460+
[[ "${_dns_server}" =~ ^127\. || "${_dns_server}" == "::1" || "${_dns_server}" =~ ^fe80: ]] && continue
461+
DOCKER_ARGS+=("--dns" "${_dns_server}")
462+
done < <(awk '/^nameserver/ {print $2}' "${_dns_resolv_file}" 2>/dev/null)
463+
452464
# DOCKER_PRIVILEGED=no switches to a narrow capability set.
453465
if [[ "${DOCKER_PRIVILEGED:-yes}" == "yes" ]]; then
454466
DOCKER_ARGS+=("--privileged")

0 commit comments

Comments
 (0)