mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Reformat code with black and isort.
This commit is contained in:
@@ -82,6 +82,9 @@ images:
|
||||
import errno
|
||||
import traceback
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
|
||||
DockerException,
|
||||
)
|
||||
from ansible_collections.community.docker.plugins.module_utils.common_api import (
|
||||
AnsibleDockerClient,
|
||||
RequestException,
|
||||
@@ -91,8 +94,6 @@ from ansible_collections.community.docker.plugins.module_utils.util import (
|
||||
is_image_name_id,
|
||||
)
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException
|
||||
|
||||
|
||||
class ImageManager(DockerBaseClass):
|
||||
def __init__(self, client, results):
|
||||
@@ -103,73 +104,86 @@ class ImageManager(DockerBaseClass):
|
||||
parameters = self.client.module.params
|
||||
self.check_mode = self.client.check_mode
|
||||
|
||||
self.path = parameters['path']
|
||||
self.path = parameters["path"]
|
||||
|
||||
self.load_images()
|
||||
|
||||
@staticmethod
|
||||
def _extract_output_line(line, output):
|
||||
'''
|
||||
"""
|
||||
Extract text line from stream output and, if found, adds it to output.
|
||||
'''
|
||||
if 'stream' in line or 'status' in line:
|
||||
"""
|
||||
if "stream" in line or "status" in line:
|
||||
# Make sure we have a string (assuming that line['stream'] and
|
||||
# line['status'] are either not defined, falsish, or a string)
|
||||
text_line = line.get('stream') or line.get('status') or ''
|
||||
text_line = line.get("stream") or line.get("status") or ""
|
||||
output.extend(text_line.splitlines())
|
||||
|
||||
def load_images(self):
|
||||
'''
|
||||
"""
|
||||
Load images from a .tar archive
|
||||
'''
|
||||
"""
|
||||
# Load image(s) from file
|
||||
load_output = []
|
||||
try:
|
||||
self.log(f"Opening image {self.path}")
|
||||
with open(self.path, 'rb') as image_tar:
|
||||
with open(self.path, "rb") as image_tar:
|
||||
self.log(f"Loading images from {self.path}")
|
||||
res = self.client._post(self.client._url("/images/load"), data=image_tar, stream=True)
|
||||
res = self.client._post(
|
||||
self.client._url("/images/load"), data=image_tar, stream=True
|
||||
)
|
||||
for line in self.client._stream_helper(res, decode=True):
|
||||
self.log(line, pretty_print=True)
|
||||
self._extract_output_line(line, load_output)
|
||||
except EnvironmentError as exc:
|
||||
if exc.errno == errno.ENOENT:
|
||||
self.client.fail(f"Error opening archive {self.path} - {exc}")
|
||||
self.client.fail(f"Error loading archive {self.path} - {exc}", stdout='\n'.join(load_output))
|
||||
self.client.fail(
|
||||
f"Error loading archive {self.path} - {exc}",
|
||||
stdout="\n".join(load_output),
|
||||
)
|
||||
except Exception as exc:
|
||||
self.client.fail(f"Error loading archive {self.path} - {exc}", stdout='\n'.join(load_output))
|
||||
self.client.fail(
|
||||
f"Error loading archive {self.path} - {exc}",
|
||||
stdout="\n".join(load_output),
|
||||
)
|
||||
|
||||
# Collect loaded images
|
||||
loaded_images = []
|
||||
for line in load_output:
|
||||
if line.startswith('Loaded image:'):
|
||||
loaded_images.append(line[len('Loaded image:'):].strip())
|
||||
if line.startswith('Loaded image ID:'):
|
||||
loaded_images.append(line[len('Loaded image ID:'):].strip())
|
||||
if line.startswith("Loaded image:"):
|
||||
loaded_images.append(line[len("Loaded image:") :].strip())
|
||||
if line.startswith("Loaded image ID:"):
|
||||
loaded_images.append(line[len("Loaded image ID:") :].strip())
|
||||
|
||||
if not loaded_images:
|
||||
self.client.fail("Detected no loaded images. Archive potentially corrupt?", stdout='\n'.join(load_output))
|
||||
self.client.fail(
|
||||
"Detected no loaded images. Archive potentially corrupt?",
|
||||
stdout="\n".join(load_output),
|
||||
)
|
||||
|
||||
images = []
|
||||
for image_name in loaded_images:
|
||||
if is_image_name_id(image_name):
|
||||
images.append(self.client.find_image_by_id(image_name))
|
||||
elif ':' in image_name:
|
||||
image_name, tag = image_name.rsplit(':', 1)
|
||||
elif ":" in image_name:
|
||||
image_name, tag = image_name.rsplit(":", 1)
|
||||
images.append(self.client.find_image(image_name, tag))
|
||||
else:
|
||||
self.client.module.warn(f'Image name "{image_name}" is neither ID nor has a tag')
|
||||
self.client.module.warn(
|
||||
f'Image name "{image_name}" is neither ID nor has a tag'
|
||||
)
|
||||
|
||||
self.results['image_names'] = loaded_images
|
||||
self.results['images'] = images
|
||||
self.results['changed'] = True
|
||||
self.results['stdout'] = '\n'.join(load_output)
|
||||
self.results["image_names"] = loaded_images
|
||||
self.results["images"] = images
|
||||
self.results["changed"] = True
|
||||
self.results["stdout"] = "\n".join(load_output)
|
||||
|
||||
|
||||
def main():
|
||||
client = AnsibleDockerClient(
|
||||
argument_spec=dict(
|
||||
path=dict(type='path', required=True),
|
||||
path=dict(type="path", required=True),
|
||||
),
|
||||
supports_check_mode=False,
|
||||
)
|
||||
@@ -183,12 +197,16 @@ def main():
|
||||
ImageManager(client, results)
|
||||
client.module.exit_json(**results)
|
||||
except DockerException as e:
|
||||
client.fail(f'An unexpected Docker error occurred: {e}', exception=traceback.format_exc())
|
||||
client.fail(
|
||||
f"An unexpected Docker error occurred: {e}",
|
||||
exception=traceback.format_exc(),
|
||||
)
|
||||
except RequestException as e:
|
||||
client.fail(
|
||||
f'An unexpected requests error occurred when trying to talk to the Docker daemon: {e}',
|
||||
exception=traceback.format_exc())
|
||||
f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}",
|
||||
exception=traceback.format_exc(),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user