Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eval operations at execution where state unknown #995

Closed
3 changes: 3 additions & 0 deletions pyinfra/api/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def make_formatted_string_command(string: str, *args, **kwargs):
return StringCommand(*string_bits)


EvalOperationAtExecution = object()


class MaskString(str):
pass

Expand Down
9 changes: 6 additions & 3 deletions pyinfra/api/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pyinfra.context import ctx_host, ctx_state

from .arguments import get_execution_kwarg_keys, pop_global_arguments
from .command import StringCommand
from .command import EvalOperationAtExecution, FunctionCommand, StringCommand
from .exceptions import OperationValueError, PyinfraError
from .host import Host
from .operations import run_host_op
Expand Down Expand Up @@ -209,16 +209,19 @@ def decorated_func(*args, **kwargs):
host.current_op_global_kwargs = global_kwargs

# Convert to list as the result may be a generator
commands = func(*args, **kwargs)
commands = [ # convert any strings -> StringCommand's
StringCommand(command.strip()) if isinstance(command, str) else command
for command in commands
for command in func(*args, **kwargs)
]

host.in_op = False
host.current_op_hash = None
host.current_op_global_kwargs = None

if EvalOperationAtExecution in commands:
logger.warning("Defering operation evaluation until execution: %s", op_meta["names"])
commands = [FunctionCommand(operation(func), args, kwargs)]

# Add host-specific operation data to state, this mutates state
operation_meta = _update_state_meta(state, host, commands, op_hash, op_meta, global_kwargs)

Expand Down
8 changes: 4 additions & 4 deletions pyinfra/api/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pyinfra
from pyinfra import logger
from pyinfra.context import ctx_host
from pyinfra.context import ctx_host, ctx_state
from pyinfra.progress import progress_spinner

from .arguments import get_executor_kwarg_keys
Expand Down Expand Up @@ -115,7 +115,6 @@ def run_condition(condition_name: str) -> bool:
all_combined_output_lines = []

for i, command in enumerate(op_data["commands"]):

status = False

executor_kwargs = base_executor_kwargs.copy()
Expand Down Expand Up @@ -217,8 +216,9 @@ def run_condition(condition_name: str) -> bool:


def _run_host_op_with_context(state: "State", host: "Host", op_hash: str):
with ctx_host.use(host):
return run_host_op(state, host, op_hash)
with ctx_state.use(state):
with ctx_host.use(host):
return run_host_op(state, host, op_hash)


def _run_host_ops(state: "State", host: "Host", progress=None):
Expand Down
Loading