Skip to content

v0.5.0 - Sudo Support

Choose a tag to compare

@elazar elazar released this 01 Nov 00:54
· 16 commits to main since this release

Added

  • Sudo support for privileged operations - The OrbStack connector now handles PyInfra's sudo and sudo_user arguments, enabling operations that require elevated privileges
  • Safe command quoting - Uses shlex.quote() for proper handling of commands with special characters, quotes, and spaces
  • Comprehensive test coverage - 9 new sudo-specific unit tests covering all command types (string commands, StringCommand objects, and lists)

Installation

pip install pyinfra-orbstack==0.5.0
# or
uv pip install pyinfra-orbstack==0.5.0

Usage Example

from pyinfra.operations import apt, server

# Run privileged commands
server.shell(
    name="Remove lock files",
    commands="rm -f /var/lib/dpkg/lock",
    _sudo=True,
)

# Run as specific user
server.shell(
    name="Run as postgres",
    commands="whoami",
    _sudo=True,
    _sudo_user="postgres",
)

# Package operations with sudo
apt.packages(
    name="Install nginx",
    packages=["nginx"],
    _sudo=True,
)

Technical Details

The sudo implementation:

  • Applies the -H flag for proper HOME directory handling (matching PyInfra's SSH connector)
  • Supports _sudo_user parameter for running commands as specific users
  • Handles all command types: plain strings, PyInfra StringCommand objects, and lists
  • Properly quotes commands to safely handle special characters