# Configure the agent backend

# Enable with:
# DESIGNATE_BACKEND_DRIVER=agent
# DESIGNATE_AGENT_BACKEND_DRIVER=<an agent backend>

# 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_AGENT_XTRACE=$(set +o | grep xtrace)
set +o xtrace

# Get agent backend configuration
# -------------------------------
if [[ -r $DESIGNATE_PLUGINS/backend-agent-$DESIGNATE_AGENT_BACKEND_DRIVER ]]; then
    # Load plugin
    source $DESIGNATE_PLUGINS/backend-agent-$DESIGNATE_AGENT_BACKEND_DRIVER
fi

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

# install_designate_backend - install any external requirements
function install_designate_backend {
    # Install the Agent Backend
    install_designate_agent_backend
}

# configure_designate_backend - make configuration changes, including those to other services
function configure_designate_backend {
    # Generate Designate pool.yaml file
    sudo tee $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
---
- name: default
  description: DevStack Agent Pool
  attributes: {}

  ns_records:
    - hostname: $DESIGNATE_DEFAULT_NS_RECORD
      priority: 1

  nameservers:
    - host: $(ipv6_unquote $DESIGNATE_SERVICE_HOST)
      port: $DESIGNATE_SERVICE_PORT_DNS

  targets:
    - type: agent
      description: Agent Instance

      masters:
        - host: $(ipv6_unquote $DESIGNATE_SERVICE_HOST)
          port: $DESIGNATE_SERVICE_PORT_MDNS

      options:
        host: $(ipv6_unquote $DESIGNATE_SERVICE_HOST)
        port: $DESIGNATE_SERVICE_PORT_AGENT
EOF

    # Configure Agent Settings
    iniset $DESIGNATE_CONF service:agent backend_driver $DESIGNATE_AGENT_BACKEND_DRIVER
    iniset $DESIGNATE_CONF service:agent host $(ipv6_unquote $DESIGNATE_SERVICE_HOST)
    iniset $DESIGNATE_CONF service:agent port $DESIGNATE_SERVICE_PORT_AGENT
    iniset $DESIGNATE_CONF service:agent masters "$DESIGNATE_SERVICE_HOST:$DESIGNATE_SERVICE_PORT_MDNS"

    # Configure the Agent Backend
    configure_designate_agent_backend
}

# init_designate_backend - initialize databases, etc.
function init_designate_backend {
    # Init the Agent Backend
    init_designate_agent_backend
}

# start_designate_backend - start any external services
function start_designate_backend {
    # Start the Agent Backend
    start_designate_agent_backend
}

# stop_designate_backend - stop any external services
function stop_designate_backend {
    # Stop the Agent Backend
    stop_designate_agent_backend
}

# cleanup_designate_backend - remove transient data and cache
function cleanup_designate_backend {
    # Cleanup the Agent Backend
    cleanup_designate_agent_backend
}

# Restore xtrace
$DP_AGENT_XTRACE
