#!/bin/bash
set -eux

install-packages dnsmasq dnsmasq-utils

function install_dnsmasq_upstart {
  cat > /etc/init/nova-bm-dnsmasq.conf << eof
start on runlevel [2345]
stop on runlevel [016]
pre-start script
  mkdir -p /tftpboot
  chown -R nova:nova /tftpboot
  killall -9 dnsmasq || echo 'no dnsmasq running'
end script

respawn
respawn limit 2 5

script
  exec dnsmasq --conf-file= \\
               --keep-in-foreground \\
               --port=0 \\
               --dhcp-boot=pxelinux.0 \\
               --bind-interfaces \\
               --pid-file=/var/run/dnsmasq.pid \\
               --interface=br-ctlplane \\
               --dhcp-range=192.0.2.65,192.0.2.69,29
end script
post-start exec sleep 1
eof
}

function install_dnsmasq_systemd {
  cat > /lib/systemd/system/nova-bm-dnsmasq.service << eof
[Unit]
Description=Nova dnsmasq service
After=openvswitch.service

[Service]
Type=forking
ExecStartPre=-/bin/killall -9 dnsmasq
ExecStart=/sbin/dnsmasq --conf-file= \\
                  --port=0 \\
                  --enable-tftp \\
                  --tftp-root=/tftpboot \\
                  --dhcp-boot=pxelinux.0 \\
                  --bind-interfaces \\
                  --pid-file=/var/run/dnsmasq.pid \\
                  --interface=br-ctlplane \\
                  --dhcp-range=192.0.2.65,192.0.2.69,29

[Install]
WantedBy=multi-user.target
Alias=nova-bm-dnsmasq.service
eof

  # Enable the service
  systemctl enable nova-bm-dnsmasq.service

}

if [ "$DIB_INIT_SYSTEM" == "upstart" ]; then
  install_dnsmasq_upstart
elif [ "$DIB_INIT_SYSTEM" == "systemd" ]; then
  install_dnsmasq_systemd
fi
