不通用版(从SVN取版本,通过MAVEN生成JAVA包,通过SALTSTACK传送到远程服务器并自动重启TOMCAT服务)PYTHON代码...

 

从昨晚写到今天啊,

第一版测试成功了。

PHP和JAVA的更新相对来说,PHP的自动化更新是简单多啦~~

当然,这只是运维工作当中的一环~~

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys,commands,subprocess  
import re,time,logging
from optparse import OptionParser

#启动日志模块
logging.basicConfig(level=logging.INFO,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='updateSVN.log',
                filemode='a')
#将输入的参数与相应的配置文件匹配,并形成特殊字典,方便后续取值
def get_svn_list(name, ip, version):
    svn_update = {}
    for line in open("data_svn_src_java"):
    line_item = line.strip("\n").split("|")
    if ip == line_item[0] and name == line_item[1]:
        line_item = line.strip("\n").split("|")
        svn_update["salt_minion_ip"] = line_item[0]
        svn_update["svn_name"] = line_item[1]
        svn_update["salt_master_dir"] = "/srv/salt/%s/" % line_item[0]
        svn_update["server_dir"] = line_item[2]
        svn_update["svn_dir"] = line_item[3]
        svn_update["package_name"] = line_item[4]
        svn_update["svn_version"] = version
    if len(svn_update) != 7:
    error_output("database have none item at your xargs.")
    return svn_update

#统一错误输出,红色文字提示
def error_output(error_str):
    print '\033[0;31;40m'
    print error_str + "<br>"
    print '\033[0m'
    sys.exit(1)

#统一调用执行系统命令,并产生相应输出及日志记录
def exec_cmd(cmd):
    str_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    (status,output) = commands.getstatusoutput(cmd)
    try:
    if int(status) == 0:
        print "%s   %s  is ---OK!<br>" % (str_now, cmd)
        logging.info(cmd + " ---is OK!")
        return output
    else:
        logging.info(cmd + " cant't finish...")
        print "%s   %s  can't finish...<br>" % (str_now, cmd)
        sys.exit(1)
    except:
    logging.info(cmd + " is Wrong")
    print "%s   %s  --- is Wrong<br>" % (str_now, cmd)
    sys.exit(1)

#在真正应用SALTSTACK之前,先测试是否正常连接
def salt_minion_test(salt_minion_ip):
    if "True" not  in exec_cmd("salt '%s' test.ping" % salt_minion_ip):
        error_output("salt minion %s is not active,please restart this minion and execute is again. " % salt_minion_ip)
    else:
    print "salt minion ip %s is ok.<br>" % salt_minion_ip
#从SVN相应目录里取出文件,存放在SALTSTACK的相应目录里(以IP作为目录存放位置)
def get_svn_file(svn_dir, svn_version, salt_master_dir):
    exec_cmd("rm -rf %s" % salt_master_dir)
    exec_cmd("mkdir -p %s" % salt_master_dir)
    svnLog_cmd ="svn co  %s %s  --username cheng168 --password chengcheng -r %s" % (svn_dir, salt_master_dir, svn_version)
    output = exec_cmd(svnLog_cmd)
    if svn_version not  in output:
        error_output("svn version is Wrong<br>")
        sys.exit(1)
#用MAVEN生成相应的JAR或WAR包
def gen_package(salt_master_dir):
    mvn_cmd = "mvn clean install -Dmaven.test.skip=true -Pprod -f %spom.xml" % (salt_master_dir)
    exec_cmd(mvn_cmd)

#由于数据频道特殊,脚本会在解压之后生成一个备份。
def update_back():
    print "back_pass"

#以下几个函数是为了区别consumer和provider的更新,主要是执行相关系统调用
def shutdown_consumer_tomcat(salt_minion_ip, server_dir):
    tomcat_dir = '/'.join(server_dir.split('/')[:-3])
    shutdown_A_cmd  = "salt '%s' cmd.run 'su - tomcat -c %s/bin/shutdown.sh'" % (salt_minion_ip, tomcat_dir)
    exec_cmd(shutdown_A_cmd)
    time.sleep(2)
    shutdown_B_cmd = """salt '%s' cmd.run "ps aufx|grep tomcat|grep %s/endorsed|grep -v grep|awk '{print $2}'|xargs kill -9" """% (salt_minion_ip, tomcat_dir)
    exec_cmd(shutdown_B_cmd)

def shutdown_provider_java(salt_minion_ip, package_name):
    shutdown_jar_cmd = """salt '%s' cmd.run "ps aufx|grep %s|grep -v grep|awk '{print $2}'|xargs kill -9" """ % (salt_minion_ip, package_name)
    exec_cmd(shutdown_jar_cmd)

def startup_consumer_tomcat(salt_minion_ip, server_dir):
    tomcat_dir = '/'.join(server_dir.split('/')[:-3])
    startup_cmd = "salt '%s' cmd.run 'su - tomcat -c %s/bin/startup.sh'" % (salt_minion_ip, tomcat_dir)
    #exec_cmd(startup_cmd)
    #此处不能再调用exec_cmd,因为不能及时获取返回值,再开一个子进程,就流畅多啦。。
    subprocess.Popen(startup_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

def startup_provider_java(salt_minion_ip, server_dir, package_name):
    #此处太过技巧化,希望以后能写成更好理解的,三引号的使用,切换用户帐号,后台执行服务器命令都全了。
    startup_cmd = """salt '%s' cmd.run 'su - tomcat -c "java -jar %s%s &"'""" % (salt_minion_ip, server_dir, package_name)
    print startup_cmd
    #exec_cmd(startup_cmd)
    subprocess.Popen(startup_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

def update_consumer_package(salt_minion_ip, server_dir, package_name):
    #几个变量主要是为了精简配置文件,将备份放在合适的目录
    package_dir = ''.join(package_name.split('.')[0])
    tomcat_dir = '/'.join(server_dir.split('/')[:-3])
    salt_master_file = "salt://%s/target/%s" % (salt_minion_ip, package_name)
    salt_minion_file = server_dir + package_name
    back_minion_dir = tomcat_dir + "/backup/"
    update_package_cmd = "salt '%s' cp.get_file %s %s makedirs=True" % (salt_minion_ip, salt_master_file, salt_minion_file)
    exec_cmd(update_package_cmd)
    unzip_cmd = "salt '%s' cmd.run 'unzip %s%s -d %s'" % (salt_minion_ip, server_dir, package_name, server_dir)
    exec_cmd(unzip_cmd)
    chown_cmd = "salt '%s' cmd.run 'chown tomcat:tomcat %s'" % (salt_minion_ip, server_dir)
    exec_cmd(chown_cmd)
    #小峤要求解压后删除WAR文件,便于后期直接更新静态文件,我在此处正好将它备份起来
    mv_cmd = "salt '%s' cmd.run 'mv %s %s'" % (salt_minion_ip, salt_minion_file, back_minion_dir)
    exec_cmd(mv_cmd)

#因为DUBBO的CONSUMER消费者为WAR包,服务提供者为JAR包,所以启动,关闭,更新上传都分成了两套函数
def update_provider_package(salt_minion_ip, server_dir, package_name):
    salt_master_file = "salt://%s/target/%s" % (salt_minion_ip, package_name)
    salt_minion_file = server_dir + package_name
    update_package_cmd = "salt '%s' cp.get_file %s %s makedirs=True" % (salt_minion_ip, salt_master_file, salt_minion_file)
    exec_cmd(update_package_cmd)
    cp_cmd = "salt '%s' cmd.run 'cp %s %s.bak'" % (salt_minion_ip, salt_minion_file, salt_minion_file)
    exec_cmd(cp_cmd)
    chown_cmd = "salt '%s' cmd.run 'chown tomcat:tomcat %s*'" % (salt_minion_ip, server_dir)
    exec_cmd(chown_cmd)

def main(name, ip, version):
    svn_update = get_svn_list(name, ip, version)
    salt_minion_test(svn_update["salt_minion_ip"])
    get_svn_file(svn_update["svn_dir"], svn_update["svn_version"], svn_update["salt_master_dir"])
    gen_package(svn_update["salt_master_dir"])
    update_back()
    #服务器和消费的前面操作都相同,但在此后,要分路径判断执行相应的操作
    if svn_update["package_name"] == "consumer.war" :
        shutdown_consumer_tomcat(svn_update["salt_minion_ip"], svn_update["server_dir"])
        update_consumer_package(svn_update["salt_minion_ip"], svn_update["server_dir"], svn_update["package_name"])
        startup_consumer_tomcat(svn_update["salt_minion_ip"], svn_update["server_dir"])
    if svn_update["package_name"] == "provider.jar" :
    shutdown_provider_java(svn_update["salt_minion_ip"], svn_update["package_name"])
    update_provider_package(svn_update["salt_minion_ip"], svn_update["server_dir"], svn_update["package_name"])
    startup_provider_java(svn_update["salt_minion_ip"], svn_update["server_dir"], svn_update["package_name"])
if __name__=="__main__":
    #输入参数判断,如有输入不完,及早提示并退出
    usage = "usage: %prog [options] arg :./update.py -v 12292 -n bbs -i 192.168.11.75"
    parser = OptionParser(usage)
    parser.add_option("-v", "--version", dest="version", help="input the version(Sample: 15435...).")
    parser.add_option("-n", "--name", dest="name", help="input the svn name(consumer|provider)")
    parser.add_option("-i", "--ip", dest="ip", help="input the ip address(1.4.8.25|11.14.7.2)")
    (options, args) = parser.parse_args()
    version = options.version
    name = options.name
    ip = options.ip
    if version is None or name is None or ip is None :
        error_output("you must input all args: ./update_svn_shuju.py -i xx  -n xx -v xx")
    main(name, ip, version)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值