gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
561 stars 102 forks source link

docker compose now has --dry-run support, please add functionality #448

Open ITJamie opened 1 year ago

ITJamie commented 1 year ago

Docker Compose Dry Run is now feature complete. You can now safely preview any Compose command with the --dry-run flag

adding support for this would allow users to easily check of the currently deployed docker state is what is defined in the docker-compose file.

this would be fantastic for salt + ansible integration with python-on-whales as it would quickly allow checking if "state" is correct or if changes would be made

gabrieldemarmiesse commented 1 year ago

Of course, we aim to be close to what is available in the cli. Do you have an idea of how the python api should behave in this situation?

ITJamie commented 1 year ago

i guess the main requirement is some kind of bool flag on the response that would show if there is_changes or not. maybe an additional flag with the list of proposed changes that the --dry-run output would make.

so some examples:

up --dry-run

docker-compose up --dry-run
[+] Building 0.0s (0/0)
[+] Running 6/0
 ✔ DRY-RUN MODE -  Container nb_development_postgresql_1                Recreated                                                                                                                                                     0.0s
 ✔ DRY-RUN MODE -  Container be1f68a8a455_nb_development_redis-cache_1  Recreated                                                                                                                                                     0.0s
 ✔ DRY-RUN MODE -  Container nb_development_redis_1                     Recreated                                                                                                                                                     0.0s
 ✔ DRY-RUN MODE -  Container nb_development_netbox-housekeeping_1       Recreated                                                                                                                                                     0.0s
 ✔ DRY-RUN MODE -  Container nb_development_netbox-worker_1             Recreated                                                                                                                                                     0.0s
 ✔ DRY-RUN MODE -  Container nb_development_netbox_1                    Recreated                                                                                                                                                     0.0s
end of 'compose up' output, interactive run is not supported in dry-run mode

here you can see each container would be recreated. so some flag to show changes would be made + the list of contianers to be created/re-created

pull --dry-run

docker-compose pull --dry-run
[+] Pulling 6/6
 ✔ DRY-RUN MODE -  redis-cache Skipped - Image is already being pulled by redis                                                                                                                                                       0.0s
 ✔ DRY-RUN MODE -  netbox-worker Skipped - Image is already being pulled by netbox                                                                                                                                                    0.0s
 ✔ DRY-RUN MODE -  netbox-housekeeping Skipped - Image is already being pulled by netbox                                                                                                                                              0.0s
 ✔ DRY-RUN MODE -  redis Pulled                                                                                                                                                                                                       1.1s
 ✔ DRY-RUN MODE -  netbox Pulled                                                                                                                                                                                                      1.1s
 ✔ DRY-RUN MODE -  postgresql Pulled

so in this case it would be good for the list of changes to show that some containers would be pulled

so the output would need to be parsed and some kind of flag set

gabrieldemarmiesse commented 1 year ago

i wonder if the exit code changes depending on if it's going to do something or not, that would help us return the correct boolean

ITJamie commented 1 year ago

doesnt look like it changes the exit code sadly...

docker-compose up -d --dry-run
[+] Building 0.0s (0/0)
[+] Running 8/0
 ✔ DRY-RUN MODE -  Container nb_development-redis-1                            Running                                                0.0s
 ✔ DRY-RUN MODE -  Container nb_development-postgresql-1                       Running                                                0.0s
 ✔ DRY-RUN MODE -  Container nb_development-netbox-housekeeping-1              Recreated                                              0.0s
 ✔ DRY-RUN MODE -  Container nb_development-netbox-worker-1                    Recreated                                              0.0s
 ✔ DRY-RUN MODE -  Container nb_development-netbox-1                           Recreated                                              0.0s
 ✔ DRY-RUN MODE -  Container 4e6cad8d1a5_nb_development-netbox-housekeeping-1  Starte...                                              0.0s
 ✔ DRY-RUN MODE -  Container a64cba570ea_nb_development-netbox-worker-1        Started                                                0.0s
 ✔ DRY-RUN MODE -  Container ee0079f3185_nb_development-netbox-1               Started                                                0.0s

nb_development % echo $?
0
ITJamie commented 4 months ago

upstream https://github.com/docker/compose/issues/10872 has been solved which now makes this possible. there is a json flag to on the dry-run output to only output parseable json

gabrieldemarmiesse commented 4 months ago

Then I'd be very happy to get a pull request to add the feature!

felixfontein commented 3 months ago

FYI: Compose 2.29.0 (https://github.com/docker/compose/releases/tag/v2.29.0) has been released with this feature included. The JSON messages emitted with it are still somewhat non-trivial to parse since the way Compose emits events is sometimes quite chaotic (like sometimes text contains the status instead of status), since it was mainly implemented so that it looks good in text output, but at least it's a lot easier to parse than before...