cluster.sh 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/bin/bash 
stty erase '^H'
stty erase '^?'

# 运行前需要安装expect; apt install expect
# 运行方式:
# ./cluster.sh -c xxx.cfg
# cfg文件内格式: 每行代表一个节点 第一列为external ip、第二列为密码、第三列为用户名、第四列为hostname、第五列为interal ip
# 注意:列与列直接用空格隔开
# 例子:
# 51.143.97.155 tbase125! root node5 10.2.0.10
# 20.94.253.116 tbase125! root node2 10.2.0.12
# 20.94.250.236 tbase125! root node3 10.2.0.13
# 20.98.72.51 tbase125! root node4 10.2.0.14

menu(){
    echo "=============================="
    echo "-------------Target-----------"
    echo "=============================="
    echo "1 cluster"
    echo "=============================="
    echo "2 dnode"
    echo "=============================="
    echo "3 arbitrator"
    echo "=============================="
26 27 28
    echo "4 alter replica"
    echo "=============================="
    echo "5 exit"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    echo "=============================="
}

cluster_menu(){
    echo "=============================="
    echo "----------Operation-----------"
    echo "=============================="
    echo "1 start cluster"
    echo "=============================="
    echo "2 stop cluster"
    echo "=============================="
    echo "3 exit"
    echo "=============================="
}

dnode_menu(){
    echo "=============================="
    echo "----------Operation-----------"
    echo "=============================="
    echo "1 start dnode"
    echo "=============================="
    echo "2 stop dnode"
    echo "=============================="
    echo "3 add dnode"
    echo "=============================="
    echo "4 drop dnode"
    echo "=============================="
    echo "5 exit"
    echo "=============================="
}

arbitrator_menu(){
    echo "=============================="
    echo "----------Operation-----------"
    echo "=============================="
    echo "1 start arbitrator"
    echo "=============================="
    echo "2 stop arbitrator"
    echo "=============================="
    echo "3 exit"
    echo "=============================="
}

print_cfg() {
    echo "=============================="
    echo "-------Configure file---------"
    echo "=============================="
    echo "Id |   IP address   |  hostname"
    i=1
    while read line || [[ -n ${line} ]]
    do
        arr=($line)
        echo " $i | ${arr[0]} | ${arr[3]}" 
        i=`expr $i + 1`;
    done < $1
    echo "=============================="
}

update(){
    expect -c "
        set timeout -1;
        spawn ssh $3@$1;
        expect {
            *yes/no* { send \"yes\r\"; exp_continue }
            *assword:* { send \"$2\r\" }
        }
        expect {
            *#* { send \"systemctl $4 taosd\r\" }
        }
        expect {
            *#* { send \"exit\r\" }
        }
        expect eof;
    "
    echo -e "\033[32mdnode successfully $4 \033[0m"
}

update_dnode(){
    i=1
    while read line || [[ -n ${line} ]]
    do
        if [[ $1 -eq $i ]]; then
        arr=($line)
        update ${arr[0]} ${arr[1]} ${arr[2]} $2
        break;
        fi
        i=`expr $i + 1`;
    done < $3
}

add_hosts() {
    expect -c "
        set timeout -1;
        spawn ssh $3@$1;
        expect {
            *yes/no* { send \"yes\r\"; exp_continue }
            *assword:* { send \"$2\r\" }
        }
        expect {
            *#* { send \"echo $4 $5 >> /etc/hosts\r\" }
        }
        expect {
            *#* { send \"exit\r\" }
        }
        expect eof;
        "
    echo -e "\033[32mSuccessfully add to /etc/hosts in $1\033[0m"
}

remove_hosts() {
    expect -c "
        set timeout -1;
        spawn ssh $3@$1;
        expect {
            *yes/no* { send \"yes\r\"; exp_continue }
            *assword:* { send \"$2\r\" }
        }
        expect {
            *#* { send \"sed -i '/$4/d\' /etc/hosts\r\" }
        }
        expect {
            *#* { send \"exit\r\" }
        }
        expect eof;
        "
    echo -e "\033[32mSuccessfully remove from /etc/hosts in $1\033[0m"
}

remove_varlibtaos() {
    expect -c "
        set timeout -1;
        spawn ssh $3@$1;
        expect {
            *yes/no* { send \"yes\r\"; exp_continue }
            *assword:* { send \"$2\r\" }
        }
        expect {
            *#* { send \"rm -rf /var/lib/taos/*\r\" }
        }
        expect {
            *#* { send \"exit\r\" }
        }
        expect eof;
        "
    echo -e "\033[32mSuccessfully remove /var/lib/taos/* in $1\033[0m"
}

scp_cfg() {
    expect -c "
        set timeout -1;
        spawn scp /etc/taos/taos.cfg $3@$1:/etc/taos;
        expect {
            *yes/no* { send \"yes\r\"; exp_continue }
            *assword:* { send \"$2\r\" }
        }
        expect eof;
        "
    echo -e "\033[32mSuccessfully scp /etc/taos/taos.cfg to $1\033[0m"
}

manage_dnode(){
    i=1
    while read line || [[ -n ${line} ]]
    do
        if [[ $1 -eq $i ]]; then
        arr=($line)
        scp_cfg ${arr[0]} ${arr[1]} ${arr[2]}
        ip=${arr[0]}
        pd=${arr[1]}
        user=${arr[2]}
        j=1
        while read line2 || [[ -n ${line2} ]]
        do
            arr2=($line2)
            if [[ $1 -ne $j ]]; then
                if [ $3 == "create" ];then
                echo "$3"
                add_hosts $ip $pd $user ${arr2[4]} ${arr2[3]}
                else
                remove_hosts $ip $pd $user ${arr2[4]} ${arr2[3]}
                fi
            fi
            j=`expr $j + 1`;
        done < $2
        remove_varlibtaos $ip $pd $user
        if [ $3 == "create" ];then
            update $ip $pd $user "start"
        else
            update $ip $pd $user "stop"
        fi
        taos -s "$3 dnode \"${arr[3]}:6030\""
        break;
        fi
        i=`expr $i + 1`;
    done < $2
    echo -e "\033[32mSuccessfully $3 dnode id $1\033[0m"
}

update_cluster() {
    while read line || [[ -n ${line} ]]
    do
        arr=($line)
        if [ $1 == "start" ]; then
        scp_cfg ${arr[0]} ${arr[1]} ${arr[2]}
        fi
        update ${arr[0]} ${arr[1]} ${arr[2]} $1
    done < $2
}

while :
do
    clear
    menu
    read -p "select mode: " n
    case $n in
    1)
        clear
        print_cfg $2
        cluster_menu
        read -p "select operation: " c
        case $c in
        1)
            update_cluster "start" $2 
            break
        ;;
        2)
            update_cluster "stop" $2 
            break
        ;;
        3)
            break
        ;;
        esac
    ;;
    2)
        clear
        print_cfg $2
        dnode_menu
        read -p "select operation: " d
        case $d in
        1)
            clear
            print_cfg $2
            read -p "select dnode: " id
            update_dnode $id "start" $2
            break
        ;;
        2)
            clear
            print_cfg $2
            read -p "select dnode: " id
            update_dnode $id "stop" $2
            break
        ;;
        3)
            clear
            print_cfg $2
            read -p "select dnode: " id
            manage_dnode $id $2 "create"
            break
        ;;
        4)
            clear
            print_cfg $2
            read -p "select dnode: " id
            manage_dnode $id $2 "drop"
            break
        ;;
        5)
            break
        ;;
        esac
    ;;
    3)
        clear
        arbitrator_menu
        read -p "select operation: " m
        case $m in
        1)
            nohup /usr/local/taos/bin/tarbitrator >/dev/null 2>&1 &
            echo -e "\033[32mSuccessfully start arbitrator $3 \033[0m"
            break
        ;;
        2)
            var=`ps -ef | grep tarbitrator | awk '{print $2}' | head -n 1`
            kill -9 $var
315
            echo -e "\033[32mSuccessfully stop arbitrator $3 \033[0m"
316 317 318 319 320 321 322 323
            break
        ;;
        3)
            break
        ;;
        esac
    ;;
    4)
324 325 326 327 328 329 330
        read -p "Enter replica number: " rep
        read -p "Enter database name: " db
        taos -s "alter database $db replica $rep"
        echo -e "\033[32mSuccessfully change $db's replica to $rep \033[0m"
        break
    ;;
    5)
331 332 333 334
        break
    ;;
    esac
done