Add update_gitea.py
This commit is contained in:
77
update_gitea.py
Normal file
77
update_gitea.py
Normal file
@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python3
|
||||
# Cron: 15 3 * * * /usr/bin/python3 /root/update_gitea.py >> /var/log/gitea_update.log 2>&1
|
||||
|
||||
|
||||
import subprocess
|
||||
import requests
|
||||
import os
|
||||
import shutil
|
||||
import pwd
|
||||
import grp
|
||||
|
||||
GITEA_BIN = "/usr/local/bin/gitea"
|
||||
SERVICE_NAME = "gitea.service"
|
||||
DOWNLOAD_DIR = "/tmp"
|
||||
GITEA_USER = "gitea"
|
||||
GITEA_GROUP = "gitea"
|
||||
|
||||
def get_current_version():
|
||||
result = subprocess.run([GITEA_BIN, "-v"], capture_output=True, text=True)
|
||||
return result.stdout.strip().split()[2] # "1.23.6"
|
||||
|
||||
def get_latest_version():
|
||||
url = "https://api.github.com/repos/go-gitea/gitea/releases/latest"
|
||||
r = requests.get(url)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
return data["tag_name"].lstrip("v")
|
||||
|
||||
def download_latest(version):
|
||||
url = f"https://dl.gitea.com/gitea/{version}/gitea-{version}-linux-amd64"
|
||||
dest_path = os.path.join(DOWNLOAD_DIR, "gitea_latest")
|
||||
r = requests.get(url, stream=True)
|
||||
with open(dest_path, 'wb') as f:
|
||||
shutil.copyfileobj(r.raw, f)
|
||||
os.chmod(dest_path, 0o755)
|
||||
return dest_path
|
||||
|
||||
def stop_service():
|
||||
subprocess.run(["systemctl", "stop", SERVICE_NAME], check=True)
|
||||
|
||||
def start_service():
|
||||
subprocess.run(["systemctl", "daemon-reexec"])
|
||||
subprocess.run(["systemctl", "daemon-reload"])
|
||||
subprocess.run(["systemctl", "start", SERVICE_NAME], check=True)
|
||||
|
||||
def replace_binary(new_binary_path):
|
||||
backup = GITEA_BIN + ".bak"
|
||||
shutil.copy2(GITEA_BIN, backup)
|
||||
shutil.copy2(new_binary_path, GITEA_BIN)
|
||||
uid = pwd.getpwnam(GITEA_USER).pw_uid
|
||||
gid = grp.getgrnam(GITEA_GROUP).gr_gid
|
||||
os.chown(GITEA_BIN, uid, gid)
|
||||
|
||||
def verify_binary():
|
||||
result = subprocess.run([GITEA_BIN, "-v"], capture_output=True, text=True)
|
||||
print("Nowa wersja:", result.stdout.strip())
|
||||
|
||||
def main():
|
||||
current = get_current_version()
|
||||
latest = get_latest_version()
|
||||
|
||||
print("Obecna wersja:", current)
|
||||
print("Najnowsza dostępna wersja:", latest)
|
||||
|
||||
if current == latest:
|
||||
print("Gitea jest aktualna. Nie wykonuję żadnych zmian.")
|
||||
return
|
||||
|
||||
path = download_latest(latest)
|
||||
stop_service()
|
||||
replace_binary(path)
|
||||
verify_binary()
|
||||
start_service()
|
||||
print("Aktualizacja zakończona.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user