Keep It Simple Stupid. Started using the CherryPy server and made classes from the parts.

This commit is contained in:
kalzu rekku
2024-10-22 22:06:19 +03:00
parent 6db5290cca
commit e620c28648
12 changed files with 600 additions and 0 deletions

View File

@ -0,0 +1,29 @@
import tomllib
from pathlib import Path
import logging
def read_toml_file(file_path: str) -> dict:
try:
with open(file_path, "rb") as file:
return tomllib.load(file)
except FileNotFoundError:
logging.error(f"File {file_path} not found.")
return {}
except tomllib.TOMLDecodeError as e:
logging.error(f"Error parsing {file_path}: {e}")
return {}
def write_toml_file(file_path: str, data: dict) -> None:
with open(file_path, "w") as file:
toml.dump(data, file)
def create_backup(file_path: str) -> str:
config_path = Path(file_path)
backup_dir = config_path.parent / "backups"
backup_dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_file = backup_dir / f"{config_path.stem}_{timestamp}.toml"
shutil.copy2(file_path, backup_file)
logging.info(f"Backup created: {backup_file}")
return backup_file

View File

@ -0,0 +1,36 @@
from pathlib import Path
import shutil
import logging
import subprocess
from datetime import datetime
def read_config(config_file: str) -> str:
with open(config_file, 'r') as file:
return file.read()
def write_config(config_file: str, content: str) -> None:
with open(config_file, 'w') as file:
file.write(content)
def create_backup(config_file: str) -> str:
config_path = Path(config_file)
backup_dir = config_path.parent / "backups"
backup_dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_file = backup_dir / f"{config_path.stem}_{timestamp}.conf"
shutil.copy2(config_file, backup_file)
logging.info(f"Backup created: {backup_file}")
return backup_file
def reload_wireguard_service(config_file: str):
interface = Path(config_file).stem
try:
subprocess.run(["wg-quick", "down", interface], check=True)
subprocess.run(["wg-quick", "up", interface], check=True)
logging.info(f"WireGuard service for interface {interface} restarted successfully")
return True
except subprocess.CalledProcessError as e:
logging.error(f"Failed to reload WireGuard service: {str(e)}")
return False