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

# To enable, add the following to local.conf
#
# [[local|localrc]]
# enable_plugin senlin https://git.openstack.org/openstack/senlin

# Dependencies:
#
# - functions
# - HORIZON_DIR

# stack.sh
# ---------
# - config_senlin_dashboard
# - configure_senlin
# - cleanup_senlin
# - cleanup_senlin_dashboard
# - create_senlin_cache_dir
# - create_senlin_accounts
# - init_senlin
# - install_senlinclient
# - install_senlin
# - install_senlin_dashboard
# - is_senlin_enabled
# - start_senlin
# - stop_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_BIN_DIR=$(get_python_exec_prefix)
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}

SENLIN_DASHBOARD_DIR=$DEST/senlin-dashboard
SENLIN_DASHBOARD_REPO=${SENLIN_DASHBOARD_REPO:-${GIT_BASE}/openstack/senlin-dashboard.git}
SENLIN_DASHBOARD_BRANCH=${SENLIN_DASHBOARD_BRANCH:-master}

# Toggle for deploying senlin-api under Apache(mod_wsgi)
SENLIN_USE_MOD_WSGI=${SENLIN_USE_MOD_WSGI:-False}


# 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 {
    _cleanup_senlin_apache_wsgi
    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

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

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

    if [ "$USE_SYSTEMD" != "False" ]; then
        setup_systemd_logging $SENLIN_CONF
    fi

    if [ "$LOG_COLOR" == "True" ] && [ "$USE_SYSTEMD" == "False" ] && [ "$SENLIN_USE_MOD_WSGI" == "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 cafile $SSL_BUNDLE_FILE
    iniset $SENLIN_CONF keystone_authtoken auth_url $KEYSTONE_AUTH_URI
    iniset $SENLIN_CONF keystone_authtoken www_authenticate_uri $KEYSTONE_SERVICE_URI_V3
    iniset $SENLIN_CONF keystone_authtoken username senlin
    iniset $SENLIN_CONF keystone_authtoken password $SERVICE_PASSWORD
    iniset $SENLIN_CONF keystone_authtoken project_name $SERVICE_TENANT_NAME
    iniset $SENLIN_CONF keystone_authtoken project_domain_name Default
    iniset $SENLIN_CONF keystone_authtoken user_domain_name Default
    iniset $SENLIN_CONF keystone_authtoken auth_type password
    iniset $SENLIN_CONF keystone_authtoken service_token_roles_required True

    # 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

    # Zaqar options for message receiver
    iniset $SENLIN_CONF zaqar auth_type password
    iniset $SENLIN_CONF zaqar username zaqar
    iniset $SENLIN_CONF zaqar password $SERVICE_PASSWORD
    iniset $SENLIN_CONF zaqar project_name $SERVICE_TENANT_NAME
    iniset $SENLIN_CONF zaqar auth_url $KEYSTONE_AUTH_URI/v3
    iniset $SENLIN_CONF zaqar user_domain_name Default
    iniset $SENLIN_CONF zaqar project_domain_name Default

    if [ "$SENLIN_USE_MOD_WSGI" == "True" ]; then
        _config_senlin_apache_wsgi
    fi
}

# _cleanup_senlin_apache_wsgi() - Remove WSGI files, disable and remove Apache vhost file
function _cleanup_senlin_apache_wsgi {
    if [ "$SENLIN_USE_MOD_WSGI" == "True" ]; then
        sudo rm -f $(apache_site_config_for senlin-api)
    fi
}

# _config_senlin_apache_wsgi() - Configure mod_wsgi
function _config_senlin_apache_wsgi {

    local senlin_apache_conf
    senlin_apache_conf=$(apache_site_config_for senlin-api)
    local senlin_api_port=$SENLIN_API_PORT
    local venv_path=""

    sudo cp $SENLIN_DIR/devstack/apache-senlin.template $senlin_apache_conf
    sudo sed -e "
        s|%PORT%|$senlin_api_port|g;
        s|%APACHE_NAME%|$APACHE_NAME|g;
        s|%SENLIN_BIN_DIR%|$SENLIN_BIN_DIR|g;
        s|%USER%|$STACK_USER|g;
        s|%VIRTUALENV%|$venv_path|g
    " -i $senlin_apache_conf
}

# init_senlin() - Initialize database
function init_senlin {

    # (re)create senlin database
    recreate_database senlin utf8

    $SENLIN_BIN_DIR/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 --upgrade python-senlinclient
    fi
}

# install_senlin_dashboard() - Collect source and prepare
function install_senlin_dashboard {
    # NOTE(Liuqing): workaround for devstack bug: 1540328
    # https://bugs.launchpad.net/devstack/+bug/1540328
    # where devstack install 'test-requirements' but should not do it
    # for senlin-dashboard project as it installs Horizon from url.
    # Remove following two 'mv' commands when mentioned bug is fixed.
    if use_library_from_git "senlin-dashboard"; then
        git_clone $SENLIN_DASHBOARD_REPO $SENLIN_DASHBOARD_DIR $SENLIN_DASHBOARD_BRANCH
        mv $SENLIN_DASHBOARD_DIR/test-requirements.txt $SENLIN_DASHBOARD_DIR/_test-requirements.txt
        setup_develop $SENLIN_DASHBOARD_DIR
        mv $SENLIN_DASHBOARD_DIR/_test-requirements.txt $SENLIN_DASHBOARD_DIR/test-requirements.txt
    else
        pip_install --upgrade senlin-dashboard
    fi
}

# configure_senlin_dashboard() - Set config files
function config_senlin_dashboard {
    # Install Senlin Dashboard as plugin for Horizon
    ln -sf $SENLIN_DASHBOARD_DIR/senlin_dashboard/enabled/_50_senlin.py $HORIZON_DIR/openstack_dashboard/local/enabled/_50_senlin.py
    # Enable senlin policy
    ln -sf $SENLIN_DASHBOARD_DIR/senlin_dashboard/conf/senlin_policy.json $HORIZON_DIR/openstack_dashboard/conf/senlin_policy.json
}

# cleanup_senlin_dashboard() - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_senlin_dashboard {
    sudo rm -rf $HORIZON_DIR/openstack_dashboard/local/enabled/_50_senlin.py
    sudo rm -rf $HORIZON_DIR/openstack_dashboard/conf/senlin_policy.json
}

# install_senlin() - Collect source and prepare
function install_senlin {
    git_clone $SENLIN_REPO $SENLIN_DIR $SENLIN_BRANCH
    setup_develop $SENLIN_DIR
    if [ "$SENLIN_USE_MOD_WSGI" == "True" ]; then
        install_apache_wsgi
    fi
}

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

    local enabled_site_file
    enabled_site_file=$(apache_site_config_for senlin-api)
    if [ -f ${enabled_site_file} ] && [ "$SENLIN_USE_MOD_WSGI" == "True" ]; then
        enable_apache_site senlin-api
        restart_apache_server
        tail_log senlin-api /var/log/$APACHE_NAME/senlin-api.log
    else
        run_process sl-api "$SENLIN_BIN_DIR/senlin-api --config-file=$SENLIN_CONF"
    fi
}

# stop_senlin() - Stop running processes
function stop_senlin {
    # Kill the screen windows
    stop_process sl-eng

    if [ "$SENLIN_USE_MOD_WSGI" == "True" ]; then
        disable_apache_site senlin-api
        restart_apache_server
    else
        stop_process sl-api
    fi
}

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

    create_service_user "senlin" "admin"

    local senlin_service=$(get_or_create_service "senlin" \
        "clustering" "Senlin Clustering Service")
    if [ "$SENLIN_USE_MOD_WSGI" == "True" ]; then
        senlin_api_url="$SERVICE_PROTOCOL://$SENLIN_API_HOST/cluster"
    else
        senlin_api_url="$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT"
    fi

    get_or_create_endpoint $senlin_service \
        "$REGION_NAME" \
        "$senlin_api_url" \
        "$senlin_api_url" \
        "$senlin_api_url"

    # get or add '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:
