前提条件:
1、安装linux系统
2、打开linux系统的ssh服务
3、关闭linux系统远程防火墙
4、在linux系统安装tomcat服务
5、提前准备好应用war包
6、需要安装paramiko服务
使用场景:
节省重复又麻烦的手动部署动作,自动化多服务部署提高效率。
第一步:安装jdk依赖包
第二步:安装tomcat包
#coding=utf-8
import paramiko
import time
# 创建SSH客户端用来远程连接服务器进行操作
class sshClient:
# 构造函数,创建一个ssh服务对象
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链接失败:①请检查ip、端口、用户名、密码是否错误;②请检查目标服务是否开启ssh并关闭防火墙。", e
# 调用远程命令的函数
def cmd(self, cmd):
try:
transport = self.ssh.get_transport()
channel = transport.open_session() # 连接成功后打开一个channel
channel.get_pty() # 打开远程的terminal
channel.exec_command(cmd) # 执行cmd命令
result = []
while True:
r = channel.recv(1024) # 接收socket返回的数据
if not r:
break
else:
result.append(r)
channel.close()
return "".join(result)
except Exception as e:
print "cmd执行失败。", e
# 下载远程地址的文件
def down(self, localdir, romatedir):
try:
ftp = self.ssh.open_sftp()
ftp.get(romatedir, localdir)
ftp.close()
except Exception as e:
print "文件下载失败。", e
# 上传本地文件到远程地址
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
# 关闭ssh链接
def close(self):
self.ssh.close()
# 创建自动化部署Tomcat服务,用于自动化部署和运行
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正在执行杀死Tomcat线程...\033[0m"
ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}' | xargs kill -9")
print ""
# 创建Tomcat7文件夹
print "\033[1;35m创建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 ""
# 启动tomcat
print "\033[1;35m启动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是否启动
print "\033[1;35m检查tomcat是否启动...\033[0m"
print ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}'")
# 关闭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()
第三:上传应用并启动tomcat
#coding=utf-8
import paramiko
import time
# 创建SSH客户端用来远程连接服务器进行操作
class sshClient:
# 构造函数,创建一个ssh服务对象
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链接失败:①请检查ip、端口、用户名、密码是否错误;②请检查目标服务是否开启ssh并关闭防火墙。", e
# 调用远程命令的函数
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() # 连接成功后打开一个channel
channel.get_pty() # 打开远程的terminal
channel.exec_command(cmd) # 执行cmd命令
while True:
result = channel.recv(1024) # 接收socket返回的数据
if not result:
break
print result,
channel.close()
except Exception as e:
print "cmd执行失败。", e
# 下载远程地址的文件
def down(self, localdir, romatedir):
try:
ftp = self.ssh.open_sftp()
ftp.get(romatedir, localdir)
ftp.close()
except Exception as e:
print "文件下载失败。", e
# 上传本地文件到远程地址
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
# 关闭ssh链接
def close(self):
self.ssh.close()
# 创建自动化部署Tomcat服务,用于自动化部署和运行
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)
'''
自动化部署tomcat
1.关闭tomcat服务
2.杀死tomcat线程
3.备份应用
4.删除老应用
5.上传代码war包
6.启动tomcat服务
7.打印tomcat日志
8.检查tomcat是否启动
'''
print ""
# 关闭Tomcat服务
print "\033[1;35m正在执行关闭Tomcat服务...\033[0m"
print self.tomcaturl + "/bin/shutdown.sh"
ssh.cmd(self.tomcaturl + "/bin/shutdown.sh")
print ""
# 杀死Tomcat线程
print "\033[1;35m正在执行杀死Tomcat线程...\033[0m"
ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}' | xargs kill -9")
print ""
# 备份应用
print "\033[1;35m正在执行备份应用...\033[0m"
ssh.cmd('cp -r ' + self.tomcaturl + '/webapps/webtest ' + self.tomcaturl + '/webtest_' + time.strftime('%Y%m%d_%H%M%S'))
print ""
# 删除老版本应用
print "\033[1;35m正在执行删除老版本应用...\033[0m"
ssh.cmd('rm -rf ' + self.tomcaturl + '/webapps/webtest*')
print ""
# 上传代码war包
print "\033[1;35m正在执行上传代码war包...\033[0m"
ssh.upload(self.localwarurl, self.romatewarurl)
print ""
# 启动tomcat
print "\033[1;35m正在执行启动Tomcat服务...\033[0m"
ssh.cmd("nohup " + self.tomcaturl + "/bin/startup.sh")
print ""
# 查看tomcat日志
print "\033[1;35m正在执行查看Tomcat日志...\033[0m"
ssh.cmd("cat " + self.tomcaturl + "/logs/catalina.out")
print ""
# 检查tomcat是否启动
print "\033[1;35m正在执行检查Tomcat是否启动...\033[0m"
ssh.cmd("ps aux | grep " + self.tomcaturl + " | grep -v grep | awk '{print $2}'")
# 关闭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")
'''
自动化部署tomcat
1.关闭tomcat服务
2.杀死tomcat线程
3.备份应用
4.删除老应用
5.上传代码war包
6.启动tomcat服务
7.打印tomcat日志
8.检查tomcat是否启动
'''
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 "自动化部署Tomcat服务,IP ",host, "开始"
dep = deployTomcat(host, port, username, password, tomcaturl, localwarurl, romatewarurl)
dep.run()
print "自动化部署Tomcat服务,IP ", host, "结束"
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 "自动化部署Tomcat服务,IP ", host, "开始"
dep = deployTomcat(host, port, username, password, tomcaturl, localwarurl, romatewarurl)
dep.run()
print "自动化部署Tomcat服务,IP ", host, "结束"