#!/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.
##########################################################

function install_ganglia ()
{
    # Install ganglia
    install-packages libganglia ganglia-gmond ganglia-gmond-modules-python ganglia-devel ganglia-gmetad ganglia-web
}


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.2.5.17/ambari.repo -O /etc/yum.repos.d/ambari.repo
    install-packages ambari-server ambari-agent

    install-packages ambari-log4j

    # ====== 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-packages mysqld mysql mysql-server mysql-connector-java

    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 hadoop-mapreduce hadoop-mapreduce-historyserver

    install-packages snappy snappy-devel htpasswd

    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
}


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

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

    install-packages ambari-server ambari-agent

    # ====== 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-packages hadoop hadoop-libhdfs hadoop-hdfs hadoop-native hadoop-pipes hadoop-sbin hadoop-lzo hadoop-lzo-native hadoop-mapreduce hadoop-mapreduce-historyserver hadoop-client oozie oozie-client zookeeper hbase pig mysql mysql-server mysql-client webhcat-tar-hive webhcat-tar-pig hdp_mon_dashboard

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

    # Install sqoop
    install-packages sqoop

    # Install tez
    install-packages tez

    # Install openssl
    install-packages openssl

    install_ganglia

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

}


# Start of Main HDP Install Element

# Make sure wget is installed
install-packages wget

# 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

