#!/bin/bash
# IBM(c) 2013 EPL license http://www.eclipse.org/legal/epl-v10.html


if [ "$(uname -s|tr 'A-Z' 'a-z')" = "linux" ];then
   str_dir_name=`dirname $0`
   . $str_dir_name/xcatlib.sh
fi

#-------------------------------------------------------------------------------
#=head1  install_puppet_server
#=head2  This command installs the puppet server on a xCAT server or node.
#        It is used on Ubuntu only.
#        Usage:  
#        set $PUPPETSERVER as the puppet server on site table. The default is
#        site.master
#           install_puppet_server   
#         or
#           updatenode <node> install_puppet_server
#=cut
#-------------------------------------------------------------------------------

#Figure out what os this node has
if [ "$(uname -s)" = "Linux" ]; then
    result=`cat /etc/*release* 2>&1`
    echo $result|grep "Ubuntu" > /dev/null
    if [ $? -eq 0 ]; then
        os="Ubuntu"
    else
        echo $result | grep  "Red Hat" > /dev/null
        if [ $? -eq 0 ]; then
            os="RedHat"
	else
	    echo $result | grep  "SUSE" > /dev/null
            if [ $? -eq 0 ]; then
		os="Sles"
	    fi
        fi
    fi
fi

if [ "$os" == "Ubuntu" ]; then
    #cleanup the old installations
    echo "Removing old puppet packages....."
    apt-get -y autoremove puppetmaster puppet --purge
    rm -Rf /etc/puppet/*
    rm -Rf /var/lib/puppet/*
    
    echo "Adding new repositories....."
    #get os release name
    urelease="precise" #default release name
    urelease=`cat /etc/lsb-release |grep DISTRIB_CODENAME |cut -d= -f2`
    
    #add ubuntu repositories from the net.
    #We use us mirror, what about for the customers of other contires? 
    echo "deb http://us.archive.ubuntu.com/ubuntu/ $urelease main" > /etc/apt/sources.list.d/os_remote.list
    echo "deb http://us.archive.ubuntu.com/ubuntu/ ${urelease}-updates main" >> /etc/apt/sources.list.d/os_remote.list
    echo "deb http://us.archive.ubuntu.com/ubuntu/ $urelease universe" >> /etc/apt/sources.list.d/os_remote.list
    echo "deb http://us.archive.ubuntu.com/ubuntu/ ${urelease}-updates universe" >> /etc/apt/sources.list.d/os_remote.list
    
    #add the puppet lab repositories 
    repname=puppetlabs-release-${urelease}.deb
    wget -N --waitretry=10 --random-wait -T 60  http://apt.puppetlabs.com/$repname   -P /tmp 2>> /tmp/wget.log
    rc=$?
    if [ $rc -eq 0 ] && [ -f /tmp/$repname ]; then
	dpkg -i /tmp/$repname
	apt-get update
    else 
	echo "Cannot download  http://apt.puppetlabs.com/$repname"
	exit 1
    fi
    rm /tmp/$repname 

    #refresh the repository
    apt-get -y update
    
    
    echo "Installing puppet server....."
    #install puppet server
    apt-get -y install puppetmaster
    
    #install rake and git
    apt-get -y install rake git
    
    #Now configure the pupper server
    result=`dirname $0`
    ${result}/config_puppet_server "$@"
    
    #restart puppet master
    #service puppetmaster stop
    stopservice puppetmaster
    kill $(ps auxww | grep puppet | grep master | grep -v grep | awk '{print $2}') 
    #service puppetmaster start
    startservice puppetmaster
    
elif [ "$os" == "RedHat" ]; then
     echo "Puppet server installation with xCAT on RedHat is through a kit. Please refer to ... for details.".
else
    echo "Puppet server setup by xCAT is not supported yet on this plateform."
fi

exit 0


