#!/bin/bash

log()
{
    echo $(date +"%Y-%m-%d %H:%M:%S,%3N") $@
}

wait_for_host()
{
    local host=$1

    retry=20
    while (($retry > 0)); do
        log INFO "Check $host, retry: $retry."
        ssh -o ConnectTimeout=3 root@$host date > /dev/null 2>&1
        if (($? == 0)); then
            log INFO "Host $host is up."
            break
        fi
        retry=$((retry - 1))
        sleep 4
    done
    if (($retry == 0)); then
        log ERROR "Timeout!"
        exit 1
    fi
}

set_repo()
{
    log INFO "Check repo."
    if [ ! -d "/etc/yum.repos.d.orig" ]; then
        mv /etc/yum.repos.d /etc/yum.repos.d.orig
        mkdir -p /etc/yum.repos.d
    fi
    mv /tmp/depot.repo /etc/yum.repos.d/
}

disable_selinux()
{
    log INFO "Disable SELinux."
    setenforce 0
    cat > /etc/selinux/config << __EOF__
SELINUX=permissive
SELINUXTYPE=targeted
__EOF__
}

install_redis()
{
    log INFO "Install Redis."
    dnf install -y redis

    for role in master slave; do
        log INFO "Enable redis-$role."
        cp -r /etc/systemd/system/redis.service.d \
                /etc/systemd/system/redis-$role.service.d
        file=/usr/lib/systemd/system/redis-$role.service
        cp /usr/lib/systemd/system/redis.service $file
        sed -i -e "s/redis.conf/redis-$role.conf/" \
                -e "s/shutdown/shutdown redis-$role/" $file
        mkdir /var/lib/redis-$role
        chown redis:redis /var/lib/redis-$role
        mv -f /tmp/redis-$role.conf /etc/redis/
        systemctl enable redis-$role
    done
    i=1
    while ((i <= 10)); do
        log INFO "Start redis-master and redis-slave #$i."
        systemctl restart redis-master
        systemctl restart redis-slave
        sleep 2
        systemctl is-active redis-master && systemctl is-active redis-slave
        if [ $? == 0 ]; then
            break
        else
            i=$((i + 1))
            sleep 5
        fi
    done
}

deploy()
{
    local ha=$1

    echo ""
    log INFO "Deploy Redis."
    set_repo
    disable_selinux
    install_redis
}

bootstrap()
{
    local addrs=$1

    log INFO "Bootstrap Redis cluster."
    masters=
    slaves=
    IFS=','; a=(${addrs}); unset IFS
    for ((i=0; i<$((${#a[*]})); i++)); do
        masters="$masters ${a[$i]}:6379"
        slaves="$slaves ${a[$i]}:6378"
    done
    redis-cli --cluster create $masters $slaves \
            --cluster-replicas 1 --cluster-yes
}

help()
{
    echo "Help"
    echo "$0 deploy <host> <id> <ha>"
}

start()
{
    local copy_files tmp_files host id ha

    if [ -z "$2" ]; then help; exit 1; else host=$2; fi
    if [ -z "$3" ]; then help; exit 1; else id=$3; fi

    if [ "$cli_path" != "/tmp" ]; then
        copy_files="$0"
    fi
    case "$1" in
    deploy)
        if [ "$copy_files" ]; then
            tmp_path=/tmp/$id
            copy_files="$copy_files $tmp_path/depot.repo
                    $tmp_path/redis-master.conf $tmp_path/redis-slave.conf"
        else
            deploy
        fi
        ;;
    bootstrap)
        if [ "$copy_files" ]; then
            copy_files="$copy_files"
        else
            bootstrap $4 $5 $6
        fi
        ;;
    *)
        help
        return
        ;;
    esac
    if [ "$copy_files" ]; then
        echo ""
        log INFO $@
        wait_for_host $host
        log INFO "Copy files to $host."
        scp $copy_files root@$host:/tmp/
        log INFO "Run $cli_name on $host."
        ssh root@$host /tmp/$cli_name "$@"
        log INFO "Clean up on $host."
        for f in $copy_files; do
            tmp_files="$tmp_files /tmp/$(basename $f)"
        done
        ssh root@$host rm -f $tmp_files
    fi
}


cli_name=$(basename $0)
cli_path=$(dirname $0)
start "$@"

exit 0

