diff --git a/plugins/module_utils/weldrapiv1.py b/plugins/module_utils/weldrapiv1.py index 57d72091..75fc4b0a 100644 --- a/plugins/module_utils/weldrapiv1.py +++ b/plugins/module_utils/weldrapiv1.py @@ -1,10 +1,12 @@ # # (c) 2022, Adam Miller (admiller@redhat.com) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +import hashlib import json import os import shutil import socket # noqa E402 +from pathlib import Path from ansible.module_utils._text import to_bytes from ansible.module_utils.six.moves.urllib.parse import quote @@ -368,7 +370,7 @@ def get_compose_failed(self): results = json.load(self.weldr.request.open("GET", "http://localhost/api/v1/compose/failed")) return results - def get_compose_image(self, compose_uuid, dest): + def get_compose_image(self, compose_uuid, dest, digest_function): """ # api.router.GET("/api/v:version/compose/image/:uuid", api.composeImageHandler) """ @@ -378,11 +380,20 @@ def get_compose_image(self, compose_uuid, dest): method="GET", unix_socket=self.weldr.unix_socket, ) + + if Path(dest).exists(): + # Check if we need to overwrite + tmpdigest = digest_function(tmpfile) + destdigest = digest_function(dest) + if tmpdigest == destdigest: + os.remove(tmpfile) + return None + shutil.copy(tmpfile, dest) with open(dest) as fd: os.fsync(fd) os.remove(tmpfile) - return None + return dest def get_compose_metadata(self, compose_uuid): """ diff --git a/plugins/modules/export_compose.py b/plugins/modules/export_compose.py index ee98e62b..301f7078 100644 --- a/plugins/modules/export_compose.py +++ b/plugins/modules/export_compose.py @@ -53,6 +53,7 @@ def main(): + changed: bool = False module = AnsibleModule( argument_spec=dict( compose_id=dict(type="str", required=True), @@ -62,12 +63,16 @@ def main(): weldr = Weldr(module) - weldr.api.get_compose_image( + filepath = weldr.api.get_compose_image( module.params["compose_id"], module.params["dest"], + module.digest_from_file, ) + if filepath is not None: + changed: bool = True - module.exit_json(msg="Exported compose payload to %s" % module.params["dest"]) + module.exit_json(msg="Exported compose payload to %s" % module.params["dest"], + changed=changed) if __name__ == "__main__":