#!/bin/sh
# Make this the non-active xCAT management node,
# i.e. standby management node.
#
# CHANGE HISTORY:
#
# NAME:        deactive-mn
#
# SYNTAX:       deactive-mn -i <nic> -v <virtual ip>
#
# DESCRIPTION:  Make this node be the standby xCAT MN.
#
# FLAGS:
#               -i      the nic that the virtual ip address attaches to,
#                       for AIX, it could be en0 or en1 or ...,
#                       for Linux, it could be eth0:1 or eth1:2 or ...
#               -v      virtual ip address
#
# DEPENDENCIES:
#
# CHANGE HISTORY:
#
# Date          Author  Vers.   Description
# -----------------------------------------------------------------------------
# 2011-12-08   JDW     1.0     Original
# 2013-06-06   xCAT    1.1     Updated version to ship with xCAT
# 2017-10-12   xCAT    2.13.8  Enhance
SHAREDVG=sharedvg # For AIX, shared volumn group name
DBDIR=/var/lib/pgsql # database directory
SHAREDFS="/install /etc/xcat /root/.xcat /tftpboot" # Shared file systems
USEDB2=no # if DB2 is being used
USETEAL=no # if TEAL is being used
USEDFM=no # if DFM is being used
USEUNMOUNT=yes # umount share data directory
POWER775=no # Power 775 cluster
CHANGEHOSTNAME=no # change the hostname from virtual ip hostname to the original hostname
exit_code=0
xcatdb=postgresql # support postgrel,DB2,mysql

usage()
{
        cat << EOF
Usage: $PROGNAME -i <nic> -v <virtual ip>

EOF
}

runcmd()
{
    echo "Running command: $@"
    a=0
    while true
    do
        if [ $a -eq 5 ]; then
            echo "$@ [Failed]"
            exit_code=1
            return 1
        elif [ $a -gt 0 ]; then
            sleep 3
            echo "Retry $a ...... $@"
        fi
        $@
        if [ $? -eq 0 ]; then
            echo "$@ [Passed]"
            return 0
        else
           a=$[$a+1]
        fi
    done
}

################################################################################

PROGNAME=$(basename $0)

while getopts ':i:v:m:' opt
do
        case "$opt" in
                i)      NIC=$OPTARG
                        ;;
                v)      VIP=$OPTARG
                        ;;
                *)      usage
                        exit 1
                        ;;
        esac
done

# NIC is a required option
if [ -z $NIC ]
then
    echo "Error: -i is a required option"
    usage
    exit 1
fi
# Virtual IP is a required option
if [ -z $VIP ]
then
    echo "Error: -v is a required option"
    usage
    exit 1
fi

##############################################################################

#echo "Removing CRON job entries for backup...."
#cronEdit -d /usr/local/cfg/cron.ems-root

##############################################################################

# Backup NIM if failing over to backup EMS
if [ `uname` = "AIX" ]
then
	echo "Backing up NIM...."
	backupFile=/install/nim-backup/nim.backup.$(date +%Y-%m-%d)
	date
	runcmd "/usr/lpp/bos.sysmgt/nim/methods/m_backup_db $backupFile"

	echo "Stopping NIM...."
	runcmd "stopsrc -s nimesis"
fi

echo "Stopping Console Server...."
if [ `uname` = "AIX" ]
then
    runcmd "stopsrc -s conserver"
else # Linux
    runcmd "service conserver stop"
fi

echo  "Stopping DHCP...."
if [ `uname` = "AIX" ]
then
    runcmd "stopsrc -s dhcpcd"
else # Linux
    runcmd "service dhcpd stop"
fi

##############################################################################
if [ USETEAL = "yes" ]
then
    if [ `uname` = "AIX" ]
    then
        echo "Stopping Teal...."
        stopsrc -s teal
        stopsrc -s teal_ll
    else # Linux
        service teal stop
        service teal_ll stop
    fi
fi

if [ POWER775 = "yes" ]
then
    echo "Stopping CNM daemon...."
    chnwm -d
fi

if [ $USEDFM = "yes" ]
then
    echo "Stopping Hardware Server...."
    if [ `uname` = "AIX" ]
    then
        stopsrc -s hdwr_svr
    else # Linux
        service hdwr_svr stop
    fi
fi

#echo "Stop xCAT on the Service Nodes...."
#xdsh service -t 30 "stopsrc -s xcatd"

echo "Stopping xCAT...."
if [ `uname` = "AIX" ]
then
    stopsrc -s xcatd
else # Linux
    runcmd "service xcatd stop"
fi

echo "Stopping database $xcatdb...."
if [ `uname` = "AIX" ]
then
    if [ $xcatdb = "DB2" ]
    then
        runcmd "su - xcatdb -c \"db2 connect reset ; db2 force applications all ; db2 terminate ; db2stop force\""
    elif [ $xcatdb = "mysql" ]
    then
        runcmd "stopsrc -g mysql"
    fi
else # Linux
    if [ $xcatdb != "SQLite" ]
    then
        runcmd "service $xcatdb stop"
    fi
fi

##############################################################################
if [ $USEDB2 = "yes" ]
then
    # Kill nicely, wait a little time for processes to clean-up....
    for fs in $SHAREDFS $DBDIR
    do
	echo "Killing processes (nicely) accessing filesystem ${fs}...."
        if [ `uname` = "AIX" ]
        then
	        runcmd "fuser -uxc -k ${fs}"
        else # Linux
            runcmd "fuser -u -k ${fs}"
        fi
    done
fi

# AIX only, Linux has the -l flag could hanldle everything
if [ `uname` = "AIX" ]
then
    sleep 20
    #  ....then kill nastily i.e. kill -9
    for fs in $SHAREDFS
    do
    	    echo "Killing processes (kill -9) accessing filesystem ${fs}...."
	    fuser -uxc -K 9 ${fs}
    done
fi


##############################################################################

echo "Re-configuring NTP...."
if [ `uname` = "AIX" ]
then
    runcmd "stopsrc -s xntpd"
    #cp /usr/local/cfg/ntp.conf.secondary /etc/ntp.conf
    runcmd "startsrc -s xntpd"
else # Linux
    runcmd "service ntpd restart"
fi

echo "Un-exporting NFS filesystems and stop NFS...."
if [ `uname` = "AIX" ]
then
    runcmd "exportfs  -ua"
    runcmd "stopsrc -g nfs"
else # Linux
    runcmd "exportfs -ua"
    runcmd "service nfs stop"
fi

echo "Unmounting shared filesystems ...."
if [ `uname` = "AIX" ]
then
    for fs in $SHAREDFS
    do
        runcmd "umount $fs"
    done
else
    if [ $USEUNMOUNT = "yes" ]
    then
        for fs in $SHAREDFS $DBDIR
        do
            runcmd "umount -l $fs"
        done
    fi
fi

if [ `uname` = "AIX" ]
then
    varyoffvg sharedvg
fi

if [ $CHANGEHOSTNAME = "yes" ]
then
    echo "Setting hostname...."
    if [ `uname` = "AIX" ]
    then
        BOOT_HOSTNAME=$(lsattr -El inet0 -a hostname | awk '{print $2}')
    else # Linux
        if [ -f "/etc/hostname" ]; then
            BOOT_HOSTNAME=`cat /etc/hostname`
        else
            BOOT_HOSTNAME=`grep HOSTNAME /etc/sysconfig/network | cut -f 2 -d "="`
        fi
    fi
    runcmd "hostname ${BOOT_HOSTNAME}"
fi

# Remove virtual ip addresses

# Firstly determine the correct IP addresses for each interface
echo "Removing IP Aliase...."
if [ `uname` = "AIX" ]
then
    runcmd "ifconfig $NIC delete $VIP"
else # Linux
    ifconfig $NIC 0.0.0.0 0.0.0.0
    ip addr show |grep $VIP 2>&1 1>/dev/null
    if [ $? -ne 0 ]; then
         echo "IP Aliase $NIC $VIP is removed.[Passed]"
    else
         echo "Fail to remove IP Aliase $NIC $VIP. [Failed]"
    fi
fi

echo ""
echo ""
if [ $exit_code -eq 0 ]
then
    echo "This machine is set to standby management node successfully..."
else
    echo "This machine is set to standby management node, but some service are failed."
fi
exit $exit_code
