#!/bin/bash -x
# 配置集群服务

if [ -z "$SSH" ];then
    source /unas/sbin/dualcontroller/config/prepare
fi

if [ -z "$PCS" ];then
    PCS=pcs
fi

syncconf_enable_dir=/unas/etc/dualcontroller/syncconf-enabled
syncconf_avail_dir=/unas/etc/dualcontroller/syncconf-available
# 共享存储上的状态目录基准
DATA_POOL_CFG="/unas/etc/dualcontroller/cfg/dualcontroller"

srvsync() {
    name=$1
    act=$2
    source /unas/etc/dualcontroller/srv/$name
    if [ -n "$SERVICE" ];then
        /unas/sbin/dualcontroller/tool/nodes systemctl $act --now $SERVICE
    fi
    SERVICE=
}

ctdb_setup() {
    # ctdb 资源，提供给需要双活的服务使用

    if [ ! -d $DATA_POOL_CFG/run ]; then
        mkdir -p $DATA_POOL_CFG/run
    fi

    if ! $PCS resource config SRV-ctdb-pre >/dev/null 2>&1; then
        $PCS resource create SRV-ctdb-pre ocf:heartbeat:anything \
            binfile="/bin/bash" \
            cmdline_options="-c '/unas/sbin/dualcontroller/tool/nodes > /etc/ctdb/nodes && exec sleep infinity'" \
            op monitor interval=30s
        $PCS resource clone SRV-ctdb-pre meta clone-max=2 clone-node-max=1 notify=true interleave=true
        $PCS constraint colocation add SRV-ctdb-pre-clone with ocfs0-clone
        $PCS constraint order ocfs0-clone then SRV-ctdb-pre-clone
    fi
    if ! $PCS resource config SRV-ctdb >/dev/null 2>&1; then
        $PCS resource create SRV-ctdb ocf:heartbeat:CTDB \
            ctdb_recovery_lock="$DATA_POOL_CFG/run/ctdb.lock" \
            ctdb_dbdir="/var/lib/ctdb" \
            op monitor interval=10s timeout=20s
        $PCS resource clone SRV-ctdb meta clone-max=2 clone-node-max=1 notify=true interleave=true
        $PCS constraint colocation add SRV-ctdb-clone with SRV-ctdb-pre-clone
        $PCS constraint order SRV-ctdb-pre-clone then SRV-ctdb-clone
    fi
}

handover() {
    name=$1
    [ -f $syncconf_avail_dir/$name ] && ln -sf $syncconf_avail_dir/$name $syncconf_enable_dir/
    /unas/sbin/dualcontroller/tool/distribute $syncconf_enable_dir/

    res_name="UNAS_$name"
    if $PCS resource config $res_name >/dev/null 2>&1; then return 0; fi

    srvsync $name disable

    mkdir -p $DATA_POOL_CFG/$name

    # 针对不同服务的双活特殊配置
    case "$name" in
        main)
            # main 资源包含 account/apache/desktop/system，算是基础核心，其他服务依赖它
            $PCS resource create $res_name systemd:dc-srv@main \
                op stop timeout=20s on-fail=ignore
            if [ "$RUNMODE" == "aa" ]; then
                # 双活模式需要 ctdb 来同步状态，所以放在前面
                $PCS resource clone $res_name meta clone-max=2 clone-node-max=1 notify=true interleave=true
                $PCS constraint colocation add $res_name-clone with SRV-ctdb-clone
                $PCS constraint order SRV-ctdb-clone then $res_name-clone
            else
                # 主备模式，绑定主节点
                $PCS constraint colocation add $res_name with UNAS_Main_Node
                $PCS constraint order MNT_unas_etc_dualcontroller_cfg then $res_name kind=Optional
            fi
            ;;
        smb)
            $PCS resource create $res_name systemd:dc-srv@$name \
                op stop timeout=20s on-fail=ignore
            if [ "$RUNMODE" == "aa" ]; then
                # 双活模式需要 ctdb 来同步状态，所以放在前面
                $PCS resource clone $res_name meta clone-max=2 clone-node-max=1 notify=true interleave=true
                $PCS constraint colocation add $res_name-clone with SRV-ctdb-clone
                $PCS constraint order SRV-ctdb-clone then $res_name-clone

                # SMB 需要 CTDB 来同步 TDB 数据库和处理文件锁
                $PCS constraint colocation add $res_name-clone with SRV-ctdb-clone
                $PCS constraint order SRV-ctdb-clone then $res_name-clone
                /unas/sbin/dualcontroller/tool/ctdb add
            else
                # 主备模式，绑定主节点
                $PCS constraint colocation add $res_name with UNAS_Main_Node
                $PCS constraint order MNT_unas_etc_dualcontroller_cfg then $res_name kind=Optional
            fi
            ;;
        nfs)
            $PCS resource create $res_name systemd:dc-srv@$name \
                op stop timeout=20s on-fail=ignore
            if [ "$RUNMODE" == "aa" ]; then
                $PCS resource clone $res_name meta clone-max=2 clone-node-max=1 notify=true interleave=true

                $PCS constraint colocation add $res_name-clone with SRV-ctdb-clone
                $PCS constraint order SRV-ctdb-clone then $res_name-clone
            else
                # 主备模式，绑定主节点
                $PCS constraint colocation add $res_name with UNAS_Main_Node
                $PCS constraint order MNT_unas_etc_dualcontroller_cfg then $res_name kind=Optional
                $PCS constraint order mount_ready then $res_name kind=Optional
            fi
            ;;
        ftp)
            $PCS resource create $res_name systemd:dc-srv@$name \
                op stop timeout=20s on-fail=ignore
            if [ "$RUNMODE" == "aa" ]; then
                $PCS resource clone $res_name meta clone-max=2 clone-node-max=1 notify=true interleave=true

                $PCS constraint colocation add $res_name-clone with ocfs0-clone
                $PCS constraint order ocfs0-clone then $res_name-clone
            else
                # 主备模式，绑定主节点
                $PCS constraint colocation add $res_name with UNAS_Main_Node
                $PCS constraint order MNT_unas_etc_dualcontroller_cfg then $res_name kind=Optional
            fi
            ;;
        iscsi)
            if [ "$RUNMODE" == "aa" ]; then
            # AA 模式下用的方式不一样
                true
                ## 约束：必须在共享存储 ocfs0 挂载后启动
                #$PCS constraint colocation add $res_name-clone with ocfs0-clone
                #$PCS constraint order ocfs0-clone then $res_name-clone
            else
                $PCS resource create $res_name systemd:dc-srv@$name \
                    op stop timeout=20s on-fail=ignore
                # 主备模式，绑定主节点
                $PCS constraint colocation add $res_name with UNAS_Main_Node
                $PCS constraint order MNT_unas_etc_dualcontroller_cfg then $res_name kind=Optional
            fi
            ;;
        afp)
            $PCS resource create $res_name systemd:dc-srv@$name \
                op stop timeout=20s on-fail=ignore
            if [ "$RUNMODE" == "aa" ]; then
                $PCS resource clone $res_name meta clone-max=2 clone-node-max=1 notify=true interleave=true

                # 约束：必须在共享存储 ocfs0 挂载后启动
                $PCS constraint colocation add $res_name-clone with ocfs0-clone
                $PCS constraint order ocfs0-clone then $res_name-clone
            else
                # 主备模式，绑定主节点
                $PCS constraint colocation add $res_name with UNAS_Main_Node
                $PCS constraint order MNT_unas_etc_dualcontroller_cfg then $res_name kind=Optional
            fi
            ;;
    esac

}

handoff() {
    name=$1
    res_name="UNAS_$name"
    if $PCS resource config $res_name >/dev/null 2>&1; then
        $PCS resource remove $res_name
        [ -L $syncconf_enable_dir/$name ] && rm $syncconf_enable_dir/$name
        /unas/sbin/dualcontroller/tool/distribute $syncconf_enable_dir/
        srvsync $name enable
    fi
}

# 基础核心 (main 资源包含 account/apache/desktop/system)
for core in account apache desktop system; do
    [ -L $syncconf_enable_dir/$core ] || ln -sf $syncconf_avail_dir/$core $syncconf_enable_dir/
done
if [ "$RUNMODE" == "aa" ]; then
    # 双活模式需要 ctdb 来同步状态，所以放在前面
    ctdb_setup
fi
handover main

for srv in smb nfs ftp iscsi afp; do
    # 将服务名转为大写变量名 (如 smb -> SMB) 进行检查
    VAR_NAME=$(echo "$srv" | tr '[:lower:]' '[:upper:]')
    eval val=\$$VAR_NAME
    if [ "$val"z = 1z ];then
        handover $srv
    else
        handoff $srv
    fi
done

if [ `basename $0` == service ];then
    exit 0
fi
