#!/bin/bash

. /usr/lib/eole/ihm.sh
. /usr/lib/eole/utils.sh

activer_haute_dispo=$(CreoleGet activer_haute_dispo)
nom_machine=$(CreoleGet nom_machine)
option="$1"

if [ "${activer_haute_dispo}" == "non" ]
then
# Si haute dispo désactivée, on stoppe corosync
    service pacemaker stop
    pgrep -f pacemaker  > /dev/null 2>&1 && pkill -f pacemaker > /dev/null 2>&1
    pgrep -f pacemaker > /dev/null 2>&1 &&  pkill -9 pacamaker 2>&1 >/dev/null
    service corosync stop
    pgrep -f corosync > /dev/null 2>&1 && pkill -f corosync > /dev/null 2>&1
    pgrep -f corosync > /dev/null 2>&1 &&  pkill -9 corosync 2>&1 >/dev/null
    rm -f /var/lib/pacemaker/cib/*
else
    corosync_dial_if=$(CreoleGet corosync_dial_if)
    id eole &>/dev/null
    if [ $? -eq 0 ]
    then
        groups eole |grep haclient >/dev/null
        if [ $? -ne 0 ]
        then
            usermod -G haclient -a eole
        fi
    fi
    # On genere un bi-clef rsa sur chaque machine si il n'existe pas
    if [ ! -e /root/.ssh/id_rsa.pub ] &&
            [ ! -e /root/.ssh/id_rsa ]
        then
            ssh-keygen -t rsa -b 2048 -f /root/.ssh/id_rsa -N "" -q
    fi

    # on renseigne le noeud distant
    if [ "${activer_haute_dispo}" == "maitre" ]
    then
        remote_node=$(CreoleGet nom_machine_esclave)
    elif [ "${activer_haute_dispo}" == "esclave" ]
    then
        remote_node=$(CreoleGet nom_machine_maitre)
    fi

    if [ "$option" = "instance" ]
    then
        ## Suppression de l'ancienne conf pacemaker
        if [ -e /var/lib/pacemaker/cib/cib.xml ]
        then
            # Arrête corosync et pacemaker
            /usr/share/eole/sbin/haute_dispo_maintenance activate
            rm -f /var/lib/pacemaker/cib/*
        fi
    fi
    # Lancement corosync et pacemaker si arrêtés
    /usr/share/eole/sbin/haute_dispo_maintenance deactivate

    if [ "${activer_haute_dispo}" = "maitre" ]
    then
        /usr/share/eole/sbin/appliquer_hautedispo
        # On attend que les ressources soient montées
        wait_true_retcode "Attente ressources montées" "crm resource show 2>&1|grep -s -q 'Started'"
    fi
    # Le cluster sort du mode maintenance
    /usr/share/eole/sbin/haute_dispo_maintenance manage
    /usr/share/eole/sbin/haute_dispo_maintenance unmigrate

    if [ ! "$option" = "reconfigure" ]; then
        EchoOrange "Les serveurs maître et esclave doivent pouvoir dialoguer en ssh sur l'interface $corosync_dial_if"
        Question_ouinon "Voulez-vous synchroniser les noeuds ?" 'True' "non"
        rep=$?
    else
        rep=1
    fi
    if [ $rep -eq 0 ]
    then
        # on attend que les 2 noeuds soient Online
        crm node online ${nom_machine} > /dev/null 2>&1
        cmd="crm_mon -1|grep -qs  -e \"^Online: \[ ${nom_machine} ${remote_node} \]\" -e \"^Online: \[ ${remote_node} ${nom_machine} \]\""
        wait_true_retcode "Attente noeuds $nom_machine et ${remote_node} Online" "${cmd}"
        crm resource show VIPCluster > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
            # On attend que les ressources soient montées si elles sont déclarées
            wait_true_retcode "Attente ressources montées" "crm resource show 2>&1|grep -s -q 'Started'"
        fi
        /usr/share/eole/sbin/synchro-nodes.sh
    else
        if [ ! "$option" = "reconfigure" ]; then
            EchoOrange "Pour forcer la synchronisation, vous pouvez lancer le script 'synchro-nodes.sh' manuellement une fois les deux noeuds 'Online'"
            EchoOrange "Vous pouvez vérifier leur status à l'aide de la commande 'crm_mon -1'"
        fi
    fi
    exit 0
fi
