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

Apt proxy fallback #602

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ The following environment variables are supported:
docker-compose up -d
echo 'APT_PROXY=http://172.17.0.1:3142' >> config

* `APT_PROXY_FALLBACK` (Default: unset)

Setting this option to `1` will make apt fallback to direct connection when proxy connection fails.

* `BASE_DIR` (Default: location of `build.sh`)

**CAUTION**: Currently, changing this value will probably break build.sh
Expand Down
9 changes: 6 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export PUBKEY_SSH_FIRST_USER
export CLEAN
export IMG_NAME
export APT_PROXY
export APT_PROXY_FALLBACK

export STAGE
export STAGE_DIR
Expand Down Expand Up @@ -280,9 +281,11 @@ if [[ ! "$FIRST_USER_NAME" =~ ^[a-z][-a-z0-9_]*$ ]]; then
exit 1
fi

if [[ -n "${APT_PROXY}" ]] && ! curl --silent "${APT_PROXY}" >/dev/null ; then
echo "Could not reach APT_PROXY server: ${APT_PROXY}"
exit 1
if ! proxy_check; then
if [[ "${APT_PROXY_FALLBACK}" != "1" ]]; then
echo "Could not reach APT_PROXY server: ${APT_PROXY}"
exit 1
fi
fi

if [[ -n "${WPA_PASSWORD}" && ${#WPA_PASSWORD} -lt 8 || ${#WPA_PASSWORD} -gt 63 ]] ; then
Expand Down
2 changes: 2 additions & 0 deletions export-image/01-set-sources/01-run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash -e

rm -f "${ROOTFS_DIR}/etc/apt/apt.conf.d/51cache"
rm -f "${ROOTFS_DIR}/etc/apt/apt.conf.d/52fallback"
rm -f "${ROOTFS_DIR}/bin/detect-proxy"
find "${ROOTFS_DIR}/var/lib/apt/lists/" -type f -delete
on_chroot << EOF
apt-get update
Expand Down
16 changes: 14 additions & 2 deletions scripts/common
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ bootstrap(){
local BOOTSTRAP_CMD=debootstrap
local BOOTSTRAP_ARGS=()

export http_proxy=${APT_PROXY}

if proxy_check; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, if proxy_check fails, this seems to be disabling it regardless of whether APT_PROXY_FALLBACK is set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since bootstrap is one of the earliest operations that fetch files, checking proxy in build.sh shadowed the bug in my test environment, I think. Fixed now.

export http_proxy=${APT_PROXY}
fi

BOOTSTRAP_ARGS+=(--arch armhf)
BOOTSTRAP_ARGS+=(--components "main,contrib,non-free")
BOOTSTRAP_ARGS+=(--keyring "${STAGE_DIR}/files/raspberrypi.gpg")
BOOTSTRAP_ARGS+=(--exclude=info)
BOOTSTRAP_ARGS+=(--include=ca-certificates)
BOOTSTRAP_ARGS+=(--include=curl)
BOOTSTRAP_ARGS+=("$@")
printf -v BOOTSTRAP_STR '%q ' "${BOOTSTRAP_ARGS[@]}"

Expand Down Expand Up @@ -98,3 +101,12 @@ update_issue() {
echo -e "Raspberry Pi reference ${IMG_DATE}\nGenerated using ${PI_GEN}, ${PI_GEN_REPO}, ${GIT_HASH}, ${1}" > "${ROOTFS_DIR}/etc/rpi-issue"
}
export -f update_issue

proxy_check(){
if [[ -n "${APT_PROXY}" ]] && ! curl --silent "${APT_PROXY}" >/dev/null ; then
return 1
else
return 0
fi
}
export -f proxy_check
15 changes: 13 additions & 2 deletions stage0/00-configure-apt/00-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ sed -i "s/RELEASE/${RELEASE}/g" "${ROOTFS_DIR}/etc/apt/sources.list"
sed -i "s/RELEASE/${RELEASE}/g" "${ROOTFS_DIR}/etc/apt/sources.list.d/raspi.list"

if [ -n "$APT_PROXY" ]; then
install -m 644 files/51cache "${ROOTFS_DIR}/etc/apt/apt.conf.d/51cache"
sed "${ROOTFS_DIR}/etc/apt/apt.conf.d/51cache" -i -e "s|APT_PROXY|${APT_PROXY}|"
if [ "$APT_PROXY_FALLBACK" == "1" ]; then
install -m 644 files/52fallback "${ROOTFS_DIR}/etc/apt/apt.conf.d/52fallback"
install -m 755 files/detect-proxy "${ROOTFS_DIR}/bin/detect-proxy"
sed "${ROOTFS_DIR}/bin/detect-proxy" -i -e "s|APT_PROXY|${APT_PROXY}|"
else
install -m 644 files/51cache "${ROOTFS_DIR}/etc/apt/apt.conf.d/51cache"
sed "${ROOTFS_DIR}/etc/apt/apt.conf.d/51cache" -i -e "s|APT_PROXY|${APT_PROXY}|"

rm -f "${ROOTFS_DIR}/etc/apt/apt.conf.d/52fallback"
rm -f "${ROOTFS_DIR}/bin/detect-proxy"
fi
else
rm -f "${ROOTFS_DIR}/etc/apt/apt.conf.d/51cache"
rm -f "${ROOTFS_DIR}/etc/apt/apt.conf.d/52fallback"
rm -f "${ROOTFS_DIR}/bin/detect-proxy"
fi

cat files/raspberrypi.gpg.key | gpg --dearmor > "${ROOTFS_DIR}/etc/apt/trusted.gpg.d/raspberrypi-archive-stable.gpg"
Expand Down
1 change: 1 addition & 0 deletions stage0/00-configure-apt/files/52fallback
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Acquire::http::Proxy-Auto-Detect "/bin/detect-proxy";
6 changes: 6 additions & 0 deletions stage0/00-configure-apt/files/detect-proxy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
if curl --silent "APT_PROXY" >/dev/null; then
echo -n "APT_PROXY"
else
echo -n "DIRECT"
fi