In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this article, the editor introduces in detail "how to use the paramiko module to write the publishing machine", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the paramiko module to write the publishing machine" can help you solve your doubts.
Note: for the sake of security, sensitive information such as ip, username and password have been converted.
First, the scene: every time the version is released, the publisher is required to copy the files one by one to the production machine, which is not only time-consuming, but also error-prone, reducing the enthusiasm of engineers.
Second, the solution: need to use python script to write a distributor, automatic distribution.
Third, the script to achieve the function: automatically merge develop_svn (development svn) code into online_svn (production svn), automatically back up the remote server code, manually compile online_svn code through ecplice, and then automatically copy the compiled class file to the production machine according to the path inside filelist, and automatically restart the tomcat service on the server.
IV. Specific steps:
1. Developers provide a list of svn files similar to the following:
The contents of filelist.txt:
/ trunk/src/ha/lalala/controller/BoardController.java
/ trunk/src/ha/lalala/pda/PdaWaybillController.java
/ trunk/WebRoot/jsp/productReview/list.jsp
2. Set up develop_svn and online_svn directories
3. Write faban.py script
Click (here) to collapse or open
#-*-coding:utf-8-*-
Import Crypto # paramiko module depends on Crypto module
The import paramiko # paramiko module is a module for wnidows remote linux machines
Import os
Import sys
The method of having copy folder under import shutil # shutil module
Import time # use its hibernation function sleep here
Import subprocess
Import glob
# Update SVN
Def UpdateSVN (path):
P = subprocess.Popen (['svn','update',path], shell=True,stdout=subprocess.PIPE)
Print p.stdout.readlines ()
# define functions to merge svn
Def MergeSVN (develop_svn,online_svn):
With open ('filelist.txt') as f:
For index,line in enumerate (fjournal 1): # 1 means that the index value starts at 1
Line = line.replace ('/ trunk','') # alternate path
Line = line.replace ('\ nnewline') # replace the newline, which means the newline character
Develop_svn_path=develop_svn + line # splicing path
Online_svn_path = online_svn + line # splicing path
Print "% d copying:% s-- >% s"% (index,develop_svn_path,online_svn_path)
If not os.path.exists (os.path.dirname (online_svn_path)): # if the directory does not exist, create a directory. Note that the exists method returns a Boolean value, so negate it with Not
Os.mkdir (os.path.dirname (online_svn_path)) # create a directory
Shutil.copy (develop_svn_path,online_svn_path) # copy the code in develop_svn_path to the online_svn_path directory
Print ('\ n') # outputs a newline
Print ("merge the SVN directory has been completed, please compile the code manually through ecplice" .decode ('utf-8') .encode (' gb2312'))
Print ('\ n') # outputs a newline
# definition function is used to back up code remotely-- back up machines in Beijing
Def BackupCode (hostname,port,username,password):
Ssh = paramiko.SSHClient () # create an object that connects to the server from the client, that is, the instantiation of the class
Ssh.load_system_host_keys () # load the host key
Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # accepts hosts that are not in the local Known_host file. Otherwise, ssh needs to enter yes manually.
Ssh.connect (hostname,port,username,password) # ssh connection
Stdin,stdout,stderr = ssh.exec_command (get_pty=False,command='dir=$ (date +% Y%m%d%H%M%S) & &\
Rsync-zrtopg-- exclude geadPortrait/\
-exclude=idcard/-exclude=temp/-exclude=upload\
-- exclude=files-- exclude=temporary_zip/\
/ opt/apache-tomcat-8.0.27/webapps/ROOT / backup/tms/$dir') # execute the rsync command of the remote machine for backup. The rsync command does not add the-v parameter, so the normal information will not be output. Only if an error is reported, the content will be output.
Result = stdout.read () # content output
Error = stderr.read () # error output
Print result
Print error
# determine whether there is an error output. If not, the backup is successful, otherwise the backup fails.
If result.strip () = "" and error.strip () = "":
Print's is perfect, backup successful, backup location is in / backup directory of remote machine ".decode ('utf-8') .encode (' gb2312')% hostname
Print ('\ n')
Else:
Print's is bad, backup failed .decode ('utf-8') .encode (' gb2312') hostname
Print ('\ n')
Ssh.close () # remember to close the ssh connection to paramiko
# define a function to back up the code remotely-back up the machine of Tencent in Hong Kong
Def BackupCode_HK_QQ (hostname,port,username,password):
Ssh = paramiko.SSHClient () # create an object that connects to the server from the client, that is, the instantiation of the class
Ssh.load_system_host_keys () # load the host key
Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # accepts hosts that are not in the local Known_host file. Otherwise, ssh needs to enter yes manually.
Ssh.connect (hostname,port,username,password) # ssh connection
Stdin,stdout,stderr = ssh.exec_command (get_pty=False,command='dir=$ (date +% Y%m%d%H%M%S) & &\
Rsync-zrtopg-- exclude geadPortrait/\
-exclude=idcard/-exclude=temp/-exclude=upload\
-- exclude=files-- exclude=temporary_zip/\
/ opt/tomcat-9090-tms/webapps/ROOT / backup/tms/$dir') # execute the rsync command of the remote machine for backup. The rsync command does not add the-v parameter, so the normal information will not be output. Only if an error is reported, the content will be output.
Result = stdout.read () # content output
Error = stderr.read () # error output
Print result
Print error
# determine whether there is an error output. If not, the backup is successful, otherwise the backup fails.
If result.strip () = "" and error.strip () = "":
Print's is perfect, backup successful, backup location is in / backup directory of remote machine ".decode ('utf-8') .encode (' gb2312')% hostname
Print ('\ n')
Else:
Print's is bad, backup failed .decode ('utf-8') .encode (' gb2312') hostname
Print ('\ n')
Ssh.close () # remember to close the ssh connection to paramiko
# define a function to remotely execute the publishing action
Def Publish (hostname,port,username,password,local_base_path,remote_base_path):
Count = 0
Trans = paramiko.Transport (hostname,port) # establish a transport connection for paramiko
Trans.connect (username=username,password=password) # establish a connection
Sftp = paramiko.SFTPClient.from_transport (trans) # establish a connection
With open ('filelist.txt','r') as f:
For line in f:
# Local path preprocessing
Localpath_filename = line.replace ('/ trunk/src','WebRoot/WEB-INF/classes')
Localpath_filename = localpath_filename.replace ('/ trunk/WebRoot','WebRoot')
Localpath_filename = localpath_filename.replace ('.java', '.class')
Localpath_filename = localpath_filename.replace ('\ nnewline substitution') # replace line breaks
# construct the real local path of the parent class
Localpath_filename = local_base_path + localpath_filename
# construct a real parent remote path
Remotepath_filename = remote_base_path + localpath_filename.replace ('online_svn/WebRoot/','')
# copy the parent class to the remote machine
Print "% s is publishing:% s-- >% s"% (hostname,localpath_filename,remotepath_filename)
Try:
Sftp.listdir (os.path.dirname (remotepath_filename)) # plus error handling, if the directory does not exist, create a directory
Except IOError:
Sftp.mkdir (os.path.dirname (remotepath_filename))
Sftp.put (localpath_filename,remotepath_filename)
Count + = 1
Print "*% s file issued successfully *"% count
# use glob module to find subclasses
Path_filename = os.path.split (localpath_filename) # split: returns a tuple containing the path and file name of the file
Filename_splitext = os.path.splitext (path_filename [1]) # remove the file extension
Localpath_subclassfilenames = glob.glob ('% sram% swatches'% (path_filename [0], filename_ plitext [0]))
# copy subclasses to the directory of the remote machine
For localpath_subclassfilename in localpath_subclassfilenames:
Localpath_subclassfilename = localpath_subclassfilename.replace ('\\', rattlespace')
Remotepath_subclassfilename = remote_base_path + localpath_subclassfilename.replace ('online_svn/WebRoot/','')
Print "% s is publishing:% s-- >% s"% (hostname,localpath_subclassfilename,remotepath_subclassfilename)
Sftp.put (localpath_subclassfilename,remotepath_subclassfilename)
Count + = 1
Print "* the% s file was copied successfully. Note that the file is a subclass ^ _ ^ *"% count
Print "% s machine distribution complete!" .decode ('utf-8') .encode (' gb2312')% hostname
Print ('\ n')
Trans.close () # remember to close the transport connection to paramiko
# define a function to restart the service
Def RestartService (hostname,port,username,password):
Ssh = paramiko.SSHClient () # create an object that connects to the server from the client, that is, the instantiation of the class
Ssh.load_system_host_keys () # load the host key
Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # accepts hosts that are not in the local Known_host file. Otherwise, ssh needs to enter yes manually.
Ssh.connect (hostname,port,username,password) # ssh connection
Stdin,stdout,stderr = ssh.exec_command (get_pty=False,command='/usr/local/shell/restartservice.sh')
Result = stdout.read () # content output
Error = stderr.read () # error output
Print's is restarting the service. ".decode ('utf-8') .encode (' gb2312')% hostname
Print result
Print error
Print ('\ n')
Ssh.close () # remember to close the ssh connection to paramiko
# main program
If _ _ name__ = ='_ _ main__':
# Update SVN
While True:
Temp = str (raw_input ('1) do you want Update SVN? [yes/no]: '.decode (' utf-8'). Encode ('gb2312')
Tips = temp.strip () .lower ()
If tips = 'yes':
Print "Now begin Update SVN..."
UpdateSVN ('develop_svn')
Break
Elif tips = 'no':
Break
# merge svn
While True:
Temp = str (raw_input ('2) would you like to merge the development svn into the production svn? [yes/no]: '.decode (' utf-8'). Encode ('gb2312')
Tips = temp.strip () .lower ()
If tips = 'yes':
Print "Now begin Merge SVN..."
MergeSVN ('develop_svn','online_svn')
Break
Elif tips = 'no':
Break
# back up the code on the remote machine
While True:
Temp = str (raw_input ('3) would you like to back up the code on the remote server before publishing? [yes/no]: '.decode (' utf-8'). Encode ('gb2312')
Tips = temp.strip () .lower ()
If tips = 'yes':
Print "Now begin backup code..."
BackupCode (hostname = '10.2.88.2 tms',password xxx' port =' 22 minutes charge username = 'tms',password =' Username)
BackupCode (hostname = '10.2.88.3 tms',password xxx' port =' 22 minutes charge username = 'tms',password =' Username)
BackupCode (hostname = '10.2.88.13 tms',password xxx' port =' 22 minutes charge username = 'Username =' xxx')
BackupCode (hostname = '10.2.88.14 tms',password xxx' port =' 22 minutes charge username = 'Username =' xxx')
BackupCode_HK_QQ (hostname = '10.144.89.252recording field port =' 22nd record field name = 'tms',password =' xxx')
Break
Elif tips = 'no':
Break
# prompt whether to compile the code with eclipse
While True:
Temp = str (raw_input ('4) hint: did you manually compile the tms code through eclse [yes/no]:')
Tips = temp.strip () .lower ()
If tips = 'yes':
Break
Elif tips = 'no':
Break
# pre-release version
While True:
Temp = str (raw_input ('5) do you need to test the release version on the pre-release machine 10.2.88.3 first? [yes/no]: '.decode (' utf-8'). Encode ('gb2312')
Tips = temp.strip () .lower ()
If tips = 'yes':
Print "Now begin publish code..."
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/')
Print "Now begin restart service..."
RestartService (hostname = '10.2.88.3 tms',password tmsOo798' port =' 22 minutes charge username = 'tms',password =' Username)
Break
Elif tips = 'no':
Break
# production of full-hair version
While True:
Temp = str (raw_input ('6) would you like to start publishing on all official servers, including 10.2.88.13, 14 and 2? [yes/no]: '.decode (' utf-8'). Encode ('gb2312')
Tips = temp.strip () .lower ()
If tips = 'yes':
Print "Now begin publish code..."
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/')
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/')
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/')
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/')
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/')
Break
Elif tips = 'no':
Break
# restart the service
While True:
Temp = str (raw_input ('7) would you like to start restarting services on all official servers, including 10.2.88.13, 14, 2, 2, 3, 10.144.89.252? [yes/no]: '.decode (' utf-8'). Encode ('gb2312')
Tips = temp.strip () .lower ()
If tips = 'yes':
Print "Now begin restart service..."
RestartService (hostname = '10.2.88.13 tms',password xxx' port =' 22 minutes charge username = 'Username =' xxx')
Print "please wait 60s..."
Time.sleep (60)
RestartService (hostname = '10.2.88.14 tms',password xxx' port =' 22 minutes charge username = 'Username =' xxx')
Print "please wait 60s..."
Time.sleep (60)
RestartService (hostname = '10.2.88.2 tms',password xxx' port =' 22 minutes charge username = 'tms',password =' Username)
Print "please wait 60s..."
Time.sleep (60)
RestartService (hostname = '10.2.88.3 tms',password xxx' port =' 22 minutes charge username = 'tms',password =' Username)
Print "please wait 60s..."
Time.sleep (60)
RestartService (hostname = '10.144.89.252recording field port =' 22nd record field name = 'tms',password =' xxx')
Break
Elif tips = 'no':
Break
# prompt whether to manually submit the online_svn code
While True:
Temp = str (raw_input ('8) hint: have you submitted the online_svn code manually? [yes/no]:')
Tips = temp.strip () .lower ()
If tips = 'yes':
Break
Elif tips = 'no':
Break
# what to say when quitting the journey
Print "\ n"
Print "*" * 50
Print "Dear, your completion is complete, remember to test whether the business is normal or not!"
Print "*" * 50
Sys.exit ()
# add the following sentence to run the python script with windows double-click, otherwise double-click the script will flash by
Raw_input ()
After reading this, the article "how to write a publisher with paramiko module" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.