gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
516 stars 100 forks source link

Allow docker stack deploy from stdin #581

Open derfl0 opened 2 months ago

derfl0 commented 2 months ago

Hey, thank you for this great project. I am currently trying to replace subprocess calls in my code by your lib. I have some logic in the stack template and therefore need to heavily modify it according to my needs.

Since v1.25 it is possible to read the compose.yml from stdin (See: https://docs.docker.com/reference/cli/docker/stack/deploy/ ) which I use to read from a modified string.

Is it possible to implement this option in the lib as well?

gabrieldemarmiesse commented 2 months ago

Hi @derfl0 sorry for the delay, I've been busy with other things.

Currently python-on-whales doesn't support this. I would be happy to support it in the future. Though reading from stdin in python doesn't make much sense (it makes sense in a CLI).

What I would propose is that we find a good name for a new argument to this function: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/stack/#python_on_whales.components.stack.cli_wrapper.StackCLI.deploy and this argument would take a string as input. Inside the string would be the yaml of your stack. Would that work for you?

derfl0 commented 2 months ago

Hey @gabrieldemarmiesse , no need to apologise as you work free of charge ;)

the proposed solution is exactly what I would need.

Talking about Stdin: Having a communication interface with the subprocess below would be awesome. Currently, in my native approach, I use the communicate method ( https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate ) Background: I use a mysql server and create databases on demand via a separate mysql client container. This involves a lot of very ugly escaping if it must be a one-line start parameter. It is way easier, if you just start the container and then use "communicate" to execute your commands one by one. Of course this does only work for local containers as swarm does not support exec on service/task. I can also open a different issue for this, if it is feature material.

# pseudo example
with docker.run("mysql", detach=True) as mysql_container:
    print(mysql_container.communicate("show databases;"))