赞
踩
- pkg_dir=/var/www/html/deploy/packages
- cp -r myproject-$web $pkg_dir
- rm -rf $pkg_dir/myproject-$web/.git
- cd $pkg_dir
- tar zcf myproject-$web.tar.gz myproject-$web
- rm -rf myproject-$web
- md5sum myproject-$web.tar.gz | awk '{print $1}' > myproject-$web.tar.gz.md5
- cd ..
- echo -n $web > version.txt
- import os,requests,hashlib,tarfile
- #web_ver_path:web服务器的version.txt文件路径
- #ver_url:jenkins服务器的version.txt文件的URL路径
- def has_new_ver(web_ver_path,ver_url):
- if os.path.exists(web_ver_path) == False:
- return True #没有该路径,表示没有该项目文件,需要下载
- with open(web_ver_path,mode='r') as f:
- local_ver = f.read()#读取本地的版本号
- response = requests.get(ver_url)
- if local_ver != response.text:
- return True #版本号不同需要下载
- return False
- def file_is_ok(web_tar_path,jenkins_tar_md5_url):
- m=hashlib.md5()
- with open(web_tar_path,mode='rb') as f:
- while True:
- data = f.read(4096)
- if len(data) == 0:
- break
- m.update(data)
- resp = requests.get(jenkins_tar_md5_url)
- #去掉换行符
- if resp.text.strip() == m.hexdigest():
- return True
- return False
- def deploy(web_tar_path,dest,web_deploy):
- tar = tarfile.open(web_tar_path,mode="r")
- tar.extractall(path=web_deploy)
- tar.close()
- #截取项目名
- file_name = web_tar_path.split("/")[-1][:-7]
- deploy_dir = os.path.join(web_deploy,file_name)
- #创建软连接
- if os.path.exists(dest) == True:
- os.remove(dest)
- os.symlink(deploy_dir,dest)
- if __name__ == '__main__':
- web_ver_path='/var/www/deploy/version.txt'
- ver_url='http://192.168.10.30/deploy/version.txt'
- if has_new_ver(web_ver_path,ver_url) == False:
- print('没有新版本,不需要下载')
- exit()
- resp = requests.get(ver_url)
- #jenkins_tar_url:获取jenkins的tar包URL路径
- #web_tar_path:web下载tar的路径
- jenkins_tar_url=f'http://192.168.10.30/deploy/packages/myproject-{resp.text}.tar.gz'
- web_tar_path=f'/var/www/download/myproject-{resp.text}.tar.gz'
- #下载tar包
- with open(web_tar_path,mode='wb') as f:
- f.write(requests.get(jenkins_tar_url).content)
- #计算md5值的函数
- jenkins_tar_md5_url=f'http://192.168.10.30/deploy/packages/myproject-{resp.text}.tar.gz.md5'
- if file_is_ok(web_tar_path,jenkins_tar_md5_url) == False:
- print('资源不完整')
- os.remove(web_tar_path)
- #项目部署
- dest = '/var/www/html/current' #软连接
- web_deploy = '/var/www/deploy' #部署目录
- deploy(web_tar_path,dest,web_deploy)
- #更新版本文件
- if os.path.exists(web_ver_path) == True:
- os.remove(web_ver_path)
- with open(web_ver_path,mode='w') as fw:
- fw.write(resp.text)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。