#!/bin/bash
#
# lib/senlin
# Install and start **Senlin** service

# To enable, add the following to localrc
#
#   ENABLED_SERVICES+=,senlin,sl-api,sl-eng

# Dependencies:
#
# - functions

# stack.sh
# ---------
# - install_senlinclient
# - install_senlin
# - configure_senlinclient
# - configure_senlin
# - init_senlin
# - start_senlin
# - stop_senlin
# - cleanup_senlin

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


# Defaults
# --------

# set up default
SENLIN_AUTH_CACHE_DIR=${SENLIN_AUTH_CACHE_DIR:-/var/cache/senlin}
SENLIN_CONF_DIR=/etc/senlin
SENLIN_CONF=$SENLIN_CONF_DIR/senlin.conf
SENLIN_API_HOST=${SENLIN_API_HOST:-$HOST_IP}
SENLIN_API_PORT=${SENLIN_API_PORT:-8778}

SENLIN_DIR=$DEST/senlin
SENLIN_REPO=${SENLIN_REPO:-${GIT_BASE}/openstack/senlin.git}
SENLIN_BRANCH=${SENLIN_BRANCH:-master}

SENLINCLIENT_DIR=$DEST/python-senlinclient
SENLINCLIENT_REPO=${SENLINCLIENT_REPO:-${GIT_BASE}/openstack/python-senlinclient.git}
SENLINCLIENT_BRANCH=${SENLINCLIENT_BRANCH:-master}


# Functions
# ---------

# Test if any Senlin services are enabled
function is_senlin_enabled {
    [[ ,${ENABLED_SERVICES} =~ ,"sl-" ]] && return 0
    return 1
}

# cleanup_senlin() - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_senlin {
    sudo rm -rf $SENLIN_AUTH_CACHE_DIR
    sudo rm -rf $SENLIN_CONF_DIR
}

# configure_senlin() - Set config files, create data dirs, etc
function configure_senlin {

    if [[ ! -d $SENLIN_CONF_DIR ]]; then
        sudo mkdir -p $SENLIN_CONF_DIR
    fi
    sudo install -d -o $STACK_USER $SENLIN_CONF_DIR

    SENLIN_ENGINE_HOST=${SENLIN_ENGINE_HOST:-$SERVICE_HOST}
    SENLIN_ENGINE_PORT=${SENLIN_ENGINE_PORT:-8778}
    SENLIN_API_PASTE_FILE=$SENLIN_CONF_DIR/api-paste.ini
    SENLIN_POLICY_FILE=$SENLIN_CONF_DIR/policy.json

    cp $SENLIN_DIR/etc/senlin/api-paste.ini $SENLIN_API_PASTE_FILE
    cp $SENLIN_DIR/etc/senlin/policy.json $SENLIN_POLICY_FILE

    # common options
    iniset $SENLIN_CONF DEFAULT verbose "True"
    iniset $SENLIN_CONF DEFAULT debug "$ENABLE_DEBUG_LOG_LEVEL"
    iniset $SENLIN_CONF DEFAULT auth_encryption_key $(generate_hex_string 16)
    iniset $SENLIN_CONF DEFAULT region_name_for_services "$REGION_NAME"

    iniset $SENLIN_CONF DEFAULT use_syslog $SYSLOG
    if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
        # Add color to logging output
        setup_colorized_logging $SENLIN_CONF DEFAULT
    fi

    # rpc
    iniset_rpc_backend senlin $SENLIN_CONF

    # OpenStack API
    iniset $SENLIN_CONF senlin_api bind_port $SENLIN_API_PORT

    # Database connection
    iniset $SENLIN_CONF database connection `database_connection_url senlin`

    # Keystone authtoken middleware
    #configure_auth_token_middleware $SENLIN_CONF senlin $SENLIN_AUTH_CACHE_DIR
    iniset $SENLIN_CONF keystone_authtoken identity_uri $KEYSTONE_AUTH_URI
    iniset $SENLIN_CONF keystone_authtoken cafile $SSL_BUNDLE_FILE
    iniset $SENLIN_CONF keystone_authtoken signing_dir $SENLIN_AUTH_CACHE_DIR
    iniset $SENLIN_CONF keystone_authtoken auth_uri $KEYSTONE_AUTH_URI/v3
    iniset $SENLIN_CONF keystone_authtoken admin_user senlin
    iniset $SENLIN_CONF keystone_authtoken admin_password $SERVICE_PASSWORD
    iniset $SENLIN_CONF keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME

    # Senlin service credentials
    iniset $SENLIN_CONF authentication auth_url $KEYSTONE_AUTH_URI/v3
    iniset $SENLIN_CONF authentication service_username senlin
    iniset $SENLIN_CONF authentication service_password $SERVICE_PASSWORD
    iniset $SENLIN_CONF authentication service_project_name $SERVICE_TENANT_NAME
}

# init_senlin() - Initialize database
function init_senlin {

    # (re)create senlin database
    recreate_database senlin utf8

    $SENLIN_DIR/bin/senlin-manage db_sync
    create_senlin_cache_dir
}

# create_senlin_cache_dir() - Part of the init_senlin() process
function create_senlin_cache_dir {
    # Create cache dirs
    sudo mkdir -p $SENLIN_AUTH_CACHE_DIR
    sudo install -d -o $STACK_USER $SENLIN_AUTH_CACHE_DIR
}

# install_senlinclient() - Collect source and prepare
function install_senlinclient {
    if use_library_from_git "python-senlinclient"; then
        git_clone $SENLINCLIENT_REPO $SENLINCLIENT_DIR $SENLINCLIENT_BRANCH
        setup_develop $SENLINCLIENT_DIR
    else
        pip_install python-senlinclient
    fi
}

# install_senlin() - Collect source and prepare
function install_senlin {
    git_clone $SENLIN_REPO $SENLIN_DIR $SENLIN_BRANCH
    setup_develop $SENLIN_DIR
}

# start_senlin() - Start running processes, including screen
function start_senlin {
    run_process sl-eng "$SENLIN_DIR/bin/senlin-engine --config-file=$SENLIN_CONF"
    run_process sl-api "$SENLIN_DIR/bin/senlin-api --config-file=$SENLIN_CONF"
}

# stop_senlin() - Stop running processes
function stop_senlin {
    # Kill the screen windows
    local serv
    for serv in sl-eng sl-api; do
        stop_process $serv
    done
}

# create_senlin_accounts() - Set up common required senlin accounts
function create_senlin_accounts {

    create_service_user "senlin" "admin"

    if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then

        local senlin_service=$(get_or_create_service "senlin" \
                "clustering" "Senlin Clustering Service")
        get_or_create_endpoint $senlin_service \
            "$REGION_NAME" \
            "$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT/v1/\$(tenant_id)s" \
            "$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT/v1/\$(tenant_id)s" \
            "$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT/v1/\$(tenant_id)s"
    fi

    # get or adds 'service' role to 'senlin' on 'demo' project
    get_or_add_user_project_role "service" "senlin" "demo"
}

# Restore xtrace
$XTRACE

# Tell emacs to use shell-script-mode
## Local variables:
## mode: shell-script
## End:
