nginx tomcat集羣配置實現無痛重啓服務教程python語言版本

原文:nginx tomcat集羣配置實現無痛重啓服務教程python語言版本

源代碼下載地址:http://www.zuidaima.com/share/1876042912140288.htm


上一次分享的是shell版本的:nginx tomcat集羣配置實現無痛重啓服務教程

感覺shell語法的怪異實在難以忍受,但java在處理腳本,和shell交互方面的天然弱勢導致我最終選擇了python來做最代碼的各種腳本實現,通過實現無痛重啓tomcat的腳本後發現除了調試不方便外,python作爲腳本和shell交互簡直是神器。

下面是腳本實現:

#encoding=utf8

import re
import os
import commands
import time
import urllib2
from urllib2 import URLError
import socket

tomcat_ps_name="apache-tomcat-6_"
flag=tomcat_ps_name+"\d{4}"
ports=["8080", "8081"]
nginx_path="/usr/local/nginx/"
nginx_vhost_conf_path=nginx_path+"conf/vhost/"
nginx_bin_path=nginx_path+"sbin/nginx"
zuidaima_conf_name="www.zuidaima.com_%s.conf"
tomcat_path="/usr/local/apache-tomcat-_"
zuidaima_conf_name_bak_suffix=".bak"
tomcat_startup_bin_path="/bin/startup.sh"
tomcat_shutdown_bin_path="/bin/shutdown.sh"
ps_grep_tomcat="ps -ef|grep "
zuidaima_domain="http://www.zuidaima.com"
kill_tomcat_pid=ps_grep_tomcat+tomcat_ps_name+"%s|awk '{print $2}'|xargs kill"

#通過conf文件是否是bak來確認正在運行的tomcat端口
def find_running_tomcat_port():
    for _port in ports:
         tomcat_conf_name=nginx_vhost_conf_path+zuidaima_conf_name%(_port)
         if(os.path.exists(tomcat_conf_name)):
             port=_port
             break
    if not is_tomcat_port_running(port):
        return -1
    return port

#判斷指定的tomcat端口是否在運行
def is_tomcat_port_running(tomcat_port):
    ret=request_share_url(tomcat_port)
    if ret==200:
        return 1
    return 0

#請求帶端口的share地址
def request_share_url(tomcat_port):
    socket.setdefaulttimeout(10)
    url=zuidaima_domain+":"+tomcat_port+"/share.htm"
    ret=-1
    try:
        res=urllib2.urlopen(url)
	ret=res.code
    except URLError, e:
        print "request url#"+url+" error"
    return ret

#切換nginx的tomcat端口
def switch_nginx_conf(running_tomcat_port, stoped_tomcat_port):
    running_tomcat_conf_name=nginx_vhost_conf_path+zuidaima_conf_name%(running_tomcat_port)
    if(not os.path.exists(running_tomcat_conf_name)):
        return -1
    stoped_tomcat_conf_name=nginx_vhost_conf_path+zuidaima_conf_name%(stoped_tomcat_port)
    if(not os.path.exists(stoped_tomcat_conf_name+zuidaima_conf_name_bak_suffix)):
        return -2
    os.rename(running_tomcat_conf_name, running_tomcat_conf_name+zuidaima_conf_name_bak_suffix)
    os.rename(stoped_tomcat_conf_name+zuidaima_conf_name_bak_suffix, stoped_tomcat_conf_name)
    return 1

#啓動指定端口的tomcat服務
def startup_tomcat(tomcat_port):
    shutdown_tomcat(tomcat_port)
    outputs=commands.getoutput(tomcat_path+tomcat_port+tomcat_startup_bin_path)
    time.sleep(5) # 休眠5秒
    while(not is_tomcat_port_running(tomcat_port)):
        print "start tomcat "+tomcat_port
        time.sleep(5) # 休眠5秒
    return 1

#停止指定端口的tomcat服務
def shutdown_tomcat(tomcat_port):
    commands.getoutput(kill_tomcat_pid%(tomcat_port))
    while(is_tomcat_port_running(tomcat_port)):
        print "stop tomcat "+tomcat_port
        time.sleep(5) # 休眠5秒
    return 1

#切換tomcat服務
def switch_tomcat(running_tomcat_port, stoped_tomcat_port):
    startup_tomcat(stoped_tomcat_port)
    shutdown_tomcat(running_tomcat_port)
    return 1
#reload nginx conf
def reload_nginx_conf():
    commands.getoutput(nginx_bin_path+" -s reload")
    return 1

def start():
    print "start to switch tomcat"

    running_tomcat_port=find_running_tomcat_port()
    if running_tomcat_port==-1:
        print "running tomcat & conf is invalid"
        return
    ports.remove(running_tomcat_port)
    stoped_tomcat_port=ports[0]

    print "start to switch tomcat from "+running_tomcat_port+" to "+stoped_tomcat_port
    ret=switch_tomcat(running_tomcat_port, stoped_tomcat_port)
    if(ret!=1):
        print "fail to switch_tomcat,ret:", ret
        return

    print "start to switch nginx conf"
    ret=switch_nginx_conf(running_tomcat_port, stoped_tomcat_port)
    if(ret!=1):
        print "fail to switch_nginx_conf,ret:", ret
        return

    print "start to reload nginx conf"
    ret=reload_nginx_conf()
    if(ret!=1):
        print "fail to reload_nginx_conf,ret:", ret
        return

    print "finish to switch tomcat"

start()

有圖有真相:

20140621163041952.jpg

另外阿里雲服務器自帶的python版本是Python 2.4.3 (#1, Jan  9 2013, 06:49:54)的,在編寫腳本的過程中很多語法都是高版本纔有的,所以大家在學習python的過程中要注意版本的問題。

該版本的實現上因爲python是高級語言,所以業務實現上很嚴密,比如啓動時根據conf文件的後綴來確認誰是正在運行的tomcat,執行完tomcat的startup.sh後,再次通過urllib去請求該端口的share.htm確保啓動確實成功。

enjoy it.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章