#!/usr/bin/python2.7
#coding=utf-8
'''
Created on Oct 19
@author wangyunhua@smeyun.com Update Version TO Wan
Version 1.1
'''
import os,sys,hashlib
import time
import traceback as tb
from argparse import ArgumentParser
from subprocess import call
import MySQLdb
import json
import logging
l = logging.getLogger('update')
reload(sys)
sys.setdefaultencoding('utf8')
#获取时间用于打包备份命名
D_TIME = time.strftime('%Y%m%d%H%M')
#作为结果输出
logs = []
#获取脚本当前目录
cfd = os.path.dirname(os.path.abspath(__file__))
update_service = []
Service_Names = []
#MYSQl信息设置
MYSQLHOST='127.0.0.1'
MYSQLUSER='root'
MYSQLPASSWD='Feng!1900'
def scall(CMD_LINE):
'''linux shell '''
l.debug('CMD_LINE : %r',CMD_LINE)
return call(CMD_LINE,shell=True)
def os_system(cmd):
result = os.popen(cmd)
res = result.read()
return res.splitlines()
#封装的自定义打印模块
def head_print():
print '33[1;31;40m'
#print '*' * 50
#print ' '
#封装的自定义打印模块
def tail_print():
#print ' '
#print '*' * 50
print '33[0m'
#输出操作结果
def output_result():
head_print()
for count in range(len(logs)):
print " "
for content in logs[count]:
if 'MD5校验成功!' in content:
print content
break
print content
tail_print()
class services:
def __init__(self,shotername,serverID,servername,JS_status,JD_path,JI_path,Jenkins_name):
self.shotername = shotername
self.serverID = serverID
self.servername = servername
self.JS_status = JS_status
self.JD_path = JD_path
self.JI_path = JI_path
self.Jenkins_name = Jenkins_name
def Server_sql():
conn = MySQLdb.connect(host=MYSQLHOST,user=MYSQLUSER,passwd=MYSQLPASSWD,db='ccdb')
curs = conn.cursor()
try:
curs.execute("select serverID,shotername,servername,JS_status,JD_path,JI_path,Jenkins_name from t_jenkins_server_list WHERE JS_status='1';")
conn.commit()
data = curs.fetchall()
#print u'fetchall()返回的数据:',data
conn.close()
jsonData = []
for row in data:
result = {}
result['serverID'] = int(row[0])
result['shotername'] = str(row[1])
result['servername'] = str(row[2])
result['JS_status'] = int(row[3])
result['JD_path'] = str(row[4])
result['JI_path'] = str(row[5])
result['Jenkins_name'] = str(row[6])
jsonData.append(result)
#print u'转换为列表字典的原始数据:',jsonData
l.debug('转换为列表字典的原始数据 : %r',jsonData)
return json.dumps(jsonData)
except:
print 'MySQL connect fail...'
else:
#使用json.dumps将数据转换为json格式,json.dumps方法默认会输出成这种格式"u5377u76aeu6298u6263",加ensure_ascii=False,则能够防止中文乱码。
#JSON采用完全独立于语言的文本格式,事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
#json.dumps()是将原始数据转为json(其中单引号会变为双引号),而json.loads()是将json转为原始数据。
jsondatar=json.dumps(jsonData,ensure_ascii=False,sort_keys=True)
#去除首尾的中括号
return jsondatar[1:len(jsondatar)-1]
def pack(ob,webnames):
#取出相关服务器信息和状态
d_list=json.loads(Server_sql())
for wname in webnames:
for s_list in d_list:
if s_list['servername'] == wname:
service_object = services(s_list['serverID'],s_list['shotername'],s_list['servername'],s_list['JS_status'],
s_list['JD_path'],s_list['JI_path'],s_list['Jenkins_name'])
update_service.append(service_object)
def pack_info():
#取出相关服务器信息和状态
d_dat=json.loads(Server_sql())
for s_list in d_dat:
service_name = s_list['servername']
Service_Names.append(service_name)
Service_Names.append('all')
def md5res(filenew,fileold):
file_new= file(filenew,'r')
file_old= file(fileold,'r')
m1 = hashlib.md5()
m1.update(file_new.read())
m_new=m1.hexdigest()
file_new.close()
m2 = hashlib.md5()
m2.update(file_old.read())
m_old=m2.hexdigest()
file_old.close()
if m_new == m_old:
return True
else:
return False
def md5_file(ob):
content = []
version_file='%s/%s'%(ob.JD_path,ob.Jenkins_name)
bak_md='''%s/'''%ob.JI_path
bak_file= bak_md + ob.Jenkins_name
old_md='/data/jarbackup/%s/'%ob.servername
old_file='%s/%s_%s'%(old_md,ob.Jenkins_name,D_TIME)
head = "%s: "%ob.servername
l.debug('executing %r',head)
content.append(head)
if os.path.exists(version_file):
ver_md5shl='''/usr/bin/md5sum %s |cut -d ' ' -f1'''%(version_file)
content_file = "版本库文件MD5:" + str(os_system(ver_md5shl)[0])
content.append(content_file)
l.debug('executing %r',ver_md5shl)
if os.path.exists(bak_file):
bak_md5shl='''/usr/bin/md5sum %s |cut -d ' ' -f1'''%(bak_file)
content_bak = "拷贝文件MD5:" + str(os_system(bak_md5shl)[0])
content.append(content_bak)
l.debug('executing %r',bak_md5shl)
if os.path.exists(old_file):
fei_md5shl='''/usr/bin/md5sum %s |cut -d ' ' -f1'''%(old_file)
content_fei = "废弃文件MD5:" + str(os_system(fei_md5shl)[0])
content.append(content_fei)
l.debug('executing %r',fei_md5shl)
logs.append(content)
def copy_version(ob):
# shotername gateway
# servername gatewayServer
# JD_path /data/workspace/git-pre-release-base/public/com.foresee.ftcsp.gateway
# JI_path /data/BusinesstoCustomer/program/gatewayServer
# Jenkins_name com.foresee.ftcsp.gateway-0.0.1.jar
#/data/workspace/git-pre-release-develop/customer/com.foresee.ftcsp.customer.rest/target/com.foresee.ftcsp.customer.rest-0.0.1.jar
#/data/BusinesstoCustomer/program/customerServer
version_file='%s/%s'%(ob.JD_path,ob.Jenkins_name)
bak_md='''%s/'''%ob.JI_path
bak_file= bak_md + ob.Jenkins_name
old_md='/data/jarbackup/%s/'%ob.servername
old_file='%s/%s_%s'%(old_md,ob.Jenkins_name,D_TIME)
l.debug('executing bak_md %r',bak_md)
l.debug('executing bak_file %r',bak_file)
l.debug('executing version_file %r',version_file)
l.debug('executing old_file %r',old_file)
l.debug('executing old_md %r',old_md)
oldshl='/usr/bin/diff %s %s'%(old_file,version_file)
#copy文件
if os.path.exists(version_file):
if not os.path.exists(old_md):
scall('mkdir -p %s'%old_md)
if not os.path.exists(bak_md):
scall('mkdir -p %s'%bak_md)
s = os.listdir(bak_md)
if len(s) < 1:
shl = '''rsync -rv %s %s'''%(version_file,bak_file)
scall(shl)
else:
if not md5res(bak_file,version_file):
mvshl='/bin/mv %s %s'%(bak_file,old_file)
scall(mvshl)
shl = '''rsync -rv %s %s'''%(version_file,bak_file)
scall(shl)
print "%s 版本目录文件已经更新,文件已经备份"%ob.servername
else:
print "无需更新版本包无变更!"
#检测参数,执行操作
def handle(args):
logging.basicConfig(format="%(message)s")
l.level = logging.INFO
if args.verbose:
l.level =logging.DEBUG
op = args.operation
ob = args.object
webnames = args.webname
if 'all' in webnames:
pack_info()
Service_Names.remove('all')
webname = set(Service_Names)
pack(ob,webname)
else:
pack(ob,webnames)
for content in update_service:
if op == 'ver':
copy_version(content)
md5_file(content)
if op == 'md5':
md5_file(content)
#检测输入,
def main():
pack_info()
ACTIONS = ['ver','md5']
GROUPS = ['version',]
webname = set(Service_Names)
webname = list(webname)
webname.sort()
parser = ArgumentParser()
try:
parser.add_argument('-v','--verbose',action='store_true',help='detail output')
parser.add_argument('operation',choices=ACTIONS,help='action name')
parser.add_argument('-o','--object',nargs='+',choices=GROUPS,help='please install object name')
parser.add_argument('-w','--webname',nargs='+',choices=webname,help='update servername')
except:
print '%r'%tb.format_exc()
args = parser.parse_args()
handle(args)
output_result()
if __name__ == '__main__':
main()
'''
python version.py ver -w customerServer
1.add log
'''