Refactor: Implement Hysteria server update in Python
This commit is contained in:
@ -18,7 +18,7 @@ class Command(Enum):
|
|||||||
'''Contains path to command's script'''
|
'''Contains path to command's script'''
|
||||||
INSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'install.sh')
|
INSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'install.sh')
|
||||||
UNINSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'uninstall.sh')
|
UNINSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'uninstall.sh')
|
||||||
UPDATE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'update.sh')
|
UPDATE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'update.py')
|
||||||
RESTART_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restart.py')
|
RESTART_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restart.py')
|
||||||
CHANGE_PORT_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_port.py')
|
CHANGE_PORT_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_port.py')
|
||||||
CHANGE_SNI_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_sni.sh')
|
CHANGE_SNI_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_sni.sh')
|
||||||
@ -137,7 +137,7 @@ def uninstall_hysteria2():
|
|||||||
|
|
||||||
def update_hysteria2():
|
def update_hysteria2():
|
||||||
'''Updates Hysteria2.'''
|
'''Updates Hysteria2.'''
|
||||||
run_cmd(['bash', Command.UPDATE_HYSTERIA2.value])
|
run_cmd(['python3', Command.UPDATE_HYSTERIA2.value])
|
||||||
|
|
||||||
|
|
||||||
def restart_hysteria2():
|
def restart_hysteria2():
|
||||||
|
|||||||
105
core/scripts/hysteria2/update.py
Normal file
105
core/scripts/hysteria2/update.py
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import shutil
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from init_paths import *
|
||||||
|
from paths import *
|
||||||
|
|
||||||
|
CONFIG_BACKUP = "/etc/hysteria/config_backup.json"
|
||||||
|
SERVICE_FILE = "/etc/systemd/system/hysteria-server.service"
|
||||||
|
OLD_CONFIG_PATH = "/etc/hysteria/config.yaml"
|
||||||
|
|
||||||
|
def backup_config():
|
||||||
|
print("📦 Backing up the current configuration...")
|
||||||
|
try:
|
||||||
|
shutil.copy(CONFIG_FILE, CONFIG_BACKUP)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error: Failed to back up configuration: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def restore_config():
|
||||||
|
print("♻️ Restoring configuration from backup...")
|
||||||
|
try:
|
||||||
|
shutil.move(CONFIG_BACKUP, CONFIG_FILE)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error: Failed to restore configuration: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def install_latest_hysteria():
|
||||||
|
print("⬇️ Downloading and installing the latest version of Hysteria2...")
|
||||||
|
try:
|
||||||
|
cmd = 'bash -c "$(curl -fsSL https://get.hy2.sh/)"'
|
||||||
|
result = subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
return result.returncode == 0
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error during installation: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def modify_systemd_service():
|
||||||
|
print("⚙️ Modifying systemd service to use config.json...")
|
||||||
|
try:
|
||||||
|
with open(SERVICE_FILE, 'r') as f:
|
||||||
|
service_data = f.read()
|
||||||
|
|
||||||
|
new_data = service_data.replace(
|
||||||
|
"Description=Hysteria Server Service (config.yaml)",
|
||||||
|
"Description=Hysteria Server Service (Blitz Panel)"
|
||||||
|
)
|
||||||
|
|
||||||
|
new_data = new_data.replace(str(OLD_CONFIG_PATH), str(CONFIG_FILE))
|
||||||
|
|
||||||
|
with open(SERVICE_FILE, 'w') as f:
|
||||||
|
f.write(new_data)
|
||||||
|
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error: Failed to modify systemd service: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def restart_hysteria():
|
||||||
|
print("🔄 Restarting Hysteria2 service...")
|
||||||
|
try:
|
||||||
|
subprocess.run(["systemctl", "daemon-reload"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
subprocess.run(["python3", CLI_PATH, "restart-hysteria2"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error: Failed to restart Hysteria2: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("🚀 Starting the update process for Hysteria2...")
|
||||||
|
|
||||||
|
if not backup_config():
|
||||||
|
print("❌ Aborting update due to failed backup.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not install_latest_hysteria():
|
||||||
|
print("❌ Installation failed. Restoring previous config...")
|
||||||
|
restore_config()
|
||||||
|
restart_hysteria()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not restore_config():
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not modify_systemd_service():
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if os.path.exists(OLD_CONFIG_PATH):
|
||||||
|
os.remove(OLD_CONFIG_PATH)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Failed to remove old YAML config: {e}")
|
||||||
|
|
||||||
|
if not restart_hysteria():
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print("\n✅ Hysteria2 has been successfully updated.")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@ -1,47 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Source the path.sh script to load the CONFIG_FILE variable
|
|
||||||
source /etc/hysteria/core/scripts/path.sh
|
|
||||||
|
|
||||||
echo "Starting the update process for Hysteria2..."
|
|
||||||
echo "Backing up the current configuration..."
|
|
||||||
cp "$CONFIG_FILE" /etc/hysteria/config_backup.json
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Failed to back up configuration. Aborting update."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Downloading and installing the latest version of Hysteria2..."
|
|
||||||
bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Failed to download or install the latest version. Restoring backup configuration."
|
|
||||||
mv /etc/hysteria/config_backup.json "$CONFIG_FILE"
|
|
||||||
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Restoring configuration from backup..."
|
|
||||||
mv /etc/hysteria/config_backup.json "$CONFIG_FILE"
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Failed to restore configuration from backup."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Modifying systemd service to use config.json..."
|
|
||||||
sed -i "s|/etc/hysteria/config.yaml|$CONFIG_FILE|" /etc/systemd/system/hysteria-server.service
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Failed to modify systemd service."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm /etc/hysteria/config.yaml
|
|
||||||
systemctl daemon-reload >/dev/null 2>&1
|
|
||||||
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Failed to restart Hysteria2 service."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Hysteria2 has been successfully updated."
|
|
||||||
echo ""
|
|
||||||
exit 0
|
|
||||||
Reference in New Issue
Block a user