Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_save.py

raw
1 #!/usr/bin/python
2 # coding: utf-8 -*-
3
4 # Copyright (c) 2020, Sagi Shnaidman <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 DOCUMENTATION = r"""
11 module: podman_save
12 short_description: Saves podman image to tar file
13 author: Sagi Shnaidman (@sshnaidm)
14 description:
15 - podman save saves an image to either docker-archive, oci-archive, oci-dir
16 (directory with oci manifest type), or docker-dir (directory with v2s2 manifest type)
17 on the local machine, default is docker-archive.
18
19 options:
20 image:
21 description:
22 - Image to save.
23 type: list
24 elements: str
25 required: true
26 compress:
27 description:
28 - Compress tarball image layers when pushing to a directory using the 'dir' transport.
29 (default is same compression type, compressed or uncompressed, as source)
30 type: bool
31 dest:
32 description:
33 - Destination file to write image to.
34 type: str
35 required: true
36 aliases:
37 - path
38 format:
39 description:
40 - Save image to docker-archive, oci-archive (see containers-transports(5)), oci-dir
41 (oci transport), or docker-dir (dir transport with v2s2 manifest type).
42 type: str
43 choices:
44 - docker-archive
45 - oci-archive
46 - oci-dir
47 - docker-dir
48 multi_image_archive:
49 description:
50 - Allow for creating archives with more than one image. Additional names will be
51 interpreted as images instead of tags. Only supported for docker-archive.
52 type: bool
53 force:
54 description:
55 - Force saving to file even if it exists.
56 type: bool
57 default: True
58 executable:
59 description:
60 - Path to C(podman) executable if it is not in the C($PATH) on the
61 machine running C(podman)
62 default: 'podman'
63 type: str
64 requirements:
65 - "Podman installed on host"
66 """
67
68 RETURN = """
69 """
70
71 EXAMPLES = """
72 # What modules does for example
73 - containers.podman.podman_save:
74 image: nginx
75 dest: /tmp/file123.tar
76 - containers.podman.podman_save:
77 image:
78 - nginx
79 - fedora
80 dest: /tmp/file456.tar
81 multi_image_archive: true
82 """
83
84 import os # noqa: E402
85 from ansible.module_utils.basic import AnsibleModule # noqa: E402
86 from ..module_utils.podman.common import remove_file_or_dir # noqa: E402
87
88
89 def save(module, executable):
90 changed = False
91 command = [executable, "save"]
92 cmd_args = {
93 "compress": ["--compress"],
94 "dest": ["-o=%s" % module.params["dest"]],
95 "format": ["--format=%s" % module.params["format"]],
96 "multi_image_archive": ["--multi-image-archive"],
97 }
98 for param in module.params:
99 if module.params[param] is not None and param in cmd_args:
100 command += cmd_args[param]
101 for img in module.params["image"]:
102 command.append(img)
103 if module.params["force"]:
104 changed = True
105 dest = module.params["dest"]
106 if os.path.exists(dest):
107 if module.check_mode:
108 return changed, "", ""
109 try:
110 remove_file_or_dir(dest)
111 except Exception as e:
112 module.fail_json(msg="Error deleting %s path: %s" % (dest, e))
113 else:
114 changed = not os.path.exists(module.params["dest"])
115 if module.check_mode:
116 return changed, "", ""
117 rc, out, err = module.run_command(command)
118 if rc != 0:
119 module.fail_json(msg="Error: %s" % (err))
120 return changed, out, err
121
122
123 def main():
124 module = AnsibleModule(
125 argument_spec=dict(
126 image=dict(type="list", elements="str", required=True),
127 compress=dict(type="bool"),
128 dest=dict(type="str", required=True, aliases=["path"]),
129 format=dict(
130 type="str",
131 choices=["docker-archive", "oci-archive", "oci-dir", "docker-dir"],
132 ),
133 multi_image_archive=dict(type="bool"),
134 force=dict(type="bool", default=True),
135 executable=dict(type="str", default="podman"),
136 ),
137 supports_check_mode=True,
138 )
139 if module.params["compress"] and module.params["format"] not in [
140 "oci-dir",
141 "docker-dir",
142 ]:
143 module.fail_json(msg="Compression is only supported for oci-dir and docker-dir format")
144
145 executable = module.get_bin_path(module.params["executable"], required=True)
146 changed, out, err = save(module, executable)
147
148 results = {
149 "changed": changed,
150 "stdout": out,
151 "stderr": err,
152 }
153
154 module.exit_json(**results)
155
156
157 if __name__ == "__main__":
158 main()