pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

fix(connectors.local): fragile sudo handling resulting in faillocks and premature errors.

Open Dexmachi opened this issue 7 months ago • 1 comments

Closes #1043 Closes #1688

How:

Added a {0}.{PPID}.called marker + env LC_ALL=C to sudo calls, allowing Pyinfra to keep track of sudo call attempts and to have a standardized retry catch.

the marker gets removed alongside the sudo password file, in the same call.

Why:

@local sudo handling was fragile, requiring a specific locale and not dealing with auto sudo retries, resulting in faillocks and premature errors.

Test Changes:

Had to change test_ssh and test_util to assert "env LC_ALL=C " at the start of sudo and su calls.

Before/After:

Before(normal usage):

/opt/***-app/venv/bin/pyinfra @local playbooks/install1pkg/deploy.py
    Attempting to work in a virtualenv.
    If you encounter problems, please install pyinfra inside the virtualenv.


--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
    [@local] Connected

--> Preparing operation files...
    Loading: playbooks/install1pkg/deploy.py
    [@local] Ready: playbooks/install1pkg/deploy.py

--> Detected changes:
    Operation                             Change       Conditional Change   
    server.shell (commands=['echo Hy'])   1 (@local)   -                    

    Detected changes may not include every change pyinfra will execute.
    Hidden side effects of operations may alter behaviour of future operations,
    this will be shown in the results. The remote state will always be updated
    to reflect the state defined by the input operations.

    Detected changes displayed above, skip this step with -y
                             
--> Beginning operation run...
--> Starting operation: server.shell (commands=['echo Hy'])
Sinto muito, tente novamente.
Sinto muito, tente novamente.
    [@local] sudo: 3 tentativas de senha incorreta
    [@local] Error: executed 0 commands

After(normal usage):

pyinfra @local playbooks/install1pkg/deploy.py -y
--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
    [@local] Connected

--> Preparing operation files...
    Loading: playbooks/install1pkg/deploy.py
    [@local] Ready: playbooks/install1pkg/deploy.py

--> Skipping change detection
--> Beginning operation run...
--> Starting operation: server.shell (commands=['echo Hy'])
Sorry, try again.
    [@local] sudo: 1 incorrect password attempt
    [@local] sudo: no password was provided
    [@local] sudo: 1 incorrect password attempt
    [@local] Error: executed 0 commands

--> Disconnecting from hosts...
--> pyinfra error: No hosts remaining!

sudo -l
[sudo] senha para dexmachina: 
Entradas de Defaults correspondentes a dexmachina em Dionysus:

Before(ad-hoc):

/opt/***-app/venv/bin/pyinfra @local server.shell "echo Hi" -y --sudo
    Attempting to work in a virtualenv.
    If you encounter problems, please install pyinfra inside the virtualenv.


--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
    [@local] Connected

--> Preparing operation func...
    [@local] Ready: shell

--> Skipping change detection
--> Beginning operation run...
--> Starting operation: server.shell (echo Hi)
    [@local] sudo: uma senha é necessária
    [@local] Error: executed 0 commands

--> Disconnecting from hosts...
--> pyinfra error: No hosts remaining!
/opt/***-app/venv/bin/pyinfra @local server.shell "echo Hi" -y --sudo "a"
    Attempting to work in a virtualenv.
    If you encounter problems, please install pyinfra inside the virtualenv.


--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
    [@local] Connected

--> Preparing operation func...
--> Disconnecting from hosts...
--> An internal exception occurred:

  File "/usr/lib/python3.14/inspect.py", line 3155, in _bind
    raise TypeError('too many positional arguments') from None
TypeError: too many positional arguments

--> The full traceback has been written to pyinfra-debug.log
--> If this is unexpected please consider submitting a bug report on GitHub, for more information run `pyinfra --support`.

After(ad-hoc):

pyinfra @local server.shell "echo Hi" -y --sudo
--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
    [@local] Connected

--> Preparing operation func...
    [@local] Ready: shell

--> Skipping change detection
--> Beginning operation run...
--> Starting operation: server.shell (echo Hi)
[@local] sudo password: 
    [@local] sudo: a password is required
    [@local] Error: executed 0 commands

--> Disconnecting from hosts...
--> pyinfra error: No hosts remaining!
pyinfra @local server.shell "echo Hi" -y --sudo
--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
    [@local] Connected

--> Preparing operation func...
    [@local] Ready: shell

--> Skipping change detection
--> Beginning operation run...
--> Starting operation: server.shell (echo Hi)
[@local] sudo password: 
Sorry, try again.
    [@local] sudo: 1 incorrect password attempt
    [@local] sudo: no password was provided
    [@local] sudo: 1 incorrect password attempt
    [@local] Error: executed 0 commands

--> Disconnecting from hosts...
--> pyinfra error: No hosts remaining!
sudo -l
[sudo] senha para dexmachina: 
Entradas de Defaults correspondentes a dexmachina em Dionysus:
  • [x] Pull request is based on the default branch (3.x at this time)
  • [N/A] Pull request includes tests for any new/updated operations/facts
  • [x] Pull request includes documentation for any new/updated operations/facts
  • [x] Tests pass (see scripts/dev-test.sh)
  • [x] Type checking & code style passes (see scripts/dev-lint.sh)

Dexmachi avatar Apr 21 '26 18:04 Dexmachi

I'm currently fixing the merge-based errorrs, 1 sec

Dexmachi avatar May 07 '26 12:05 Dexmachi

@Fizzadar ty for the heads up!!

I'm currently qorking on a bunch of other shtuff, and got a bit burnt out for a while, but this is definitely on my back burner!

I'll make sure to address the review and try to get it to a less hacky method!

Dexmachi avatar Aug 03 '26 00:08 Dexmachi