温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用paramiko模块写发版机

发布时间:2022-01-18 15:23:39 来源:亿速云 阅读:138 作者:iii 栏目:开发技术

本文小编为大家详细介绍“如何用paramiko模块写发版机”,内容详细,步骤清晰,细节处理妥当,希望这篇“如何用paramiko模块写发版机”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

说明:为了安全,本文敏感信息如ip、用户名密码等做了转换处理。

一、场景:每次发版时,都需要发版人一个一个文件的往生产机上拷贝,这样不仅费时,而且容易出错,降低工程师的工作积极性。

二、解决方法:需用python脚本写一个发版机,自动发版。

三、脚本实现的功能:自动合并develop_svn(开发svn)代码到online_svn(生产svn)下,自动备份远程服务器代码,手工通过ecplice编译online_svn代码,然后自动把编译后的class文件根据filelist里面的路径拷贝到生产机,并自动重启服务器上的tomcat服务。

四、具体步骤:

1、开发提供类似如下svn文件列表:

filelist.txt里面的内容:

/trunk/src/ha/lalala/controller/BoardController.java

/trunk/src/ha/lalala/pda/PdaWaybillController.java

/trunk/WebRoot/jsp/productReview/list.jsp

2、建立develop_svn和online_svn目录;

如何用paramiko模块写发版机

3、编写faban.py脚本

点击(此处)折叠或打开

  1. #-*-coding:utf-8-*-

  2. import Crypto #paramiko模块依赖Crypto模块

  3. import paramiko #paramiko模块是wnidows 远程linux机器用的模块

  4. import os

  5. import sys

  6. import shutil #shutil模块下有copy文件夹的方法

  7. import time #这里使用其休眠函数sleep

  8. import subprocess

  9. import glob




  10. #Update SVN

  11. def UpdateSVN(path):

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

  13.     print p.stdout.readlines()

  14.     



  15. #定义函数用来合并svn

  16. def MergeSVN(develop_svn,online_svn):

  17.     with open('filelist.txt') as f:

  18.         for index,line in enumerate(f,1): #1表示索引值从1开始

  19.             line = line.replace('/trunk','') #替换路径

  20.             line = line.replace('\n','') #把换行替换掉,\n是换行符的意思

  21.             develop_svn_path=develop_svn + line #拼接路径

  22.             online_svn_path = online_svn + line #拼接路径

  23.             print "%d  copying:  %s ---> %s" % (index,develop_svn_path,online_svn_path)

  24.             if not os.path.exists(os.path.dirname(online_svn_path)) : #如果目录不存在,就建立一个目录,注意exists方法返回的是布尔值,所以用Not进行否定

  25.                 os.mkdir(os.path.dirname(online_svn_path)) #建立目录

  26.             shutil.copy(develop_svn_path,online_svn_path) #将develop_svn_path里面的代码拷贝到online_svn_path目录下

  27.     print('\n') #输出一个换行

  28.     print("合并SVN目录已完成,请手工通过ecplice编译代码".decode('utf-8').encode('gb2312'))

  29.     print('\n') #输出一个换行

  30.      

  31.     



  32. #定义函数用来远程备份代码--给北京的机器备份

  33. def BackupCode(hostname,port,username,password):

  34.     ssh = paramiko.SSHClient() #创建一个从客户端连接服务器端的对象,也就是类的实例化

  35.     ssh.load_system_host_keys() #加载主机秘钥

  36.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #接受不在本地Known_host文件下的主机。否则ssh需要手工输入一次yes

  37.     ssh.connect(hostname,port,username,password) #ssh连接

  38.     

  39.     stdin,stdout,stderr = ssh.exec_command(get_pty=False,command='dir=$(date +%Y%m%d%H%M%S) && \

  40.                                              rsync -zrtopg --exclude geadPortrait/ \

  41.                                            --exclude=idcard/ --exclude=temp/ --exclude=upload \

  42.                                            --exclude=files --exclude=temporary_zip/ \

  43.                                            /opt/apache-tomcat-8.0.27/webapps/ROOT  /backup/tms/$dir') #执行远程机器的rsync命令进行备份,rsync命令没加-v参数,所以正常信息不会输出,只有报错才输出内容

  44.     result = stdout.read() #内容输出

  45.     error = stderr.read() #错误输出

  46.     print result

  47.     print error

  48.     #判断是否有错误输出,没有就说备份成功,否则说备份失败

  49.     if result.strip()=="" and error.strip()=="":

  50.         print "%s 完美,备份成功,备份位置在远程机器的/backup目录下".decode('utf-8').encode('gb2312') % hostname

  51.         print('\n')

  52.     else:

  53.         print "%s 不好了,备份失败了".decode('utf-8').encode('gb2312') % hostname

  54.         print('\n')

  55.     ssh.close() #记得关闭paramiko的ssh连接







  56. #定义函数用来远程备份代码--给香港腾讯的机器备份

  57. def BackupCode_HK_QQ(hostname,port,username,password):

  58.     ssh = paramiko.SSHClient() #创建一个从客户端连接服务器端的对象,也就是类的实例化

  59.     ssh.load_system_host_keys() #加载主机秘钥

  60.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #接受不在本地Known_host文件下的主机。否则ssh需要手工输入一次yes

  61.     ssh.connect(hostname,port,username,password) #ssh连接

  62.     stdin,stdout,stderr = ssh.exec_command(get_pty=False,command='dir=$(date +%Y%m%d%H%M%S) && \

  63.                                             rsync -zrtopg --exclude geadPortrait/ \

  64.                                            --exclude=idcard/ --exclude=temp/ --exclude=upload \

  65.                                            --exclude=files --exclude=temporary_zip/  \

  66.                                            /opt/tomcat-9090-tms/webapps/ROOT  /backup/tms/$dir') #执行远程机器的rsync命令进行备份,rsync命令没加-v参数,所以正常信息不会输出,只有报错才输出内容

  67.     result = stdout.read() #内容输出

  68.     error = stderr.read() #错误输出

  69.     print result

  70.     print error

  71.     #判断是否有错误输出,没有就说备份成功,否则说备份失败

  72.     if result.strip()=="" and error.strip()=="":

  73.         print "%s 完美,备份成功,备份位置在远程机器的/backup目录下".decode('utf-8').encode('gb2312') % hostname

  74.         print('\n')

  75.     else:

  76.         print "%s 不好了,备份失败了".decode('utf-8').encode('gb2312') % hostname

  77.         print('\n')

  78.     ssh.close() #记得关闭paramiko的ssh连接



  79.         


  80. #定义函数用来远程执行发版动作

  81. def Publish(hostname,port,username,password,local_base_path,remote_base_path):

  82.     count = 0

  83.     trans = paramiko.Transport(hostname,port) #建立paramiko的transport方式连接

  84.     trans.connect(username=username,password=password) #建立连接

  85.     sftp = paramiko.SFTPClient.from_transport(trans) #建立连接

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

  87.         for line in f:

  88.             #本地路径预处理

  89.             localpath_filename = line.replace('/trunk/src','WebRoot/WEB-INF/classes')

  90.             localpath_filename = localpath_filename.replace('/trunk/WebRoot','WebRoot')

  91.             localpath_filename = localpath_filename.replace('.java','.class')

  92.             localpath_filename = localpath_filename.replace('\n','') #把换行替换掉

  93.             #构造真正的父类本地路径

  94.             localpath_filename = local_base_path + localpath_filename

  95.             #构造真正的父类远程路径

  96.             remotepath_filename = remote_base_path + localpath_filename.replace('online_svn/WebRoot/','')

  97.             #拷贝父类到远程机器上

  98.             print "%s is publishing:  %s ---> %s " % (hostname,localpath_filename,remotepath_filename)

  99.             try:

  100.                 sftp.listdir(os.path.dirname(remotepath_filename)) #加个错误处理,如果目录不存在,就建立一个目录

  101.             except IOError:

  102.                 sftp.mkdir(os.path.dirname(remotepath_filename))

  103.             sftp.put(localpath_filename,remotepath_filename)

  104.             count += 1

  105.             print "***********第%s个文件发版成功***************" % count

  106.             #用glob模块寻找子类

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

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

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

  110.             #把子类拷贝到远程机器的目录下

  111.             for localpath_subclassfilename in localpath_subclassfilenames:

  112.                 localpath_subclassfilename = localpath_subclassfilename.replace('\\',r'/')

  113.                 remotepath_subclassfilename = remote_base_path + localpath_subclassfilename.replace('online_svn/WebRoot/','')

  114.                 print "%s is publishing:  %s ---> %s " % (hostname,localpath_subclassfilename,remotepath_subclassfilename)

  115.                 sftp.put(localpath_subclassfilename,remotepath_subclassfilename)

  116.                 count += 1

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

  118.         print " %s机器发版完成!!!".decode('utf-8').encode('gb2312') % hostname

  119.         print('\n')

  120.     trans.close() #记得关闭paramiko的transport连接

  121.                 



  122. #定义函数用来重启服务

  123. def RestartService(hostname,port,username,password):

  124.     ssh = paramiko.SSHClient() #创建一个从客户端连接服务器端的对象,也就是类的实例化

  125.     ssh.load_system_host_keys() #加载主机秘钥

  126.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #接受不在本地Known_host文件下的主机。否则ssh需要手工输入一次yes

  127.     ssh.connect(hostname,port,username,password) #ssh连接

  128.     stdin,stdout,stderr = ssh.exec_command(get_pty=False,command='/usr/local/shell/restartservice.sh')

  129.     result = stdout.read() #内容输出

  130.     error = stderr.read() #错误输出

  131.     print "%s 在重启服务...".decode('utf-8').encode('gb2312') % hostname

  132.     print result

  133.     print error

  134.     print('\n')

  135.     ssh.close() #记得关闭paramiko的ssh连接

  136.     

  137.   


  138. #主程序

  139. if __name__ == '__main__':

  140.     #Update SVN

  141.     while True:

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

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

  144.         if tips == 'yes':

  145.             print "Now begin Update SVN..."

  146.             UpdateSVN('develop_svn')

  147.             break

  148.         elif tips == 'no':

  149.             break

  150.     

  151.     #合并svn

  152.     while True:

  153.         temp = str(raw_input('2) 请问你要把开发svn合并到生产svn里面吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

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

  155.         if tips == 'yes':

  156.             print "Now begin Merge SVN..."

  157.             MergeSVN('develop_svn','online_svn')

  158.             break

  159.         elif tips == 'no':

  160.             break

  161.         

  162.     #备份远程机器上的代码

  163.     while True:

  164.         temp = str(raw_input('3)请问你要在发版前,备份一下远程服务器上的代码吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

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

  166.         if tips == 'yes':

  167.             print "Now begin backup code..."

  168.             BackupCode(hostname = '10.2.88.2',port = '22',username = 'tms',password = 'xxx')

  169.             BackupCode(hostname = '10.2.88.3',port = '22',username = 'tms',password = 'xxx')

  170.             BackupCode(hostname = '10.2.88.13',port = '22',username = 'tms',password = 'xxx')

  171.             BackupCode(hostname = '10.2.88.14',port = '22',username = 'tms',password = 'xxx')

  172.             BackupCode_HK_QQ(hostname = '10.144.89.252',port = '22',username = 'tms',password = 'xxx')

  173.             break

  174.         elif tips == 'no':

  175.             break

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

  177.     while True:

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

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

  180.         if tips == 'yes':

  181.             break

  182.         elif tips == 'no':

  183.             break

  184.     

  185.     #预发版

  186.     while True:

  187.         temp = str(raw_input('5)请问你需要先单独在预发版机器10.2.88.3上测试一下子发版吗?[yes/no]:'.decode('utf-8').encode('gb2312')))

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

  189.         if tips == 'yes':

  190.             print "Now begin publish code..."

  191.             Publish(hostname='10.2.88.3',port='22',username='tms',password='tmsOo798',local_base_path='online_svn/',remote_base_path='/opt/apache-tomcat-8.0.27/webapps/ROOT/')

  192.             print "Now begin restart service..."

  193.             RestartService(hostname = '10.2.88.3',port = '22',username = 'tms',password = 'tmsOo798')

  194.             break

  195.         elif tips == 'no':

  196.             break


  197.     #生产全发版

  198.     while True:

  199.         temp = str(raw_input('6)请问你要开始在所有正式服务器上进行发版吗,包括10.2.88.13/14/2/3,10.144.89.252?[yes/no]:'.decode('utf-8').encode('gb2312')))

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

  201.         if tips == 'yes':

  202.             print "Now begin publish code..."

  203.             Publish(hostname='10.2.88.13',port='22',username='tms',password='xxx',local_base_path='online_svn/',remote_base_path='/opt/apache-tomcat-8.0.27/webapps/ROOT/')

  204.             Publish(hostname='10.2.88.14',port='22',username='tms',password='xxx',local_base_path='online_svn/',remote_base_path='/opt/apache-tomcat-8.0.27/webapps/ROOT/')

  205.             Publish(hostname='10.2.88.2',port='22',username='tms',password='xxx',local_base_path='online_svn/',remote_base_path='/opt/apache-tomcat-8.0.27/webapps/ROOT/')

  206.             Publish(hostname='10.2.88.3',port='22',username='tms',password='xxx',local_base_path='online_svn/',remote_base_path='/opt/apache-tomcat-8.0.27/webapps/ROOT/')

  207.             Publish(hostname='10.144.89.252',port='22',username='tms',password='xxx',local_base_path='online_svn/',remote_base_path='/opt/tomcat-9090-tms/webapps/ROOT/')


  208.             break

  209.         elif tips == 'no':

  210.             break



  211.     #重启服务

  212.     while True:

  213.         temp = str(raw_input('7)请问你要开始在所有正式服务器上重启服务吗,包括10.2.88.13/14/2/3,10.144.89.252?[yes/no]:'.decode('utf-8').encode('gb2312')))

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

  215.         if tips == 'yes':

  216.             print "Now begin restart service..."

  217.             RestartService(hostname = '10.2.88.13',port = '22',username = 'tms',password = 'xxx')

  218.             print "please wait 60s..."

  219.             time.sleep(60)

  220.             RestartService(hostname = '10.2.88.14',port = '22',username = 'tms',password = 'xxx')

  221.             print "please wait 60s..."

  222.             time.sleep(60)

  223.             RestartService(hostname = '10.2.88.2',port = '22',username = 'tms',password = 'xxx')

  224.             print "please wait 60s..."

  225.             time.sleep(60)

  226.             RestartService(hostname = '10.2.88.3',port = '22',username = 'tms',password = 'xxx')

  227.             print "please wait 60s..."

  228.             time.sleep(60)

  229.             RestartService(hostname = '10.144.89.252',port = '22',username = 'tms',password = 'xxx')

  230.             break

  231.         elif tips == 'no':

  232.             break


  233.     #提示是否手工提交online_svn代码

  234.     while True:

  235.         temp = str(raw_input('8) 提示:请问你手工提交online_svn代码了吗?[yes/no]:'))

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

  237.         if tips == 'yes':

  238.             break

  239.         elif tips == 'no':

  240.             break


  241.     #退出本程时要说的话

  242.     print "\n"

  243.     print "*" * 50

  244.     print "亲,您的完版完成,记得测试业务是否正常哦!!!"

  245.     print "*" * 50

  246.     sys.exit()


  247. #加入下面这句话,才能用windows双击运行python脚本,否则双击脚本会一闪而过

  248. raw_input()

读到这里,这篇“如何用paramiko模块写发版机”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI