Skip to content

Commit 12db66f

Browse files
committed
build: Add robust proxy handling
- Add proxy handling for apt in different enviroments - fix non proxy env invalid error Signed-off-by: Nabendu Maiti <nabendu.bikash.maiti@intel.com>
1 parent 80bad96 commit 12db66f

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

.devcontainer/devcontainer.json

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,21 @@
1111
4200
1212
],
1313
"containerEnv": {
14-
"HTTP_PROXY": "${localEnv:http_proxy}",
15-
"HTTPS_PROXY": "${localEnv:https_proxy}",
16-
"NO_PROXY": "${localEnv:no_proxy}",
17-
"http_proxy": "${localEnv:http_proxy}",
18-
"https_proxy": "${localEnv:https_proxy}",
19-
"no_proxy": "${localEnv:no_proxy}"
14+
"HTTP_PROXY": "${localEnv:HTTP_PROXY:}",
15+
"HTTPS_PROXY": "${localEnv:HTTPS_PROXY:}",
16+
"NO_PROXY": "${localEnv:NO_PROXY:}",
17+
"http_proxy": "${localEnv:http_proxy:}",
18+
"https_proxy": "${localEnv:https_proxy:}",
19+
"no_proxy": "${localEnv:no_proxy:}"
2020
},
21-
"runArgs": [
22-
"--env",
23-
"HTTP_PROXY=${env:http_proxy:-}",
24-
"--env",
25-
"HTTPS_PROXY=${env:https_proxy:-}",
26-
"--env",
27-
"NO_PROXY=${env:no_proxy:-}",
28-
"--env",
29-
"http_proxy=${env:http_proxy:-}",
30-
"--env",
31-
"https_proxy=${env:https_proxy:-}",
32-
"--env",
33-
"no_proxy=${env:no_proxy:-}"
34-
],
35-
3621
"portsAttributes": {
3722
"4200": {
3823
"label": "Angular Dev Server",
3924
"onAutoForward": "notify"
4025
}
4126
},
42-
// Use 'onCreateCommand' to run commands once when the container is created.
43-
"onCreateCommand": "if [ -n \"$HTTP_PROXY\" ] || [ -n \"$HTTPS_PROXY\" ]; then sudo bash -c \"echo 'Acquire::http::Proxy \\\"'$HTTP_PROXY'\\\";' > /etc/apt/apt.conf.d/proxy.conf && echo 'Acquire::https::Proxy \\\"'$HTTPS_PROXY'\\\";' >> /etc/apt/apt.conf.d/proxy.conf\"; fi",
4427
// Use 'postCreateCommand' to run commands after the container is created.
45-
"postCreateCommand": "/bin/bash .devcontainer/post-create.sh"
28+
"postCreateCommand": "sed -i 's/\\r$//' .devcontainer/post-create.sh && /bin/bash .devcontainer/post-create.sh"
4629
// Configure tool-specific properties.
4730
// "customizations": {},
4831
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.

.devcontainer/post-create.sh

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#!/bin/bash
32
set -e
43

@@ -8,19 +7,29 @@ for var in HTTP_PROXY HTTPS_PROXY NO_PROXY http_proxy https_proxy no_proxy; do
87
if [ -z "$v" ]; then unset $var; fi
98
done
109

11-
# Configure apt proxy if proxies are present
10+
# Strip all trailing '/' or '\\' from proxy URLs for apt config
11+
strip_trailing_slash() {
12+
local url="$1"
13+
# Remove all trailing / or \
14+
url="${url%%*(/|\\)}"
15+
# Fallback for Bash < 4.0 (no extglob): use sed
16+
echo "$url" | sed 's%[\\/]*$%%'
17+
}
18+
1219
if [ -n "$HTTP_PROXY" ] || [ -n "$http_proxy" ] || [ -n "$HTTPS_PROXY" ] || [ -n "$https_proxy" ]; then
1320
echo "Configuring apt to use proxy..."
1421
sudo mkdir -p /etc/apt/apt.conf.d
22+
# Remove all trailing / or \\ from proxy URLs
23+
apt_http_proxy="$(strip_trailing_slash "${HTTP_PROXY:-${http_proxy:-}}")"
24+
apt_https_proxy="$(strip_trailing_slash "${HTTPS_PROXY:-${https_proxy:-}}")"
1525
sudo tee /etc/apt/apt.conf.d/99proxy > /dev/null <<EOF
16-
Acquire::http::Proxy \"${HTTP_PROXY:-${http_proxy:-}}\";
17-
Acquire::https::Proxy \"${HTTPS_PROXY:-${https_proxy:-}}\";
26+
Acquire::http::Proxy "$apt_http_proxy";
27+
Acquire::https::Proxy "$apt_https_proxy";
1828
EOF
1929
fi
2030

2131
npm install
2232

23-
2433
sudo apt-get update
2534
sudo apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libnss3 libxss1 libasound2 libxtst6 xauth xvfb
2635
npm install

0 commit comments

Comments
 (0)