# lib/designate_plugins/backend-powerdns
# Configure the powerdns backend

# Enable with:
# DESIGNATE_BACKEND_DRIVER=powerdns

# Dependencies:
# ``functions`` file
# ``designate`` configuration

# install_designate_backend - install any external requirements
# configure_designate_backend - make configuration changes, including those to other services
# init_designate_backend - initialize databases, etc.
# start_designate_backend - start any external services
# stop_designate_backend - stop any external services
# cleanup_designate_backend - remove transient data and cache

# Save trace setting
DP_PDNS_XTRACE=$(set +o | grep xtrace)
set +o xtrace

# Defaults
# --------
if is_fedora; then
    POWERDNS_CFG_DIR=/etc/pdns
else
    POWERDNS_CFG_DIR=/etc/powerdns
fi

# Entry Points
# ------------

# install_designate_backend - install any external requirements
function install_designate_backend {
    if is_ubuntu; then
        PDNS=pdns-server
    elif is_fedora || is_suse; then
        PDNS=pdns
    else
        PDNS=pdns-server
    fi

    if is_service_enabled mysql; then
        PDNS+=" pdns-backend-mysql"
    elif is_service_enabled postgresql; then
        PDNS+=" pdns-backend-pgsql"
    else
        die $LINENO "PowerDNS backend only supports MySQL / PostgreSQL"
    fi

    install_package $PDNS
    sudo rm -rf $POWERDNS_CFG_DIR/pdns.d
}

# configure_designate_backend - make configuration changes, including those to other services
function configure_designate_backend {
    iniset $DESIGNATE_CONF pool_target:$DESIGNATE_TARGET_ID type powerdns
    iniset $DESIGNATE_CONF pool_target:$DESIGNATE_TARGET_ID masters $DESIGNATE_SERVICE_HOST:$DESIGNATE_SERVICE_PORT_MDNS
    iniset $DESIGNATE_CONF pool_target:$DESIGNATE_TARGET_ID options "connection: `database_connection_url designate_pdns`"

    # DevStack Managed PDNS NameServer
    local nameserver_id=`uuidgen`
    iniset $DESIGNATE_CONF pool:$DESIGNATE_POOL_ID nameservers $nameserver_id
    iniset $DESIGNATE_CONF pool_nameserver:$nameserver_id host $DESIGNATE_SERVICE_HOST
    iniset $DESIGNATE_CONF pool_nameserver:$nameserver_id port $DESIGNATE_SERVICE_PORT_DNS

    sudo tee $POWERDNS_CFG_DIR/pdns.conf > /dev/null <<EOF
# General Config
setgid=pdns
setuid=pdns
config-dir=$POWERDNS_CFG_DIR
socket-dir=/var/run
guardian=yes
daemon=yes
disable-axfr=no
local-address=$DESIGNATE_SERVICE_HOST
local-port=$DESIGNATE_SERVICE_PORT_DNS
master=no
slave=yes
cache-ttl=0
query-cache-ttl=0
negquery-cache-ttl=0
EOF

    if is_service_enabled mysql; then
        sudo tee -a $POWERDNS_CFG_DIR/pdns.conf > /dev/null <<EOF
# Launch gmysql backend
launch=gmysql

# gmysql parameters
gmysql-host=$DATABASE_HOST
gmysql-user=$DATABASE_USER
gmysql-password=$DATABASE_PASSWORD
gmysql-dbname=designate_pdns
gmysql-dnssec=yes
EOF
    elif is_service_enabled postgresql; then
        sudo tee -a $POWERDNS_CFG_DIR/pdns.conf > /dev/null <<EOF
# Launch gpgsql backend
launch=gpgsql

# gmysql parameters
gpgsql-host=$DATABASE_HOST
gpgsql-user=$DATABASE_USER
gpgsql-password=$DATABASE_PASSWORD
gpgsql-dbname=designate_pdns
gpgsql-dnssec=yes
EOF
    else
        die $LINENO "PowerDNS backend only supports MySQL / PostgreSQL"
    fi

    restart_service pdns
}

# init_designate_backend - initialize databases, etc.
function init_designate_backend {
    # Stop pdns so that the migration succeeds, if not you get a error
    # that the schema is still in use.
    if is_service_enabled postgresql; then
        stop_designate_backend
    fi

    # (Re)create designate_pdns database
    recreate_database designate_pdns utf8

    # Init and migrate designate_pdns database
    designate-manage powerdns sync $DESIGNATE_TARGET_ID
}

# start_designate_backend - start any external services
function start_designate_backend {
    start_service pdns
}

# stop_designate_backend - stop any external services
function stop_designate_backend {
    stop_service pdns
}

# cleanup_designate_backend - remove transient data and cache
function cleanup_designate_backend {
    :
}

# Restore xtrace
$DP_PDNS_XTRACE
