Mysql分布式数据库变更自动化部署(三)
file=$2
ssh $host "mysql -u$USER -p$PASSWD -vvv df -e \"source $releasefile;\""
if [ $? == 0 ] ; then
return 0
else
return 1
fi
}
# parse configuration file
function readcfg()
{
FIELD=$1
first=`sed -n "/\[$FIELD\]/=" $CFG`
last=`sed -n "/\[\/$FIELD\]/=" $CFG`
#echo $first,$last
sed -n "$((first+1)),$((last-1))p" $CFG > $CFGPID
sed -i '/^$/d' $CFGPID
}
# Simulation of multi thread
exec 6>&-
tmp_fifofile="$DIR/$.fifo"
mkfifo $tmp_fifofile
exec 6<>$tmp_fifofile
rm $tmp_fifofile
thread=40
for (( i=0;i<=$thread;i++ )); do
echo
done >&6
# ftp mode
if [ 'x'$OPTTYPE = 'xftp' ] ; then
echo ""
read -p "Please confirm that you have put the FTP into directory ${RELEASEDIR} [Y/N]: " next
case $next in
Y )
echo '' >/dev/null 2>&1
;;
N )
echo "exit from ftp mode."
exit 1
;;
* )
echo "error input"
;;
esac
readcfg ftpnode
# The number of hosts
NODES=`cat $CFGPID | wc -l`
# To determine the amount of release files
TOTAL=`ls $RELEASEDIR|wc -l`
if [ $TOTAL -gt 0 ] ; then
LIST=`ls -lrth $RELEASEDIR|awk '{print $9}'`
echo ""
echo "The following is the transmitted release file:"
echo "********************************"
ls -lrth $RELEASEDIR|grep -E -v '^total'|awk '{print $9}'
echo "********************************"
read -p "Are you sure you want to upload the above files to all specified nodes[Y/N]? " answer
case $answer in
Y)
i=0
for L in $LIST
do
i=`expr $i + 1`
for h in `cat $CFGPID`
do
# Create remote directory not exist
ssh $h "ls ${RDIR} |wc -l" >/dev/null 2>&1
if [ $? -ne 0 ];then
mkdir ${RDIR}
echo "Created directory ${RDIR} on $h."
fi
read -u6
{
multi_ftp $h $L
echo >&6
}&
done
wait
done
if [ $TOTAL -eq $i ] ; then
echo ""
echo "ALL release files are transferd to all specified nodes"
fi
;;
N)
echo "You have exit from ftp mode"
exit 0
;;
*)
echo "error input"
;;
esac
else
echo "No release file in release directory."
exit 1
fi
elif [ 'x'$OPTTYPE = 'xdeploy' ] ;then
if [ 'x'$RELEASEFILE = 'x' ] ;then
echo "Please give release file you want to depoly!"
exit 1
fi
if [ -f $RELEASEDIR/$RELEASEFILE ]; then
# try deploy for one node
readcfg deployfirst
# The number deployed first of hosts
NODE_FIRST=`cat $CFGPID | wc -l`
if [ $NODE_FIRST -ge 1 ]; then
for h in `cat $CFGPID`
do
deploylog=$DIRLOG/${h}_${CURPID}_process_deploy.log
echo "*************************************************"
echo "[$DATE]: Preparing to deploy $RELEASEFILE to [$h]..."
echo "*************************************************"
if [ -f "$DIR/issucess" ] ; then
echo "You deployed success this file ago on first node,Please delete file [$DIR/issucess] if you want to continue."
exit 1
fi
releasefile=$RDIR/$RELEASEFILE
multi_deploy $h $releasefile >
$deploylog 2 >&1
#multi_deploy $h $releasefile
if [ $? -eq 0 ]; then
echo "[$DATE]: The release file [$RELEASEFILE] have been deployed to $h success."
echo "The detail you refer to [$DEPLOYLOG]."
echo "[$RELEASEFILE] have been deployed one or more node,please check log!!!" > $DIR/issucess
read -p "Are you sure you want to continut deploy to remaining nodes[Y/N]? " continue
case $continue in
Y)
continue
;;
N)
echo "Input: $continue, Exit from Deployment process."
exit 1
;;
*)
echo "Error input"
exit 1
;;
esac
else
echo "[$DATE]: Release file [$RELEASEFILE] is deployed to $h fail,Please check relea