千家信息网

如何使用fabric自动化发版

发表于:2025-01-17 作者:千家信息网编辑
千家信息网最后更新 2025年01月17日,这篇文章给大家分享的是有关如何使用fabric自动化发版的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。功能:使用fabric自动化发版#-*-coding:utf8-*-f
千家信息网最后更新 2025年01月17日如何使用fabric自动化发版

这篇文章给大家分享的是有关如何使用fabric自动化发版的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

功能:使用fabric自动化发版

  1. #-*-coding:utf8-*-

  2. from fabric.api import *

  3. import subprocess

  4. import os

  5. import sys

  6. import glob

  7. import shutil


  8. env.roledefs = {

  9. 'cz-oms-core':['czoms@10.2.88.47','czoms@10.2.88.48'],

  10. 'cz-oms-core-new':['czoms@10.2.88.37','czoms@10.2.88.46'],

  11. 'cz-oms-core-new1':['czoms@10.2.88.40','czoms@10.2.88.41'],

  12. 'cz':['appuser@10.2.88.30']

  13. }




  14. env.passwords = {

  15. 'czoms@10.2.88.47:22':'fdfdfsdf',

  16. 'czoms@10.2.88.48:22':'fdfdfsdf',

  17. 'czoms@10.2.88.37:22':'fdfdfsdf',

  18. 'czoms@10.2.88.46:22':'fdfdfsdf',

  19. 'czoms@10.2.88.40:22':'fdfdfsdf',

  20. 'czoms@10.2.88.41:22':'fdfdfsdf',

  21. 'appuser@10.2.88.30:22':'appuser@0630',

  22. }



  23. #通用函数

  24. def unzip():

  25. with lcd('patch'):

  26. local('winrar x *.zip')


  27. #Update SVN

  28. def UpdateSVN(path):

  29. p = subprocess.Popen(['svn','update',path],shell=True,stdout=subprocess.PIPE)

  30. print p.stdout.readlines()




  31. #根据filelist生成发版目录

  32. def faban_dir():

  33. count = 0

  34. with open('filelist.txt','r') as f:

  35. for line in f:

  36. if line.startswith('bj-oms-api'):

  37. #filelist文件路径预处理

  38. classpath_filename = line.replace('bj-oms-api/src/main/java/','../online_svn/bj-oms/bj-oms-api/target/classes/')

  39. classpath_filename = classpath_filename.replace('.java','.class')

  40. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  41. #构造patch目录结构

  42. patchpath_filename = line.replace('bj-oms-api/src/main/java/','')

  43. patchpath_filename = patchpath_filename.replace('.java','.class')

  44. patchpath_filename = patchpath_filename.replace('\n','')

  45. patchpath_filename = 'patch/' + 'bj-oms-api/' + patchpath_filename

  46. patchpath = os.path.dirname(patchpath_filename)

  47. #如果patch目录不存在,就建立目录

  48. try:

  49. os.listdir(patchpath)

  50. except WindowsError:

  51. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  52. #拷贝父类到patch目录下

  53. parentclass_path_filename = classpath_filename

  54. patchpath_filename = patchpath_filename

  55. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  56. shutil.copy(parentclass_path_filename,patchpath_filename)

  57. count += 1

  58. print "***********第%s个文件拷贝成功***************" % count

  59. #用glob寻找子类

  60. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  61. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  62. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  63. #把子类拷贝到patch目录下

  64. for subclass_path_filename in subclass_path_filenames:

  65. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  66. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-oms/bj-oms-api/target/classes/','patch/bj-oms-api/')

  67. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  68. shutil.copy(subclass_path_filename,patchpath_filename)

  69. count += 1

  70. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count


  71. elif line.startswith('bj-oms-domain'):

  72. #filelist文件路径预处理

  73. classpath_filename = line.replace('bj-oms-domain/src/main/java/','../online_svn/bj-oms/bj-oms-domain/target/classes/')

  74. classpath_filename = classpath_filename.replace('.java','.class')

  75. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  76. #构造patch目录结构

  77. patchpath_filename = line.replace('bj-oms-domain/src/main/java/','')

  78. patchpath_filename = patchpath_filename.replace('.java','.class')

  79. patchpath_filename = patchpath_filename.replace('\n','')

  80. patchpath_filename = 'patch/' + 'bj-oms-domain/' + patchpath_filename

  81. patchpath = os.path.dirname(patchpath_filename)

  82. #如果patch目录不存在,就建立目录

  83. try:

  84. os.listdir(patchpath)

  85. except WindowsError:

  86. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  87. #拷贝父类到patch目录下

  88. parentclass_path_filename = classpath_filename

  89. patchpath_filename = patchpath_filename

  90. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  91. shutil.copy(parentclass_path_filename,patchpath_filename)

  92. count += 1

  93. print "***********第%s个文件拷贝成功***************" % count

  94. #用glob寻找子类

  95. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  96. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  97. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  98. #把子类拷贝到patch目录下

  99. for subclass_path_filename in subclass_path_filenames:

  100. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  101. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-oms/bj-oms-domain/target/classes/','patch/bj-oms-domain/')

  102. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  103. shutil.copy(subclass_path_filename,patchpath_filename)

  104. count += 1

  105. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count


  106. elif line.startswith('bj-oms-core-new1'): #位置1,不可调换,因为bj-oms-core包含bj-oms-core-new1

  107. #filelist文件路径预处理

  108. classpath_filename = line.replace('bj-oms-core-new1/src/main/java/','../online_svn/bj-oms/bj-oms-core-new1/target/classes/')

  109. classpath_filename = classpath_filename.replace('.java','.class')

  110. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  111. #构造patch目录结构

  112. patchpath_filename = line.replace('bj-oms-core-new1/src/main/java/','')

  113. patchpath_filename = patchpath_filename.replace('.java','.class')

  114. patchpath_filename = patchpath_filename.replace('\n','')

  115. patchpath_filename = 'patch/' + 'bj-oms-core-new1/' + patchpath_filename

  116. patchpath = os.path.dirname(patchpath_filename)

  117. #如果patch目录不存在,就建立目录

  118. try:

  119. os.listdir(patchpath)

  120. except WindowsError:

  121. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  122. #拷贝父类到patch目录下

  123. parentclass_path_filename = classpath_filename

  124. patchpath_filename = patchpath_filename

  125. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  126. shutil.copy(parentclass_path_filename,patchpath_filename)

  127. count += 1

  128. print "***********第%s个文件拷贝成功***************" % count

  129. #用glob寻找子类

  130. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  131. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  132. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  133. #把子类拷贝到patch目录下

  134. for subclass_path_filename in subclass_path_filenames:

  135. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  136. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-oms/bj-oms-core-new1/target/classes/','patch/bj-oms-core-new1/')

  137. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  138. shutil.copy(subclass_path_filename,patchpath_filename)

  139. count += 1

  140. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count


  141. elif line.startswith('bj-oms-core'): #位置2,不可调换,因为bj-oms-core包含bj-oms-core-new1

  142. #filelist文件路径预处理

  143. classpath_filename = line.replace('bj-oms-core/src/main/java/','../online_svn/bj-oms/bj-oms-core/target/classes/')

  144. classpath_filename = classpath_filename.replace('.java','.class')

  145. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  146. #构造patch目录结构

  147. patchpath_filename = line.replace('bj-oms-core/src/main/java/','')

  148. patchpath_filename = patchpath_filename.replace('.java','.class')

  149. patchpath_filename = patchpath_filename.replace('\n','')

  150. patchpath_filename = 'patch/' + 'bj-oms-core/' + patchpath_filename

  151. patchpath = os.path.dirname(patchpath_filename)

  152. #如果patch目录不存在,就建立目录

  153. try:

  154. os.listdir(patchpath)

  155. except WindowsError:

  156. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  157. #拷贝父类到patch目录下

  158. parentclass_path_filename = classpath_filename

  159. patchpath_filename = patchpath_filename

  160. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  161. shutil.copy(parentclass_path_filename,patchpath_filename)

  162. count += 1

  163. print "***********第%s个文件拷贝成功***************" % count

  164. #用glob寻找子类

  165. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  166. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  167. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  168. #把子类拷贝到patch目录下

  169. for subclass_path_filename in subclass_path_filenames:

  170. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  171. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-oms/bj-oms-core/target/classes/','patch/bj-oms-core/')

  172. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  173. shutil.copy(subclass_path_filename,patchpath_filename)

  174. count += 1

  175. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count


  176. elif line.startswith('bj-oms-rest'):

  177. #filelist文件路径预处理

  178. classpath_filename = line.replace('bj-oms-rest/src/main/java/','../online_svn/bj-oms/bj-oms-rest/target/classes/')

  179. classpath_filename = classpath_filename.replace('.java','.class')

  180. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  181. #构造patch目录结构

  182. patchpath_filename = line.replace('bj-oms-rest/src/main/java/','')

  183. patchpath_filename = patchpath_filename.replace('.java','.class')

  184. patchpath_filename = patchpath_filename.replace('\n','')

  185. patchpath_filename = 'patch/' + 'bj-oms-rest/' + patchpath_filename

  186. patchpath = os.path.dirname(patchpath_filename)

  187. #如果patch目录不存在,就建立目录

  188. try:

  189. os.listdir(patchpath)

  190. except WindowsError:

  191. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  192. #拷贝父类到patch目录下

  193. parentclass_path_filename = classpath_filename

  194. patchpath_filename = patchpath_filename

  195. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  196. shutil.copy(parentclass_path_filename,patchpath_filename)

  197. count += 1

  198. print "***********第%s个文件拷贝成功***************" % count

  199. #用glob寻找子类

  200. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  201. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  202. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  203. #把子类拷贝到patch目录下

  204. for subclass_path_filename in subclass_path_filenames:

  205. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  206. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-oms/bj-oms-rest/target/classes/','patch/bj-oms-rest/')

  207. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  208. shutil.copy(subclass_path_filename,patchpath_filename)

  209. count += 1

  210. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count


  211. elif line.startswith('bj-oms-jobs'):

  212. #filelist文件路径预处理

  213. classpath_filename = line.replace('bj-oms-jobs/src/main/java/','../online_svn/bj-oms/bj-oms-jobs/target/classes/')

  214. classpath_filename = classpath_filename.replace('.java','.class')

  215. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  216. #构造patch目录结构

  217. patchpath_filename = line.replace('bj-oms-jobs/src/main/java/','')

  218. patchpath_filename = patchpath_filename.replace('.java','.class')

  219. patchpath_filename = patchpath_filename.replace('\n','')

  220. patchpath_filename = 'patch/' + 'bj-oms-jobs/' + patchpath_filename

  221. patchpath = os.path.dirname(patchpath_filename)

  222. #如果patch目录不存在,就建立目录

  223. try:

  224. os.listdir(patchpath)

  225. except WindowsError:

  226. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  227. #拷贝父类到patch目录下

  228. parentclass_path_filename = classpath_filename

  229. patchpath_filename = patchpath_filename

  230. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  231. shutil.copy(parentclass_path_filename,patchpath_filename)

  232. count += 1

  233. print "***********第%s个文件拷贝成功***************" % count

  234. #用glob寻找子类

  235. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  236. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  237. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  238. #把子类拷贝到patch目录下

  239. for subclass_path_filename in subclass_path_filenames:

  240. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  241. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-oms/bj-oms-jobs/target/classes/','patch/bj-oms-jobs/')

  242. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  243. shutil.copy(subclass_path_filename,patchpath_filename)

  244. count += 1

  245. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count


  246. elif line.startswith('bj-tran'):

  247. #filelist文件路径预处理

  248. classpath_filename = line.replace('bj-tran/src/main/java/','../online_svn/bj-tran/target/classes/')

  249. classpath_filename = classpath_filename.replace('.java','.class')

  250. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  251. #构造patch目录结构

  252. patchpath_filename = line.replace('bj-tran/src/main/java/','')

  253. patchpath_filename = patchpath_filename.replace('.java','.class')

  254. patchpath_filename = patchpath_filename.replace('\n','')

  255. patchpath_filename = 'patch/' + 'bj-tran/' + patchpath_filename

  256. patchpath = os.path.dirname(patchpath_filename)

  257. #如果patch目录不存在,就建立目录

  258. try:

  259. os.listdir(patchpath)

  260. except WindowsError:

  261. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  262. #拷贝父类到patch目录下

  263. parentclass_path_filename = classpath_filename

  264. patchpath_filename = patchpath_filename

  265. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  266. shutil.copy(parentclass_path_filename,patchpath_filename)

  267. count += 1

  268. print "***********第%s个文件拷贝成功***************" % count

  269. #用glob寻找子类

  270. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  271. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  272. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  273. #把子类拷贝到patch目录下

  274. for subclass_path_filename in subclass_path_filenames:

  275. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  276. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-tran/target/classes/','patch/bj-tran/')

  277. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  278. shutil.copy(subclass_path_filename,patchpath_filename)

  279. count += 1

  280. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count



  281. elif line.startswith('bj-whm-rest'):

  282. #filelist文件路径预处理

  283. classpath_filename = line.replace('bj-whm-rest/src/main/java/','../online_svn/bj-whm-rest/target/classes/')

  284. classpath_filename = classpath_filename.replace('.java','.class')

  285. classpath_filename = classpath_filename.replace('\n','') #去掉换行符

  286. #构造patch目录结构

  287. patchpath_filename = line.replace('bj-whm-rest/src/main/java/','')

  288. patchpath_filename = patchpath_filename.replace('.java','.class')

  289. patchpath_filename = patchpath_filename.replace('\n','')

  290. patchpath_filename = 'patch/' + 'bj-whm-rest/' + patchpath_filename

  291. patchpath = os.path.dirname(patchpath_filename)

  292. #如果patch目录不存在,就建立目录

  293. try:

  294. os.listdir(patchpath)

  295. except WindowsError:

  296. os.makedirs(patchpath) #makedirs可以在windows 的patch目录下递归建立文件夹

  297. #拷贝父类到patch目录下

  298. parentclass_path_filename = classpath_filename

  299. patchpath_filename = patchpath_filename

  300. print "拷贝svn class文件到patch目录: %s ---> %s " % (parentclass_path_filename,patchpath_filename)

  301. shutil.copy(parentclass_path_filename,patchpath_filename)

  302. count += 1

  303. print "***********第%s个文件拷贝成功***************" % count

  304. #用glob寻找子类

  305. path_filename = os.path.split(classpath_filename) #split:返回一个二元组,包含文件的路径与文件名

  306. filename_splitext = os.path.splitext(path_filename[1]) #去掉文件扩展名

  307. subclass_path_filenames = glob.glob('%s/%s$*' % (path_filename[0],filename_splitext[0]))

  308. #把子类拷贝到patch目录下

  309. for subclass_path_filename in subclass_path_filenames:

  310. subclass_path_filename = subclass_path_filename.replace('\\',r'/')

  311. patchpath_filename = subclass_path_filename.replace('../online_svn/bj-whm-rest/target/classes/','patch/bj-whm-rest/')

  312. print "拷贝svn subclass文件到patch目录: %s ---> %s " % (subclass_path_filename,patchpath_filename)

  313. shutil.copy(subclass_path_filename,patchpath_filename)

  314. count += 1

  315. print "***********第%s个文件拷贝成功,注意该文件是子类哦^_^ *****************************************" % count



  316. else:

  317. print "%s:该项目没有配置,请联系管理员" % line

  318. sys.exit()



  319. #down jar包

  320. @hosts('czoms@10.2.88.47')

  321. def download_bj_oms_api_jar():

  322. get('/opt/bj-oms-core/lib/bj-oms-api-1.0-SNAPSHOT.jar','patch/bj-oms-api/')


  323. @hosts('czoms@10.2.88.47')

  324. def download_bj_oms_domain_jar():

  325. get('/opt/bj-oms-core/lib/bj-oms-domain-1.0-SNAPSHOT.jar','patch/bj-oms-domain/')


  326. @hosts('czoms@10.2.88.40')

  327. def download_bj_oms_core_new1_jar():

  328. get('/opt/bj-oms-core/lib/bj-oms-core-1.0-SNAPSHOT.jar','patch/bj-oms-core-new1/')


  329. @hosts('appuser@10.2.88.30')

  330. def download_bj_oms_rest_jar():

  331. get('/opt/bj-oms-rest/lib/bj-oms-rest-1.0-SNAPSHOT.jar','patch/bj-oms-rest/')


  332. @hosts('appuser@10.2.88.30')

  333. def download_bj_oms_job_jar():

  334. get('/opt/bj-oms-job/lib/bj-oms-job-1.0-SNAPSHOT.jar','patch/bj-oms-jobs/')


  335. @hosts('czoms@10.2.88.47')

  336. def download_bj_oms_core_jar():

  337. get('/opt/bj-oms-core/lib/bj-oms-core-1.0-SNAPSHOT.jar','patch/bj-oms-core/')



  338. #替换jar包里面的文件

  339. def replace_file_to_local_bj_oms_api_jar():

  340. with lcd('patch/bj-oms-api/'):

  341. local('jar -uvf bj-oms-api-1.0-SNAPSHOT.jar * ')

  342. '''

  343. def replace_file_to_local_bj_oms_core_new1_jar():

  344. with lcd('patch/bj-oms-core-new1/'):

  345. local('jar -uvf bj-oms-core-1.0-SNAPSHOT.jar * ')

  346. '''


  347. def replace_file_to_local_bj_oms_domain_jar():

  348. with lcd('patch/bj-oms-domain/'):

  349. local('jar -uvf bj-oms-domain-1.0-SNAPSHOT.jar * ')


  350. def replace_file_to_local_bj_oms_job_jar():

  351. with lcd('patch/bj-oms-job/'):

  352. local('jar -uvf bj-oms-job-1.0-SNAPSHOT.jar * ')


  353. def replace_file_to_local_bj_oms_rest_jar():

  354. with lcd('patch/bj-oms-rest/'):

  355. local('jar -uvf bj-oms-rest-1.0-SNAPSHOT.jar * ')


  356. def replace_file_to_local_bj_oms_core_jar():

  357. with lcd('patch/bj-oms-core/'):

  358. local('jar -uvf bj-oms-core-1.0-SNAPSHOT.jar * ')





  359. #backup

  360. @hosts('czoms@10.2.88.47')

  361. def backup_bj_oms_api_jar():

  362. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/$dir1 && cp -a /opt/bj-oms-core/lib/bj-oms-api-1.0-SNAPSHOT.jar /backup/$dir1')

  363. puts('bj-oms-api-1.0-SNAPSHOT.jar包备份到了10.2.88.47的/backup目录下')


  364. @hosts('czoms@10.2.88.47')

  365. def backup_bj_oms_domain_jar():

  366. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/$dir1 && cp -a /opt/bj-oms-core/lib/bj-oms-domain-1.0-SNAPSHOT.jar /backup/$dir1')

  367. puts('bj-oms-domain-1.0-SNAPSHOT.jar包备份到了10.2.88.47的/backup目录下')


  368. @hosts('czoms@10.2.88.40')

  369. def backup_bj_oms_core_new1_jar():

  370. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/$dir1 && cp -a /opt/bj-oms-core/lib/bj-oms-core-1.0-SNAPSHOT.jar /backup/$dir1')

  371. puts('bj-oms-core-1.0-SNAPSHOT.jar包备份到了10.2.88.40的/backup目录下')


  372. @hosts('appuser@10.2.88.30')

  373. def backup_bj_oms_rest_jar():

  374. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/$dir1 && cp -a /opt/bj-oms-rest/lib/bj-oms-rest-1.0-SNAPSHOT.jar /backup/$dir1')

  375. puts('bj-oms-rest-1.0-SNAPSHOT.jar包备份到了10.2.88.30的/backup/目录下')


  376. @hosts('appuser@10.2.88.30')

  377. def backup_bj_oms_job_jar():

  378. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/bj-oms-job/$dir1 && cp -a /opt/bj-oms-job/lib/bj-oms-job-1.0-SNAPSHOT.jar /backup/bj-oms-job/$dir1')

  379. puts('bj-oms-job-1.0-SNAPSHOT.jar包备份到了10.2.88.30的/backup/目录下')


  380. @hosts('appuser@10.2.88.30')

  381. def backup_bj_web():

  382. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/$dir1 && cp -a /opt/tomcat-oms/webapps/bj-web /backup/$dir1')

  383. puts('bj-web包备份到了10.2.88.30的/backup/目录下')


  384. @hosts('czoms@10.2.88.47')

  385. def backup_bj_oms_core_jar():

  386. run('dir1=$(date +%Y%m%d%H%M%S) && mkdir /backup/$dir1 && cp -a /opt/bj-oms-core/lib/bj-oms-core-1.0-SNAPSHOT.jar /backup/$dir1')

  387. puts('bj_oms_core包备份到了10.2.88.30的/backup/目录下')



  388. #发版,上传jar包

  389. @hosts('czoms@10.2.88.47','czoms@10.2.88.48','czoms@10.2.88.37','czoms@10.2.88.46','czoms@10.2.88.40','czoms@10.2.88.41')

  390. def put_bj_oms_api_jar_4748_3746_4041():

  391. with lcd ('patch/bj-oms-api/'):

  392. put('bj-oms-api-1.0-SNAPSHOT.jar','/opt/bj-oms-core/lib/')


  393. @hosts('appuser@10.2.88.30')

  394. def put_bj_oms_api_jar_30():

  395. with lcd ('patch/bj-oms-api/'):

  396. put('bj-oms-api-1.0-SNAPSHOT.jar','/opt/bj-oms-rest/lib')

  397. put('bj-oms-api-1.0-SNAPSHOT.jar','/opt/bj-oms-job/lib')

  398. put('bj-oms-api-1.0-SNAPSHOT.jar','/opt/tomcat-oms/webapps/bj-web/WEB-INF/lib')



  399. @hosts('czoms@10.2.88.47','czoms@10.2.88.48','czoms@10.2.88.37','czoms@10.2.88.46','czoms@10.2.88.40','czoms@10.2.88.41')

  400. def put_bj_oms_domain_jar_4748_3746_4041():

  401. with lcd ('patch/bj-oms-domain/'):

  402. put('bj-oms-domain-1.0-SNAPSHOT.jar','/opt/bj-oms-core/lib')


  403. @hosts('appuser@10.2.88.30')

  404. def put_bj_oms_domain_jar_30():

  405. with lcd ('patch/bj-oms-domain/'):

  406. put('bj-oms-domain-1.0-SNAPSHOT.jar','/opt/bj-oms-rest/lib')

  407. put('bj-oms-domain-1.0-SNAPSHOT.jar','/opt/bj-oms-job/lib')

  408. put('bj-oms-domain-1.0-SNAPSHOT.jar','/opt/tomcat-oms/webapps/bj-web/WEB-INF/lib')



  409. @roles('cz-oms-core-new1')

  410. def put_bj_oms_core_new1_jar():

  411. with lcd ('patch/bj-oms-core-new1/'):

  412. put('bj-oms-core-1.0-SNAPSHOT.jar','/opt/bj-oms-core/lib')


  413. @roles('cz')

  414. def put_bj_oms_rest_jar():

  415. with lcd ('patch/bj-oms-rest/'):

  416. put('bj-oms-rest-1.0-SNAPSHOT.jar','/opt/bj-oms-rest/lib')


  417. @roles('cz')

  418. def put_bj_oms_job_jar():

  419. with lcd ('patch/bj-oms-jobs/'):

  420. put('bj-oms-job-1.0-SNAPSHOT.jar','/opt/bj-oms-job/lib')


  421. @roles('cz')

  422. def put_bj_web():

  423. with lcd ('patch/'):

  424. put('bj-web.zip','/opt/tomcat-oms/webapps')

  425. run('cd /opt/tomcat-oms/webapps && unzip -o bj-web.zip && rm -rf bj-web.zip')


  426. @roles('cz-oms-core')

  427. def put_bj_oms_core_jar():

  428. with lcd ('patch/bj-oms-core'):

  429. put('bj-oms-core-1.0-SNAPSHOT.jar','/opt/bj-oms-core/lib')



  430. #重启服务

  431. @roles('cz-oms-core')

  432. def stop_cz_oms_core_4748():

  433. sudo('/opt/bj-oms-core/bin/stop.sh',pty=False)


  434. @roles('cz-oms-core')

  435. def start_cz_oms_core_4748():

  436. sudo('/opt/bj-oms-core/bin/start.sh',pty=False)




  437. @roles('cz-oms-core-new')

  438. def stop_cz_oms_core__new_3746():

  439. sudo('/opt/bj-oms-core/bin/stop.sh',pty=False)


  440. @roles('cz-oms-core-new')

  441. def start_cz_oms_core__new_3746():

  442. sudo('nohup /opt/bj-oms-core/bin/start.sh &',pty=False)




  443. @roles('cz-oms-core-new1')

  444. def stop_cz_oms_core__new1_4041():

  445. sudo('/opt/bj-oms-core/bin/stop.sh',pty=False)


  446. @roles('cz-oms-core-new1')

  447. def start_cz_oms_core__new1_4041():

  448. sudo('/opt/bj-oms-core/bin/start.sh',pty=False)





  449. @roles('cz')

  450. def stop_cz_bj_oms_rest_30():

  451. sudo('/opt/bj-oms-rest/bin/stop.sh',pty=False)


  452. @roles('cz')

  453. def start_cz_bj_oms_rest_30():

  454. sudo('nohup /opt/bj-oms-rest/bin/start.sh &',pty=False)




  455. @roles('cz')

  456. def stop_cz_bj_oms_job_30():

  457. sudo('/opt/bj-oms-job/bin/stop.sh',pty=False)


  458. @roles('cz')

  459. def start_cz_bj_oms_job_30():

  460. sudo('/opt/bj-oms-job/bin/start.sh',pty=False)




  461. @roles('cz')

  462. def stop_cz_tomcat_oms_30():

  463. sudo('/opt/tomcat-oms/bin/shutdown.sh',pty=False)


  464. @roles('cz')

  465. def start_cz_tomcat_oms_30():

  466. sudo('/opt/tomcat-oms/bin/start.sh',pty=False)









  467. #主程序

  468. if __name__ == '__main__':

  469. #Update SVN

  470. while True:

  471. temp = str(raw_input('1) 请问你要Update SVN吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  472. tips = temp.strip().lower()

  473. if tips == 'yes':

  474. print "Now begin Update SVN..."

  475. UpdateSVN('../online_svn/bj-oms/bj-oms-api')

  476. UpdateSVN('../online_svn/bj-oms/bj-oms-core')

  477. UpdateSVN('../online_svn/bj-oms/bj-oms-core-new')

  478. UpdateSVN('../online_svn/bj-oms/bj-oms-core-new1')

  479. UpdateSVN('../online_svn/bj-oms/bj-oms-domain')

  480. UpdateSVN('../online_svn/bj-oms/bj-oms-jobs')

  481. UpdateSVN('../online_svn/bj-oms/bj-oms-rest')

  482. UpdateSVN('../online_svn/bj-tran')

  483. UpdateSVN('../online_svn/bj-whm')

  484. break

  485. elif tips == 'no':

  486. break

  487. #删除patch目录下所有内容

  488. while True:

  489. temp = str(raw_input('2)删除patch目录下所有目录吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  490. tips = temp.strip().lower()

  491. if tips == 'yes':

  492. for line in os.listdir('patch'):

  493. shutil.rmtree('patch/%s' % line)

  494. break

  495. elif tips == 'no':

  496. break


  497. #提示是否eclipse编译代码

  498. while True:

  499. temp = str(raw_input('3) 提示:请问你手工通过eclse编译css代码了吗[yes/no]:'))

  500. tips = temp.strip().lower()

  501. if tips == 'yes':

  502. break

  503. elif tips == 'no':

  504. break



  505. #根据filelist生成发版目录

  506. while True:

  507. temp = str(raw_input('4)根据filelist生成发版目录吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  508. tips = temp.strip().lower()

  509. if tips == 'yes':

  510. print "Now begin generate faban dir..."

  511. faban_dir()

  512. break

  513. elif tips == 'no':

  514. break



  515. #unzip

  516. while True:

  517. temp = str(raw_input('5)解压zip包到本地patch目录吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  518. tips = temp.strip().lower()

  519. if tips == 'yes':

  520. print "Now begin unzip jar to patch dir..."

  521. output = subprocess.check_output(['fab','unzip'],shell=True,stderr = subprocess.STDOUT)

  522. print output

  523. break

  524. elif tips == 'no':

  525. break


  526. #download jar to localpath

  527. while True:

  528. temp = str(raw_input('6)把jar包下载到本地的patch目录吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  529. tips = temp.strip().lower()

  530. if tips == 'yes':

  531. print "Now begin download jar to patch dir..."

  532. dirs = [item for item in os.listdir('patch/') if not item.endswith('zip')]

  533. for index,item in enumerate(dirs,1):

  534. if item == 'bj-oms-api':

  535. print "%d: %s is downoading..." % (index,item)

  536. output = subprocess.check_output(['fab','download_bj_oms_api_jar'],shell=True,stderr = subprocess.STDOUT) #shell=True在windows时可以关闭cmd窗口的回显

  537. print output

  538. elif item == "bj-oms-core-new1":

  539. print "%d: %s is downoading..." % (index,item)

  540. output = subprocess.check_output(['fab','download_bj_oms_core_new1_jar'],shell=True,stderr = subprocess.STDOUT)

  541. print output

  542. elif item == "bj-oms-rest":

  543. print "%d: %s is downoading..." % (index,item)

  544. output = subprocess.check_output(['fab','download_bj_oms_rest_jar'],shell=True,stderr = subprocess.STDOUT)

  545. print output

  546. elif item == "bj-oms-jobs":

  547. print "%d: %s is downoading..." % (index,item)

  548. output = subprocess.check_output(['fab','download_bj_oms_job_jar'],shell=True,stderr = subprocess.STDOUT)

  549. print output

  550. elif item == "bj-oms-domain":

  551. print "%d: %s is downoading..." % (index,item)

  552. output = subprocess.check_output(['fab','download_bj_oms_domain_jar'],shell=True,stderr = subprocess.STDOUT)

  553. print output

  554. elif item == "bj-oms-core":

  555. print "%d: %s is downoading..." % (index,item)

  556. output = subprocess.check_output(['fab','download_bj_oms_core_jar'],shell=True,stderr = subprocess.STDOUT)

  557. print output

  558. elif item == "bj-web":

  559. pass

  560. else:

  561. print('%s下载失败,因为该文件没有在发版程序里面配置,请联系管理员') % item

  562. sys.exit()

  563. break

  564. elif tips == 'no':

  565. break




  566. #replace file to jar

  567. while True:

  568. temp = str(raw_input('7)替换jar包里面的配置文件吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  569. tips = temp.strip().lower()

  570. if tips == 'yes':

  571. print "Now begin replace class file from jar ..."

  572. dirs = [item for item in os.listdir('patch/') if not item.endswith('zip')]

  573. for index,item in enumerate(dirs,1):

  574. if item == 'bj-oms-api':

  575. print "%d: %s is replacing jar now..." % (index,item)

  576. output = subprocess.check_output(['fab','replace_file_to_local_bj_oms_api_jar'],shell=True,stderr = subprocess.STDOUT)

  577. print output

  578. elif item == "bj-oms-core-new1":

  579. while True:

  580. temp = str(raw_input('注意:因bj-oms-core-new1包里面有重复文件,所以需要手工更新jar包里面的内容,你更新了吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  581. tips = temp.strip().lower()

  582. if tips == 'yes':

  583. break

  584. elif tips == 'no':

  585. break

  586. elif item == 'bj-oms-domain':

  587. print "%d: %s is replacing jar now..." % (index,item)

  588. output = subprocess.check_output(['fab','replace_file_to_local_bj_oms_domain_jar'],shell=True,stderr = subprocess.STDOUT)

  589. print output

  590. elif item == 'bj-oms-job':

  591. print "%d: %s is replacing jar now..." % (index,item)

  592. output = subprocess.check_output(['fab','replace_file_to_local_bj_oms_job_jar'],shell=True,stderr = subprocess.STDOUT)

  593. print output

  594. elif item == 'bj-oms-rest':

  595. print "%d: %s is replacing jar now..." % (index,item)

  596. output = subprocess.check_output(['fab','replace_file_to_local_bj_oms_rest_jar'],shell=True,stderr = subprocess.STDOUT)

  597. print output

  598. elif item == 'bj-oms-core':

  599. print "%d: %s is replacing jar now..." % (index,item)

  600. output = subprocess.check_output(['fab','replace_file_to_local_bj_oms_core_jar'],shell=True,stderr = subprocess.STDOUT)

  601. print output

  602. break

  603. elif tips == 'no':

  604. break





  605. #backup jar

  606. while True:

  607. temp = str(raw_input('8)在正式发版前希望备份远程服务器的jar包吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  608. tips = temp.strip().lower()

  609. if tips == 'yes':

  610. print "Now begin backup jar to /backup..."

  611. dirs = [item for item in os.listdir('patch/') if not item.endswith('zip')]

  612. for index,item in enumerate(dirs,1):

  613. if item == 'bj-oms-api':

  614. print "%d: %s is backuping..." % (index,item)

  615. output = subprocess.check_output(['fab','backup_bj_oms_api_jar'],shell=True,stderr = subprocess.STDOUT) #shell=True在windows时可以关闭cmd窗口的回显

  616. print output

  617. print

  618. elif item == 'bj-oms-domain':

  619. print "%d: %s is backuping..." % (index,item)

  620. output = subprocess.check_output(['fab','backup_bj_oms_domain_jar'],shell=True,stderr = subprocess.STDOUT)

  621. print output

  622. elif item == 'bj-oms-core-new1':

  623. print "%d: %s is backuping..." % (index,item)

  624. output = subprocess.check_output(['fab','backup_bj_oms_core_new1_jar'],shell=True,stderr = subprocess.STDOUT)

  625. print output

  626. elif item == 'bj-oms-rest':

  627. print "%d: %s is backuping..." % (index,item)

  628. output = subprocess.check_output(['fab','backup_bj_oms_rest_jar'],shell=True,stderr = subprocess.STDOUT)

  629. print output

  630. elif item == 'bj-oms-jobs':

  631. print "%d: %s is backuping..." % (index,item)

  632. output = subprocess.check_output(['fab','backup_bj_oms_job_jar'],shell=True,stderr = subprocess.STDOUT)

  633. print output

  634. elif item == 'bj-web':

  635. print "%d: %s is backuping..." % (index,item)

  636. output = subprocess.check_output(['fab','backup_bj_web'],shell=True,stderr = subprocess.STDOUT)

  637. print output

  638. elif item == 'bj-oms-core':

  639. print "%d: %s is backuping..." % (index,item)

  640. output = subprocess.check_output(['fab','backup_bj_oms_core_jar'],shell=True,stderr = subprocess.STDOUT)

  641. print output

  642. break

  643. elif tips == 'no':

  644. break


  645. #发版,上传jar包到生产服务器

  646. while True:

  647. temp = str(raw_input('9)请问开始向生产服务器发版吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

  648. tips = temp.strip().lower()

  649. if tips == 'yes':

  650. print "Now begin publish..."

  651. dirs = [item for item in os.listdir('patch/') if not item.endswith('zip')]

  652. for index,item in enumerate(dirs,1):

  653. if item == 'bj-oms-api':

  654. print "%d: %s is publishing..." % (index,item)

  655. output = subprocess.check_output(['fab','put_bj_oms_api_jar_4748_3746_4041'],shell=True,stderr = subprocess.STDOUT) #shell=True在windows时可以关闭cmd窗口的回显

  656. print output

  657. output = subprocess.check_output(['fab','put_bj_oms_api_jar_30'],shell=True,stderr = subprocess.STDOUT)

  658. print output

  659. print

  660. elif item == 'bj-oms-domain':

  661. print "%d: %s is publishing..." % (index,item)

  662. output = subprocess.check_output(['fab','put_bj_oms_domain_jar_4748_3746_4041'],shell=True,stderr = subprocess.STDOUT)

  663. print output

  664. output = subprocess.check_output(['fab','put_bj_oms_domain_jar_30'],shell=True,stderr = subprocess.STDOUT)

  665. print output

  666. elif item == 'bj-oms-core-new1':

  667. print "%d: %s is publishing..." % (index,item)

  668. output = subprocess.check_output(['fab','put_bj_oms_core_new1_jar'],shell=True,stderr = subprocess.STDOUT)

  669. print output

  670. elif item == 'bj-oms-rest':

  671. print "%d: %s is publishing..." % (index,item)

  672. output = subprocess.check_output(['fab','put_bj_oms_rest_jar'],shell=True,stderr = subprocess.STDOUT)

  673. print output

  674. elif item == 'bj-oms-jobs':

  675. print "%d: %s is publishing..." % (index,item)

  676. output = subprocess.check_output(['fab','put_bj_oms_job_jar'],shell=True,stderr = subprocess.STDOUT)

  677. print output

  678. elif item == 'bj-web':

  679. print "%d: %s is publishing..." % (index,item)

  680. output = subprocess.check_output(['fab','put_bj_web'],shell=True,stderr = subprocess.STDOUT)

  681. print output

  682. elif item == 'bj-oms-core':

  683. print "%d: %s is publishing..." % (index,item)

  684. output = subprocess.check_output(['fab','put_bj_oms_core_jar'],shell=True,stderr = subprocess.STDOUT)

  685. print output

  686. break

  687. elif tips == 'no':

  688. break


  689. #关闭服务

  690. while True:

  691. temp = str(raw_input('10)请问开始关闭服务吗,包含服务tomcat、rest、job、core?[yes/no]:'.decode('utf-8').encode('gb2312')))

  692. tips = temp.strip().lower()

  693. if tips == 'yes':

  694. print "Now begin stop service..."

  695. output = subprocess.check_output(['fab','stop_cz_tomcat_oms_30'],shell=True,stderr = subprocess.STDOUT)

  696. print output

  697. output = subprocess.check_output(['fab','stop_cz_bj_oms_rest_30'],shell=True,stderr = subprocess.STDOUT)

  698. print output

  699. output = subprocess.check_output(['fab','stop_cz_bj_oms_job_30'],shell=True,stderr = subprocess.STDOUT)

  700. print output

  701. output = subprocess.check_output(['fab','stop_cz_oms_core_4748'],shell=True,stderr = subprocess.STDOUT)

  702. print output

  703. output = subprocess.check_output(['fab','stop_cz_oms_core__new_3746'],shell=True,stderr = subprocess.STDOUT)

  704. print output

  705. output = subprocess.check_output(['fab','stop_cz_oms_core__new1_4041'],shell=True,stderr = subprocess.STDOUT)

  706. print output


  707. elif tips == 'no':

  708. break


  709. #开启服务

  710. while True:

  711. temp = str(raw_input('11)请问开始启动服务吗,包含服务core、tomcat、rest、job?[yes/no]:'.decode('utf-8').encode('gb2312')))

  712. tips = temp.strip().lower()

  713. if tips == 'yes':

  714. print "Now begin stop service..."

  715. output = subprocess.check_output(['fab','start_cz_oms_core__new1_4041'],shell=True,stderr = subprocess.STDOUT)

  716. print output

  717. output = subprocess.check_output(['fab','start_cz_oms_core__new_3746'],shell=True,stderr = subprocess.STDOUT)

  718. print output

  719. output = subprocess.check_output(['fab','start_cz_oms_core_4748'],shell=True,stderr = subprocess.STDOUT)

  720. print output

  721. output = subprocess.check_output(['fab','start_cz_tomcat_oms_30'],shell=True,stderr = subprocess.STDOUT)

  722. print output

  723. output = subprocess.check_output(['fab','start_cz_bj_oms_rest_30'],shell=True,stderr = subprocess.STDOUT)

  724. print output

  725. output = subprocess.check_output(['fab','start_cz_bj_oms_job_30'],shell=True,stderr = subprocess.STDOUT)

  726. print output

  727. elif tips == 'no':

  728. break

感谢各位的阅读!关于"如何使用fabric自动化发版"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0