| 1 |
#!/usr/bin/python |
| 2 |
# coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2021, 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_import |
| 12 |
short_description: Import Podman container from a tar file. |
| 13 |
author: Sagi Shnaidman (@sshnaidm) |
| 14 |
description: |
| 15 |
- podman import imports a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) |
| 16 |
and saves it as a filesystem image. |
| 17 |
options: |
| 18 |
src: |
| 19 |
description: |
| 20 |
- Path to image file to load. |
| 21 |
type: str |
| 22 |
required: true |
| 23 |
commit_message: |
| 24 |
description: |
| 25 |
- Set commit message for imported image |
| 26 |
type: str |
| 27 |
change: |
| 28 |
description: |
| 29 |
- Set changes as list of key-value pairs, see example. |
| 30 |
type: list |
| 31 |
elements: dict |
| 32 |
volume: |
| 33 |
description: |
| 34 |
- Volume to import, cannot be used with change and commit_message |
| 35 |
type: str |
| 36 |
executable: |
| 37 |
description: |
| 38 |
- Path to C(podman) executable if it is not in the C($PATH) on the |
| 39 |
machine running C(podman) |
| 40 |
default: 'podman' |
| 41 |
type: str |
| 42 |
requirements: |
| 43 |
- "Podman installed on host" |
| 44 |
""" |
| 45 |
|
| 46 |
RETURN = """ |
| 47 |
image: |
| 48 |
description: info from loaded image |
| 49 |
returned: always |
| 50 |
type: dict |
| 51 |
sample: { |
| 52 |
"Id": "cbc6d73c4d232db6e8441df96af81855f62c74157b5db80a1d5...", |
| 53 |
"Digest": "sha256:8730c75be86a718929a658db4663d487e562d66762....", |
| 54 |
"RepoTags": [], |
| 55 |
"RepoDigests": [], |
| 56 |
"Parent": "", |
| 57 |
"Comment": "imported from tarball", |
| 58 |
"Created": "2021-09-07T04:45:38.749977105+03:00", |
| 59 |
"Config": {}, |
| 60 |
"Version": "", |
| 61 |
"Author": "", |
| 62 |
"Architecture": "amd64", |
| 63 |
"Os": "linux", |
| 64 |
"Size": 5882449, |
| 65 |
"VirtualSize": 5882449, |
| 66 |
"GraphDriver": { |
| 67 |
"Name": "overlay", |
| 68 |
"Data": { |
| 69 |
"UpperDir": "/home/...34/diff", |
| 70 |
"WorkDir": "/home/.../work" |
| 71 |
} |
| 72 |
}, |
| 73 |
"RootFS": { |
| 74 |
"Type": "layers", |
| 75 |
"Layers": [ |
| 76 |
"sha256:...." |
| 77 |
] |
| 78 |
}, |
| 79 |
"Labels": null, |
| 80 |
"Annotations": {}, |
| 81 |
"ManifestType": "application/vnd.oci.image.manifest.v1+json", |
| 82 |
"User": "", |
| 83 |
"History": [ |
| 84 |
{ |
| 85 |
"created": "2021-09-07T04:45:38.749977105+03:00", |
| 86 |
"created_by": "/bin/sh -c #(nop) ADD file:091... in /", |
| 87 |
"comment": "imported from tarball" |
| 88 |
} |
| 89 |
], |
| 90 |
"NamesHistory": null |
| 91 |
} |
| 92 |
""" |
| 93 |
|
| 94 |
EXAMPLES = """ |
| 95 |
# What modules does for example |
| 96 |
- containers.podman.podman_import: |
| 97 |
src: /path/to/tar/file |
| 98 |
change: |
| 99 |
- "CMD": /bin/bash |
| 100 |
- "User": root |
| 101 |
commit_message: "Importing image" |
| 102 |
- containers.podman.podman_import: |
| 103 |
src: /path/to/tar/file |
| 104 |
volume: myvolume |
| 105 |
""" |
| 106 |
|
| 107 |
import json # noqa: E402 |
| 108 |
from ansible.module_utils.basic import AnsibleModule # noqa: E402 |
| 109 |
|
| 110 |
|
| 111 |
def load(module, executable): |
| 112 |
changed = False |
| 113 |
command = [executable, "import"] |
| 114 |
if module.params["commit_message"]: |
| 115 |
command.extend(["--message", module.params["commit_message"]]) |
| 116 |
if module.params["change"]: |
| 117 |
for change in module.params["change"]: |
| 118 |
command += ["--change", "=".join(list(change.items())[0])] |
| 119 |
command += [module.params["src"]] |
| 120 |
changed = True |
| 121 |
if module.check_mode: |
| 122 |
return changed, "", "", "", command |
| 123 |
rc, out, err = module.run_command(command) |
| 124 |
if rc != 0: |
| 125 |
module.fail_json(msg="Image loading failed: %s" % (err)) |
| 126 |
image_name_line = [i for i in out.splitlines() if "sha256" in i][0] |
| 127 |
image_name = image_name_line.split(":", maxsplit=1)[1].strip() |
| 128 |
rc, out2, err2 = module.run_command([executable, "image", "inspect", image_name]) |
| 129 |
if rc != 0: |
| 130 |
module.fail_json(msg="Image %s inspection failed: %s" % (image_name, err2)) |
| 131 |
try: |
| 132 |
info = json.loads(out2)[0] |
| 133 |
except Exception as e: |
| 134 |
module.fail_json(msg="Could not parse JSON from image %s: %s" % (image_name, e)) |
| 135 |
return changed, out, err, info, command |
| 136 |
|
| 137 |
|
| 138 |
def volume_load(module, executable): |
| 139 |
changed = True |
| 140 |
command = [ |
| 141 |
executable, |
| 142 |
"volume", |
| 143 |
"import", |
| 144 |
module.params["volume"], |
| 145 |
module.params["src"], |
| 146 |
] |
| 147 |
src = module.params["src"] |
| 148 |
if module.check_mode: |
| 149 |
return changed, "", "", "", command |
| 150 |
rc, out, err = module.run_command(command) |
| 151 |
if rc != 0: |
| 152 |
module.fail_json(msg="Error importing volume %s: %s" % (src, err)) |
| 153 |
rc, out2, err2 = module.run_command([executable, "volume", "inspect", module.params["volume"]]) |
| 154 |
if rc != 0: |
| 155 |
module.fail_json(msg="Volume %s inspection failed: %s" % (module.params["volume"], err2)) |
| 156 |
try: |
| 157 |
info = json.loads(out2)[0] |
| 158 |
except Exception as e: |
| 159 |
module.fail_json(msg="Could not parse JSON from volume %s: %s" % (module.params["volume"], e)) |
| 160 |
return changed, out, err, info, command |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
module = AnsibleModule( |
| 165 |
argument_spec=dict( |
| 166 |
src=dict(type="str", required=True), |
| 167 |
commit_message=dict(type="str"), |
| 168 |
change=dict(type="list", elements="dict"), |
| 169 |
executable=dict(type="str", default="podman"), |
| 170 |
volume=dict(type="str", required=False), |
| 171 |
), |
| 172 |
mutually_exclusive=[ |
| 173 |
("volume", "commit_message"), |
| 174 |
("volume", "change"), |
| 175 |
], |
| 176 |
supports_check_mode=True, |
| 177 |
) |
| 178 |
|
| 179 |
executable = module.get_bin_path(module.params["executable"], required=True) |
| 180 |
volume_info = "" |
| 181 |
image_info = "" |
| 182 |
if module.params["volume"]: |
| 183 |
changed, out, err, volume_info, command = volume_load(module, executable) |
| 184 |
else: |
| 185 |
changed, out, err, image_info, command = load(module, executable) |
| 186 |
|
| 187 |
results = { |
| 188 |
"changed": changed, |
| 189 |
"stdout": out, |
| 190 |
"stderr": err, |
| 191 |
"image": image_info, |
| 192 |
"volume": volume_info, |
| 193 |
"podman_command": " ".join(command), |
| 194 |
} |
| 195 |
|
| 196 |
module.exit_json(**results) |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
main() |