Skip to content

Commit 74eb01d

Browse files
authored
Merge pull request #6 from OS2borgerPC/4-gør-os2borgerpc-client-konfigurerbar-via-konfigurationer-fra-admin-site
4 gør os2borgerpc client konfigurerbar via konfigurationer fra admin site
2 parents 43ac693 + ea21b53 commit 74eb01d

File tree

4 files changed

+89
-66
lines changed

4 files changed

+89
-66
lines changed

bin/jobmanager

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,34 @@ import pkg_resources
55
import random
66
import semver
77
import sys
8-
import subprocess
9-
10-
try:
11-
from os2borgerpc.client.updater import ( # noqa: F401
12-
get_newest_client_version,
13-
update_client,
14-
update_client_test,
15-
)
16-
from os2borgerpc.client.jobmanager import update_and_run # noqa: E402
17-
except ImportError:
18-
# If any of the imports fail, reinstall the client
19-
try:
20-
subprocess.check_call(
21-
[
22-
sys.executable,
23-
"-m",
24-
"pip",
25-
"install",
26-
"--force-reinstall",
27-
"os2borgerpc-client",
28-
]
29-
)
30-
sys.exit(0)
31-
except subprocess.CalledProcessError:
32-
print("Restoring client failed\n", file=sys.stderr)
33-
34-
# Higher values: Check for updates less often
35-
UPDATE_FREQUENCY = 200
36-
37-
if not os.geteuid() == 0:
38-
sys.exit("\n Only root can run this program. \n")
39-
40-
# We are root!
41-
42-
# Keep this in sync with package name in setup.py
43-
# This is deliberatly duplicated here from jobmanager.py to not be
44-
# dependent on jobmanager.py at this point
45-
OS2BORGERPC_CLIENT_VERSION = pkg_resources.get_distribution(
46-
"os2borgerpc_client"
47-
).version
48-
49-
# Perform an update-check with a random interval.
50-
if random.randint(1, UPDATE_FREQUENCY) == 1:
51-
print("Checking for new update to client.")
52-
newest_client_version = get_newest_client_version()
53-
print(
54-
f"Newest client version: {newest_client_version}, "
55-
f"Installed version: {OS2BORGERPC_CLIENT_VERSION}"
56-
)
57-
# update_client_test()
58-
if semver.compare(newest_client_version, OS2BORGERPC_CLIENT_VERSION) == 1:
8+
9+
from os2borgerpc.client.config import get_config
10+
from os2borgerpc.client.updater import get_newest_client_version, update_client
11+
from os2borgerpc.client.jobmanager import update_and_run
12+
13+
# Ensure the script is run as root
14+
if os.geteuid() != 0:
15+
sys.exit("\nOnly root can run this program.\n")
16+
17+
# Constants
18+
UPDATE_FREQUENCY = 200 # Higher values: Check for updates less often
19+
20+
# Get the current and desired client versions
21+
CURRENT_CLIENT_VERSION = pkg_resources.get_distribution("os2borgerpc_client").version
22+
DESIRED_CLIENT_VERSION = get_config("os2borgerpc_client_version")
23+
24+
# Automatically fetch the newest version if configured
25+
if DESIRED_CLIENT_VERSION.lower() == "auto" and random.randint(1, UPDATE_FREQUENCY) == 1:
26+
DESIRED_CLIENT_VERSION = get_newest_client_version()
27+
28+
# Check for updates if a desired version is specified
29+
if DESIRED_CLIENT_VERSION and DESIRED_CLIENT_VERSION.lower() != "auto":
30+
stripped_version = DESIRED_CLIENT_VERSION.lstrip("v")
31+
if semver.compare(stripped_version, CURRENT_CLIENT_VERSION) == 1:
32+
print(f"Installed client version: {CURRENT_CLIENT_VERSION}")
33+
print(f"Desired client version: {DESIRED_CLIENT_VERSION}")
5934
print("Updating client, please re-run jobmanager.")
60-
update_client()
35+
update_client(DESIRED_CLIENT_VERSION)
6136

37+
# Run the job manager
6238
update_and_run()

bin/os2borgerpc_register_in_admin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ data = config.get_data()
1313

1414
mac = data['mac']
1515
site = data['site']
16-
admin_url = data.get('admin_url', 'http://os2borgerpc-admin.magenta.dk/')
16+
admin_url = data.get('admin_url')
1717
xml_rpc_url = data.get('xml_rpc_url', '/admin-xml/')
1818
urllib.parse.urljoin(admin_url, xml_rpc_url)
1919

os2borgerpc/client/admin_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def get_default_admin(verbose=False):
88
from os2borgerpc.client.config import OS2borgerPCConfig
99

1010
conf_data = OS2borgerPCConfig().get_data()
11-
admin_url = conf_data.get("admin_url", "https://os2borgerpc.magenta-aps.dk")
11+
admin_url = conf_data.get("admin_url")
1212
xml_rpc_url = conf_data.get("xml_rpc_url", "/admin-xml/")
1313
return OS2borgerPCAdmin("".join([admin_url, xml_rpc_url]), verbose=verbose)
1414

os2borgerpc/client/updater.py

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,70 @@
66

77
import requests
88

9+
from os2borgerpc.client.config import get_config
910

10-
def get_newest_client_version():
11-
"""Get the newest client version from Pypi."""
12-
response = requests.get("https://pypi.org/pypi/os2borgerpc-client/json")
13-
json_object = response.json()
14-
newest_version = json_object["info"]["version"]
11+
def get_latest_version_from_github(repo_url):
12+
"""Get the latest version tag from a GitHub repository."""
13+
try:
14+
response = requests.get(f"https://api.github.com/repos/{repo_url}/tags")
15+
response.raise_for_status()
16+
tags = response.json()
17+
if tags:
18+
return tags[0]["name"]
19+
else:
20+
return None
21+
except requests.RequestException as e:
22+
print(f"Failed to fetch tags from GitHub: {e}", file=sys.stderr)
23+
return None
1524

16-
return newest_version
1725

26+
def get_newest_client_version():
27+
"""Get the newest client version from GitHub or PyPI based on configuration."""
28+
client_package = get_config("os2borgerpc_client_package")
29+
30+
if client_package.startswith("https://github.com/"):
31+
repo_parts = client_package.split("/")
32+
if len(repo_parts) >= 5:
33+
repo_user = repo_parts[3]
34+
repo_name = repo_parts[4].removesuffix('.git')
35+
repo_url = f"{repo_user}/{repo_name}"
36+
newest_version = get_latest_version_from_github(repo_url)
37+
if newest_version:
38+
return newest_version
39+
else:
40+
print("Could not determine the latest version from GitHub.", file=sys.stderr)
41+
return None
42+
else:
43+
try:
44+
response = requests.get(f"https://pypi.org/pypi/{client_package}/json")
45+
response.raise_for_status()
46+
json_object = response.json()
47+
newest_version = json_object["info"]["version"]
48+
return newest_version
49+
except requests.RequestException as e:
50+
print(f"Failed to fetch version from PyPI: {e}", file=sys.stderr)
51+
return None
52+
53+
def get_versioned_client_package(version):
54+
client_package = get_config("os2borgerpc_client_package")
55+
56+
if client_package.startswith("https://github.com/"):
57+
return f"git+{client_package}@{version}"
58+
59+
return f"{client_package}@{version}"
1860

19-
def update_client():
20-
"""Update the client via pip."""
61+
def update_client(version):
62+
"""Update the client via pip based on the latest version."""
2163
try:
22-
subprocess.check_call(
23-
[sys.executable, "-m", "pip", "install", "-U", "os2borgerpc-client"]
24-
)
25-
sys.exit(0)
64+
versioned_client_package = get_versioned_client_package(version)
65+
if version:
66+
subprocess.check_call(
67+
[sys.executable, "-m", "pip", "install", "-U", versioned_client_package]
68+
)
69+
sys.exit(0)
70+
else:
71+
print("Could not determine the latest version to update.", file=sys.stderr)
72+
sys.exit(1)
2673
except subprocess.CalledProcessError:
2774
print("update_client failed\n", file=sys.stderr)
2875
traceback.print_exc()

0 commit comments

Comments
 (0)