-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplex_updater.py
122 lines (98 loc) · 3.32 KB
/
plex_updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""
Plex Media Server Upgrade Script
Developed for Ubuntu Server (tested on 24.04.1)
Copyright (c) 2024 Brian Samson
License: GNU General Public License v3.0 (GPL-3.0)
See LICENSE for details.
Requires sudo privileges
"""
import os
import sys
import json
import requests
import subprocess
from datetime import datetime
import shutil
# Variables
PLEX_URL = "https://plex.tv/api/downloads/5.json"
TEMP_DIR = "/tmp/plex_upgrade"
PLEX_DEB = os.path.join(TEMP_DIR, "plexmediaserver.deb")
LOG_FILE = "/var/log/plex_upgrade.log"
# Logging function with timestamps
def log(message):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_message = f"{timestamp} - {message}"
print(log_message)
with open(LOG_FILE, "a") as log_file:
log_file.write(log_message + "\n")
# Ensure the script is run as root
def check_root():
if os.geteuid() != 0:
log("Run this script as root (use sudo).")
sys.exit(1)
# Create temporary directory
def create_temp_dir():
os.makedirs(TEMP_DIR, exist_ok=True)
log(f"Created temporary directory: {TEMP_DIR}")
# Fetch the latest Plex version URL
def get_latest_plex_url():
try:
log("Checking for the latest Plex Media Server version...")
response = requests.get(PLEX_URL)
response.raise_for_status()
data = response.json()
releases = data['computer']['Linux']['releases']
latest_url = next(
r['url'] for r in releases if r['build'] == 'linux-x86_64' and r['distro'] == 'debian'
)
log(f"Latest version URL: {latest_url}")
return latest_url
except Exception as e:
log(f"Failed to fetch Plex version info. Error: {e}")
sys.exit(1)
# Download the Plex update
def download_plex(url):
try:
log("Downloading the latest Plex Media Server version...")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(PLEX_DEB, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
log("Download completed.")
except Exception as e:
log(f"Download failed. Error: {e}")
sys.exit(1)
# Install the Plex update
def install_plex():
try:
log("Installing Plex Media Server...")
subprocess.run(["dpkg", "-i", PLEX_DEB], check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
except subprocess.CalledProcessError:
log("Install failed—attempting dependency fix.")
subprocess.run(["apt-get", "-f", "install", "-y"], check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
# Cleanup temporary files
def cleanup():
log("Removing temporary files...")
shutil.rmtree(TEMP_DIR, ignore_errors=True)
# Restart Plex Media Server
def restart_plex():
try:
log("Restarting Plex Media Server...")
subprocess.run(["systemctl", "restart", "plexmediaserver"], check=True)
log("Plex Media Server upgrade completed successfully.")
except subprocess.CalledProcessError as e:
log(f"Failed to restart Plex Media Server. Error: {e}")
sys.exit(1)
# Main function
def main():
check_root()
create_temp_dir()
latest_url = get_latest_plex_url()
download_plex(latest_url)
install_plex()
cleanup()
restart_plex()
if __name__ == "__main__":
main()