前提條件:
1、安裝linux系統(tǒng)
2深夯、打開linux系統(tǒng)的ssh服務(wù)
3抖格、關(guān)閉linux系統(tǒng)遠(yuǎn)程防火墻
4诺苹、在linux系統(tǒng)安裝tomcat服務(wù)
5咕晋、提前準(zhǔn)備好應(yīng)用war包
6、需要安裝paramiko服務(wù)
使用場(chǎng)景:
節(jié)省重復(fù)又麻煩的手動(dòng)部署動(dòng)作收奔,自動(dòng)化多服務(wù)部署提高效率掌呜。
第一步:安裝jdk依賴包
第二步:安裝tomcat包
#coding=utf-8
import paramiko
import time
# 創(chuàng)建SSH客戶端用來遠(yuǎn)程連接服務(wù)器進(jìn)行操作
class sshClient:
# 構(gòu)造函數(shù),創(chuàng)建一個(gè)ssh服務(wù)對(duì)象
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
try:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.host, self.port, self.username, self.password)
except Exception as e:
print "ssh鏈接失斊汉濉:①請(qǐng)檢查ip质蕉、端口、用戶名翩肌、密碼是否錯(cuò)誤模暗;②請(qǐng)檢查目標(biāo)服務(wù)是否開啟ssh并關(guān)閉防火墻。", e
# 調(diào)用遠(yuǎn)程命令的函數(shù)
def cmd(self, cmd):
try:
transport = self.ssh.get_transport()
channel = transport.open_session() # 連接成功后打開一個(gè)channel
channel.get_pty() # 打開遠(yuǎn)程的terminal
channel.exec_command(cmd) # 執(zhí)行cmd命令
result = []
while True:
r = channel.recv(1024) # 接收socket返回的數(shù)據(jù)
if not r:
break
else:
result.append(r)
channel.close()
return "".join(result)
except Exception as e:
print "cmd執(zhí)行失敗念祭。", e
# 下載遠(yuǎn)程地址的文件
def down(self, localdir, romatedir):
try:
ftp = self.ssh.open_sftp()
ftp.get(romatedir, localdir)
ftp.close()
except Exception as e:
print "文件下載失敗兑宇。", e
# 上傳本地文件到遠(yuǎn)程地址
def upload(self, localdir, romatedir):
try:
ftp = self.ssh.open_sftp() # 通過ssh打開ftp通訊通道
ftp.put(localdir, romatedir)
ftp.close()
except Exception as e:
print "文件上傳失敗。", e
# 關(guān)閉ssh鏈接
def close(self):
self.ssh.close()
# 創(chuàng)建自動(dòng)化部署Tomcat服務(wù)粱坤,用于自動(dòng)化部署和運(yùn)行
class deployTomcat:
def __init__(self, host, port, username, password, tomcaturl, localurl, romateurl):
self.host = host
self.port = port
self.username = username
self.password = password
self.tomcaturl = tomcaturl
self.localurl = localurl
self.romateurl = romateurl
def install(self):
ssh = sshClient(self.host, self.port, self.username, self.password)
# 殺死Tomcat線程
print "\033[1;35m正在執(zhí)行殺死Tomcat線程...\033[0m"
ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}' | xargs kill -9")
print ""
# 創(chuàng)建Tomcat7文件夾
print "\033[1;35m創(chuàng)建Tomcat7文件夾...\033[0m"
print ssh.cmd("rm -rf " + self.tomcaturl + ";mkdir " + self.tomcaturl)
print ""
# 上傳Tomcat安裝包
print "\033[1;35m上傳Tomcat安裝包...\033[0m"
print ssh.upload(self.localurl, self.romateurl)
print ""
# 解壓并安裝Tomcat
print "\033[1;35m解壓并安裝Tomcat...\033[0m"
print ssh.cmd("tar -xzf " + self.romateurl + " -C "+ self.tomcaturl +" --strip-components 1")
print ""
# 啟動(dòng)tomcat
print "\033[1;35m啟動(dòng)tomcat...\033[0m"
print ssh.cmd("nohup " + self.tomcaturl + "/bin/startup.sh")
print ""
# 查看tomcat日志
print "\033[1;35m查看tomcat日志...\033[0m"
print ssh.cmd("cat " + self.tomcaturl + "/logs/catalina.out")
print ""
# 檢查tomcat是否啟動(dòng)
print "\033[1;35m檢查tomcat是否啟動(dòng)...\033[0m"
print ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}'")
# 關(guān)閉ssh
ssh.close()
if __name__ == "__main__":
host = "172.20.10.10"
port = 22
username = "root"
password = "xxxx"
tomcaturl = "/usr/local/tomcat7"
localurl = "/Users/xxxx/Desktop/apache-tomcat-7.0.54.tar.gz"
romateurl = tomcaturl + "/apache-tomcat-7.0.54.tar.gz"
dep = deployTomcat(host, port, username, password, tomcaturl, localurl, romateurl)
dep.install()
第三:上傳應(yīng)用并啟動(dòng)tomcat
#coding=utf-8
import paramiko
import time
# 創(chuàng)建SSH客戶端用來遠(yuǎn)程連接服務(wù)器進(jìn)行操作
class sshClient:
# 構(gòu)造函數(shù)隶糕,創(chuàng)建一個(gè)ssh服務(wù)對(duì)象
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
try:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.host, self.port, self.username, self.password)
except Exception as e:
print "ssh鏈接失敗:①請(qǐng)檢查ip站玄、端口枚驻、用戶名、密碼是否錯(cuò)誤株旷;②請(qǐng)檢查目標(biāo)服務(wù)是否開啟ssh并關(guān)閉防火墻再登。", e
# 調(diào)用遠(yuǎn)程命令的函數(shù)
def cmd(self, cmd):
try:
'''
stdin, stdout, stderr = self.ssh.exec_command(cmd, get_pty=True)
err = stderr.readlines()
if len(err) > 0:
print err
for std in stdout.readlines():
print std,
'''
transport = self.ssh.get_transport()
channel = transport.open_session() # 連接成功后打開一個(gè)channel
channel.get_pty() # 打開遠(yuǎn)程的terminal
channel.exec_command(cmd) # 執(zhí)行cmd命令
while True:
result = channel.recv(1024) # 接收socket返回的數(shù)據(jù)
if not result:
break
print result,
channel.close()
except Exception as e:
print "cmd執(zhí)行失敗。", e
# 下載遠(yuǎn)程地址的文件
def down(self, localdir, romatedir):
try:
ftp = self.ssh.open_sftp()
ftp.get(romatedir, localdir)
ftp.close()
except Exception as e:
print "文件下載失敗。", e
# 上傳本地文件到遠(yuǎn)程地址
def upload(self, localdir, romatedir):
try:
ftp = self.ssh.open_sftp() # 通過ssh打開ftp通訊通道
ftp.put(localdir, romatedir)
ftp.close()
except Exception as e:
print "文件上傳失敗霎冯。", e
# 關(guān)閉ssh鏈接
def close(self):
self.ssh.close()
# 創(chuàng)建自動(dòng)化部署Tomcat服務(wù)铃拇,用于自動(dòng)化部署和運(yùn)行
class deployTomcat:
def __init__(self, host, port, username, password, tomcaturl, localwarurl, romatewarurl):
self.host = host
self.port = port
self.username = username
self.password = password
self.tomcaturl = tomcaturl
self.localwarurl = localwarurl
self.romatewarurl = romatewarurl
def run(self):
ssh = sshClient(self.host, self.port ,self.username, self.password)
'''
自動(dòng)化部署tomcat
1.關(guān)閉tomcat服務(wù)
2.殺死tomcat線程
3.備份應(yīng)用
4.刪除老應(yīng)用
5.上傳代碼war包
6.啟動(dòng)tomcat服務(wù)
7.打印tomcat日志
8.檢查tomcat是否啟動(dòng)
'''
print ""
# 關(guān)閉Tomcat服務(wù)
print "\033[1;35m正在執(zhí)行關(guān)閉Tomcat服務(wù)...\033[0m"
print self.tomcaturl + "/bin/shutdown.sh"
ssh.cmd(self.tomcaturl + "/bin/shutdown.sh")
print ""
# 殺死Tomcat線程
print "\033[1;35m正在執(zhí)行殺死Tomcat線程...\033[0m"
ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}' | xargs kill -9")
print ""
# 備份應(yīng)用
print "\033[1;35m正在執(zhí)行備份應(yīng)用...\033[0m"
ssh.cmd('cp -r ' + self.tomcaturl + '/webapps/webtest ' + self.tomcaturl + '/webtest_' + time.strftime('%Y%m%d_%H%M%S'))
print ""
# 刪除老版本應(yīng)用
print "\033[1;35m正在執(zhí)行刪除老版本應(yīng)用...\033[0m"
ssh.cmd('rm -rf ' + self.tomcaturl + '/webapps/webtest*')
print ""
# 上傳代碼war包
print "\033[1;35m正在執(zhí)行上傳代碼war包...\033[0m"
ssh.upload(self.localwarurl, self.romatewarurl)
print ""
# 啟動(dòng)tomcat
print "\033[1;35m正在執(zhí)行啟動(dòng)Tomcat服務(wù)...\033[0m"
ssh.cmd("nohup " + self.tomcaturl + "/bin/startup.sh")
print ""
# 查看tomcat日志
print "\033[1;35m正在執(zhí)行查看Tomcat日志...\033[0m"
ssh.cmd("cat " + self.tomcaturl + "/logs/catalina.out")
print ""
# 檢查tomcat是否啟動(dòng)
print "\033[1;35m正在執(zhí)行檢查Tomcat是否啟動(dòng)...\033[0m"
ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}'")
# 關(guān)閉ssh
ssh.close()
if __name__ == "__main__":
#ssh = sshClient("172.20.10.5", 22, "root", "xxx")
#ssh.cmd("cd /etc;ls -i")
#ssh.cmd("top")
#ssh.cmd("ping www.baidu.com")
'''
自動(dòng)化部署tomcat
1.關(guān)閉tomcat服務(wù)
2.殺死tomcat線程
3.備份應(yīng)用
4.刪除老應(yīng)用
5.上傳代碼war包
6.啟動(dòng)tomcat服務(wù)
7.打印tomcat日志
8.檢查tomcat是否啟動(dòng)
'''
host = "172.20.10.5"
port = 22
username = "root"
password = "xxx"
tomcaturl = "/usr/local/tomcat7"
localwarurl = "/Users/xxx/Desktop/webtest.war"
romatewarurl = tomcaturl + "/webapps/webtest.war"
print "自動(dòng)化部署Tomcat服務(wù),IP ",host, "開始"
dep = deployTomcat(host, port, username, password, tomcaturl, localwarurl, romatewarurl)
dep.run()
print "自動(dòng)化部署Tomcat服務(wù)沈撞,IP ", host, "結(jié)束"
host = "172.20.10.6"
port = 22
username = "root"
password = "xxx"
tomcaturl = "/usr/local/tomcat7"
localwarurl = "/Users/xxx/Desktop/webtest.war"
romatewarurl = tomcaturl + "/webapps/webtest.war"
print ""
print "自動(dòng)化部署Tomcat服務(wù)慷荔,IP ", host, "開始"
dep = deployTomcat(host, port, username, password, tomcaturl, localwarurl, romatewarurl)
dep.run()
print "自動(dòng)化部署Tomcat服務(wù),IP ", host, "結(jié)束"