Skip to content

Instantly share code, notes, and snippets.

@neilellis
Last active July 29, 2019 06:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neilellis/8983d6977f45f126df28 to your computer and use it in GitHub Desktop.
Save neilellis/8983d6977f45f126df28 to your computer and use it in GitHub Desktop.
Fixes links on Alpine Linux
#!/usr/bin/with-contenv sh
if [ ! -f /etc/dnsmasq-resolv.conf ]
then
cp -f /etc/resolv.conf /etc/dnsmasq-resolv.conf
echo "nameserver 127.0.0.1" > /etc/resolv.conf
fi
while ! ps -ef | grep -v grep | grep dnsmasq-resolv.conf
do
sleep 1
done
while true
do
env_vars=$(env | grep ".*_NAME=" | cut -d= -f1 | tr '\n' ' ')
echo "#Auto Generated - DO NOT CHANGE" > /tmp/hosts
for env_var in $env_vars
do
link=${env_var%_NAME}
domain=$(cat /etc/dnsmasq-resolv.conf | grep search | cut -d' ' -f2)
nameserver=$(cat /etc/dnsmasq-resolv.conf | grep nameserver | head -1 | cut -d' ' -f2)
ip=$(nslookup "${link}.${domain}" ${nameserver} | grep Address | tail -1 | cut -d: -f2 | cut -d' ' -f2)
if [ -n "$ip" ]
then
echo "${ip} ${link}" >> /tmp/hosts
else
logger "ip ${link}.${domain} skipped, it didn't resolve."
fi
done
if ! diff /tmp/hosts /etc/hosts.links
then
cp -f /tmp/hosts /etc/hosts.links
killall -HUP dnsmasq
fi
sleep 30
done
@danielo515
Copy link

Should this be used as entrypoint or can be used on build phase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment