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

Return whether and operation changed #1065

Closed tino closed 8 months ago

tino commented 9 months ago

I'd like to be able to do this:

changed = files.put("files/postgres/compose.yml", "postgres/compose.yml")
if changed:
    server.shell("cd postgres && docker compose up -d")

So I can run steps conditionally on whether something changed. Or is there a better way to do this?

PS. Sorry I post this here, I went for the Questions button, ended up on an unused Discord that linked me to Matrix, which I don't have but told me I could use Thunderbird (which I do have) but that didn't work ¯_(ツ)_/¯.

vojta001 commented 9 months ago

This is already implemented, have a look at https://docs.pyinfra.com/en/2.x/using-operations.html#operation-changes-output

tino commented 8 months ago

Nvm, I found out that this is already possible!

compose = files.put("files/postgres/compose.yml", "postgres/compose.yml")
if compose.changed:
    server.shell("cd postgres && docker compose up -d")