tomcat自动部署代码脚本

http://ask.apelearn.com/question/8020


脚本如下:==============================================================================================
#!/usr/bin/env python
##coding:utf-8
#written by xeonzhang
#Description: 自动部署tomcat程序,当启动成功nginx自动切换配置转发端口
#Create_time:20150519

import os
import subprocess 
import re
import sys
import time
import datetime
import paramiko
import pexpect 
import math
import urllib2

        

def fail_to_delete(mount_path):
    #通过nginx配置文件判断目前哪一个tomcat在运行
        is_tomcat8080_start = 'cat /usr/share/nginx/conf/nginx.conf | grep tomcat6_4_8080.conf | wc -l'
        is_tomcat8080_count = int(subprocess.check_output(is_tomcat8080_start,shell=True).strip())
        if is_tomcat8080_count == 1:
            #为1则将tomcat6_4_8082所在的服务停止
            stop_tomcat8082 = '/usr/share/tomcat6_4_8082/start.sh stop'
            os.system(stop_tomcat8082)
                print "已关闭启动失败的tomcat进程"
        #删除tomcat6_4_8082所有传进去的代码
                rm_tomcat8082_code = 'rm -rf /usr/share/tomcat6_4_8082/lubanwebapps/*'
                os.system(rm_tomcat8082_code)
                print "已删除启动失败的程序代码!"
                #传False值给外部程序
        echo_status_command = "echo ' ' >%s/tomcat_log/False" %mount_path
                os.system(echo_status_command)
        else:
            #不为1则将tomcat6_4_8080所在的服务停止
                stop_tomcat8080 = '/usr/share/tomcat6_4_8080/start.sh stop'
            os.system(stop_tomcat8080)
                print "已关闭启动失败的tomcat进程"
        #删除tomcat6_4_8080所有传进去的代码
                rm_tomcat8080_code = 'rm -rf /usr/share/tomcat6_4_8080/lubanwebapps/*'
                os.system(rm_tomcat8080_code)
                print "已删除启动失败的程序代码!"
                #传False值给外部程序
        echo_status_command = "echo ' ' >%s/tomcat_log/False" %mount_path
                os.system(echo_status_command)


def change_nginx_conf(mount_path):
    #通过nginx配置文件判断目前哪一个tomcat在运行
        is_tomcat8080_start = 'cat /usr/share/nginx/conf/nginx.conf | grep tomcat6_4_8080.conf | wc -l'
        is_tomcat8080_count = int(subprocess.check_output(is_tomcat8080_start,shell=True).strip())
        #为True则将配置文件切换成tomcat6_4_8082.conf
        if is_tomcat8080_count == 1:
            change_conf_command = "sed -i 's#tomcat6_4_8080.conf#tomcat6_4_8082.conf#g' /usr/share/nginx/conf/nginx.conf"
        os.system(change_conf_command)
        print "nginx配置已经成功切换至tomcat6_4_8082.conf!"
                #reload nginx 
                nginx_reload_command = '/etc/init.d/nginx reload'
                os.system(nginx_reload_command)
                #传True值给外部程序
        echo_status_command = "echo ' ' >%s/tomcat_log/True" %mount_path
                os.system(echo_status_command)
                #结束tomcat6_4_8080进程
                stop_tomcat8080_pid = '/usr/share/tomcat6_4_8080/start.sh stop'
                os.system(stop_tomcat8080_pid)
                print "现tomcat启动成功,原有tomcat服务进程已停止"
                #删除tomcat6_4_8080目录下的所有代码
                rm_8080_code = 'rm -rf /usr/share/tomcat6_4_8080/lubanwebapps/*'
                os.system(rm_8080_code)
                print "现tomcat启动成功,原有tomcat代码已删除"
    else:
        change_conf_command = "sed -i 's#tomcat6_4_8082.conf#tomcat6_4_8080.conf#g' /usr/share/nginx/conf/nginx.conf"
        os.system(change_conf_command)
        print "nginx配置已经成功切换至tomcat6_4_8080.conf!"
                #reload nginx 
                nginx_reload_command = '/etc/init.d/nginx reload'
                os.system(nginx_reload_command)
                #传True值给外部程序
        echo_status_command = "echo ' ' >%s/tomcat_log/True" %mount_path
        os.system(echo_status_command)        
                #结束tomcat6_4_8082进程
                stop_tomcat8082_pid = '/usr/share/tomcat6_4_8082/start.sh stop'
                os.system(stop_tomcat8082_pid)
                print "现tomcat启动成功,原有tomcat服务进程已停止"
        #删除tomcat6_4_8082目录下的所有代码
                rm_8082_code = 'rm -rf /usr/share/tomcat6_4_8082/lubanwebapps/*'
                os.system(rm_8082_code)
        print "现tomcat启动成功,原有tomcat代码已删除"                
        
def tomcat_check_httpcode(code_path, port):
    #循环查询当前几个项目返回的httpcode值
        webapps_dir_list = os.listdir(code_path)
    for w in webapps_dir_list:
            url = ' http://localhost:8082/%s' %(port, w.split('.')[0])
                response = 'None'
                try:
            response = urllib2.urlopen(url,timeout=5)
                        if str(response.getcode())[0] != '2':
                            return False
                        response.close()        
        except urllib2.URLError as e:
            if hasattr(e, 'code'):
                print 'Error code:',e.code
                                return False
            elif hasattr(e, 'reason'):
                print 'Reason:',e.reason
                                return False
                
        return True
        
        
        

        
def tomcat_check_error(mount_path):
    #判断挂载目录下日志文件目录是否存在,存在则删除
        test_log_exit = 'test -d %s/tomcat_log && rm -rf %s/tomcat_log' %(mount_path, mount_path)
    #通过nginx配置文件判断目前哪一个tomcat在运行
        is_tomcat8080_start = 'cat /usr/share/nginx/conf/nginx.conf | grep tomcat6_4_8080.conf | wc -l'
        is_tomcat8080_count = int(subprocess.check_output(is_tomcat8080_start,shell=True).strip())
        boolean_is_success = True
        if is_tomcat8080_count == 1:
            #删除tomcat8082内所有不需要的遗留物
                rm_tomcatlog_in_mount = 'test -d %s/tomcat_log && rm -rf %s/tomcat_log' %(mount_path, mount_path)
                rm_tomcat8082_pre_code = 'rm -rf /usr/share/tomcat6_4_8082/lubanwebapps/*'
                rm_tomcat8082_pre_logs = 'rm -rf /usr/share/tomcat6_4_8082/logs/*'
                os.system(rm_tomcat8082_pre_code)
                os.system(rm_tomcat8082_pre_logs)
                os.system(rm_tomcatlog_in_mount)
            #将外面挂载目录的数据拷贝到8082端口容器的webapps目下
                cp_to8082_command = 'cp -a %s/* /usr/share/tomcat6_4_8082/lubanwebapps/' %mount_path
                print "开始拷贝编译包到tomcat_8082目录..."
                os.system(cp_to8082_command)
                #清理tomcat8082 日志目录
                print "拷贝数据完成!"
                print "+----------------------------------------------------------+"
        print "***************开始启动tomcat_8082程序 *******************"
        print "+----------------------------------------------------------+"
                start_tomcat8082 = '/usr/share/tomcat6_4_8082/start.sh start'
                os.system(start_tomcat8082)
                #软链接日志文件到挂载目录里面
                ln_command = 'ln -s /home/www/data/log/tomcat6_4_8082 %s/tomcat_log' %mount_path
                os.system(ln_command)
                #循环100次
                for i in range(1,100):
                    ##根据tomcat日志判断启动是否成功
            list_error_keywords = ['Exception','SEVERE','ERROR','严重','错误']
                        list_success_keywords = []
                        #当有一个或多个错误关键字时候判断tomcat启动失败
            for e in list_error_keywords:
                is_error_command = 'cat /home/www/data/log/tomcat6_4_8082/catalina.out | grep %s | wc -l ' %e
                is_error_count = int(subprocess.check_output(is_error_command,shell=True).strip())
                if is_error_count != 0:
                    print "tomcat8082启动失败!"
                                        #停止tomcat8082的启动,并删除目录内的文件
                                        boolean_is_success = False
                    return False
                                        
                        #判断当返回httpcode值都是2开头的状态码时,返回True
                        if tomcat_check_httpcode('/usr/share/tomcat6_4_8082/lubanwebapps','8082'):
                            return True                                
            #休眠5秒                                        
            time.sleep(5)
                        
        #当参数为True的时候,判断tomcat启动成功
            if boolean_is_success == True:
                    return True                        

    else:
            #删除tomcat8080内所有不需要的遗留物
                rm_tomcat8080_pre_code = 'rm -rf /usr/share/tomcat6_4_8080/lubanwebapps/*'
                rm_tomcat8080_pre_logs = 'rm -rf /usr/share/tomcat6_4_8080/logs/*'
                rm_tomcatlog_in_mount = 'test -d %s/tomcat_log && rm -rf %s/tomcat_log' %(mount_path, mount_path)
                os.system(rm_tomcat8080_pre_code)
                os.system(rm_tomcat8080_pre_logs)
                os.system(rm_tomcatlog_in_mount)
        #将外面挂载目录的数据拷贝到8080端口容器的webapps目下
        cp_to8080_command = 'cp -a %s/* /usr/share/tomcat6_4_8080/lubanwebapps/'        %mount_path        
                print "开始拷贝编译包到tomcat_8080目录..."
                os.system(cp_to8080_command)
                print "拷贝数据完成!"
                print "+----------------------------------------------------------+"
        print "***************开始启动tomcat_8080程序 *******************"
        print "+----------------------------------------------------------+"
                start_tomcat8080 = '/usr/share/tomcat6_4_8080/start.sh start'
                os.system(start_tomcat8080)    
                #软链接日志文件到挂载目录里面
                ln_command = 'ln -s /home/www/data/log/tomcat6_4_8080 %s/tomcat_log' %mount_path
                for i in range(1,100):
                    ##根据tomcat日志判断启动是否成功
            list_error_keywords = ['Exception','SEVERE','ERROR','严重','错误']
                        list_success_keywords = []
                        #当有一个或多个错误关键字时候判断tomcat启动失败
            for e in list_error_keywords:
                is_error_command = 'cat /home/www/data/log/tomcat6_4_8080/catalina.out | grep %s | wc -l ' %e
                is_error_count = int(subprocess.check_output(is_error_command,shell=True).strip())
                if is_error_count != 0:
                    print "tomcat8080启动失败!"
                                        #停止tomcat8080的启动,并删除目录内的文件
                                        boolean_is_success = False
                    return False
                        #判断当返回httpcode值都是2开头的状态码时,返回True
                        if tomcat_check_httpcode('/usr/share/tomcat6_4_8080/lubanwebapps','8080'):
                            return True
            #休眠5秒                                        
            time.sleep(5)
                #当参数为True的时候,判断tomcat启动成功
            if boolean_is_success == True:
                    return True
                        

#主程序入口
if __name__ == "__main__" :
    #argv
    mount_path = '/usr/share/codes'
        #判断tomcat是否启动成功
    if tomcat_check_error(mount_path): 
            #成功则切换nginx配置,并且返回一个文件告知是否成功
        change_nginx_conf(mount_path)
                
        
    else:
            #不成功则删除该失败tomcat内的所有程序,并且返回一个文件告知启动失败
        fail_to_delete(mount_path)

==================================================================================
功能需求:在一台服务器或者docker容器内 安装了2个tomcat+1个nginx
nginx:用于转发
tomcat:一个正常启动运行,当有新的代码提交时候,自动将代码存入另一个非启动tomcat内,并且启动。
             判断:当新tomcat启动成功,则停掉原来的tomcat,并且修改nginx转发端口
                       当新tomcat启动不成功,则删除新tomcat内代码。返回启动不成功信息。
心得:需求不复杂,只是细节考虑的要全面,什么时候要删除什么,逻辑需要清晰。另外就是要不停的测试脚本有没有BUG存在!
        用到了urllib2库,还判断http get的code值是不是200.这是新运用到的库。

猜你喜欢

转载自blog.csdn.net/gingerredjade/article/details/71340111