Skip to content

Error while using API and add_deploy #1475

@matof

Description

@matof

After updating Pyinfra to version 3.5.1 I get error "Cannot call operations outside of Prepare/Execute stages"(pyinfra\api\operation.py", line 270) while using Pyinfra API. It is working without problem with version 3.3.X.

Our setup a is a little bit complicated but I will paste here all I think is important.

import pyinfra.api
import pyinfra.api.connect
from pyinfra.api.deploy import add_deploy
import pyinfra.local

class PyinfraStateCallback(pyinfra.api.state.BaseStateCallback):
  ...

class PyinfraRunner:

  def run_pyinfra_deploy(self):
    working_dir = ...
    hosts = [...]
    inventory = pyinfra.api.Inventory((hosts, {"ssh_user": "root"}))
    config = pyinfra.api.config.Config(
      CWD=working_dir
    )

    state = pyinfra.api.state.State(inventory, config)
    state.add_callback_handler(PyinfraStateCallback(self))

    pyinfra.api.connect.connect_all(state)

    module_name = "my.custom.deploy"
    deploy_module = importlib.import_module(module_name)
    add_deploy(state, deploy_module.task, task_context={...})

    pyinfra.api.operations.run_ops(state)

The custom deploy script (my.custom.deploy) looks like:

from pyinfra.api import deploy
from pyinfra.operations import files, python
from pyinfra import host

@deploy("Download file")
def task(task_context):
    src_path = ...
    dest_path = ...

    files.get(
        name="Download file from remote",
        src=src_path,
        dest=dest_path
    )

The line with files.get is triggering the exception.
Can you advice me if I am doing something wrong? Or did something change in version 3.5 and I shouldn't be using it in this way anymore?
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions