# Configure the Knot2 agent backend for Devstack

# Enable this pluging by adding these line to local.conf:
#
# DESIGNATE_BACKEND_DRIVER=agent
# DESIGNATE_AGENT_BACKEND_DRIVER=knot2

# install_designate_agent_backend - install any external requirements
# configure_designate_agent_backend - make configuration changes, including those to other services
# init_designate_agent_backend - initialize databases, etc.
# start_designate_agent_backend - start any external services
# stop_designate_agent_backend - stop any external services
# cleanup_designate_agent_backend - remove transient data and cache

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

# Defaults
# --------
KNOT_SERVICE_NAME=knot
KNOT_CFG_DIR=/etc/knot
KNOT_VAR_DIR=/var/lib/knot
KNOT_USER=knot
KNOT_GROUP=knot

if is_fedora; then
    echo "only Ubuntu is supported right now"
fi

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

# install_designate_agent_backend - install any external requirements
function install_designate_agent_backend {
    if is_ubuntu; then
        # https://github.com/oerdnj/deb.sury.org/issues/56
        LC_ALL=C.UTF-8 sudo add-apt-repository --yes ppa:cz.nic-labs/knot-dns
        sudo apt-get update
        echo "---- available knot package ---"
        sudo apt-cache show knot
        echo "---- installing knot ---"
        sudo apt-get install -y knot
    else
        echo "only Ubuntu is supported right now"
        exit 1
    fi
}

# configure_designate_agent_backend - make configuration changes, including those to other services
function configure_designate_agent_backend {

    # [re]create the config database
    stop_service knot
    sudo sh -c "rm /var/lib/knot/*zone /var/lib/knot/*/*.mdb -f"
    sudo knotc conf-init -v

    # Create /etc/default/knot
    cat <<EOF | sudo tee /etc/default/knot
# Created by $0 on $(date)
KNOTD_ARGS="-C /var/lib/knot/confdb"
EOF

    # Apply this workaround for bug
    # https://gitlab.labs.nic.cz/labs/knot/issues/455
    sudo sh -c "cd /etc/default/ && test -f knotd || ln -s knot knotd"

    start_service knot
    sleep 1

    # Ensure the confdb is present
    sudo test -f /var/lib/knot/confdb/data.mdb

    # Create the configuration
    MINIDNS_IPADDR=$(ipv6_unquote $DESIGNATE_SERVICE_HOST)

    sudo knotc conf-begin
    sudo knotc conf-set server.listen $(ipv6_unquote $DESIGNATE_SERVICE_HOST)@$DESIGNATE_SERVICE_PORT_DNS
    sudo knotc conf-set remote[minidns]
    sudo knotc conf-set remote[minidns].address $(ipv6_unquote $DESIGNATE_SERVICE_HOST)@$DESIGNATE_SERVICE_PORT_MDNS
    sudo knotc conf-set template[default]
    sudo knotc conf-set template[default].master minidns
    sudo knotc conf-set template[default].acl acl_minidns
    sudo knotc conf-set template[default].semantic-checks on
    # Create localdomain as a workaround for
    # https://gitlab.labs.nic.cz/labs/knot/issues/457
    sudo knotc conf-set zone[localdomain]
    sudo knotc conf-set log.any info
    sudo knotc conf-set log.target syslog
    sudo knotc conf-set acl[acl_minidns]
    sudo knotc conf-set acl[acl_minidns].address $DESIGNATE_SERVICE_HOST
    sudo knotc conf-set acl[acl_minidns].action notify
    echo "--------------"
    sudo knotc conf-diff
    echo "--------------"
    sudo knotc conf-commit
    sudo knotc conf-check

    # Ensure the zone survives a restart
    sleep 1
    sudo service knot restart
    sleep 1
    sudo knotc zone-status localdomain

    echo "Testing Knot: this should return the daemon version"
    dig @$(ipv6_unquote $DESIGNATE_SERVICE_HOST) -p$DESIGNATE_SERVICE_PORT_DNS version.server CH TXT
}

# init_designate_agent_backend - initialize databases, etc.
function init_designate_agent_backend {
    :
}

# start_designate_agent_backend - start any external services
function start_designate_agent_backend {
    start_service knot
}

# stop_designate_agent_backend - stop any external services
function stop_designate_agent_backend {
    stop_service knot
}

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

# Restore xtrace
$DP_AGENT_KNOT_XTRACE
