#!/usr/bin/env bash
#
# Copyright (C) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#

#
# Usage:
#    update-failover <configure|remove|sanity>
#

# $Id: update-failover,v 1.9 2004/07/01 19:16:58 taru Exp $

set -e

eval `ultrapossum-config init`

tmp=`tempfile`
trap "rm -f $tmp; eval `ultrapossum-config term`" 0

configure_mon() {
  monid="MON"
  if test "x$TYPE" = "xmaster" && test "x$BACKUP" != "x"; then
    progress "Configuring mon... "
    add_startmark "##" $monid > $ULTRAPOSSUM_MON_CF
    sed -e "s/#ROOTDN#/$ROOTDN/" -e "s/#LOADBALANCER#/$LOADBALANCER/" \
      -e "s/#HOST#/$HOST/" \
      -e "s/#FAILOVER_LDAP_INTERVAL#/$FAILOVER_LDAP_INTERVAL/" \
      -e "s/#FAILOVER_LDAP_FAILURE_INTERVAL#/$FAILOVER_LDAP_FAILURE_INTERVAL/" \
      -e "s/#FAILOVER_LDAP_ALERTAFTER#/$FAILOVER_LDAP_ALERTAFTER/" \
       < $ULTRAPOSSUM_MON_CFIN >> $ULTRAPOSSUM_MON_CF
    add_endmark "##" $monid >> $ULTRAPOSSUM_MON_CF
    add_end_vaconf $MONCF $ULTRAPOSSUM_MON_CF $monid
    progress "Configuring mon... done"

    if test "x$MONUSER" != "xroot"; then
      progress "Registering $MONUSER as sudoers... "
      add_startmark "##" $monid > $tmp
      echo "$MONUSER ALL = (ALL) NOPASSWD: $MODULEDIR/server/startup" >> $tmp
      add_endmark "##" $monid >> $tmp
      add_end_vaconf $SUDOERS $tmp $monid
      chmod 440 $SUDOERS
      progress "Registering $MONUSER as sudoers... done"
    else
      remove_mon_sudoers
    fi

  else
    remove_mon
  fi
}

remove_mon_sudoers()
{
  if test -f "$SUDOERS"; then
    strip_vaconf $SUDOERS $monid
    chmod 440 $SUDOERS
  fi
}

remove_mon()
{
  remove_mon_sudoers
  if test -f "$MONCF"; then strip_vaconf $MONCF $monid; fi
  /bin/rm -f $ULTRAPOSSUM_MON_CF
}

configure_heartbeat() {
  if test "x$TYPE" = "xmaster" && test "x$BACKUP" != "x"; then
    progress "Configuring heartbeat... "
    strip_vaconf $HACF "HACF"

    # ha.cf configuration
    add_startmark "##" "HACF" > $tmp
    for n in $MASTER $BACKUP
    do
      if egrep "^node $n" $HACF > /dev/null; then
        :
      else
        echo "node $n" >> $tmp
      fi
    done
    add_endmark "##" "HACF" >> $tmp
    add_end_vaconf $HACF $tmp "HACF"

    # haresources configuration
    if test "x$ULTRAPOSSUMMARK" = "xULTRAPOSSUM"; then
      add_startmark "##" "HARESOURCES" > $tmp
      if include "$BACKUP" "$SYNCBACKUPS"; then
        echo "$MASTER syncbackup $VIRTUAL $ULTRAPOSSUMSERVICES" >> $tmp
      else
        echo "$MASTER $VIRTUAL $ULTRAPOSSUMSERVICES" >> $tmp
      fi
      add_endmark "##" "HARESOURCES" >> $tmp
      add_end_vaconf $HARESOURCES $tmp "HARESOURCES"
    fi
    progress "Configuring heartbeat... done"
  else
    if test -f $HACF; then strip_vaconf $HACF "HACF"; fi
    if test -f $HARESOURCES; then strip_vaconf $HARESOURCES "HARESOURCES"; fi
  fi
}

sanity_check() {
  sanity_error=0
  if test "x$TYPE" = "xmaster" && test "x$BACKUP" != "x"; then
    if ! test -f "/etc/init.d/heartbeat"; then
      echo "E: In order to use failover environment, need /etc/init.d/heartbeat" 1>&2
      exit 1
    fi
    if ! include "$BACKUP" "$SLAVES"; then
      echo "E: BACKUP '$BACKUP' is not a slave server" 1>&2
      exit 1
    fi
    val=`getvalue $DIRECTORY/ultrapossum-environ SLURPDSLAVES`
    if test "x$val" != "x$SLURPDSLAVES"; then
      echo "E: unable to change slurpd slave servers when failover environment" 1>&2
      exit 1
    fi
    if test -f $HACF; then
      :
    else
      progress "Installing New version of ha.cf... "
      $MODULEDIR/failover/ha.cf.sh > $HACF
      progress "Installing New version of ha.cf... done"
    fi
    if test -f $HARESOURCES; then
      :
    else
      progress "Installing New Version of haresources... "
      cat $TEMPLATEDIR/haresources.in > $HARESOURCES
      progress "Installing New Version of haresources... done"
    fi
    if test -f $AUTHKEYS; then
      :
    else
      progress "Installing New version of authkeys... "
      cp /dev/null $AUTHKEYS
      chmod 640 $AUTHKEYS
      $MODULEDIR/failover/authkeys.sh > $AUTHKEYS
      progress "Installing New version of authkeys... done"
    fi
  fi
  if test "x$BACKUP" != "x" && ! include "$BACKUP" "$SYNCBACKUPS"; then
  # If we use a backup server, need to set NFSDIR or nice_failback on
    if test "x$NFSDIR" = "x"; then
      if test "`egrep "^nice_failback" $HACF | awk -F' ' '{print $2;}'`" != "on"; then
        echo "E: In order to use a failover backup server, need to set NFSDIR or set nice_failback on in ha.cf" 1>&2
        sanity_error=1
      fi
    fi
  fi
  if test "$sanity_error" != "0"; then
    exit 1
  fi
}

configure() {

  sanity_check

  sed -e "s/#HOST#/$HOST/" -e "s/#UPDATEDN#/$UPDATEDN/" \
    -e "s/#CREDENTIALS#/$CREDENTIALS/" \
         < $ULTRAPOSSUM_RECOVERY_CONFIN > $ULTRAPOSSUM_RECOVERY_CONF

  configure_mon
  configure_heartbeat

}

remove() {
  remove_mon
  /bin/rm -f $ULTRAPOSSUM_RECOVERY_CONF
  if test -f "$HACF"; then strip_vaconf $HACF "HACF"; fi
  if test -f $HARESOURCES; then strip_vaconf $HARESOURCES "HARESOURCES"; fi
}

case "x$1" in
	xconfigure)
		configure
  		ultrapossum-config set .status ULTRAPOSSUM_MODULE_FAILOVER=installed
		/bin/rm -f $CONFSTATUS
	;;
	xremove)
		remove
		ultrapossum-config remove .status ULTRAPOSSUM_MODULE_FAILOVER
		/bin/rm -f $CONFSTATUS
	;;
	xsanity)
		sanity_check
	;;
	x)
		echo "Usage: $0 <configure|remove|sanity>" 1>&2
		exit 1
	;;
	x*)
		echo "Unknown argument: $1" 1>&2
		exit 1
	;;
esac

