博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不通用版(从SVN取版本,通过MAVEN生成JAVA包,通过SALTSTACK传送到远程服务器并自动重启TOMCAT服务)PYTHON代码...
阅读量:6464 次
发布时间:2019-06-23

本文共 7697 字,大约阅读时间需要 25 分钟。

从昨晚写到今天啊,

第一版测试成功了。

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

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

复制代码
#!/usr/bin/env python# -*- coding: utf-8 -*-import os,sys,commands,subprocess  import re,time,loggingfrom 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 + "
" 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!
" % (str_now, cmd) logging.info(cmd + " ---is OK!") return output else: logging.info(cmd + " cant't finish...") print "%s %s can't finish...
" % (str_now, cmd) sys.exit(1) except: logging.info(cmd + " is Wrong") print "%s %s --- is Wrong
" % (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.
" % 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
") 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)
复制代码

转载地址:http://gzrzo.baihongyu.com/

你可能感兴趣的文章
Ruby 开发包 BitNami RubyStack
查看>>
如何在 MAC OS X 中建立 RAM Disk
查看>>
链表判断是否有环与判断环入口
查看>>
windows2003系统,oracle9i崩溃后的恢复
查看>>
参加CSDN2014博客比赛,兄弟们帮忙投上宝贵一票吧!
查看>>
nginx 部署 https
查看>>
requirejs
查看>>
Redis实战(5)数据类型三Lists
查看>>
ACI-数据中心架构考虑因素
查看>>
shell变量介绍及变量子串的使用
查看>>
zeromq的使用
查看>>
SQL _Server实现字符串MD5加密
查看>>
服务器硬件监控之Check_openmanage
查看>>
java 1.7watchservice特性学习 文件变更监控
查看>>
【多线程】1.分支合并框架
查看>>
java.util.concurrent.atomic.AtomicBoolean
查看>>
Semaphore
查看>>
Mac环境下svn的使用
查看>>
spring MappingJackson2HttpMessageConverter GBK 乱码问题
查看>>
2018年底遇到的前端面试题
查看>>