From 7937b0f0ae7492cc579a5344b87aad1b7490c0c9 Mon Sep 17 00:00:00 2001 From: MRColor Date: Tue, 29 Oct 2024 10:21:51 +0100 Subject: [PATCH] fix(helper.py): using getpass.getuser() instead of os.getlogin() as it is more robust and does not cause the "No such device or address" issue --- utils/helper.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/utils/helper.py b/utils/helper.py index 6df19ff..407b94f 100644 --- a/utils/helper.py +++ b/utils/helper.py @@ -1,9 +1,11 @@ import os +import getpass import platform import subprocess import logging from colorama import Fore, Style + def is_user_root(): """ Check if the current user is the root user on Linux. @@ -11,6 +13,7 @@ def is_user_root(): """ return os.geteuid() == 0 if platform.system().lower() == 'linux' else False + def is_user_in_docker_group(): """ Check if the current user is in the Docker group on Linux. @@ -18,11 +21,14 @@ def is_user_in_docker_group(): """ if platform.system().lower() != 'linux': return True - - user = os.getlogin() + # use getpass.getuser() instead of os.getlogin() as it is more robust + user = getpass.getuser() + logging.info(f"Detected user: {user}") + logging.info(f"Checking if user '{user}' is in the Docker group...") groups = subprocess.run(["groups", user], capture_output=True, text=True) return "docker" in groups.stdout + def create_docker_group_if_needed(): """ Create the Docker group if it doesn't exist and add the current user to it on Linux. @@ -37,7 +43,8 @@ def create_docker_group_if_needed(): subprocess.run(["sudo", "groupadd", "docker"], check=True) logging.info(f"{Fore.GREEN}Docker group created successfully.{Style.RESET_ALL}") - user = os.getlogin() + # use getpass.getuser() instead of os.getlogin() as it is more robust + user = getpass.getuser() logging.info(f"Adding user '{user}' to Docker group...") subprocess.run(["sudo", "usermod", "-aG", "docker", user], check=True) logging.info(f"{Fore.GREEN}User '{user}' added to Docker group. Please log out and log back in.{Style.RESET_ALL}") @@ -45,6 +52,7 @@ def create_docker_group_if_needed(): logging.error(f"{Fore.RED}Failed to add user to Docker group: {e}{Style.RESET_ALL}") raise RuntimeError("Failed to add user to Docker group.") from e + def run_docker_command(command, use_sudo=False): """ Run a Docker command, optionally using sudo. @@ -60,6 +68,7 @@ def run_docker_command(command, use_sudo=False): command.insert(0, "sudo") return subprocess.run(command, check=True, capture_output=True, text=True) + def setup_service(service_name="docker.binfmt", service_file_path='./.resources/.files/docker.binfmt.service'): """ Set up a service on Linux systems, defaulting to setting up the Docker binfmt service.