#!/usr/bin/bash
#############################################################################
#  Copyright (C) 2013-2015 Lawrence Livermore National Security, LLC.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Albert Chu <chu11@llnl.gov>
#  LLNL-CODE-644248
#
#  This file is part of Magpie, scripts for running Hadoop on
#  traditional HPC systems.  For details, see https://github.com/llnl/magpie.
#
#  Magpie is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Magpie is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Magpie.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

# These are functions to be called by magpie-run

source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-submission-type
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-user
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-defaults
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-log
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-paths

# Return 0 if service up, 1 if not
__Magpie_run_check_zeppelin_up () {
    cd ${ZEPPELIN_HOME}

    local serverschk=0
    local serversup=0
    for server in `cat ${ZEPPELIN_CONF_DIR}/masters`
    do
        serverschk=`expr ${serverschk} + 1`
        local serverisup=`${MAGPIE_REMOTE_CMD:-ssh} ${MAGPIE_REMOTE_CMD_OPTS} $server ps -ef | grep -c zeppelin`
        if [ "${serverisup}" -gt "1" ]
        then
            serversup=`expr ${serversup} + 1`
        fi
    done

    echo "$serversup/$serverschk Zeppelin Servers are up."
    if [ "$serverschk" -eq "$serversup" ]
    then
        return 0
    fi

    return 1
}

Magpie_run_start_zeppelin () {
    if [ "${ZEPPELIN_SETUP}" == "yes" ] && [ "${magpie_run_prior_startup_successful}" == "true" ]
    then
        if [ "${magpie_run_spark_setup_successful}" == "0" ]
        then
            Magpie_output_internal_error "Attempt to setup Zeppelin without Spark being setup"
            magpie_run_zeppelin_should_be_torndown=0
            magpie_run_zeppelin_setup_successful=0
            return 1
        fi

        cd ${ZEPPELIN_HOME}

        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            # Make variables unspecified for launching
            Magpie_make_all_local_dirs_unspecified

            echo "Starting Zeppelin"
            ${ZEPPELIN_HOME}/bin/zeppelin-daemon.sh --config ${ZEPPELIN_CONF_DIR} start

            # Make variables specific now within Magpie
            Magpie_make_all_local_dirs_node_specific

            # My rough estimate for setup time is 30 seconds
            local sleepwait=30

            echo "Waiting ${sleepwait} seconds to allow Zeppelin daemons to setup"
            sleep ${sleepwait}
            magpie_run_total_sleep_wait=`expr ${magpie_run_total_sleep_wait} + ${sleepwait}`
        fi

        echo "*******************************************************"
        echo "*"
        echo "* Zeppelin Information"
        echo "*"
        echo "* To access Zeppelin's web interface, you'll want to navigate your browser to:"
        echo "*"
        echo "*     http://${ZEPPELIN_MASTER_NODE}:${default_zeppelin_port}"
        echo "*"
        echo "* To access zeppelin directly, you'll want to:"
        echo "*"
        echo "*   ${MAGPIE_REMOTE_CMD:-ssh}${MAGPIE_REMOTE_CMD_OPTS:+" "}${MAGPIE_REMOTE_CMD_OPTS} ${ZEPPELIN_MASTER_NODE}"
        if echo $MAGPIE_SHELL | grep -q csh
        then
            echo "*   setenv JAVA_HOME \"${JAVA_HOME}\""
            echo "*   setenv ZEPPELIN_HOME \"${ZEPPELIN_HOME}\""
            echo "*   setenv ZEPPELIN_CONF_DIR \"${ZEPPELIN_CONF_DIR}\""
        else
            echo "*   export JAVA_HOME=\"${JAVA_HOME}\""
            echo "*   export ZEPPELIN_HOME=\"${ZEPPELIN_HOME}\""
            echo "*   export ZEPPELIN_CONF_DIR=\"${ZEPPELIN_CONF_DIR}\""
        fi
        echo "*"
        echo "* Then you can do as you please. To interact with Zeppelin you can use the applications available in:"
        echo "*"
        echo "*   \$ZEPPELIN_HOME/${zeppelincmdprefix}"
        echo "*"

        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            echo "* If running interactively, sourcing"
            echo "*"
            echo "* ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}"
            echo "*"
            echo "* will set most common environment variables for your job."
            echo "*"
        fi
        echo "*******************************************************"

        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            if echo $MAGPIE_SHELL | grep -q csh
            then
                echo "setenv ZEPPELIN_HOME \"${ZEPPELIN_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ZEPPELIN_MASTER_NODE \"${ZEPPELIN_MASTER_NODE}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ZEPPELIN_SERVER_PORT \"${ZEPPELIN_SERVER_PORT}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
            else
                echo "export ZEPPELIN_HOME=\"${ZEPPELIN_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ZEPPELIN_MASTER_NODE=\"${ZEPPELIN_MASTER_NODE}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ZEPPELIN_SERVER_PORT=\"${ZEPPELIN_SERVER_PORT}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
            fi
            echo "" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
        fi

        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            # Return 0 if service up, 1 if not
            Magpie_check_service_up "Zeppelin" "__Magpie_run_check_zeppelin_up"

            if [ $? -eq 0 ]
            then
                magpie_run_zeppelin_should_be_torndown=1
                magpie_run_zeppelin_setup_successful=1
            else
                magpie_run_zeppelin_should_be_torndown=1
                magpie_run_zeppelin_setup_successful=0
                magpie_run_prior_startup_successful=false
            fi
        else
            magpie_run_zeppelin_should_be_torndown=1
            magpie_run_zeppelin_setup_successful=1
        fi
    else
        magpie_run_zeppelin_should_be_torndown=0
        magpie_run_zeppelin_setup_successful=1
    fi
}

Magpie_run_zeppelin () {
    if [ "${ZEPPELIN_JOB}" == "checkzeppelinup" ]
    then
        echo "*******************************************************"
        echo "* Running CheckZeppelinUp"
        echo "*******************************************************"
        ${MAGPIE_SCRIPTS_HOME}/magpie/run/magpie-run-execute script ${MAGPIE_SCRIPTS_HOME}/magpie/job/magpie-job-zeppelin-checkzeppelinup.py &
        local scriptpid=$!
        wait $scriptpid
    else
        Magpie_output_internal_error "ZEPPELIN_JOB = ${ZEPPELIN_JOB} not handled"
    fi
}

Magpie_run_stop_zeppelin () {
    if [ "${magpie_run_spark_teardown_complete}" == "1" ]
    then
        Magpie_output_internal_warning "tearing down Zeppelin after Spark"
    fi

    if [ "${ZEPPELIN_SETUP}" == "yes" ] && [ "${magpie_run_zeppelin_should_be_torndown}" == "1" ]
    then
        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            cd ${ZEPPELIN_HOME}

            # Make variables unspecified for launching
            Magpie_make_all_local_dirs_unspecified

            echo "Stopping Zeppelin"
            ${ZEPPELIN_HOME}/bin/zeppelin-daemon.sh --config ${ZEPPELIN_CONF_DIR} stop

            # Make variables specific now within Magpie
            Magpie_make_all_local_dirs_node_specific
        fi
    fi
    magpie_run_zeppelin_teardown_complete=1
}
