#!/bin/bash

#
# Creates and configures a haproxy cartridge instance - enables http level
# request scaling in/to the app.
#

# Constants.
CART_NAME="haproxy"
CART_VERSION="1.4"

# Exit on any errors
set -e

#
#  print usage/help.
#
function print_help {
    echo "Usage: $0 app-name namespace uuid"

    echo "$0 $@" | logger -p local0.notice -t openshift_origin_haproxy_configure
    exit 1
}

function generate_ssh_keys {
    /bin/mkdir -p $HAPROXY_DIR/.ssh/
    /bin/rm -f $HAPROXY_DIR/.ssh/haproxy_id_rsa $HAPROXY_DIR/.ssh/haproxy_id_rsa.pub
    /usr/bin/ssh-keygen -N '' -f $HAPROXY_DIR/.ssh/haproxy_id_rsa > /dev/null

    touch "$HAPROXY_DIR/.ssh/known_hosts"
    touch "$HAPROXY_DIR/.ssh/config"
    /bin/chown root.$uuid -R "$HAPROXY_DIR/.ssh/"
    chmod 0750 "$HAPROXY_DIR/.ssh/"
    chmod 0640 "$HAPROXY_DIR/.ssh/haproxy_id_rsa" "$HAPROXY_DIR/.ssh/haproxy_id_rsa.pub"
    chmod 0660 "$HAPROXY_DIR/.ssh/config" "$HAPROXY_DIR/.ssh/known_hosts"
    observe_setup_var_lib_dir "$HAPROXY_DIR/.ssh/"
}


#
# main():
#
while getopts 'd' OPTION
do
    case $OPTION in
        d) set -x
        ;;
        ?) print_help
        ;;
    esac
done


[ $# -eq 4 -o $# -eq 3 ] || print_help

cartridge_type="$CART_NAME-$CART_VERSION"
source /etc/openshift/node.conf
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/util

setup_embedded_configure "$1" $2 $3
disable_cgroups

CART_INFO_DIR=${CARTRIDGE_BASE_PATH}/embedded/$cartridge_type/info

export HAPROXY_DIR=$(get_cartridge_instance_dir "$cartridge_type")


#
# Generate key pair
#
generate_ssh_keys

public_key=$(cat $HAPROXY_DIR/.ssh/haproxy_id_rsa.pub | awk '{print $2}')
add_app_ssh_key $CART_NAME $public_key

add_broker_auth_key

#
# Create the core of the application
#
create_cartridge_instance_dir "$cartridge_type"
pushd "$HAPROXY_DIR" > /dev/null
mkdir -p conf conf.d logs run sessions
ln -sf /usr/lib64/httpd/modules modules
ln -sf /etc/httpd/conf/magic conf/magic
touch conf/gear-registry.db
popd > /dev/null

#
# Find two open localhost IP
#
IP=`embedded_find_open_ip $uid "$APP_HOME"`
echo "export OPENSHIFT_HAPROXY_INTERNAL_IP='$IP'" > "$APP_HOME/.env/OPENSHIFT_HAPROXY_INTERNAL_IP"
export IP2=`embedded_find_open_ip $uid "$APP_HOME"`
echo "export OPENSHIFT_HAPROXY_STATUS_IP='$IP2'" > "$APP_HOME/.env/OPENSHIFT_HAPROXY_STATUS_IP"

$CART_INFO_DIR/bin/deploy_httpd_config.sh $application $uuid $IP

mkdir -p $APP_HOME/.openshift
chown ${uuid} $APP_HOME/.openshift $HAPROXY_DIR/run/ $HAPROXY_DIR/logs/ $HAPROXY_DIR/sessions $HAPROXY_DIR/conf/gear-registry.db
observe_setup_cart_instance_dir

# Alter PATH so that the haproxy app_ctl.sh script is called.
sed -i "s#export \s*PATH=#export PATH=$CART_INFO_DIR/bin/:#" $APP_HOME/.env/PATH

#
# Start the server
#
start_app

#
# Setup environment variables
#
echo "export OPENSHIFT_HAPROXY_LOG_DIR='$HAPROXY_DIR/logs/'" > $APP_HOME/.env/OPENSHIFT_HAPROXY_LOG_DIR

#
# Create virtualhost definition for apache
#
$CART_INFO_DIR/bin/deploy_httpd_proxy.sh $application $namespace $uuid $IP

restart_httpd_graceful
enable_cgroups
