Skip to content

Instantly share code, notes, and snippets.

@jpswade
Created April 13, 2018 21:39
Show Gist options
  • Save jpswade/37175d2df23768f835cd31971e6c3125 to your computer and use it in GitHub Desktop.
Save jpswade/37175d2df23768f835cd31971e6c3125 to your computer and use it in GitHub Desktop.
Update hosts file provided
#!/usr/bin/env bash
set -e
APP_HOSTS="www1.example.com www2.example.com"
KUBE_IP=127.0.0.1
[[ -f /tmp/hosts.new ]] && rm -f /tmp/hosts.new
for APP_HOST in ${APP_HOSTS}; do
grep "$APP_HOST" /etc/hosts | while read FOUND; do
if [[ ${FOUND} == "$KUBE_IP $APP_HOST" ]]; then
echo "=== $FOUND"
else
[[ -f /tmp/hosts.new ]] || cp -f /etc/hosts /tmp/hosts.new
echo "--- $FOUND"
grep -v "$FOUND" /tmp/hosts.new > /tmp/hosts.tmp && mv -f /tmp/hosts.tmp /tmp/hosts.new
fi
done
if [[ ! $(grep "$KUBE_IP $APP_HOST" /etc/hosts) ]]; then
echo "+++ $KUBE_IP $APP_HOST"
echo "$KUBE_IP $APP_HOST" >> /tmp/hosts.new
fi
done
[[ -f /tmp/hosts.new ]] && sudo -- sh -c -e "cp -f /etc/hosts /etc/hosts.bak && mv -f /tmp/hosts.new /etc/hosts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment