Commit 820a89a6 authored by Muhammad Umair Khan's avatar Muhammad Umair Khan
Browse files

Update root user constraint handling

- Abort deployment with clear error if attempted directly as root
- Correct instruction in error message to advise running pyinfra without sudo
parent 2dba707a
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -163,13 +163,23 @@ def get_target_user_and_home():
    Determines the target SSH/deployment user and their home directory (~ folder).
    - For localhost (@local), uses the currently logged-in user (SUDO_USER/USER).
    - For remote targets (<user>@<host>), uses the username specified in K8S_MASTERS.
    - Never uses /root, as etsi-mec-sandbox and its nested etsi-mec-sandbox-frontend submodule always reside in /home/<user>.
    - Enforces that the deployment must be under a normal user's home directory.
    - Raises an error if executed directly as the root user.
    """
    masters = get_k8s_masters()
    if masters and masters[0][0] != "@local":
        target_user = masters[0][1]
    else:
        target_user = os.environ.get("SUDO_USER") or os.environ.get("USER", getpass.getuser())
        
    if target_user == "root":
        raise ValueError(
            "Deployment as the 'root' user is not supported. "
            "Please run the script as a normal user without sudo (e.g., 'pyinfra inventory.py deploy.py -y'). "
            "Sudo passwords will be prompted during execution. "
            "This ensures the sandbox is properly installed in a /home/<user> directory."
        )
        
    target_home = f"/home/{target_user}"
    return target_user, target_home