用python脚本实现自动部署环境(二)

来源:互联网 发布:seo从入门到精通 编辑:程序博客网 时间:2024/05/19 00:47

之前写了一个博客是关于paramiko的安装的

就是为了今天的博客打基础

今天就记录下怎么用paramiko模块对环境进行部署(贴出来关键的脚本片段,总不能一直做伸手党吧,自己写点儿东西如果想用我这个方法的话)

对于我们现有的系统的测试环境部署,大致步骤分为:停掉服务,删除编译过的文件,删除.war文件,删除缓存文件,把war包放到服务器,启动服务,查看日志是否报错

接下来就要从以下几方面考虑:

1:从提测单中读取到服务器地址,服务所在路径,war包所在的svn路径,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:             app = {}             for i in range(len(colnames)):                app[colnames[i]] = row[i]             list.append(app)    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包,缓存文件)()

# 删除dirs/webapps下filename和packagename文件夹下所有文件(以下将分别调用ssh的方法执行cmd命令)cmds2='rm -rf '+dirs+'/webapps/'+filename
用2的方法执行cmds2命令删除文件

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:启动服务

6:下载日志

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)            sftp.get(os.path.join(remote_dir,f),os.path.join(local_dir,f))#下载                      print'Download file success %s ' % datetime.datetime.now()            print''            print'##########################################'        t.close()    except Exception:        print 'content error'




0 0