此脚本用于自动化部署redis集群和nutcracker集群,已经过调试验证。使用时需要根据实际部署修改。
#!/bin/sh
ssh_usr=cuideshun
ssh_pwd=cuideshun
nut_path=./nutcracker
nut_serv=nutcracker
nut_log=nutcracker.log
redis_path=./redis
redis_serv=redis-server
function remote_exec () {
expect -c "
set timeout 2
spawn $1
expect {
\"password:\" { send \"$ssh_pwd\r\" }
}
expect eof
"
}
function start () {
echo "begin to install ..."
nut_cluster=$(ls $nut_path | grep yml | sed 's/.yml//')
for nut in $nut_cluster;do
#install nutcracker
echo $nut_cluster
echo "begin to install nutcracker $nut"
nut_cfg=$nut".yml"
echo $nut
tmp=$(/sbin/ifconfig | grep $nut)
echo $tmp
if [ -z "$tmp" ];then
remote_exec "ssh $ssh_usr@$nut mkdir nutcracker"
remote_exec "scp $nut_path/$nut_serv $ssh_usr@$nut:~/nutcracker"
remote_exec "scp $nut_path/$nut_cfg $ssh_usr@$nut:~/nutcracker"
remote_exec "ssh $ssh_usr@$nut $nut_path/$nut_serv -c $nut_path/$nut_cfg -o $nut_path/$nut_log -d"
else
$nut_path/$nut_serv -c $nut_path/$nut_cfg -o $nut_path/$nut_log -d
fi
#install redis
redis_cluster=$(grep - $nut_path/$nut".yml" | \
sed 's/-//' | \
uniq -u)
for redis in $redis_cluster;do
redis_ip=$(echo $redis | cut -d : -f 1)
redis_port=$(echo $redis | cut -d : -f 2)
redis_cfg="redis"$redis_port".conf"
echo "begin to install redis $redis_ip"
cp $redis_path/redis.conf $redis_path/$redis_cfg
sed -i "s/^port.*/port $redis_port/" $redis_path/$redis_cfg
if [ -z "$(/sbin/ifconfig | grep $redis_ip)" ];then
remote_exec "ssh $ssh_usr@$redis_ip mkdir redis"
remote_exec "scp $redis_path/$redis_serv $ssh_usr@$redis_ip:~/redis"
remote_exec "scp $redis_path/$redis_cfg $ssh_usr@$redis_ip:~/redis"
remote_exec "ssh $ssh_usr@$redis_ip $redis_path/$redis_serv $redis_path/$redis_cfg &"
else
$redis_path/$redis_serv $redis_path/$redis_cfg &
fi
rm -rf $redis_path/$redis_cfg
done;
done;
}
function stop () {
nut_cluster=$(ls $nut_path| grep yml | sed 's/.yml//')
for nut in $nut_cluster;do
echo "begin to stop nutcracker $nut"
if [ -z "$(/sbin/ifconfig | grep $nut)" ];then
remote_exec "ssh $ssh_usr@$nut rm -rf nutcracker"
else
pkill nutcracker
fi
redis_cluster=$(grep - $nut_path/$nut".yml" | \
sed 's/-//' | \
uniq -u)
for redis in $redis_cluster;do
redis_ip=$(echo $redis | cut -d : -f 1)
echo "begin to stop redis $redis_ip"
if [ -z "$(/sbin/ifconfig | grep $redis_ip)" ];then
remote_exec "ssh $ssh_usr@$redis_ip pkill redis"
remote_exec "ssh $ssh_usr@$redis_ip rm -rf redis"
else
pkill redis
fi
done;
done;
}
function check () {
echo "begin to check ..."
nut_cluster=$(ls $nut_path| grep yml | sed 's/.yml//')
for nut in $nut_cluster;do
echo "begin to check nutcracker $nut"
if [ -z "$(/sbin/ifconfig | grep $nut)" ];then
echo "debug1"
remote_exec "ssh $ssh_usr@$nut ps aux | grep nutcracker | grep -v grep > result"
echo "debug2"
remote_exec "scp $ssh_usr@$nut:~/result ."
echo "debug3"
remote_exec "ssh $ssh_usr@$nut rm -rf result"
echo "debug4"
tmp=$(cat ./result)
rm -rf result
else
tmp=$(ps aux | grep nutcracker | grep -v grep)
fi
if [ -z "$tmp" ];then
echo "nutcracker $nut start fail" >> check-result
else
echo "nutcracker $nut start succ" >> check-result
fi
redis_cluster=$(grep - $nut_path/$nut".yml" | \
sed 's/-//' | \
uniq -u)
for redis in $redis_cluster;do
redis_ip=$(echo $redis | cut -d : -f 1)
echo "begin to check redis $redis_ip"
if [ -z "$(/sbin/ifconfig | grep $redis_ip)" ];then
remote_exec "ssh $ssh_usr@$redis_ip ps aux | grep redis | grep -v grep > result"
remote_exec "scp $ssh_usr@$redis_ip:~/result ."
remote_exec "ssh $ssh_usr@$redis_ip rm -rf result"
tmp=$(cat ./result)
rm -rf result
else
tmp=$(ps aux | grep redis | grep -v grep)
fi
if [ -z "$tmp" ];then
echo "redis $redis_ip start fail" >> check-result
else
echo "redis $redis_ip start succ" >> check-result
fi
done;
done;
echo "##################show check result##############################"
cat check-result
echo "##################show check result##############################"
rm -rf check-result
}
function restart () {
stop
start
}
if [ "$1" = "start" ];then
start
elif [ "$1" = "check" ];then
check
elif [ "$1" = "stop" ];then
stop
elif [ "$1" = "restart" ];then
restart
else
echo "usage: install start|stop|restart|check"
fi
内容来源于网络如有侵权请私信删除
- 还没有人评论,欢迎说说您的想法!