#!/bin/bash

# Copyright (c) 2013 Hortonworks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##########################################################
# Element install script for HDP.
#
# Please set the DIB_HDP_VERSION environment variable
# to configure the install to use a given version.
# Currently, only 1.3 and 2.0 versions are supported for
# HDP.
##########################################################

if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
    set -x
fi
set -ue
set -o pipefail

function install_ganglia {
    # Install ganglia
    local gv="3.5.0-99"
    install-packages libganglia-$gv ganglia-gmond-$gv ganglia-gmond-modules-python-$gv ganglia-devel-$gv ganglia-gmetad-$gv ganglia-web-3.5.7-99
}

function install_mysql {
    # install mysql
    install-packages mysql mysql-server mysql-connector-java
}


function install_nagios {
    install-packages nagios hdp_mon_nagios_addons
}


function install_fping {
    install-packages fping
}


function install_perl_libraries {
    install-packages perl-Digest-SHA1 perl-Digest-HMAC perl-Crypt-DES perl-Net-SNMP
}

function install_ambari {
    install-packages ambari-server ambari-agent ambari-log4j
}


function install_nss {
    install-packages nss-softokn-freebl
}


function installHDP_1_3 {
    # ====== INSTALL Ambari =======
    cd /tmp
    wget -nv http://s3.amazonaws.com/public-repo-1.hortonworks.com/ambari/centos6/1.x/updates/1.4.3.38/ambari.repo -O /etc/yum.repos.d/ambari.repo

    install_ambari

    # ====== INSTALL HDP =======
    wget -nv http://s3.amazonaws.com/public-repo-1.hortonworks.com/HDP/centos6/1.x/updates/1.3.2.0/hdp.repo -O /etc/yum.repos.d/hdp.repo

    install_mysql

    install_perl_libraries

    install_nss

    install-packages net-snmp net-snmp-utils

    install-packages hadoop hadoop-libhdfs hadoop-native hadoop-pipes hadoop-sbin hadoop-lzo lzo lzo-devel hadoop-lzo-native

    install-packages snappy snappy-devel

    install-packages oozie zookeeper hbase webhcat-tar-hive sqoop  oozie-client extjs-2.2-1 hive hcatalog pig webhcat-tar-pig

    install-packages python-rrdtool rrdtool-devel rrdtool

    install_ganglia

    install_nagios

    install_fping
}


function installHDP_2_0 {
    install-packages net-snmp net-snmp-utils

    # ====== INSTALL Ambari =======
    cd /tmp
    wget -nv http://s3.amazonaws.com/public-repo-1.hortonworks.com/ambari/centos6/1.x/updates/1.4.3.38/ambari.repo -O /etc/yum.repos.d/ambari.repo

    install_ambari

    # ====== INSTALL HDP =======
    wget -nv http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0/hdp.repo -O /etc/yum.repos.d/hdp.repo

    install_perl_libraries

    install-packages python-rrdtool

    install-packages extjs

    install_nss

    install_mysql

    # install Hadoop packages
    install-packages hadoop hadoop-libhdfs hadoop-hdfs hadoop-lzo hadoop-lzo-native hadoop-mapreduce hadoop-mapreduce-historyserver hadoop-client oozie oozie-client zookeeper hbase pig webhcat-tar-hive webhcat-tar-pig hive hcatalog

    # Install Yarn
    install-packages hadoop-yarn hadoop-yarn-nodemanager hadoop-yarn-proxyserver hadoop-yarn-resourcemanager

    # Install sqoop
    install-packages sqoop

    # Install openssl
    install-packages openssl

    install_ganglia

    install_nagios

    install_fping

    # install compression libraries
    install-packages snappy snappy-devel lzo lzo-devel

}


# Start of Main HDP Install Element

# Call version-specific script to install the desired version of HDP
if [[ $DIB_HDP_VERSION == "1.3"  ]]; then
    echo "Installing HDP Version $DIB_HDP_VERSION..."
    installHDP_1_3
else
    if [[ $DIB_HDP_VERSION == "2.0"  ]]; then
        echo "Installing HDP Version $DIB_HDP_VERSION..."
        installHDP_2_0
    else
        echo "Invalid HDP Version specified, exiting install."
        exit 1
    fi
fi
