用python指令碼實現自動部署環境(二)

2021-07-30 11:58:19 字數 2976 閱讀 2446

之前寫了乙個部落格是關於paramiko的安裝的

就是為了今天的部落格打基礎

今天就記錄下怎麼用paramiko模組對環境進行部署(貼出來關鍵的指令碼片段,總不能一直做伸手黨吧,自己寫點兒東西如果想用我這個方法的話)

對於我們現有的系統的測試環境部署,大致步驟分為:停掉服務,刪除編譯過的檔案,刪除.war檔案,刪除快取檔案,把war包放到伺服器,啟動服務,檢視日誌是否報錯

接下來就要從以下幾方面考慮:

def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0):

data = open_excel(file)

table = data.sheets()[by_index]

nrows = table.nrows #行數

ncols = table.ncols #列數

colnames = table.row_values(colnameindex) #某一行資料

list =

for rownum in

range(1,nrows):

row = table.row_values(rownum)

if row:

for i in

range(len(colnames)):

return list

for i in 

range(len(excel_table_byindex(file="c:\sittest.xlsx"))): #獲取excel的行數作為迴圈取出想要的ip,dir,warpackag

ip = excel_table_byindex(file="c:\sittest.xlsx")[i]['ip']

2:連線到伺服器,kill掉tomcat程序()

ssh = paramiko.sshclient()

ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())

ssh.connect(ip, 22, '這裡是伺服器使用者名稱', '這裡是伺服器登入密碼')

stdin, stdout, stderr = ssh.exec_command("ps -ef|grep tomcat") # 找到tomcat程序

lines = stdout.readlines()

# 因為可能獲取的tomcat程序不止乙個所以要查詢到服務所在的程序

for line in lines:

if dirs in line:

pid = line.split(' ')[5]

3:刪除檔案(編譯後的檔案,原war包,快取檔案)()

4:上傳檔案

def commitfiletoserver(ip,remote_dir,local_dir):

try:

t = paramiko.transport((ip,22))

t.connect(username='這裡是登入使用者名稱',password='這裡是登入密碼')

sftp=paramiko.sftpclient.from_transport(t)

files=sftp.listdir(remote_dir)

for f in files:

print

''print

'#########################################'

print

'beginning to download file from %s %s ' % (ip,datetime.datetime.now())

print

'downloading file:',os.path.join(remote_dir,f)

sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f))#上傳

print

'download file success %s ' % datetime.datetime.now()

print

''print

'##########################################'

t.close()

except

exception:

print

'content error'

5:啟動服務

def getfilefromserver(ip,remote_dir,local_dir):

try:

t = paramiko.transport((ip,22))

t.connect(username='這裡是登入使用者名稱',password='這裡是登入密碼')

sftp=paramiko.sftpclient.from_transport(t)

files=sftp.listdir(remote_dir)

for f in files:

print

''print

'#########################################'

print

'beginning to download file from %s %s ' % (ip,datetime.datetime.now())

print

'downloading file:',os.path.join(remote_dir,f)

print

'download file success %s ' % datetime.datetime.now()

print

''print

'##########################################'

t.close()

except

exception:

print

'content error'

自動部署指令碼

author wangyang buildtime 12 06 05 desc 自動部署指令碼 currentdir pwd 判斷命令是否成功執行 isok 依次執行命令 rm rf sessionserver tradecore tradeweb isok deploy.log tar xzvf ...

部署python自動化指令碼 搭建自動化指令碼執行環境

年前把平台的指令碼寫好了,準備在虛擬機器中全部執行一遍。沒想到在虛擬機器中配置了很長時間,才把環境搭好。為了不再犯這配置環境的苦,在這簡單說說環境的搭建吧。因為寫的指令碼是在win7上實現的,所以虛擬機器我安裝的也是win7系統。1 安裝python 2 配置環境變數 python安裝成功後,將安裝...

自動部署指令碼 Linux shell

技術文件 linux,shell,自動部署,tomcat ssh key 自動登入 免密碼登入 生成ssh key 在本機 用ssh keygen t rsa命令生成公鑰,ssh 下生成 id rsa,id rsa.pub 兩檔案 講公鑰上傳至遠端伺服器 並新增到authorized keys檔案中...