#!/bin/bash -ex


########################################################################################################
#
#
# Ambari server configuration
# 
#
########################################################################################################
# Install and configure the ntp service
yum -y install ntp
chkconfig ntpd on
service ntpd start

# Disable iptables. This is a pragmatic decision is you are using Security groups and network ACLs.
service iptables stop
chkconfig iptables off

# Disable selinux. Not required on Amazon Linux but required on Centos/Redhat
perl -npe 's/^SELINUX=.*$/SELINUX=permissive/' -i /etc/sysconfig/selinux

# Configure the hostname
perl -npe 's/^HOSTNAME=.*$/HOSTNAME=ambariserver.example.com/' -i /etc/sysconfig/network

# Update the hostname
hostname ambariserver.example.com

# Update /etc/hosts to reflect to refect the correct private ip addresses of each host

cat >> /etc/hosts << 'EOF'
10.100.0.100    ambariserver.example.com    ambariserver
10.100.1.100    hadoopmaster.example.com    hadoopmaster
10.100.2.100    hadoopslave1.example.com    hadoopslave1
EOF


# Add the ambari yum repo to the local repository list
wget http://public-repo-1.hortonworks.com/ambari/centos6/1.x/GA/ambari.repo
cp ambari.repo /etc/yum.repos.d/
yum -y install epel-release

# Install the ambari server
yum -y install ambari-server

# setup the ambari server
# This fails on Amazon Linux. I suspect it's looking for an OS identifier such as Red Hat or Centos.
ambari-server setup --silent

# start the ambaris server
ambari-server start


########################################################################################################
#
#
# Hadoop master configuration prior to registering with ambari.example.com
# 
#
########################################################################################################
# Install and configure the ntp service
yum -y install ntp
chkconfig ntpd on
service ntpd start

# Disable iptables. This is a pragmatic decision is you are using Security groups and network ACLs.
service iptables stop
chkconfig iptables off

# Disable selinux. Not required on Amazon Linux but required on Centos/Redhat
perl -npe 's/^SELINUX=.*$/SELINUX=permissive/' -i /etc/sysconfig/selinux

# Configure the hostname
perl -npe 's/^HOSTNAME=.*$/HOSTNAME=hadoopmaster.example.com/' -i /etc/sysconfig/network

# Update the hostname
hostname hadoopmaster.example.com


# Update /etc/hosts to reflect to refect the correct private ip addresses of each host

cat >> /etc/hosts << EOF
10.100.0.100    ambariserver.example.com    ambariserver
10.100.1.100    hadoopmaster.example.com    hadoopmaster
10.100.2.100    hadoopslave1.example.com    hadoopslave1
EOF


cat > /tmp/diskpartition.txt << EOF
unit: sectors
/dev/xvdg1 : start=       63, size=838785717, Id=83
/dev/xvdg2 : start=        0, size=        0, Id= 0
/dev/xvdg3 : start=        0, size=        0, Id= 0
/dev/xvdg4 : start=        0, size=        0, Id= 0
EOF

sfdisk /dev/xvdg < /tmp/diskpartition.txt

mkfs.ext4 /dev/xvdg1
mkdir /mnt/hdfs

# get UUID of new disk add to /etc/fstab
blkid /dev/xvdg1
echo Must update /etc/fstab e.g. UUID="bdbe39c9-4632-4663-9e3e-38da843781a7"     /mnt/hdfs       ext4 defaults 0
0

########################################################################################################
#
#
# Hadoop slave configuration prior to registering with ambari.example.com
# 
#
########################################################################################################
# Install and configure the ntp service
yum -y install ntp
chkconfig ntpd on
service ntpd start

# Disable iptables. This is a pragmatic decision is you are using Security groups and network ACLs.
service iptables stop
chkconfig iptables off

# Disable selinux. Not required on Amazon Linux but required on Centos/Redhat
perl -npe 's/^SELINUX=.*$/SELINUX=permissive/' -i /etc/sysconfig/selinux

# Configure the hostname
perl -npe 's/^HOSTNAME=.*$/HOSTNAME=hadoopslave1.example.com/' -i /etc/sysconfig/network

# Update the hostname
hostname hadoopslave1.example.com

# Update /etc/hosts to reflect to refect the correct private ip addresses of each host

cat >> /etc/hosts << EOF
10.100.0.100    ambariserver.example.com    ambariserver
10.100.1.100    hadoopmaster.example.com    hadoopmaster
10.100.2.100    hadoopslave1.example.com    hadoopslave1
EOF

cat > /tmp/diskpartition.txt << EOF
unit: sectors
/dev/xvdg1 : start=       63, size=838785717, Id=83
/dev/xvdg2 : start=        0, size=        0, Id= 0
/dev/xvdg3 : start=        0, size=        0, Id= 0
/dev/xvdg4 : start=        0, size=        0, Id= 0
EOF

sfdisk /dev/xvdg < /tmp/diskpartition.txt

mkfs.ext4 /dev/xvdg1
mkdir /mnt/hdfs
# get UUID of new disk add to /etc/fstab
blkid /dev/xvdg1
echo Must update /etc/fstab e.g. UUID="bdbe39c9-4632-4663-9e3e-38da843781a7"     /mnt/hdfs       ext4 defaults 0
0




