Last active
July 29, 2019 06:09
-
-
Save neilellis/8983d6977f45f126df28 to your computer and use it in GitHub Desktop.
Fixes links on Alpine Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should this be used as entrypoint or can be used on build phase?