python+paramiko库+svn写的自动化部署脚本
2016-01-06 14:49
615 查看
第一篇博文
直接开门见山的说了。
这是件什么事?:每次部署都是复制本地的文件粘贴到服务器端,因为路径复杂,所以费时且手工容易出漏洞。
一直在想有什么办法可以解决这种,因为以前在微软的一个牛人同事做过一个脚本实现自动化部署,正巧最近在学习python,就用python写一个自动化得脚本吧。
1.首先是环境:SVN的版本控制,windows的本地,linux服务器。
2.思路:应用python,代入cmd脚本,加上强大的python库来做这件事。
3.再详细的思路:
第一步:利用SVN的cmd下得脚本,获取到对应版本修订号下修改log。用正则表达式取出修改后的文件名及路径
第二步:因为是java程序,这样取出来的是xx.java文件,但需要部署上去的是编译后的xx.class文件,所以需要取到本地编译过后文件。
第三步:安装python paramiko库.
第四步:just build and run it
__author__ = 'Administrator' # coding:utf8 import os import re url = "svn://192.168.30.111/ZXJY_ALL" localPath = 'E:\quanli360\项目' vision_num = raw_input('type a vision num: ') # 需要运行SVN的命令 svn_command = 'svn log ' + localPath + '\p2p_standard_2.0_quanli -r ' + vision_num + ' -v' #转码让system执行得时候可以适应中文 svn_command = svn_command.decode("utf-8").encode("gbk") print 'run shell is===', svn_command #执行svn_command a = os.popen(svn_command).read() print a #正则取出版本号得更新文件路径 print "取出文件路径:" def regPath(strPath): reg = 'p2p_standard_2.0_quanli\\s*\\w*\\W*\\S*' regCom = re.compile(reg) pathList = re.findall(regCom, strPath) #print pathList return pathList res = regPath(a) print '=='*60 def regPathC(strPath): reg = '\\com\\\\s*\\w*\\W*\\S*' regCom = re.compile(reg) pathList = re.findall(regCom, strPath) #print pathList return pathList def regPathS(strPath): reg = '\\site\\\\s*\\w*\\W*\\S*' regCom = re.compile(reg) pathList = re.findall(regCom, strPath) #print pathList return pathList #sftp命令上传至服务器,<目前只能先删除待更新文件,再上传文件过去> import paramiko username = 'work' password = 'Ex42tko4{G4NT@{^' hostname = '118.244.204.239' port = 2022 def deployServer(locath, objPath): try: t = paramiko.Transport((hostname, port)) t.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(t) #rename sftp.remove(objPath) print 'Now start' sftp.put(locath, objPath) print 'good' print '=='*60 t.close(); except Exception, e: import traceback traceback.print_exc() try: t.close() except: pass # 如果一个版本号下多个文件挨个取出并补全路径,执行部署 for x in range(len(res)): realLocPath = localPath + '\\' + res[x] realLocPath = realLocPath.replace("/", "\\") print (realLocPath) print '=='*60 if realLocPath.__contains__("\\com\\"): cutStr = regPathC(realLocPath) cutStr = ''.join(cutStr) #匹配本地编译后的文件 cutStr = cutStr.replace(".java", ".class") realLocPath = "E:\\apache-tomcat-6.0.37\\webapps\\archer\\WEB-INF\\classes\\" + cutStr cutStrL = cutStr.replace("\\", "/") objectPath = "/home/work/tomcat_instances/quanli360.com-instance-3/webapps/archer/WEB-INF/classes/" + cutStrL print 'Source:-->',realLocPath print 'Server:-->',objectPath deployServer(realLocPath,objectPath) elif realLocPath.__contains__("\\site\\"): print 'start site' cutStr = regPathS(realLocPath) print cutStr cutStr = ''.join(cutStr) realLocPath = 'E:\\apache-tomcat-6.0.37\\webapps\\archer\\site' + cutStr objectPath = '/home/work/tomcat_instances/quanli360.com-instance-3/webapps/archer/' + cutStr cutStrL = cutStr.replace("\\", "/") objectPath = '' + cutStrL print cutStr,realLocPath, objectPath # deployServer(realLocPath,objectPath) else: print ("no contains") # deployLoc(realLocPath) print '=='*60
相关文章推荐
- 二、Python安装扩展库
- Python开发简单爬虫
- Python数据分析笔记
- Python入门基础
- python操作MySQL数据库(MySQLdb
- Python环境搭建
- 一、Python安装下载
- 北京python培训哪家靠谱?
- python count(), lower(), ord(), chr(), isalpha()
- 北京python培训哪家好
- python 类、对象、方法、属性
- python+arrow按月生成表
- python中if __name__ == '__main__': 的解析
- Selenium+Python+Chrome 浏览器操作
- python之路-随笔 python处理excel文件
- python入门笔记(3)
- 中国GDP什么时候会超过美国
- python中if __name__ == '__main__': 的解析
- 用python抓网页数据
- django 阿里云上 ubuntu-x64 python+django+mysql 开发环境搭建