AppConfig and common_utils further developed for Zenrale configuration
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
import gettext
|
||||
import locale
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from typing import Dict, Any
|
||||
|
||||
|
||||
@ -24,7 +25,14 @@ class AppConfig:
|
||||
# Configuration files
|
||||
SETTINGS_FILE: Path = CONFIG_DIR / "settings"
|
||||
KEYS_FILE: Path = CONFIG_DIR / "keys"
|
||||
SYSTEMD_USER_FOLDER: Path = Path.home() / ".config/systemd/user"
|
||||
AUTOSTART_SERVICE: Path = Path.home() / ".config/systemd/user/wg_start.service"
|
||||
DEFAULT_SETTINGS: Dict[str, str] = {
|
||||
"# Configuration": "on",
|
||||
"# Theme": "dark",
|
||||
"# Tooltips": True,
|
||||
"# Autostart": "off",
|
||||
}
|
||||
|
||||
# Updates
|
||||
# 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year
|
||||
@ -79,7 +87,8 @@ class AppConfig:
|
||||
@classmethod
|
||||
def ensure_directories(cls) -> None:
|
||||
"""Ensures that all required directories exist"""
|
||||
cls.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
if not cls.CONFIG_DIR.exists():
|
||||
cls.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
cls.TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@classmethod
|
||||
@ -91,29 +100,40 @@ class AppConfig:
|
||||
)
|
||||
cls.SETTINGS_FILE.write_text(content)
|
||||
|
||||
@classmethod
|
||||
def get_image_paths(cls) -> Dict[str, Path]:
|
||||
"""Returns paths to UI images"""
|
||||
return {
|
||||
"main_icon": cls.SYSTEM_PATHS["image_path"] / "48/wg_vpn.png",
|
||||
"warning": cls.CONFIG_DIR / "images/warning.png",
|
||||
"success": cls.CONFIG_DIR / "images/success.png",
|
||||
"error": cls.CONFIG_DIR / "images/error.png",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_autostart_content(cls) -> str:
|
||||
"""Returns the content for the autostart service file"""
|
||||
SYSTEMD_FILE: list[str] = [
|
||||
"[Unit]",
|
||||
"Description=Automatic Tunnel Start",
|
||||
"After=network-online.target",
|
||||
"",
|
||||
"[Service]",
|
||||
"Type=oneshot",
|
||||
"ExecStartPre=/bin/sleep 5",
|
||||
"ExecStart=/usr/local/bin/start_wg.py",
|
||||
"",
|
||||
"[Install]",
|
||||
"WantedBy=default.target",
|
||||
]
|
||||
if not cls.SYSTEMD_USER_FOLDER.exists():
|
||||
cls.SYSTEMD_USER_FOLDER.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
return """[Unit]Description=Automatic Tunnel Start
|
||||
After=network-online.target
|
||||
for line in SYSTEMD_FILE:
|
||||
cls.AUTOSTART_SERVICE.write_text(line)
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStartPre=/bin/sleep 5
|
||||
ExecStart=/usr/local/bin/start_wg.py
|
||||
[Install]
|
||||
WantedBy=default.target"""
|
||||
process = subprocess.run(
|
||||
["systemctl", "--user", "enable", "wg_start.service"],
|
||||
stdout=subprocess.PIPE,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
print(process.stdout)
|
||||
if process.returncode == 0:
|
||||
print("File for autostart created successfully")
|
||||
print(process.stdout)
|
||||
else:
|
||||
print(f"Error with the following code... {process.returncode}")
|
||||
|
||||
|
||||
# here is inizialize the class for translate strrings
|
||||
|
Reference in New Issue
Block a user