`

使用fabric部署tomcat集群项目

阅读更多

参考资料:

【1】http://python.jobbole.com/87241/

 

1、Fabric简介:

Fabric是一个Python的库,它提供了丰富的同SSH交互的接口,可以用来在本地或远程机器上自动化、流水化地执行Shell命令。因此它非常适合用来做应用的远程部署及系统维护。其上手也极其简单,你需要的只是懂得基本的Shell命令。

2、Fabric安装:

 

#!/bin/bash

#create by huangdengji
#version:1.0
#create time:2017-09-27 15:22:00
#email:huangdengji@126.com

#description: install fabric
#1.install python2.7.14
#2.install pip
#3.install fabric

set -m 

#安装依赖
yum -y install wget gcc openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
cd /home/hdj/
[ -d /home/hdj/fabric ] && { 
        rm -rf /home/hdj/fabric 
}
mkdir fabric
cd fabric
#安装Python2.7
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar -xvf Python-2.7.14.tgz
cd Python-2.7.14
./configure --prefix=/usr/local/python2.7
make
make install
/usr/local/python2.7/bin/python2.7 -V
[ $? -ne 0 ] && { 
        echo 'python 2.7 install fail'
        exit 1 
}
ln -fs /usr/local/python2.7/bin/python2.7 /usr/bin/python
#由于yum依赖于Python2.6,需修改yum文件
yumFirstLine=`head -n 1 /usr/bin/yum`
[ ${#yumFirstLine} -eq 17 ] && {
        sed -i 's#/usr/bin/python#/usr/bin/python2.6#g' /usr/bin/yum
        echo 'modify /usr/bin/yum first line to /usr/bin/python2.6'
}
echo '------------------------------------------------------------------------------------------------------------------'
echo ''
echo ''
echo ''
echo ''
echo ''
echo 'python 2.7 install success'
echo ''
echo ''
echo ''
echo ''
echo ''
echo '------------------------------------------------------------------------------------------------------------------'
#安装pip
cd ..
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
ln -s /usr/local/python2.7/bin/pip /usr/bin/pip
#安装fabric
pip install fabric
ln -s /usr/local/python2.7/bin/fab /usr/bin/fab
echo '------------------------------------------------------------------------------------------------------------------'
echo ''
echo ''
echo ''
echo ''
echo ''
echo 'fab install success'
echo ''
echo ''
echo ''
echo ''
echo ''
echo '------------------------------------------------------------------------------------------------------------------'

 3、编写本地打包shell脚本

 

 

#!/bin/bash

#create by huangdengji
#version:2.0
#create time:2017-09-25 18:22:00
#email:huangdengji@126.com

#description:do repackage project to tar.gz 

set -m 

projectName="ROOT"
tomcatDir="/resources/apache-tomcat-6.0.41/webapps/"


cd ${tomcatDir}
rm -rf ${projectName}.tar.gz
tar -zcf ${projectName}.tar.gz ${projectName}

 4、编写远程服务器中,重新部署项目的shell脚本

 

 

#!/bin/bash

#create by huangdengji
#version:1.0
#create time:2017-09-12 18:22:00
#email:huangdengji@126.com

#description:do reploy tomcat project

set -m 

source /etc/profile
tomcatName='apache-tomcat-6.0.41'
projectName='ROOT'
deployEndTag='.tar.gz'
deployName=${projectName}${deployEndTag}


projectDir='/u0/'${tomcatName}'/webapps/'
#str=`ps -ef | grep ${tomcatName}`
str=`ps -ef | grep ${tomcatName} | grep -v grep | grep -v tail`
pid=`echo $str | awk '{print $2}'`
echo ${tomcatName}' pid:'${pid}

[ -f /home/hdj/${deployName} ] || {
    echo '/home/hdj/'${deployName}' not exist'
    exit 1
}

kill -9 ${pid}
if [ $? -eq 0 ]
then
    echo "kill tomcat success!"
else
    echo "kill tomcat fail!"
fi

sleep 3

while true
do
    count=`ps -ef | grep ${tomcatName} | grep -v grep | grep -v tail | wc -l`
    [ ${count} -eq 0 ] && { 
        break 
    } && { 
        kill -9 ${pid}
        sleep 1
    }
done


#path='/project_bak/'$(date +%Y%m%d%H%M)
path='/project_bak/'

if [ -d ${path} ] 
then
    rm -rf ${path}
    mkdir -p ${path}
else
    mkdir -p ${path}
fi

if [ -d ${path} ]
then
    echo 'created dir:'${path}' success!'
else
    echo 'created dir:'${path}' fail!'
    exit 1
fi


if [ -d ${projectDir}${deployName} ]
then
    mv ${projectDir}${deployName} ${path}
    if [ $? -eq 0 ]
    then
        echo 'project back to dir:'${path}' success!'
    else
        echo 'project back to dir:'${path}' fail!'
        exit 1
    fi
else
    echo ${projectDir}${deployName}' is first depoly'
fi


mv /home/hdj/${deployName} ${projectDir}
if [ $? -eq 0 ]
then
    echo 'deplory project to dir:'${projectDir}' success!'
else
    echo 'deplory project to dir:'${projectDir}' fail!'
    exit 1
fi


cd ${projectDir}

rm -rf ${project}


tar -zxf ${deployName}

/u0/${tomcatName}/bin/startup.sh
echo 'starting '${tomcatName}' ...'

 5、遍写fabric执行的Python脚本,来实现多服务器的:本地代码打包、上传至远程服务器、远程服务重启

 

#!/usr/bin/env python
# encoding: utf-8

from fabric.api import local,cd,run,env,put,sudo

env.hosts=['root@10.255.8.57:22','root@10.255.8.59:22','root@10.255.8.61:22'] #ssh要用到的参数
env.password = '111111'
tomcatName='apache-tomcat-6.0.41'
projectName='ROOT'


def local_package():
    local('sh /home/hdj/fab_task/repackage.sh')
    put('/resources/'+tomcatName+'/webapps/'+projectName+'.tar.gz', '/home/hdj/')
def update_to_remote():
    print "remote update"
    with cd('/home/hdj'):   #cd用于进入某个目录
        run('sh redeploy.sh')  #远程操作用run

def update():
    local_package()
    update_to_remote()

 

 6、执行fab命令,完成批量部署

fab -f redeploy.py update

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics