#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: 陈二狗
# 灰度环境更新脚本 执行方式:python deploy.py (all|appname)
import os, sys
import paramiko
import select
import time
import re
import threading
# 远程操作
def SSHCMD(host, command):
pkey = paramiko.RSAKey.from_private_key_file('/home/tzg/.ssh/id_rsa')
trans = paramiko.Transport((host, 22))
trans.connect(username='tzg', pkey=pkey)
ssh = paramiko.SSHClient()
ssh._transport = trans
stdin, stdout, stderr = ssh.exec_command(command)
print(stdout.read().decode())
trans.close()
# 把应用包上传到相应服务器
def SSHSCP(host, localpath, remotepath):
pkey = paramiko.RSAKey.from_private_key_file('/home/tzg/.ssh/id_rsa')
trans = paramiko.Transport((host, 22))
trans.connect(username='tzg', pkey=pkey)
sftp = paramiko.SFTPClient.from_transport(trans)
sftp.put(localpath, remotepath)
trans.close()
# 重启tomcat 打印ERROR日志和启动完成日志
def RUNTOMCAT(host, command):
pkey = paramiko.RSAKey.from_private_key_file('/home/tzg/.ssh/id_rsa')
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(host, username='tzg', pkey=pkey)
transport = s.get_transport()
channel = transport.open_session()
channel.exec_command(command)
while 1:
if channel.exit_status_ready():
break
else:
rl, wl, xl = select.select([channel], [], [], 1)
if len(rl) > 0:
recv = channel.recv(1024)
if re.search('] ERROR', recv) <> None:
print recv
if re.search('Server startup in', recv) <> None:
print recv
break
# 部署单个java应用在单台服务器上
def DEPLOYWAR(host, warname, appname):
print "%s 上 %s 开始更新" % (host, appname)
SSHSCP(host, '/opt/tomcat-war/%s' % warname, '/tmp/%s' % warname)
SSHCMD(host, 'mkdir /opt/web-data-bak/tzg-%s_%s' % (appname, deploytime))
SSHCMD(host, 'unzip -oq /tmp/%s -d /opt/web-data-bak/tzg-%s_%s' % (warname, appname, deploytime))
SSHCMD(host, 'rm -rf /opt/web-data/tzg-%s' % appname)
SSHCMD(host, 'ln -s /opt/web-data-bak/tzg-%s_%s /opt/web-data/tzg-%s' % (appname, deploytime, appname))
RUNTOMCAT(host, '/opt/scripts/tomcat.sh %s restart' % appname)
print "\033[1;33m %s 上 %s 已更新完成 \033[0m" % (host, appname)
# 部署单个java应用在多台服务器上
def RUNTOMCATALL(name):
for host in warmap.get(name)[2]:
DEPLOYWAR(host, warmap.get(name)[0], name)
# 部署一批应用,多线程方式同时部署节省时间
def DEPLOY(list):
if len(list) > 0:
for name in list:
t = threading.Thread(target=RUNTOMCATALL, args=(name,))
t.setDaemon(False)
t.start()
time.sleep(15)
# 部署时间,给应用加上时间戳
deploytime = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
# 所有应用和服务器信息,如应用部署和服务器信息有变动应及时修改, key[1] 有 0 2 4 三种值 代表分批启动的三种类型应用
warmap={'b': ['b.war', 4, ['cunguanweb01',]],
'actprovider': ['tzg-actprovider.war', 2, ['cunguanrmi01', ]],
'act': ['tzg-act.war', 4, ['cunguanweb01',]],
'asset': ['tzg-asset.war',4, ['cunguanapp01', ]],
'autoinvest': ['tzg-autoinvest.war', 2, ['cunguanrmi01', ]],
'awardsgrant': ['tzg-awardsgrant.war', 2, ['cunguanrmi02',]],
'bi': ['tzg-bi-provider.war', 2, ['cunguanrmi01',]],
'bondstransfer': ['tzg-bondstransfer-provider.war', 2, ['cunguanrmi02',]],
'callback': ['tzg-callback-provider.war', 2, ['cunguanweb01', ]],
'copperbao': ['tzg-copperBao-provider.war', 2, ['cunguanrmi03', ]],
'growthpoint': ['tzg-growthpoint-provider.war', 2, ['cunguanrmi02', ]],
'merchant': ['tzg-merchant.war', 4, ['cunguanapp01', ]],
'message': ['tzg-message-provider.war', 2, ['cunguanrmi03', ]],
'mq': ['tzg-mq.war', 4, ['cunguanrmi03',]],
'pay': ['tzg-pay-provider.war', 2, ['cunguanrmi02',]],
'pyramid': ['tzg-pyramid-provider.war', 2, ['cunguanrmi01', ]],
'rest': ['tzg-rest.war', 4, ['cunguanweb01', ]],
'rmi': ['tzg-rmi-server.war', 0, ['cunguanrmi01',]],
'salary': ['tzg-salary.war', 2, ['cunguanrmi03',]],
'wap': ['tzg-wap.war', 4, ['cunguanweb01',]],
'web': ['tzg-web.war', 4, ['cunguanweb01',]],
'cas': ['cas.war', 4, ['cunguanapp01',]],
'partner': ['tzg-partner.war', 4, ['cunguanapp01',]],
'portal': ['tzg-portal.war', 4, ['cunguanapp01',]],
'risk': ['tzg-risk.war', 4, ['cunguanapp01',]],
'store': ['tzg-store.war', 4, ['cunguanapp01',]],
}
# 定义分三批启动的应用列表
firstlist = []
secondlist = []
thirdlist = []
# 外部传参
applist = sys.argv
applist.remove(sys.argv[0])
# 定义参数为 all 时的应用列表
if applist[0] == 'all':
firstlist = ['rmi']
secondlist = ['actprovider', 'autoinvest', 'awardsgrant', 'bi', 'bondstransfer', 'callback', 'copperbao', 'growthpoint', 'message', 'pay', 'pyramid', 'salary']
thirdlist = ['b', 'act', 'rest', 'web', 'wap','mq']
else:
for name in applist:
if warmap.get(name)[1] == 0:
firstlist.append(name)
elif warmap.get(name)[1] == 2:
secondlist.append(name)
elif warmap.get(name)[1] == 4:
thirdlist.append(name)
else:
print "%s 不存在" % name
if firstlist == []:
pass
else:
print firstlist
if secondlist == []:
pass
else:
print secondlist
if thirdlist == []:
pass
else:
print thirdlist
alllist = [firstlist,secondlist,thirdlist]
if __name__ == "__main__":
for i in alllist:
DEPLOY(i)
java应用多线程批量更新脚本
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本篇属于【Java多线程系列】文章第二章【多线程编程的实现与应用】的第一小节内容,我们会来学习创建线程的几种方式,...
- 本篇属于【Java多线程系列】文章第二章【多线程编程的实现与应用】的第三小节内容,我们会来学习线程的优先级概念。 ...
- 本篇属于【Java多线程系列】文章第二章【多线程编程的实现与应用】的第二小节内容,我们会来学习线程的生命周期,作为...