pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

FR: config.SKIP_IDEMPOTENCY flag

Open Dexmachi opened this issue 8 months ago • 2 comments

Is your feature request related to a problem? Please describe

By using Pyinfra as an API, one might use it in pipelines to calculate state drifts with facts to have less syscalls for one specific edge case, or to expand the operation (for instance, applying an sudo configuration file per user with a flag). By calculating these drifts by hand and utilizing Pyinfra's operations, the drift gets calculated again internally, which results in an higher execution overhead.

Describe the solution you'd like

I'd like an config.SKIP_IDEMPOTENCY flag paired with an _skip_idempotency in order to... well, skip idempotency if the user states "I want these packages installed", then Pyinfra should simply run the command with the selected packages, erroring out if the command fails. This would clearly be a more advanced configuration, so I don't think it would be all that bad

This is meant to not rely on server.shell and use Pyinfra's builtin abstractions, which are already made to be safe and easy to use, other than being easier for other tools to use the API.

The CLI could intercept this and pop out a warning about this as well!

Dexmachi avatar Mar 21 '26 02:03 Dexmachi

(yes, this is an edge case I actually have to deal with.)

Dexmachi avatar Mar 21 '26 02:03 Dexmachi

After brainstorming for a good long while with my software architecture teacher (and hitting SEVERAL fact mocking walls), I've came up with a lil solution

Instead of trying to guess what the operation does, what if the op explicitly declares exactly what it does? I've added this exact escape hatch:


# pyinfra.api.operation._wrap_operation.command_generator

...
               if getattr(state.config, "EXECUTION_ONLY", False):
                   if direct_execution:
                       logger.info(
                           f"[{host.name}] Running in EXECUTION_ONLY mode, running {get_operation_name_from_func(func)} without diffing"
                       )
                       for command in direct_execution(*args, **kwargs):
                           yield (
                               StringCommand(command.strip())
                               if isinstance(command, str)
                               else command
                           )
                       return
                   else:
                       op_name = get_operation_name_from_func(func)
                       logger.warning(
                           f"[{host.name}] Op {op_name} is not yet supported in EXECUTION_ONLY mode. Falling back to normal execution."
                       )

               for command in func(*args, **kwargs):
                   if isinstance(command, str):
                       command = StringCommand(command.strip())
                   yield command

...

No magic, backwards compatibility and opt-in boilerplate!

yeah, this approach is quite boilerplatey, and will require the refactoring of a lot of the operations existing (only to support the EXECUTION_ONLY flag), which I can in fact do.

I've only made this into a Issue comment instead of a PR to check how y'all think of this approach

Dexmachi avatar Apr 03 '26 22:04 Dexmachi