首页 > 编程语言 >git 集成部署的python代码部分

git 集成部署的python代码部分

时间:2023-03-20 18:32:01浏览次数:40  
标签:集成 git ver tar python url web1 path os

import os, requests, hashlib, tarfile
def depoly(web1_tar_path, web1_deploy_dir, current):
tar = tarfile.open(web1_tar_path, mode="r") # 解包
tar.extractall(path=web1_deploy_dir)
tar.close()
path = web1_tar_path[:-7].split("/")[-1]
path = os.path.join(web1_deploy_dir, path)
# 如果软连接存在,则删除
if os.path.exists(current):
os.remove(current)
os.symlink(path, current)



def file_ok(web1_tar_path, jenkins_tar_md5_url):
m = hashlib.md5()
with open(web1_tar_path, mode="rb") as fr:
while True:
data = fr.read(4096) # 4K
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 has_new_version(web1_ver_path, ver_url):
if os.path.exists(web1_ver_path) == False:
return True # 空服务器情况
with open(web1_ver_path,mode="r") as fr:
local_ver = fr.read() # 本地版本号
resp = requests.get(ver_url)
if resp.text != local_ver:
return True
return False
# 判断web1服务器是否需要下载资源
# ver_url:jenkins服务器中ver.txt文件的url地址
# web1_ver_path: web1服务器中ver.txt文件路径
ver_url = "http://192.168.88.30/deploy/ver.txt"
web1_ver_path = "/var/www/deploy/ver.txt"
if has_new_version(web1_ver_path, ver_url) == False:
print("没有新版本")
exit()
# jenkins_tar_url: 下载tar包的url地址
# web1_tar_path: 本地下载tar包的路径
r = requests.get(ver_url)
jenkins_tar_url = f"http://192.168.88.30/deploy/packages/myweb-{r.text}.tar.gz"
web1_tar_path = f"/var/www/download/myweb-{r.text}.tar.gz"
with open(web1_tar_path, mode="wb") as fw: # 下载
resp = requests.get(jenkins_tar_url)
fw.write(resp.content)
jenkins_tar_md5_url = jenkins_tar_url + ".md5"
if file_ok(web1_tar_path,jenkins_tar_md5_url)==False:
print("文件已损坏!")
os.remove(web1_tar_path)
exit()
web1_deploy_dir = "/var/www/deploy"
current = "/var/www/html/current" # 软连接
depoly(web1_tar_path,web1_deploy_dir,current) # 部署
# 更新本地版本号文件
if os.path.exists(web1_ver_path):
os.remove(web1_ver_path)
with open(web1_ver_path, mode="w") as fw:
fw.write(r.text)

运行结果:

/usr/local/bin/python3 /root/PycharmProjects/NSD211/day14/demo1.py

Process finished with exit code 0

标签:集成,git,ver,tar,python,url,web1,path,os
From: https://blog.51cto.com/u_15937426/6138487

相关文章