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

Add docker_compose_v2 module #739

Merged
merged 3 commits into from
Jan 3, 2024

Conversation

felixfontein
Copy link
Collaborator

@felixfontein felixfontein commented Jan 2, 2024

SUMMARY

Implements a basic module wrapping the Docker CLI compose plugin.

I copied some of the output parsing code from #586. Unfortunately docker compose's status reporting is pretty primitive, see also docker/compose#10872.

Fixes #216.
Closes #586.

Reference for changes to compose over time: https://docs.docker.com/compose/release-notes/

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

docker_compose_v2

@felixfontein felixfontein force-pushed the compose branch 5 times, most recently from ba25363 to 9e5a10a Compare January 2, 2024 14:17
@felixfontein
Copy link
Collaborator Author

This is now ready for review!

Comment on lines 117 to 123
- The Docker compose CLI plugin has no stable output format (see for example U(https://github.com/docker/compose/pull/10918)),
and for the main operations also no machine friendly output format. The module tries to accomodate this with various
version-dependent behavior adjustments and with testing older and newer versions of the Docker compose CLI plugin.

Currently the module is tested with multiple plugin versions between 2.18.1 and 2.23.3. The exact list of plugin versions
will change over time. New releases of the Docker compose CLI plugin can break this module at any time.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lel-amri I've added the above note in response to #586 (comment).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great !

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, I think this is about as good as you can get there. At least we can take heart in the fact that if Docker were to change the command's UX too drastically, it would break people's brains in addition to the Ansible code, so hopefully they keep it pretty stable.

Copy link
Contributor

@briantist briantist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! Nice to finally have a v2 option. Shame there's no stable/machine-friendly output from the tool.

@@ -1,2 +1,3 @@
.azure-pipelines/scripts/publish-codecov.py replace-urlopen
plugins/modules/docker_compose_v2.py validate-modules:return-syntax-error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what in this module triggers a return syntax error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the type: raw IIRC. Some older versions might also barf on choices in return values.

Emit warnings (or things we assume are warnings), and report unparsable
messages to the user so they can report them to us.
@felixfontein felixfontein merged commit b774837 into ansible-collections:main Jan 3, 2024
121 checks passed
@felixfontein felixfontein deleted the compose branch January 3, 2024 07:05
@felixfontein
Copy link
Collaborator Author

@lel-amri thanks for your work on this in #586!
@lel-amri @briantist @geerlingguy thanks for reviewing!

@b-reich
Copy link

b-reich commented Jan 11, 2024

@felixfontein Many thanks for your PR. Is there still the possibility for a docker compose pull or docker compose build --pull?

@felixfontein
Copy link
Collaborator Author

@b-reich right now no. I'm currently working on a docker_compose_v2_pull module which models docker compose pull. I originally wanted to have it done on last weekend, but parsing the docker compose pull output turned out to be more complex than I hoped for. I hope I can find some time this weekend to finish it.

About docker compose build: I think it's also a good idea to have that as a separate module, and I can try taking a look at it. It would be great if you could create a new issue for this (a proper feature request) and sketch out one or multiple scenarios in which you (or others) would use docker compose build.

@b-reich
Copy link

b-reich commented Jan 12, 2024

@b-reich right now no. I'm currently working on a docker_compose_v2_pull module which models docker compose pull. I originally wanted to have it done on last weekend, but parsing the docker compose pull output turned out to be more complex than I hoped for. I hope I can find some time this weekend to finish it.

About docker compose build: I think it's also a good idea to have that as a separate module, and I can try taking a look at it. It would be great if you could create a new issue for this (a proper feature request) and sketch out one or multiple scenarios in which you (or others) would use docker compose build.

Ahh I understand. If there are things I can support, please ping me.
Is there any advantages over creating an extra module over the v1 behaviour as parameter?
The Issue #750

@felixfontein
Copy link
Collaborator Author

Ahh I understand. If there are things I can support, please ping me.

Thanks!

Is there any advantages over creating an extra module over the v1 behaviour as parameter?

Short answer: look at docker_image and compare its usage to the new docker_image_* modules.

Long answer: it reduces the complexity, both of the code and of the user interface (the argument spec). This benefits both the user (less bugs, documentation easier to understand since the amount of options per module is small, you don't have to wonder whether option force now applies to pulling, starting, stopping, building, removing, or multiple of these, ...) and contributors (the code is simpler, there are no potentially surprising interactions between different 'modes' of the same module that you didn't think of, ...). Also it makes handling check mode easier, since it's easy to document which operations support check mode and which don't (push doesn't, pull only partially, build and tag support it fully - so what should the documentation for docker_image say? you don't want to read a wall of text and compare it to every of your docker_image tasks to figure out which one work fine in check mode and which don't).

There are some cases where a combined module makes things easier to express (like for docker_image: you can build, tag, and push the tagged image in one single task!). I hope that there are no cases where separate modules make something impossible or something common very inefficient. For docker_compose_v2 and docker_compose_v2_pull (and of course for docker_image_*), I'm pretty sure that this is the case. For docker_compose_v2_build it's kind of a guess, that's why I ask for use-cases to hopefully notice if a separate module is a bad idea :)

@felixfontein
Copy link
Collaborator Author

(Just look at https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#parameters and note that originally the options grouped in build were top-level options as well. Of course in the beginning there were a lot less of them, and it didn't look so bad, but then someone requests a new option x for building, then someone a new option y, and eventually you end up with 20 options just specificly for building images. Then you have five more for tagging, four for pulling, three for pushing, and you end up with a large soup of parameters. Obviously the numbers I mentioned here are a bit exaggerated, but they're not that far from the truth, especially if you look at docker build --help and start looking for things docker_image doesn't yet support.)

@felixfontein
Copy link
Collaborator Author

PR for docker_compose_v2_pull module: #751

@pgassmann
Copy link

pgassmann commented Jan 15, 2024

@felixfontein separate modules for compose, pull and build are ok for me.

Another option is to have basic pull and build flag options in the standard docker_compose_v2 module, and for more special cases, have a separate pull/build module.

This matches the compose command, the compose up comand has --build and --pull options, and there are separate compose pull and compose build commands with more options.

I just need an way to update services with locally built images for regular maintenance updates.

I now see, that the pull option is already implemented for the docker_compose_v2 module.

Separate Issue for the build feature: #750

@pgassmann
Copy link

the module currently does not have support for --diff.
But the return values contain a nice list of actions.

@felixfontein Would it be possible to display those in diff mode? or are there strict conventions for diff output?

I now implemented a workaround with a second task:

- name: docker compose up ({{ webserver_domain }} in {{ webserver_path }})
  community.docker.docker_compose_v2:
    project_src: '{{ webserver_path }}'
    state: present
    remove_orphans: yes
    pull: "{{ webserver_docker_pull }}"
    build: "{{ webserver_docker_pull }}"
  register: webserver__register_compose

- name: print actions by compose
  vars:
    actions: "{{ webserver__register_compose.actions }}"
  debug:
    var: actions
  when: webserver__register_compose.changed
  changed_when: webserver__register_compose.changed

This generates the following output. also works in check mode:

TASK [teamapps.general.webserver : docker compose up (www-dev.example.com) in /container/contao)] *******
changed: [website-dev1]

TASK [teamapps.general.webserver : print actions by compose] ***********
changed: [website-dev1] => 
  actions:
  - id: contao-sftp-1
    status: Starting
    what: container

@felixfontein
Copy link
Collaborator Author

@pgassmann I guess the question is what you expect from diff mode :) I agree that the actions are better than nothing, but it's not really what I personally would expect (I would expect it to tell me which configuration changes trigger the actions - but that's not so easy to retrieve). Let's maybe create an issue and collect ideas.

@goldyfruit
Copy link

goldyfruit commented Jan 21, 2024

@felixfontein what is the equivalent of the files argument with the new module?

Previously I was doing like this:

  community.docker.docker_compose:
    project_src: "{{ _ovos_installer_working_directory }}/{{ _ovos_installer_project_name }}/compose"
    project_name: "{{ _project_name }}"
    files: "{{ _composition_files }}"
    pull: true
    remove_orphans: "{{ ovos_installer_docker_compose_remove_orphans }}"
    remove_images: "{{ ovos_installer_docker_compose_remove_images }}"
    remove_volumes: "{{ ovos_installer_docker_compose_remove_volumes }}"

@felixfontein
Copy link
Collaborator Author

@goldyfruit there is no equivalent at the moment. I've never used that feature nor saw it being used, so I didn't want to implement it in the first version to avoid screwing it up ;) Please create an issue (Feature Request) for it.

@goldyfruit
Copy link

@goldyfruit there is no equivalent at the moment. I've never used that feature nor saw it being used, so I didn't want to implement it in the first version to avoid screwing it up ;) Please create an issue (Feature Request) for it.

Feature request created. 👍

@felixfontein
Copy link
Collaborator Author

If someone is interested, #776 adds support for a scale option to docker_compose_v2.

@pgassmann
Copy link

Ansible 9.2.0 was released yesterday, containing the new docker_compose_v2 modules: https://github.com/ansible-community/ansible-build-data/blob/main/9/CHANGELOG-v9.rst

BTW: Is there anywhere a better readable, rendered version of this Changelog for ansible-community?

@felixfontein
Copy link
Collaborator Author

BTW: Is there anywhere a better readable, rendered version of this Changelog for ansible-community?

@pgassmann not yet, I'm currently working of creating a MD version, I'll hopefully have time to get a first working version done on Friday. I'm currently running very low on spare time for any open source work, but from Friday on it will be better :)

(If you're interested: ansible-community/antsibull-changelog#139)

In the meantime you can complain to GitHub which screwed up the RST preview of quite many changelogs and other RST documents (https://github.com/orgs/community/discussions/86715).

@pgassmann
Copy link

@felixfontein don't apologize for not having much time for open source work. You are the most active and helpful contributor on the issues that affect me. It's telling, that you are the one who already implemented a a solution that fixes the issue in my question.
Thank you for all your efforts, I appreciate your support!

Shouldn't the changelog be part of the rendered website or a docs site like:
https://ansible-community-website.readthedocs.io/
or https://docs.ansible.com

@felixfontein
Copy link
Collaborator Author

The Ansible changelog traditionally always was on git only and was never part of the docsite (docs.ansible.com); I don't know the reasons for that. One possible reason might be that the docsite build takes quite some time, so it's usually only updated after the actual release happens, which can be confusing since for some time (a few hours at most) the changelog wouldn't mention the latest version.

In any case, here's a markdown version of the Ansible 9 changelog: https://github.com/felixfontein/ansible-build-data/blob/markdown/9/CHANGELOG-v9.md (PR: ansible-community/ansible-build-data#364). And here's the community.docker changelog as markdown: https://github.com/felixfontein/community.docker/blob/changelog/CHANGELOG.md (PR: #788).

@briantist
Copy link
Contributor

I think it would be worthwhile to publish the changelog if possible. Individual collections can already do this fairly easily if they want to with a symlink and a small change, see:

But I can't speak to what this process would be for the community package as a whole, nor how delayed its publishing might be.

The collection-level published changelog could be added with a small PR for anyone who's interested in doing so!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker-compose-v2 Docker Compose v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docker-compose 2.0.0 is no longer written in Python, thus breaking the docker_compose module
7 participants