mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-23 07:43:36 +05:00
Compare commits
5 Commits
4915896099
...
3d5e83d5ab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d5e83d5ab | ||
|
|
edd5f5c6fd | ||
|
|
8ff0b9d81d | ||
|
|
22e8e314db | ||
|
|
12bd8eb799 |
@@ -14,7 +14,7 @@ from pathlib import Path
|
||||
from typing import List, Union, Optional, Type, TypeVar
|
||||
|
||||
from kiauh.utils.constants import SYSTEMD, CURRENT_USER
|
||||
from kiauh.utils.system_utils import create_directory
|
||||
from kiauh.utils.filesystem_utils import create_directory
|
||||
|
||||
B = TypeVar(name="B", bound="BaseInstance", covariant=True)
|
||||
|
||||
|
||||
@@ -29,18 +29,16 @@ class RemoveMenu(BaseMenu):
|
||||
1: self.remove_klipper,
|
||||
2: self.remove_moonraker,
|
||||
3: MainsailRemoveMenu,
|
||||
4: self.remove_mainsail_config,
|
||||
5: self.remove_fluidd,
|
||||
6: self.remove_fluidd_config,
|
||||
7: self.remove_klipperscreen,
|
||||
8: self.remove_crowsnest,
|
||||
9: self.remove_mjpgstreamer,
|
||||
10: self.remove_pretty_gcode,
|
||||
11: self.remove_telegram_bot,
|
||||
12: self.remove_obico,
|
||||
13: self.remove_octoeverywhere,
|
||||
14: self.remove_mobileraker,
|
||||
15: self.remove_nginx,
|
||||
6: self.remove_klipperscreen,
|
||||
7: self.remove_crowsnest,
|
||||
8: self.remove_mjpgstreamer,
|
||||
9: self.remove_pretty_gcode,
|
||||
10: self.remove_telegram_bot,
|
||||
11: self.remove_obico,
|
||||
12: self.remove_octoeverywhere,
|
||||
13: self.remove_mobileraker,
|
||||
14: self.remove_nginx,
|
||||
},
|
||||
footer_type=BACK_FOOTER,
|
||||
)
|
||||
@@ -57,17 +55,17 @@ class RemoveMenu(BaseMenu):
|
||||
| INFO: Configurations and/or any backups will be kept! |
|
||||
|-------------------------------------------------------|
|
||||
| Firmware & API: | Webcam Streamer: |
|
||||
| 1) [Klipper] | 8) [Crowsnest] |
|
||||
| 2) [Moonraker] | 9) [MJPG-Streamer] |
|
||||
| 1) [Klipper] | 6) [Crowsnest] |
|
||||
| 2) [Moonraker] | 7) [MJPG-Streamer] |
|
||||
| | |
|
||||
| Klipper Webinterface: | Other: |
|
||||
| 3) [Mainsail] | 10) [PrettyGCode] |
|
||||
| 4) [Mainsail-Config] | 11) [Telegram Bot] |
|
||||
| 5) [Fluidd] | 12) [Obico for Klipper] |
|
||||
| 6) [Fluidd-Config] | 13) [OctoEverywhere] |
|
||||
| | 14) [Mobileraker] |
|
||||
| Touchscreen GUI: | 15) [NGINX] |
|
||||
| 7) [KlipperScreen] | |
|
||||
| 3) [Mainsail] | 8) [PrettyGCode] |
|
||||
| 4) [Fluidd] | 9) [Telegram Bot] |
|
||||
| | 10) [Obico for Klipper] |
|
||||
| Touchscreen GUI: | 11) [OctoEverywhere] |
|
||||
| 5) [KlipperScreen] | 12) [Mobileraker] |
|
||||
| | 13) [NGINX] |
|
||||
| | |
|
||||
"""
|
||||
)[1:]
|
||||
print(menu, end="")
|
||||
@@ -78,12 +76,6 @@ class RemoveMenu(BaseMenu):
|
||||
def remove_moonraker(self):
|
||||
moonraker_setup.run_moonraker_setup(install=False)
|
||||
|
||||
def remove_mainsail(self):
|
||||
mainsail_setup.run_mainsail_removal()
|
||||
|
||||
def remove_mainsail_config(self):
|
||||
print("remove_mainsail_config")
|
||||
|
||||
def remove_fluidd(self):
|
||||
print("remove_fluidd")
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ def run_mainsail_removal(
|
||||
if remove_mr_updater_section:
|
||||
remove_updater_section("update_manager mainsail")
|
||||
if remove_ms_config:
|
||||
remove_ms_config_dir()
|
||||
remove_mainsail_cfg_dir()
|
||||
remove_mainsail_cfg_symlink()
|
||||
if remove_mr_updater_section:
|
||||
remove_updater_section("update_manager mainsail-config")
|
||||
if remove_msc_printer_cfg_include:
|
||||
@@ -63,8 +64,8 @@ def remove_mainsail_dir() -> None:
|
||||
def remove_nginx_config() -> None:
|
||||
Logger.print_status("Removing Mainsails NGINX config ...")
|
||||
try:
|
||||
remove_file(Path(NGINX_SITES_AVAILABLE).joinpath("mainsail"), True)
|
||||
remove_file(Path(NGINX_SITES_ENABLED).joinpath("mainsail"), True)
|
||||
remove_file(Path(NGINX_SITES_AVAILABLE, "mainsail"), True)
|
||||
remove_file(Path(NGINX_SITES_ENABLED, "mainsail"), True)
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to remove Mainsail NGINX config:\n{e.stderr.decode()}"
|
||||
@@ -82,8 +83,8 @@ def remove_nginx_logs() -> None:
|
||||
return
|
||||
|
||||
for instance in im.instances:
|
||||
remove_file(Path(instance.log_dir).joinpath("mainsail-access.log"))
|
||||
remove_file(Path(instance.log_dir).joinpath("mainsail-error.log"))
|
||||
remove_file(Path(instance.log_dir, "mainsail-access.log"))
|
||||
remove_file(Path(instance.log_dir, "mainsail-error.log"))
|
||||
|
||||
except (OSError, subprocess.CalledProcessError) as e:
|
||||
Logger.print_error(f"Unable to NGINX logs:\n{e}")
|
||||
@@ -114,7 +115,7 @@ def remove_updater_section(name: str) -> None:
|
||||
cm.write_config()
|
||||
|
||||
|
||||
def remove_ms_config_dir() -> None:
|
||||
def remove_mainsail_cfg_dir() -> None:
|
||||
Logger.print_status("Removing mainsail-config ...")
|
||||
if not Path(MAINSAIL_CONFIG_DIR).exists():
|
||||
Logger.print_info(f"'{MAINSAIL_CONFIG_DIR}' does not exist. Skipping ...")
|
||||
@@ -126,6 +127,17 @@ def remove_ms_config_dir() -> None:
|
||||
Logger.print_error(f"Unable to delete '{MAINSAIL_CONFIG_DIR}':\n{e}")
|
||||
|
||||
|
||||
def remove_mainsail_cfg_symlink() -> None:
|
||||
Logger.print_status("Removing mainsail.cfg symlinks ...")
|
||||
im = InstanceManager(Moonraker)
|
||||
for instance in im.instances:
|
||||
Logger.print_status(f"Removing symlink from '{instance.cfg_dir}' ...")
|
||||
try:
|
||||
remove_file(Path(instance.cfg_dir, "mainsail.cfg"))
|
||||
except subprocess.CalledProcessError:
|
||||
Logger.print_error("Failed to remove symlink!")
|
||||
|
||||
|
||||
def remove_printer_cfg_include() -> None:
|
||||
Logger.print_status("Remove mainsail-config include from printer.cfg ...")
|
||||
im = InstanceManager(Klipper)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
# ======================================================================= #
|
||||
|
||||
import os.path
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
@@ -38,20 +39,23 @@ from kiauh.modules.mainsail.mainsail_utils import (
|
||||
symlink_webui_nginx_log,
|
||||
)
|
||||
from kiauh.modules.moonraker.moonraker import Moonraker
|
||||
from kiauh.utils import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED
|
||||
from kiauh.utils.common import check_install_dependencies
|
||||
from kiauh.utils.filesystem_utils import (
|
||||
unzip,
|
||||
copy_upstream_nginx_cfg,
|
||||
copy_common_vars_nginx_cfg,
|
||||
create_nginx_cfg,
|
||||
create_symlink,
|
||||
remove_file,
|
||||
)
|
||||
from kiauh.utils.input_utils import get_confirm, get_number_input
|
||||
from kiauh.utils.logger import Logger
|
||||
from kiauh.utils.system_utils import (
|
||||
download_file,
|
||||
unzip,
|
||||
create_upstream_nginx_cfg,
|
||||
create_nginx_cfg,
|
||||
delete_default_nginx_cfg,
|
||||
enable_nginx_cfg,
|
||||
set_nginx_permissions,
|
||||
get_ipv4_addr,
|
||||
control_systemd_service,
|
||||
create_common_vars_nginx_cfg,
|
||||
)
|
||||
|
||||
|
||||
@@ -114,8 +118,9 @@ def run_mainsail_installation() -> None:
|
||||
"mainsail-updater.conf",
|
||||
)
|
||||
im_mr.restart_all_instance()
|
||||
if is_klipper_installed and install_ms_config:
|
||||
download_mainsail_config()
|
||||
if install_ms_config and is_klipper_installed:
|
||||
download_mainsail_cfg()
|
||||
create_mainsail_cfg_symlink(im_kl.instances)
|
||||
patch_moonraker_conf(
|
||||
im_mr.instances,
|
||||
"mainsail-config",
|
||||
@@ -125,8 +130,8 @@ def run_mainsail_installation() -> None:
|
||||
patch_printer_config(im_kl.instances)
|
||||
im_kl.restart_all_instance()
|
||||
|
||||
create_upstream_nginx_cfg()
|
||||
create_common_vars_nginx_cfg()
|
||||
copy_upstream_nginx_cfg()
|
||||
copy_common_vars_nginx_cfg()
|
||||
create_mainsail_nginx_cfg(mainsail_port)
|
||||
if is_klipper_installed:
|
||||
symlink_webui_nginx_log(im_kl.instances)
|
||||
@@ -156,7 +161,7 @@ def download_mainsail() -> None:
|
||||
raise
|
||||
|
||||
|
||||
def download_mainsail_config() -> None:
|
||||
def download_mainsail_cfg() -> None:
|
||||
try:
|
||||
Logger.print_status("Downloading mainsail-config ...")
|
||||
rm = RepoManager(MAINSAIL_CONFIG_REPO_URL, target_dir=MAINSAIL_CONFIG_DIR)
|
||||
@@ -166,13 +171,27 @@ def download_mainsail_config() -> None:
|
||||
raise
|
||||
|
||||
|
||||
def create_mainsail_cfg_symlink(klipper_instances: List[Klipper]) -> None:
|
||||
Logger.print_status("Create symlink of mainsail.cfg ...")
|
||||
source = Path(MAINSAIL_CONFIG_DIR, "mainsail.cfg")
|
||||
for instance in klipper_instances:
|
||||
target = instance.cfg_dir
|
||||
Logger.print_status(f"Linking {source} to {target}")
|
||||
try:
|
||||
create_symlink(source, target)
|
||||
except subprocess.CalledProcessError:
|
||||
Logger.print_error("Creating symlink failed!")
|
||||
|
||||
|
||||
def create_mainsail_nginx_cfg(port: int) -> None:
|
||||
root_dir = MAINSAIL_DIR
|
||||
source = Path(NGINX_SITES_AVAILABLE, "mainsail")
|
||||
target = Path(NGINX_SITES_ENABLED, "mainsail")
|
||||
try:
|
||||
Logger.print_status("Creating NGINX config for Mainsail ...")
|
||||
root_dir = MAINSAIL_DIR
|
||||
delete_default_nginx_cfg()
|
||||
remove_file(Path("/etc/nginx/sites-enabled/default"), True)
|
||||
create_nginx_cfg("mainsail", port, root_dir)
|
||||
enable_nginx_cfg("mainsail")
|
||||
create_symlink(source, target, True)
|
||||
set_nginx_permissions()
|
||||
Logger.print_ok("NGINX config for Mainsail successfully created.")
|
||||
except Exception:
|
||||
|
||||
@@ -38,6 +38,7 @@ from kiauh.modules.moonraker import (
|
||||
from kiauh.modules.moonraker.moonraker import Moonraker
|
||||
from kiauh.modules.moonraker.moonraker_dialogs import print_moonraker_overview
|
||||
from kiauh.modules.moonraker.moonraker_utils import create_example_moonraker_conf
|
||||
from kiauh.utils.filesystem_utils import check_file_exist
|
||||
from kiauh.utils.input_utils import (
|
||||
get_confirm,
|
||||
get_selection_input,
|
||||
@@ -49,7 +50,6 @@ from kiauh.utils.system_utils import (
|
||||
install_python_requirements,
|
||||
update_system_package_lists,
|
||||
install_system_packages,
|
||||
check_file_exists,
|
||||
)
|
||||
|
||||
|
||||
@@ -181,9 +181,9 @@ def install_moonraker_packages(moonraker_dir: Path) -> None:
|
||||
def install_moonraker_polkit() -> None:
|
||||
Logger.print_status("Installing Moonraker policykit rules ...")
|
||||
|
||||
legacy_file_exists = check_file_exists(Path(POLKIT_LEGACY_FILE))
|
||||
polkit_file_exists = check_file_exists(Path(POLKIT_FILE))
|
||||
usr_file_exists = check_file_exists(Path(POLKIT_USR_FILE))
|
||||
legacy_file_exists = check_file_exist(Path(POLKIT_LEGACY_FILE), True)
|
||||
polkit_file_exists = check_file_exist(Path(POLKIT_FILE), True)
|
||||
usr_file_exists = check_file_exist(Path(POLKIT_USR_FILE), True)
|
||||
|
||||
if legacy_file_exists or (polkit_file_exists and usr_file_exists):
|
||||
Logger.print_info("Moonraker policykit rules are already installed.")
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# ======================================================================= #
|
||||
# Copyright (C) 2020 - 2023 Dominik Willner <th33xitus@gmail.com> #
|
||||
# #
|
||||
@@ -9,18 +8,144 @@
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license #
|
||||
# ======================================================================= #
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from zipfile import ZipFile
|
||||
|
||||
|
||||
from kiauh.utils import (
|
||||
NGINX_SITES_AVAILABLE,
|
||||
MODULE_PATH,
|
||||
NGINX_CONFD,
|
||||
)
|
||||
from kiauh.utils.logger import Logger
|
||||
|
||||
|
||||
def check_file_exist(file_path: Path, sudo=False) -> bool:
|
||||
"""
|
||||
Helper function for checking the existence of a file |
|
||||
:param file_path: the absolute path of the file to check
|
||||
:param sudo: use sudo if required
|
||||
:return: True, if file exists, otherwise False
|
||||
"""
|
||||
if sudo:
|
||||
try:
|
||||
command = ["sudo", "find", file_path]
|
||||
subprocess.check_output(command, stderr=subprocess.DEVNULL)
|
||||
return True
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
else:
|
||||
if Path(file_path).exists():
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def create_directory(_dir: Path) -> None:
|
||||
"""
|
||||
Helper function for creating a directory or skipping if it already exists |
|
||||
:param _dir: the directory to create
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
if not os.path.isdir(_dir):
|
||||
os.makedirs(_dir, exist_ok=True)
|
||||
Logger.print_ok(f"Created directory: {_dir}")
|
||||
except OSError as e:
|
||||
Logger.print_error(f"Error creating folder: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def create_symlink(source: Path, target: Path, sudo=False) -> None:
|
||||
try:
|
||||
cmd = ["ln", "-sf", source, target]
|
||||
if sudo:
|
||||
cmd.insert(0, "sudo")
|
||||
subprocess.run(cmd, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
Logger.print_error(f"Failed to create symlink: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def remove_file(file_path: Path, sudo=False) -> None:
|
||||
try:
|
||||
command = f"{'sudo ' if sudo else ''}rm -f {file_path}"
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True, shell=True)
|
||||
cmd = f"{'sudo ' if sudo else ''}rm -f {file_path}"
|
||||
subprocess.run(cmd, stderr=subprocess.PIPE, check=True, shell=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Cannot remove file {file_path}: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def unzip(file: str, target_dir: str) -> None:
|
||||
"""
|
||||
Helper function to unzip a zip-archive into a target directory |
|
||||
:param file: the zip-file to unzip
|
||||
:param target_dir: the target directory to extract the files into
|
||||
:return: None
|
||||
"""
|
||||
with ZipFile(file, "r") as _zip:
|
||||
_zip.extractall(target_dir)
|
||||
|
||||
|
||||
def copy_upstream_nginx_cfg() -> None:
|
||||
"""
|
||||
Creates an upstream.conf in /etc/nginx/conf.d
|
||||
:return: None
|
||||
"""
|
||||
source = os.path.join(MODULE_PATH, "res", "upstreams.conf")
|
||||
target = os.path.join(NGINX_CONFD, "upstreams.conf")
|
||||
try:
|
||||
command = ["sudo", "cp", source, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create upstreams.conf: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def copy_common_vars_nginx_cfg() -> None:
|
||||
"""
|
||||
Creates a common_vars.conf in /etc/nginx/conf.d
|
||||
:return: None
|
||||
"""
|
||||
source = os.path.join(MODULE_PATH, "res", "common_vars.conf")
|
||||
target = os.path.join(NGINX_CONFD, "common_vars.conf")
|
||||
try:
|
||||
command = ["sudo", "cp", source, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create upstreams.conf: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def create_nginx_cfg(name: str, port: int, root_dir: str) -> None:
|
||||
"""
|
||||
Creates an NGINX config from a template file and replaces all placeholders
|
||||
:param name: name of the config to create
|
||||
:param port: listen port
|
||||
:param root_dir: directory of the static files
|
||||
:return: None
|
||||
"""
|
||||
tmp = f"{Path.home()}/{name}.tmp"
|
||||
shutil.copy(os.path.join(MODULE_PATH, "res", "nginx_cfg"), tmp)
|
||||
with open(tmp, "r+") as f:
|
||||
content = f.read()
|
||||
content = content.replace("%NAME%", name)
|
||||
content = content.replace("%PORT%", str(port))
|
||||
content = content.replace("%ROOT_DIR%", root_dir)
|
||||
f.seek(0)
|
||||
f.write(content)
|
||||
f.truncate()
|
||||
|
||||
target = os.path.join(NGINX_SITES_AVAILABLE, name)
|
||||
try:
|
||||
command = ["sudo", "mv", tmp, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create '{target}': {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
@@ -19,14 +19,7 @@ import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from typing import List, Literal
|
||||
from zipfile import ZipFile
|
||||
|
||||
from kiauh.utils import (
|
||||
NGINX_CONFD,
|
||||
MODULE_PATH,
|
||||
NGINX_SITES_AVAILABLE,
|
||||
NGINX_SITES_ENABLED,
|
||||
)
|
||||
from kiauh.utils.input_utils import get_confirm
|
||||
from kiauh.utils.logger import Logger
|
||||
|
||||
@@ -215,21 +208,6 @@ def install_system_packages(packages: List[str]) -> None:
|
||||
kill(f"Error installing packages:\n{e.stderr.decode()}")
|
||||
|
||||
|
||||
def create_directory(_dir: Path) -> None:
|
||||
"""
|
||||
Helper function for creating a directory or skipping if it already exists |
|
||||
:param _dir: the directory to create
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
if not os.path.isdir(_dir):
|
||||
os.makedirs(_dir, exist_ok=True)
|
||||
Logger.print_ok(f"Created directory: {_dir}")
|
||||
except OSError as e:
|
||||
Logger.print_error(f"Error creating folder: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def mask_system_service(service_name: str) -> None:
|
||||
"""
|
||||
Mask a system service to prevent it from starting |
|
||||
@@ -245,21 +223,6 @@ def mask_system_service(service_name: str) -> None:
|
||||
raise
|
||||
|
||||
|
||||
def check_file_exists(file_path: Path) -> bool:
|
||||
"""
|
||||
Helper function for checking the existence of a file where
|
||||
elevated permissions are required |
|
||||
:param file_path: the absolute path of the file to check
|
||||
:return: True if file exists, otherwise False
|
||||
"""
|
||||
try:
|
||||
command = ["sudo", "find", file_path]
|
||||
subprocess.check_output(command, stderr=subprocess.DEVNULL)
|
||||
return True
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
|
||||
# this feels hacky and not quite right, but for now it works
|
||||
# see: https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
|
||||
def get_ipv4_addr() -> str:
|
||||
@@ -327,116 +290,6 @@ def download_progress(block_num, block_size, total_size) -> None:
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def unzip(file: str, target_dir: str) -> None:
|
||||
"""
|
||||
Helper function to unzip a zip-archive into a target directory |
|
||||
:param file: the zip-file to unzip
|
||||
:param target_dir: the target directory to extract the files into
|
||||
:return: None
|
||||
"""
|
||||
with ZipFile(file, "r") as _zip:
|
||||
_zip.extractall(target_dir)
|
||||
|
||||
|
||||
def create_upstream_nginx_cfg() -> None:
|
||||
"""
|
||||
Creates an upstream.conf in /etc/nginx/conf.d
|
||||
:return: None
|
||||
"""
|
||||
source = os.path.join(MODULE_PATH, "res", "upstreams.conf")
|
||||
target = os.path.join(NGINX_CONFD, "upstreams.conf")
|
||||
try:
|
||||
command = ["sudo", "cp", source, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create upstreams.conf: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def create_common_vars_nginx_cfg() -> None:
|
||||
"""
|
||||
Creates a common_vars.conf in /etc/nginx/conf.d
|
||||
:return: None
|
||||
"""
|
||||
source = os.path.join(MODULE_PATH, "res", "common_vars.conf")
|
||||
target = os.path.join(NGINX_CONFD, "common_vars.conf")
|
||||
try:
|
||||
command = ["sudo", "cp", source, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create upstreams.conf: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def create_nginx_cfg(name: str, port: int, root_dir: str) -> None:
|
||||
"""
|
||||
Creates an NGINX config from a template file and replaces all placeholders
|
||||
:param name: name of the config to create
|
||||
:param port: listen port
|
||||
:param root_dir: directory of the static files
|
||||
:return: None
|
||||
"""
|
||||
tmp = f"{Path.home()}/{name}.tmp"
|
||||
shutil.copy(os.path.join(MODULE_PATH, "res", "nginx_cfg"), tmp)
|
||||
with open(tmp, "r+") as f:
|
||||
content = f.read()
|
||||
content = content.replace("%NAME%", name)
|
||||
content = content.replace("%PORT%", str(port))
|
||||
content = content.replace("%ROOT_DIR%", root_dir)
|
||||
f.seek(0)
|
||||
f.write(content)
|
||||
f.truncate()
|
||||
|
||||
target = os.path.join(NGINX_SITES_AVAILABLE, name)
|
||||
try:
|
||||
command = ["sudo", "mv", tmp, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create '{target}': {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def delete_default_nginx_cfg() -> None:
|
||||
"""
|
||||
Deletes a default NGINX config
|
||||
:return: None
|
||||
"""
|
||||
default_cfg = Path("/etc/nginx/sites-enabled/default")
|
||||
if not check_file_exists(default_cfg):
|
||||
return
|
||||
|
||||
try:
|
||||
command = ["sudo", "rm", default_cfg]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to delete '{default_cfg}': {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def enable_nginx_cfg(name: str) -> None:
|
||||
"""
|
||||
Helper method to enable an NGINX config |
|
||||
:param name: name of the config to enable
|
||||
:return: None
|
||||
"""
|
||||
source = os.path.join(NGINX_SITES_AVAILABLE, name)
|
||||
target = os.path.join(NGINX_SITES_ENABLED, name)
|
||||
if check_file_exists(Path(target)):
|
||||
return
|
||||
|
||||
try:
|
||||
command = ["sudo", "ln", "-s", source, target]
|
||||
subprocess.run(command, stderr=subprocess.PIPE, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Unable to create symlink: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
|
||||
def set_nginx_permissions() -> None:
|
||||
"""
|
||||
Check if permissions of the users home directory
|
||||
|
||||
Reference in New Issue
Block a user