Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created July 25, 2014 18:50
Show Gist options
  • Save stevenharman/8bc6848c6ac2284c214c to your computer and use it in GitHub Desktop.
Save stevenharman/8bc6848c6ac2284c214c to your computer and use it in GitHub Desktop.
A bin/bootstrap script to install and configure Dnsmasq as a local DNS server for *.dev TLDs.
#!/bin/sh
brew_install_if_needed() {
package=$1
brew info $package | grep "Not installed" > /dev/null 2>&1
if [[ "$?" -eq "0" ]]; then
brew install $package
fi
}
add_git_remote_if_needed() {
name=$1
url=$2
git remote | grep $name > /dev/null 2>&1
if [[ "$?" -eq "1" ]]; then
git remote add $name $url
fi
}
# Configure local DNS Server to handle .dev TLDs
setup_local_dns_server() {
dnsmasq_config=`brew --prefix`/etc/dnsmasq.conf
daemon_config=/Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
dev_resolver=/etc/resolver/dev
success=0
# Dnsmasq will route *.dev to 127.0.0.1
if [ -f $dnsmasq_config ]; then
let success=1
else
echo "address=/dev/127.0.0.1" > $dnsmasq_config
fi
# Use dnsmasq as nameserver for .dev TLDs
if [ -f $dev_resolver ]; then
let success=1
else
sudo bash -c "mkdir -p /etc/resolver && echo \"nameserver 127.0.0.1\" > $dev_resolver"
fi
# Launch Dnsmasq on startup
sudo cp `brew --prefix dnsmasq`/homebrew.mxcl.dnsmasq.plist $daemon_config
sudo launchctl load $daemon_config
if [[ "$success" -ne "0" ]]; then
echo "I couldn't setup a local DNS sever for .dev TLD. Manual steps required."
printf "\e[36m"
echo ""
echo "---------------------------------"
echo "Some manual setup steps required; verify the following:"
echo ""
echo "1) Your $dnsmasq_config sets 'address=/dev/127.0.0.1'"
echo "2) Your $dev_resolver sets 'nameserver 127.0.0.1'"
echo "3) Your $daemon_config is loaded by launchctl"
fi
}
brew_install_if_needed dnsmasq
setup_local_dns_server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment