背景:
通过jenkins出了linux部署包后需要手动从jenkins下载,并scp到测试环境服务器,再解压替换重启服务等,手动操作太low,又碎片化时间,我打算做成自动化。只需要跑一下jenkins job,几分钟后打开页面验证功能就好了。
-
方案1:通过ftp进行上传下载,目前是使用的这种方式
-
方案2:通过SimpleHTTPServerWithUpload和SimpleHTTPServer在包所在的机器(jenkins
node)上起2个服务,通过端口请求进行上传和下载(未写代码实现
)
*稍麻烦的一点是:需要轮询去判断是否有新的文件产生
搭建ftp服务器,以及踩的一些坑,我下面写。*
方案1:
python实现ftp上传和下载:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import parser
from ftplib import FTP
import os
import sys
import time
import socket
import datetime
import errno
from dateutil import parser
class MyFTP:
"""
ftp自动下载、自动上传脚本,可以递归目录操作
"""
def __init__(self, host, port=21):
""" 初始化 FTP 客户端
参数:
host:ip地址
port:端口号
"""
# print("__init__()---> host = %s ,port = %s" % (host, port))
self.host = host
self.port = port
self.ftp = FTP()
# 重新设置下编码方式
self.ftp.encoding = 'gbk'
self.log_file = open("log.txt", "a")
self.file_list = []
def login(self, username, password):
""" 初始化 FTP 客户端
参数:
username: uftp2
password: uftp2
"""
try:
timeout = 600
socket.setdefaulttimeout(timeout)
# 0主动模式 1 #被动模式
self.ftp.set_pasv(True)
# 打开调试级别2,显示详细信息
# self.ftp.set_debuglevel(2)
self.debug_print('开始尝试连接到 %s' % self.host)
self.ftp.connect(self.host, self.port)
self.debug_print('成功连接到 %s' % self.host)
self.debug_print('开始尝试登录到 %s' % self.host)
self.ftp.login(username, password)
self.debug_print('成功登录到 %s' % self.host)
self.debug_print(self.ftp.welcome)
except Exception as err:
self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
pass
def formatSize(self, bytes):
"""
# 字节bytes转化kb\m\g
:return: str
"""
try:
bytes = float(bytes)
kb = bytes / 1024
except:
print("传入的字节格式不对")
return "Error"
if kb >= 1024:
M = kb / 1024
if M >= 1024:
G = M / 1024
return "%fG" % (G)
else:
return "%fM" % (M)
else:
return "%fkb" % (kb)
def is_same_size(self, local_file, remote_file):
"""判断远程文件和本地文件大小是否一致
参数:
local_file: 本地文件
remote_file: 远程文件
"""
try:
ssize = self.ftp.size(remote_file)
remote_file_size = self.formatSize(ssize)
except Exception as err:
# self.debug_print("is_same_size() 错误描述为:%s" % err)
remote_file_size = -1
try:
local_file_size = self.formatSize(os.path.getsize(local_file))
except Exception as err:
# self.debug_print("is_same_size() 错误描述为:%s" % err)
local_file_size = -1
self.debug_print('local_file_size:%s , remote_file_size:%s' % (local_file_size, remote_file_size))
if remote_file_size == local_file_size:
return 1
else:
return 0
def download_file(self, local_file, remote_file):
"""从ftp下载文件
参数:
local_file: 本地文件
remote_file: 远程文件
"""
self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
if self.is_same_size(local_file, remote_file):
self.debug_print('%s 文件大小相同,无需下载' % local_file)
return
else:
try:
self.debug_print('>>>>>>>>>>>>下载文件 %s 到 %s ... ...' % (remote_file, local_file))
# buf_size = 1024
file_handler = open(local_file, 'wb')
self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write)
file_handler.close()
self.debug_print('>>>>>>>>>>>>下载完成 ... ...')
except Exception as err:
self.debug_print('下载文件出错,出现异常:%s ' % err)
return
def download_file_tree(self, local_path, remote_path):
"""从远程目录下载多个文件到本地目录
参数:
local_path: 本地路径
remote_path: 远程路径
"""
print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
try:
self.ftp.cwd(remote_path)
except Exception as err:
self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
return
if not os.path.isdir(local_path):
self.debug_print('