feat(klipper): check for required user-groups

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2023-10-28 19:33:18 +02:00
parent ce0daa52ae
commit 2a100c2934
4 changed files with 59 additions and 4 deletions

View File

@@ -17,7 +17,7 @@ from pathlib import Path
from typing import List
from kiauh.instance_manager.base_instance import BaseInstance
from kiauh.utils.constants import SYSTEMD, KLIPPER_DIR, KLIPPER_ENV_DIR
from kiauh.utils.constants import CURRENT_USER, SYSTEMD, KLIPPER_DIR, KLIPPER_ENV_DIR
from kiauh.utils.logger import Logger
from kiauh.utils.system_utils import create_directory
@@ -31,7 +31,7 @@ class Klipper(BaseInstance):
def __init__(self, name: str):
super().__init__(name=name,
prefix="klipper",
user=pwd.getpwuid(os.getuid())[0],
user=CURRENT_USER,
data_dir_name=self._get_data_dir_from_name(name))
self.klipper_dir = KLIPPER_DIR
self.env_dir = KLIPPER_ENV_DIR

View File

@@ -11,14 +11,15 @@
import os
import re
import grp
import subprocess
from pathlib import Path
from typing import Optional, List, Union
from kiauh.instance_manager.instance_manager import InstanceManager
from kiauh.modules.klipper.klipper import Klipper
from kiauh.modules.klipper.klipper_utils import print_instance_overview
from kiauh.utils.constants import KLIPPER_DIR, KLIPPER_ENV_DIR
from kiauh.modules.klipper.klipper_utils import print_instance_overview, print_missing_usergroup_dialog
from kiauh.utils.constants import CURRENT_USER, KLIPPER_DIR, KLIPPER_ENV_DIR
from kiauh.utils.input_utils import get_user_confirm, get_user_number_input, \
get_user_string_input, get_user_selection_input
from kiauh.utils.logger import Logger
@@ -99,6 +100,7 @@ def install_klipper(instance_manager: InstanceManager) -> None:
# step 4: check/handle conflicting packages/services
# step 5: check for required group membership
check_user_groups()
def setup_klipper_prerequesites() -> None:
@@ -234,3 +236,32 @@ def remove_multi_instance(instance_manager: InstanceManager) -> None:
instance_manager.delete_instance(del_remnants=False)
instance_manager.reload_daemon()
def check_user_groups():
current_groups = [grp.getgrgid(gid).gr_name for gid in os.getgroups()]
missing_groups = []
if "tty" not in current_groups:
missing_groups.append("tty")
if "dialout" not in current_groups:
missing_groups.append("dialout")
if not missing_groups:
return
print_missing_usergroup_dialog(missing_groups)
if not get_user_confirm(f"Add user '{CURRENT_USER}' to group(s) now?"):
Logger.warn("Skipped adding user to required groups. You might encounter issues.")
return
try:
for group in missing_groups:
Logger.print_info(f"Adding user '{CURRENT_USER}' to group {group} ...")
command = ["sudo", "usermod", "-a", "-G", group, CURRENT_USER]
subprocess.run(command, check=True)
Logger.print_ok(f"Group {group} assigned to user '{CURRENT_USER}'.")
except subprocess.CalledProcessError as e:
Logger.print_error(f"Unable to add user to usergroups: {e}")
raise
Logger.print_warn("Remember to relog/restart this machine for the group(s) to be applied!")

View File

@@ -37,3 +37,21 @@ def print_instance_overview(instances: List[BaseInstance], show_index=False,
print(f"| {COLOR_CYAN}{line}{RESET_FORMAT}|")
print_back_footer()
def print_missing_usergroup_dialog(missing_groups) -> None:
print("/=======================================================\\")
print(f"| {COLOR_YELLOW}WARNING: Your current user is not in group:{RESET_FORMAT} |")
if "tty" in missing_groups:
print(f"| {COLOR_CYAN}● tty{RESET_FORMAT} |")
if "dialout" in missing_groups:
print(f"| {COLOR_CYAN}● dialout{RESET_FORMAT} |")
print("| |")
print("| It is possible that you won't be able to successfully |")
print("| connect and/or flash the controller board without |")
print("| your user being a member of that group. |")
print("| If you want to add the current user to the group(s) |")
print("| listed above, answer with 'Y'. Else skip with 'n'. |")
print("| |")
print(f"| {COLOR_YELLOW}INFO:{RESET_FORMAT} |")
print(f"| {COLOR_YELLOW}Relog required for group assignments to take effect!{RESET_FORMAT} |")
print("\\=======================================================/")

View File

@@ -9,6 +9,9 @@
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import os
import pwd
from pathlib import Path
# text colors and formats
@@ -19,6 +22,9 @@ COLOR_RED = "\033[91m" # bright red
COLOR_CYAN = "\033[96m" # bright cyan
RESET_FORMAT = "\033[0m" # reset format
# current user
CURRENT_USER = pwd.getpwuid(os.getuid())[0]
SYSTEMD = "/etc/systemd/system"
KLIPPER_DIR = f"{Path.home()}/klipper"