pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.91k stars 382 forks source link

pyinfra doesn't ask for sudo password when config.SUDO or sudo = True and get_pty = True #766

Closed jmpolom closed 2 years ago

jmpolom commented 2 years ago

Describe the bug

from pyinfra import config
from pyinfra.operations import server

config.SUDO = True

server.shell(
    name = 'Try it with shell',
    commands = 'systemctl --user --machine=dwspectrum@.host daemon-reload',
    get_pty = True
)

or

from pyinfra import config
from pyinfra.operations import server

server.shell(
    name = 'Try it with shell',
    commands = 'systemctl --user --machine=dwspectrum@.host daemon-reload',
    get_pty = True,
    sudo = True
)

Either of the above will result in pyinfra not asking for a sudo password, which is different from the behavior when get_pty is not specified and pyinfra asks for a sudo password if one is not supplied via a kwarg or configuration parameter. This seems like buggy inconsistent behavior.

Example output when it doesn't prompt for password:

~/homenet/router/pyinfra master ?3 ❯ pyinfra -vvv hosts.py test_systemd.py
--> Loading config...
--> Loading inventory...

--> Connecting to hosts...
    [archon] Connected

--> Preparing operations...
    Loading: test_systemd.py
    [archon] Ready: test_systemd.py

--> Proposed changes:
    Groups: routers / hosts
    [archon]   Operations: 1   Commands: 1

--> Beginning operation run...
--> Starting operation: Try it with shell
[archon] >>> sudo -H -n sh -c 'systemctl --user --machine=dwspectrum@.host daemon-reload'
[archon] sudo: a password is required
    [archon] Error
--> pyinfra error: No hosts remaining!

To get around this issue, either config.USE_SUDO_PASSWORD needs to be set to True/the password value, or the equivalent kwarg can be passed to the operation with the same values.

To Reproduce

Add get_pty=True to an operation.

Expected behavior

pyinfra prompts for sudo password as it does when get_pty is default/False.

Meta

Fizzadar commented 2 years ago

Replicated this and get_pty means output lines have \r at the end - I've added a fix (+test) for this in https://github.com/Fizzadar/pyinfra/commit/f5cb24e5ae99f457fc7d725d9d087c2fdc5b7ed7.

Fizzadar commented 2 years ago

Now released in v1.7!