zoukankan      html  css  js  c++  java
  • 版本库管理脚本

    1. #!/usr/bin/python2.7
    2. #coding=utf-8
    3. '''
    4. Created on Oct 19
    5. @author wangyunhua@smeyun.com Update Version TO Wan
    6. Version 1.1
    7. '''
    8. import os,sys,hashlib
    9. import time
    10. import traceback as tb
    11. from argparse import ArgumentParser
    12. from subprocess import call
    13. import MySQLdb
    14. import json
    15. import logging
    16. l = logging.getLogger('update')
    17. reload(sys)
    18. sys.setdefaultencoding('utf8')
    19. #获取时间用于打包备份命名
    20. D_TIME = time.strftime('%Y%m%d%H%M')
    21. #作为结果输出
    22. logs = []
    23. #获取脚本当前目录
    24. cfd = os.path.dirname(os.path.abspath(__file__))
    25. update_service = []
    26. Service_Names = []
    27. #MYSQl信息设置
    28. MYSQLHOST='127.0.0.1'
    29. MYSQLUSER='root'
    30. MYSQLPASSWD='Feng!1900'
    31. def scall(CMD_LINE):
    32. '''linux shell '''
    33. l.debug('CMD_LINE : %r',CMD_LINE)
    34. return call(CMD_LINE,shell=True)
    35. def os_system(cmd):
    36. result = os.popen(cmd)
    37. res = result.read()
    38. return res.splitlines()
    39. #封装的自定义打印模块
    40. def head_print():
    41. print '33[1;31;40m'
    42. #print '*' * 50
    43. #print ' '
    44. #封装的自定义打印模块
    45. def tail_print():
    46. #print ' '
    47. #print '*' * 50
    48. print '33[0m'
    49. #输出操作结果
    50. def output_result():
    51. head_print()
    52. for count in range(len(logs)):
    53. print " "
    54. for content in logs[count]:
    55. if 'MD5校验成功!' in content:
    56. print content
    57. break
    58. print content
    59. tail_print()
    60. class services:
    61. def __init__(self,shotername,serverID,servername,JS_status,JD_path,JI_path,Jenkins_name):
    62. self.shotername = shotername
    63. self.serverID = serverID
    64. self.servername = servername
    65. self.JS_status = JS_status
    66. self.JD_path = JD_path
    67. self.JI_path = JI_path
    68. self.Jenkins_name = Jenkins_name
    69. def Server_sql():
    70. conn = MySQLdb.connect(host=MYSQLHOST,user=MYSQLUSER,passwd=MYSQLPASSWD,db='ccdb')
    71. curs = conn.cursor()
    72. try:
    73. curs.execute("select serverID,shotername,servername,JS_status,JD_path,JI_path,Jenkins_name from t_jenkins_server_list WHERE JS_status='1';")
    74. conn.commit()
    75. data = curs.fetchall()
    76. #print u'fetchall()返回的数据:',data
    77. conn.close()
    78. jsonData = []
    79. for row in data:
    80. result = {}
    81. result['serverID'] = int(row[0])
    82. result['shotername'] = str(row[1])
    83. result['servername'] = str(row[2])
    84. result['JS_status'] = int(row[3])
    85. result['JD_path'] = str(row[4])
    86. result['JI_path'] = str(row[5])
    87. result['Jenkins_name'] = str(row[6])
    88. jsonData.append(result)
    89. #print u'转换为列表字典的原始数据:',jsonData
    90. l.debug('转换为列表字典的原始数据 : %r',jsonData)
    91. return json.dumps(jsonData)
    92. except:
    93. print 'MySQL connect fail...'
    94. else:
    95. #使用json.dumps将数据转换为json格式,json.dumps方法默认会输出成这种格式"u5377u76aeu6298u6263",加ensure_ascii=False,则能够防止中文乱码。
    96. #JSON采用完全独立于语言的文本格式,事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
    97. #json.dumps()是将原始数据转为json(其中单引号会变为双引号),而json.loads()是将json转为原始数据。
    98. jsondatar=json.dumps(jsonData,ensure_ascii=False,sort_keys=True)
    99. #去除首尾的中括号
    100. return jsondatar[1:len(jsondatar)-1]
    101. def pack(ob,webnames):
    102. #取出相关服务器信息和状态
    103. d_list=json.loads(Server_sql())
    104. for wname in webnames:
    105. for s_list in d_list:
    106. if s_list['servername'] == wname:
    107. service_object = services(s_list['serverID'],s_list['shotername'],s_list['servername'],s_list['JS_status'],
    108. s_list['JD_path'],s_list['JI_path'],s_list['Jenkins_name'])
    109. update_service.append(service_object)
    110. def pack_info():
    111. #取出相关服务器信息和状态
    112. d_dat=json.loads(Server_sql())
    113. for s_list in d_dat:
    114. service_name = s_list['servername']
    115. Service_Names.append(service_name)
    116. Service_Names.append('all')
    117. def md5res(filenew,fileold):
    118. file_new= file(filenew,'r')
    119. file_old= file(fileold,'r')
    120. m1 = hashlib.md5()
    121. m1.update(file_new.read())
    122. m_new=m1.hexdigest()
    123. file_new.close()
    124. m2 = hashlib.md5()
    125. m2.update(file_old.read())
    126. m_old=m2.hexdigest()
    127. file_old.close()
    128. if m_new == m_old:
    129. return True
    130. else:
    131. return False
    132. def md5_file(ob):
    133. content = []
    134. version_file='%s/%s'%(ob.JD_path,ob.Jenkins_name)
    135. bak_md='''%s/'''%ob.JI_path
    136. bak_file= bak_md + ob.Jenkins_name
    137. old_md='/data/jarbackup/%s/'%ob.servername
    138. old_file='%s/%s_%s'%(old_md,ob.Jenkins_name,D_TIME)
    139. head = "%s: "%ob.servername
    140. l.debug('executing %r',head)
    141. content.append(head)
    142. if os.path.exists(version_file):
    143. ver_md5shl='''/usr/bin/md5sum %s |cut -d ' ' -f1'''%(version_file)
    144. content_file = "版本库文件MD5:" + str(os_system(ver_md5shl)[0])
    145. content.append(content_file)
    146. l.debug('executing %r',ver_md5shl)
    147. if os.path.exists(bak_file):
    148. bak_md5shl='''/usr/bin/md5sum %s |cut -d ' ' -f1'''%(bak_file)
    149. content_bak = "拷贝文件MD5:" + str(os_system(bak_md5shl)[0])
    150. content.append(content_bak)
    151. l.debug('executing %r',bak_md5shl)
    152. if os.path.exists(old_file):
    153. fei_md5shl='''/usr/bin/md5sum %s |cut -d ' ' -f1'''%(old_file)
    154. content_fei = "废弃文件MD5:" + str(os_system(fei_md5shl)[0])
    155. content.append(content_fei)
    156. l.debug('executing %r',fei_md5shl)
    157. logs.append(content)
    158. def copy_version(ob):
    159. # shotername gateway
    160. # servername gatewayServer
    161. # JD_path /data/workspace/git-pre-release-base/public/com.foresee.ftcsp.gateway
    162. # JI_path /data/BusinesstoCustomer/program/gatewayServer
    163. # Jenkins_name com.foresee.ftcsp.gateway-0.0.1.jar
    164. #/data/workspace/git-pre-release-develop/customer/com.foresee.ftcsp.customer.rest/target/com.foresee.ftcsp.customer.rest-0.0.1.jar
    165. #/data/BusinesstoCustomer/program/customerServer
    166. version_file='%s/%s'%(ob.JD_path,ob.Jenkins_name)
    167. bak_md='''%s/'''%ob.JI_path
    168. bak_file= bak_md + ob.Jenkins_name
    169. old_md='/data/jarbackup/%s/'%ob.servername
    170. old_file='%s/%s_%s'%(old_md,ob.Jenkins_name,D_TIME)
    171. l.debug('executing bak_md %r',bak_md)
    172. l.debug('executing bak_file %r',bak_file)
    173. l.debug('executing version_file %r',version_file)
    174. l.debug('executing old_file %r',old_file)
    175. l.debug('executing old_md %r',old_md)
    176. oldshl='/usr/bin/diff %s %s'%(old_file,version_file)
    177. #copy文件
    178. if os.path.exists(version_file):
    179. if not os.path.exists(old_md):
    180. scall('mkdir -p %s'%old_md)
    181. if not os.path.exists(bak_md):
    182. scall('mkdir -p %s'%bak_md)
    183. s = os.listdir(bak_md)
    184. if len(s) < 1:
    185. shl = '''rsync -rv %s %s'''%(version_file,bak_file)
    186. scall(shl)
    187. else:
    188. if not md5res(bak_file,version_file):
    189. mvshl='/bin/mv %s %s'%(bak_file,old_file)
    190. scall(mvshl)
    191. shl = '''rsync -rv %s %s'''%(version_file,bak_file)
    192. scall(shl)
    193. print "%s 版本目录文件已经更新,文件已经备份"%ob.servername
    194. else:
    195. print "无需更新版本包无变更!"
    196. #检测参数,执行操作
    197. def handle(args):
    198. logging.basicConfig(format="%(message)s")
    199. l.level = logging.INFO
    200. if args.verbose:
    201. l.level =logging.DEBUG
    202. op = args.operation
    203. ob = args.object
    204. webnames = args.webname
    205. if 'all' in webnames:
    206. pack_info()
    207. Service_Names.remove('all')
    208. webname = set(Service_Names)
    209. pack(ob,webname)
    210. else:
    211. pack(ob,webnames)
    212. for content in update_service:
    213. if op == 'ver':
    214. copy_version(content)
    215. md5_file(content)
    216. if op == 'md5':
    217. md5_file(content)
    218. #检测输入,
    219. def main():
    220. pack_info()
    221. ACTIONS = ['ver','md5']
    222. GROUPS = ['version',]
    223. webname = set(Service_Names)
    224. webname = list(webname)
    225. webname.sort()
    226. parser = ArgumentParser()
    227. try:
    228. parser.add_argument('-v','--verbose',action='store_true',help='detail output')
    229. parser.add_argument('operation',choices=ACTIONS,help='action name')
    230. parser.add_argument('-o','--object',nargs='+',choices=GROUPS,help='please install object name')
    231. parser.add_argument('-w','--webname',nargs='+',choices=webname,help='update servername')
    232. except:
    233. print '%r'%tb.format_exc()
    234. args = parser.parse_args()
    235. handle(args)
    236. output_result()
    237. if __name__ == '__main__':
    238. main()
    239. '''
    240. python version.py ver -w customerServer
    241. 1.add log
    242. '''
  • 相关阅读:
    二分+树状数组/线段树(区间更新) HDOJ 4339 Query
    数论(GCD) HDOJ 4320 Arcane Numbers 1
    拓扑排序/DFS HDOJ 4324 Triangle LOVE
    离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
    KMP HDOJ 4300 Clairewd's message
    高精度模板
    前缀+排序 HDOJ 4311 Meeting point-1
    Kruskal HDOJ 4313 Matrix
    最短路(Dijkstra) HDOJ 4318 Power transmission
    (转)C语言运算符优先级 详细列表
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/13329834.html
Copyright © 2011-2022 走看看