批量更新的代码java_java应用多线程批量更新脚本

#!/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)