#!/bin/bash
# 移交服务 及 清理服务

# 断电这个脚本执行的任务没生效
# 尝试打印到日志
set -x
exec >> /unas/tmp/dc-handover-$2.log 2>> /unas/tmp/dc-handover-$2-err.log
date

# 如果时间足够，其实不用单独移交服务，直接fence就行

handover_res=(UNAS_Node_IP0 UNAS_Node_IP1)

case "$1" in
    ban)
        echo "Ban resources on this node"
        host=$(hostname)
        for res in "${handover_res[@]}";do
            pcs resource ban $res $host
        done
        ;;
    clear)
        echo "Clear resources on this node"
        host=$(hostname)
        for res in "${handover_res[@]}";do
            pcs resource clear $res $host
        done
        pcs resource cleanup
        ;;
    off)
        host=$(hostname)
        # 强制关机
        pcs stonith fence $host --off
        ;;
    offline)
        echo "Ban resources on this node"
        host=$(hostname)
        for res in "${handover_res[@]}";do
            pcs resource ban $res $host
        done
        # 如果不开始关机，它会一直运行下去
        # 由于停止过程有些资源会出错（比如拔出节点后ocfs2全都连不上了），会触发stonith
        # 由于硬件固件设置，stonith会直接断电，但不会正确返回退出码
        # 服务都能正确移交，但是ocfs2等资源报错，应该影响不大，UNCLEAN也无所谓
        poweroff
        # 强制关机
        #pcs stonith fence $host --off
        ;;
    *)
        echo "Usage: $0 {ban|clear|off|offline}"
        exit 1
        ;;
esac

