Shell脚本实现自动安装zookeeper

2019-09-23 09:28:53丽君

#   3) install_zookeeper "2181" "192.168.122.201:2888:3888" "192.168.122.202:2888:3888" "192.168.122.203:2888:3888" 
#   4) install_zookeeper "2181:/var/lib/zookeeper" "192.168.122.201:2888:3888" "192.168.122.202:2888:3888" "192.168.122.203:2888:3888" 
#*********************************************************** 
ERR_INVALID_ZK_SERVERS=1001 
 
function install_zookeeper() { 
    echo -e "<INFO> install zookeeper on cluster ..." 
    #chk_root 
 
    local ret clientPort dataDir len i ZOO_CFG server serverPort appPort destip destlogin 
 
    serverPort=2888 
    appPort=3888 
 
    # parse the first argument 
    split_to_array $1 ":" 
 
    # the port at which the clients will connect 
    clientPort=${array[0]} 
 
    # the directory where the snapshot is stored 
    dataDir="/var/lib/zookeeper" 
    if [ ${#array[*]} -eq 2 ]; then 
        dataDir=${array[1]} 
    fi 
 
    echo -e "<INFO> clientPort: $clientPort" 
    echo -e "<INFO> dataDir: $dataDir" 
 
    # zookeeper configure file 
    ZOO_CFG="/usr/lib/zookeeper/conf/zoo.cfg" 
 
    # get list of servers: args 
    shift 
    local argc=$# 
 
    if [ $argc -eq 1 -o $argc -eq 3 -o $argc -eq 5 -o $argc -eq 7 ]; then 
        echo -e "<INFO> zookeeper servers in cluster: [$argc]" 
    else 
        echo -e "<ERROR> invalid zookeeper servers: [$argc]" 
        exit $ERR_INVALID_ZK_SERVERS; 
    fi 
 
    local argv="$@" 
 
    OLD_IFS="$IFS" 
    IFS=" " 
    local args=($argv) 
    IFS="$OLD_IFS" 
 
    # array variable 
    local ipaddrs=() 
    local servers=() 
 
    local sid=0 
    for a in ${args[@]} 
    do 
        let sid++ 
 
        # check if server format is either of: