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

Global or per-operation class env vars #646

Closed taranlu-houzz closed 2 weeks ago

taranlu-houzz commented 3 years ago

Is your feature request related to a problem? Please describe

Not sure if this is actually a feature that would make sense to add, but this is the issue that I am dealing with:

The command brew update used by the brew.update() operation automatically stashes any local changes that have been made to brew recipes (one might do this if you need to patch a brew recipe to get at a specific version that doesn't have an @version). The way to get around this is to set the environment var HOMEBREW_DEVELOPER when running the brew commands. In order to do this via pyinfra, I need to pass that in via the env param to each brew.* operation, which is tedious and can be error prone (since brew will just stash the modifications and carry on as though nothing out of the ordinary happened).

Describe the solution you'd like

I'd like a way to specify a set of environment vars that will be passed automatically to all invocations of a particular operation class, possibly as some sort of special group_data key?

Maybe, something like this:

pyinfra_operations_brew = {
    "HOMBREW_DEVELOPER": 1,
}

There is probably a more elegant solution, but that is the gist of what I am hoping for.

Fizzadar commented 3 years ago

One option here is to use the ENV config var which would apply to all operations within a deploy; ie:

# config.py

ENV = {
    "HOMBREW_DEVELOPER": 1,
}

# or, new style recommended in v1.5+

from pyinfra import config

config.ENV = {
    "HOMBREW_DEVELOPER": 1,
}

I believe this would provide a suitable solution? Setting environment variables for a given set of operations would be a bit confusing, I think, vs. this global approach.

Fizzadar commented 2 weeks ago

The _env global argument supports this now. https://docs.pyinfra.com/en/3.x/arguments.html#shell-control-features