Skip to content

Commit

Permalink
Reapply "Fix transports issues in podman_image (#619)" (#754) (#755)
Browse files Browse the repository at this point in the history
This reverts commit 391e4c5.

Signed-off-by: Sagi Shnaidman <[email protected]>
  • Loading branch information
sshnaidm authored May 29, 2024
1 parent 0e24de1 commit 4985d48
Show file tree
Hide file tree
Showing 3 changed files with 548 additions and 41 deletions.
73 changes: 33 additions & 40 deletions plugins/modules/podman_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
type: str
choices:
- dir
- docker
- docker-archive
- docker-daemon
- oci-archive
Expand Down Expand Up @@ -557,16 +558,8 @@ def present(self):
self.results['changed'] = True

if self.push:
# Push the image
if '/' in self.image_name:
push_format_string = 'Pushed image {image_name}'
else:
push_format_string = 'Pushed image {image_name} to {dest}'
self.results['actions'].append(push_format_string.format(image_name=self.image_name, dest=self.push_args['dest']))
self.results['changed'] = True
if not self.module.check_mode:
self.results['image'], output = self.push_image()
self.results['stdout'] += "\n" + output
self.results['image'], output = self.push_image()
self.results['stdout'] += "\n" + output
if image and not self.results.get('image'):
self.results['image'] = image

Expand Down Expand Up @@ -787,45 +780,44 @@ def push_image(self):

# Build the destination argument
dest = self.push_args.get('dest')
dest_format_string = '{dest}/{image_name}'
regexp = re.compile(r'/{name}(:{tag})?'.format(name=self.name, tag=self.tag))
if not dest:
if '/' not in self.name:
self.module.fail_json(msg="'push_args['dest']' is required when pushing images that do not have the remote registry in the image name")

# If the push destination contains the image name and/or the tag
# remove it and warn since it's not needed.
elif regexp.search(dest):
dest = regexp.sub('', dest)
self.module.warn("Image name and tag are automatically added to push_args['dest']. Destination changed to {dest}".format(dest=dest))
transport = self.push_args.get('transport')

if dest and dest.endswith('/'):
dest = dest[:-1]
if dest is None:
dest = self.image_name

transport = self.push_args.get('transport')
if transport:
if not dest:
self.module.fail_json("'push_args['transport'] requires 'push_args['dest'] but it was not provided.")
if transport == 'docker':
dest_format_string = '{transport}://{dest}'
elif transport == 'ostree':
dest_format_string = '{transport}:{name}@{dest}'
else:
dest_format_string = '{transport}:{dest}'

dest_string = dest_format_string.format(transport=transport, name=self.name, dest=dest, image_name=self.image_name,)

# Only append the destination argument if the image name is not a URL
if '/' not in self.name:
args.append(dest_string)

rc, out, err = self._run(args, ignore_errors=True)
if rc != 0:
self.module.fail_json(msg="Failed to push image {image_name}: {err}".format(image_name=self.image_name, err=err))
last_id = self._get_id_from_output(
out + err, contains=':', split_on=':')

return self.inspect_image(last_id), out + err
if transport == 'docker-daemon' and ":" not in dest:
dest_format_string = '{transport}:{dest}:latest'
dest_string = dest_format_string.format(transport=transport, name=self.name, dest=dest)
else:
dest_string = dest

if "/" not in dest_string and "@" not in dest_string and "docker-daemon" not in dest_string:
self.module.fail_json(msg="Destination must be a full URL or path to a directory.")

args.append(dest_string)
self.module.log("PODMAN-IMAGE-DEBUG: Pushing image {image_name} to {dest_string}".format(
image_name=self.image_name, dest_string=dest_string))
self.results['actions'].append(" ".join(args))
self.results['podman_actions'].append(" ".join([self.executable] + args))
self.results['changed'] = True
out, err = '', ''
if not self.module.check_mode:
rc, out, err = self._run(args, ignore_errors=True)
if rc != 0:
self.module.fail_json(msg="Failed to push image {image_name}".format(
image_name=self.image_name),
stdout=out, stderr=err,
actions=self.results['actions'],
podman_actions=self.results['podman_actions'])

return self.inspect_image(self.image_name), out + err

def remove_image(self, image_name=None):
if image_name is None:
Expand Down Expand Up @@ -922,6 +914,7 @@ def main():
'docker-daemon',
'oci-archive',
'ostree',
'docker'
]
),
),
Expand Down
Loading

0 comments on commit 4985d48

Please sign in to comment.