Last active
July 21, 2016 03:51
-
-
Save Dude4Linux/11693144b260ad5dba7e to your computer and use it in GitHub Desktop.
Test dnsmasq with LXC containers
This file contains 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
#!/bin/bash | |
# | |
# test-dnsmasq | |
# | |
app="wordpress" | |
container="${app}-container" | |
domain="${app}.local.lxc" | |
LEASES="/var/lib/misc/dnsmasq.leases" | |
release_lease() { | |
local name=$1 | |
# release dnsmasq lease, if any before creating new container | |
container_mac=$(grep "\s${name}\s" ${LEASES} | awk '{print $2}') | |
container_ip=$( grep "\s${name}\s" ${LEASES} | awk '{print $3}') | |
dhcp_release natbr0 ${container_ip} ${container_mac} | |
} | |
wait_for_host() { | |
# wait for host name resolution | |
# it does not mean that host is up | |
local hostname="$1" | |
local retries="${2:-10}" | |
local delay="${3:-3}" | |
for (( i=1; i<=${retries}; i++ )); do | |
host ${hostname} > /dev/null && return 0; | |
sleep ${delay} | |
done | |
return 1; | |
} | |
# main | |
echo "Starting Test: $(host ${container})" | |
lxc-create -q -n ${container} -t turnkey -- ${app} -i ~/inithooks.conf | |
echo "Container created: $(host ${container})" | |
lxc-start -d -n ${container} | |
wait_for_host ${container} | |
echo "Container started: $(host -W30 ${container})" | |
sleep 5; | |
echo "Container started+5: $(host ${container})" | |
sleep 5; | |
echo "Container started+10: $(host ${container})" | |
lxc-stop -n ${container} | |
echo "Container stopped: $(host ${container})" | |
lxc-destroy -n ${container} | |
echo "Container destroyed: $(host ${container})" | |
release_lease ${container} | |
echo "dnsmasq lease released: $(host ${container})" | |
service dnsmasq reload | |
echo "dnsmasq reloaded: $(host ${container})" | |
# end |
Corrected a mistake pointed out by Anton and clarified that we're using the default inithooks.conf.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script was used to demonstrate the interaction between dnsmasq and LXC when a container is created, started and then destroyed. Uncomment the subroutine calls to observe the effect of wait_for_host and release_lease. These subroutines were incorporated into nginx-proxy and lxc-turnkey template. Since there is not yet a hook for lxc-destroy, we do the next best thing and release any old leases before re-creating a container using the same name.