Skip to content

Commit

Permalink
feat: enable export_compose module to return changed state
Browse files Browse the repository at this point in the history
using the digest function specified by module_utils, we can do a hash comparison if the dest file already exists and return a little extra signal (the written dest) from get_compose_image to determine if we changed or not
  • Loading branch information
jharmison-redhat committed Oct 18, 2023
1 parent 7fe8e3a commit 26060a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 13 additions & 2 deletions plugins/module_utils/weldrapiv1.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#
# (c) 2022, Adam Miller ([email protected])
# 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
Expand Down Expand Up @@ -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)
"""
Expand All @@ -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):
"""
Expand Down
9 changes: 7 additions & 2 deletions plugins/modules/export_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@


def main():
changed: bool = False
module = AnsibleModule(
argument_spec=dict(
compose_id=dict(type="str", required=True),
Expand All @@ -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__":
Expand Down

0 comments on commit 26060a5

Please sign in to comment.