Skip to content

Commit c95fa9b

Browse files
committed
Handle no sudo, switch to poststart script
1 parent 5f1ea15 commit c95fa9b

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

src/add-host/NOTES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
## Changelog
3+
4+
| Version | Notes |
5+
| ------- | ------------------------------------------------------------------- |
6+
| 1.0.3 | Handle no sudo, switch to poststart script |
7+
| 1.0.1 | Initial version |

src/add-host/devcontainer-feature.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "add-host",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"name": "Add Host",
55
"description": "Add a hosts file entry in the dev container.",
66
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/add-host",
@@ -16,5 +16,7 @@
1616
"description": "The IP Address to associate with the host name"
1717
}
1818
},
19-
"postStartCommand": "sudo cp /etc/hosts /etc/hosts_old && cat /etc/hosts_old | sed '/host.docker.internal$/d' > /tmp/hosts && cat /etc/hosts_temp | tee -a /tmp/hosts > /dev/null && sudo cp /tmp/hosts /etc/hosts"
19+
"postStartCommand": {
20+
"azure-cli-persistence": "/usr/local/share/stuartleeks-devcontainer-features/add-host/scripts/poststart.sh"
21+
}
2022
}

src/add-host/install.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
#!/usr/bin/env bash
2-
#-------------------------------------------------------------------------------------------------------------
3-
# Copyright (c) Microsoft Corporation. All rights reserved.
4-
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5-
#-------------------------------------------------------------------------------------------------------------
6-
#
7-
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/hugo.md
8-
# Maintainer: The VS Code and Codespaces Teams
92

103
set -e
114

5+
LIFECYCLE_SCRIPTS_DIR="/usr/local/share/stuartleeks-devcontainer-features/add-host/scripts"
6+
127
if [[ -z $HOST_IP ]];
138
then
149
echo "HOST_IP is not set. Exiting..."
@@ -23,5 +18,7 @@ if [[ $(command -v sudo > /dev/null; echo $?) == 0 ]]; then
2318
else
2419
echo "$HOST_IP $HOST_NAME" >> /etc/hosts_temp
2520
fi
26-
echo "Done!"
2721

22+
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"
23+
cp poststart.sh "${LIFECYCLE_SCRIPTS_DIR}/poststart.sh"
24+
echo "Done!"

src/add-host/poststart.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
6+
echo "Activating feature 'add-host'"
7+
echo "User: $(id -un)"
8+
echo "User home: $HOME"
9+
10+
# Parse "HOST_IP HOST_NAME" from /etc/hosts_temp
11+
if [[ -f /etc/hosts_temp ]]; then
12+
while read -r line; do
13+
if [[ $line =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then
14+
HOST_IP=$(echo "$line" | awk '{print $1}')
15+
HOST_NAME=$(echo "$line" | awk '{print $2}')
16+
break
17+
fi
18+
done < /etc/hosts_temp
19+
else
20+
echo "No /etc/hosts_temp file found. Exiting..."
21+
exit 1
22+
fi
23+
24+
echo "HOST_IP: $HOST_IP"
25+
echo "HOST_NAME: $HOST_NAME"
26+
27+
if [[ $(command -v sudo > /dev/null; echo $?) == 0 ]]; then
28+
have_sudo=true
29+
else
30+
have_sudo=false
31+
fi
32+
33+
# backup hosts file
34+
if [[ $have_sudo == true ]]; then
35+
sudo cp /etc/hosts /etc/hosts_old
36+
else
37+
cp /etc/hosts /etc/hosts_old
38+
fi
39+
40+
# remove old entry
41+
cat /etc/hosts_old | sed "/$HOST_NAME\$/d" > /tmp/hosts
42+
# add new entry
43+
cat /etc/hosts_temp | tee -a /tmp/hosts > /dev/null
44+
45+
# copy temp hosts file to /etc/hosts
46+
if [[ $have_sudo == true ]]; then
47+
sudo cp /tmp/hosts /etc/hosts
48+
else
49+
cp /tmp/hosts /etc/hosts
50+
fi

0 commit comments

Comments
 (0)