swee zrewidował ten Gist . Przejdź do rewizji
1 file changed, 2 insertions
forgejoupdate.py
@@ -2,6 +2,8 @@ | |||
2 | 2 | # download this file and run | |
3 | 3 | # python3 forgejoupdate.py | |
4 | 4 | # | |
5 | + | # If you aren't root, use sudo or doas. | |
6 | + | # | |
5 | 7 | # If you use an init system to run Forgejo, you can let this script automatically restart Forgejo for you | |
6 | 8 | # (Assuming Forgejo's service name is `forgejo`) | |
7 | 9 | # By passsing the arguments `systemd` or `openrc` |
swee zrewidował ten Gist . Przejdź do rewizji
1 file changed, 75 insertions
forgejoupdate.py(stworzono plik)
@@ -0,0 +1,75 @@ | |||
1 | + | # HOW TO USE THIS SCRIPT | |
2 | + | # download this file and run | |
3 | + | # python3 forgejoupdate.py | |
4 | + | # | |
5 | + | # If you use an init system to run Forgejo, you can let this script automatically restart Forgejo for you | |
6 | + | # (Assuming Forgejo's service name is `forgejo`) | |
7 | + | # By passsing the arguments `systemd` or `openrc` | |
8 | + | # | |
9 | + | # python3 forgejoupdate.py systemd | |
10 | + | ||
11 | + | from tqdm import tqdm | |
12 | + | import requests | |
13 | + | import subprocess | |
14 | + | import sys | |
15 | + | import hashlib | |
16 | + | import os | |
17 | + | import stat | |
18 | + | print("Checking latest version...") | |
19 | + | req = requests.get("https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/latest").json() | |
20 | + | latest = req["tag_name"][1:] | |
21 | + | download = "" | |
22 | + | sha = "" | |
23 | + | for asset in req["assets"]: | |
24 | + | if asset["name"] == f"forgejo-{latest}-linux-amd64": | |
25 | + | download = asset["browser_download_url"] | |
26 | + | if asset["name"] == f"forgejo-{latest}-linux-amd64.sha256": | |
27 | + | sha = requests.get(asset["browser_download_url"]).content.decode().strip().split(" ")[0] | |
28 | + | ||
29 | + | print(f"Latest: {latest}") | |
30 | + | print(f"Download Exists: {download != ''}") | |
31 | + | print(f"Checksum Exists: {sha != ''}") | |
32 | + | ||
33 | + | # 5 | |
34 | + | try: | |
35 | + | current = subprocess.run(["forgejo", "-v"], stdout=subprocess.PIPE).stdout.decode().strip().split(" ")[5][:-1] | |
36 | + | except: | |
37 | + | current = 0 | |
38 | + | print(f"Current: {current if current != 0 else 'Not installed!'}") | |
39 | + | if current == latest: | |
40 | + | print("No need to update.") | |
41 | + | sys.exit() | |
42 | + | ||
43 | + | print("Downloading latest version...") | |
44 | + | ||
45 | + | response = requests.get(download, stream=True) | |
46 | + | ||
47 | + | total_size = int(response.headers.get("content-length", 0)) | |
48 | + | block_size = 1024 | |
49 | + | ||
50 | + | h = hashlib.sha256() | |
51 | + | ||
52 | + | with tqdm(total=total_size, unit="B", unit_scale=True, leave=False) as progress_bar: | |
53 | + | with open("forgejo.part", "wb") as file: | |
54 | + | for data in response.iter_content(block_size): | |
55 | + | progress_bar.update(len(data)) | |
56 | + | file.write(data) | |
57 | + | h.update(data) | |
58 | + | ||
59 | + | if sha != h.hexdigest(): | |
60 | + | print(f"ERROR: SHA256 doesn't match! {hex} ≠ {h.hexdigest()}") | |
61 | + | os.remove("forgejo.part") | |
62 | + | sys.exit(1) | |
63 | + | else: | |
64 | + | print("Checksum verify success!") | |
65 | + | ||
66 | + | os.chmod("forgejo.part", stat.S_IXOTH | stat.S_IROTH | stat.S_IWUSR) | |
67 | + | ||
68 | + | os.rename("forgejo.part", "/usr/local/bin/forgejo") | |
69 | + | ||
70 | + | if len(sys.argv) == 2: | |
71 | + | print("Restarting...") | |
72 | + | if sys.argv[1] == "systemd": | |
73 | + | os.system("systemctl restart forgejo") | |
74 | + | elif sys.argv[1] == "openrc": | |
75 | + | os.system("rc-service forgejo restart") |