Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work with pihole DHCP #19

Open
ohthehugemanatee opened this issue Jun 16, 2024 · 2 comments · May be fixed by #20
Open

Does not work with pihole DHCP #19

ohthehugemanatee opened this issue Jun 16, 2024 · 2 comments · May be fixed by #20

Comments

@ohthehugemanatee
Copy link
Contributor

ohthehugemanatee commented Jun 16, 2024

Though it supports enabling DHCP by variable, some capabilities are missing:

  1. sync DHCP reservations (flat file in dnsmasq)
  2. sync active DHCP leases (flat file in pihole dir)
  3. when keepalived moves the VIP, enable/disable DHCP option accordingly (only one DHCP server should be active at a time) (pihole -a enabledhcp / pihole -a disabledhcp)
  4. add dhcp-option=6,${virtual_ip} into a dnsmasq conf file

I'd send a PR but I'm not sure how to have keepalived trigger a script in response to a change in state. At a guess I could add to the status check script to check if the current node is master or backup and respond accordingly.

@ohthehugemanatee
Copy link
Contributor Author

ohthehugemanatee commented Jun 16, 2024

I threw together a little hard coded version of a keepalive check script that tries to keep dhcpd enabled/disabled appropriately. that takes care of number 3, and the others are very straightforward.

#!/bin/bash
set -e

[ "$(docker inspect -f "{{.State.Health.Status}}" pihole)" = "healthy" ] && HEALTHY=0 || HEALTHY=1

PIHOLE_HOME="/home/ohthehugemanatee/pihole"

# if all of these are true, then return 0, else return 1
if  [ ${HEALTHY} ]; then
  # If we own the primary IP.
  if /usr/sbin/ip a |grep -q 10.10.10.40 ; then
     # Ensure DHCP is enabled.
     if ! [ -f ${PIHOLE_HOME}/dnsmasq.d/02-pihole-dhcp.conf ]; then
       /usr/bin/docker exec -d pihole /usr/local/bin/pihole -a enabledhcp "10.10.10.100" "10.10.10.251" "10.10.10.1" "24" "vert"
     fi
  else
    # Ensure DHCP is disabled.
    if [ -f ${PIHOLE_HOME}/dnsmasq.d/02-pihole-dhcp.conf ]; then
        /usr/bin/docker exec -d pihole /usr/local/bin/pihole -a disabledhcp
    fi
  fi
  exit $HEALTHY
else
  exit $HEALTHY
fi

Got any good suggestions for getting those variables into the script? I guess I need to get them from ansible and do it in the template, but if you have them handy as bash vars I'll use that instead.

@ohthehugemanatee
Copy link
Contributor Author

I'm now also syncing the whole dnsmasq directory except the dhcp config file, as well as the dhcp leases file. This takes care of numbers 1 and 2.

RSYNC_LEASES=$(rsync -a --info=name -e "ssh $key $host_key_check" $target:$pihole_dir/pihole/dhcp.leases $sync_dir)
  if [ $? -eq 0 ]; then
    if [ -n "$RSYNC_LEASES" ]; then
      sudo cp --preserve=timestamps $sync_dir/dhcp.leases $pihole_dir/pihole
    fi
  fi

  if ! [ -d ${pihole_dir}/dnsmasq.d ]; then
    mkdir -p ${pihole_dir}/dnsmasq.d
  fi

  RSYNC_DNSMASQ=$(rsync -a --info=name -e "ssh $key $host_key_check" --exclude '02-pihole-dhcp.conf' $target:$pihole_dir/dnsmasq.d/* $sync_dir/dnsmasq.d/)
  if [ $? -eq 0 ]; then
    if [ -n "$RSYNC_DNSMASQ" ]; then
      sudo cp --preserve=timestamps $sync_dir/dnsmasq.d/* $pihole_dir/dnsmasq.d
    fi
  fi

The whole setup is working for me... now I'll work on getting it into ansible/templates.

@ohthehugemanatee ohthehugemanatee linked a pull request Jun 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant