| 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_load |
| 12 |
short_description: Load image from a tar file. |
| 13 |
author: Sagi Shnaidman (@sshnaidm) |
| 14 |
description: |
| 15 |
- podman load loads an image from either an oci-archive or a docker-archive stored |
| 16 |
on the local machine into container storage. |
| 17 |
podman load is used for loading from the archive generated by podman save, |
| 18 |
that includes the image parent layers. |
| 19 |
options: |
| 20 |
input: |
| 21 |
description: |
| 22 |
- Path to image file to load. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
aliases: |
| 26 |
- path |
| 27 |
executable: |
| 28 |
description: |
| 29 |
- Path to C(podman) executable if it is not in the C($PATH) on the |
| 30 |
machine running C(podman) |
| 31 |
default: 'podman' |
| 32 |
type: str |
| 33 |
requirements: |
| 34 |
- "Podman installed on host" |
| 35 |
""" |
| 36 |
|
| 37 |
RETURN = """ |
| 38 |
image: |
| 39 |
description: info from loaded image |
| 40 |
returned: always |
| 41 |
type: dict |
| 42 |
sample: [ |
| 43 |
{ |
| 44 |
"Annotations": {}, |
| 45 |
"Architecture": "amd64", |
| 46 |
"Author": "", |
| 47 |
"Comment": "from Bitnami with love", |
| 48 |
"ContainerConfig": { |
| 49 |
"Cmd": [ |
| 50 |
"nami", |
| 51 |
"start", |
| 52 |
"--foreground", |
| 53 |
"wildfly" |
| 54 |
], |
| 55 |
"Entrypoint": [ |
| 56 |
"/app-entrypoint.sh" |
| 57 |
], |
| 58 |
"Env": [ |
| 59 |
"PATH=/opt/bitnami/java/bin:/opt/bitnami/wildfly/bin:/opt/bitnami/nami/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", |
| 60 |
"IMAGE_OS=debian-9", |
| 61 |
"NAMI_VERSION=0.0.9-0", |
| 62 |
"GPG_KEY_SERVERS_LIST=ha.pool.sks-keyservers.net \ |
| 63 |
hkp://p80.pool.sks-keyservers.net:80 keyserver.ubuntu.com hkp://keyserver.ubuntu.com:80 pgp.mit.edu", |
| 64 |
"TINI_VERSION=v0.13.2", |
| 65 |
"TINI_GPG_KEY=595E85A6B1B4779EA4DAAEC70B588DFF0527A9B7", |
| 66 |
"GOSU_VERSION=1.10", |
| 67 |
"GOSU_GPG_KEY=B42F6819007F00F88E364FD4036A9C25BF357DD4", |
| 68 |
"BITNAMI_IMAGE_VERSION=14.0.1-debian-9-r12", |
| 69 |
"BITNAMI_APP_NAME=wildfly", |
| 70 |
"WILDFLY_JAVA_HOME=", |
| 71 |
"WILDFLY_JAVA_OPTS=", |
| 72 |
"WILDFLY_MANAGEMENT_HTTP_PORT_NUMBER=9990", |
| 73 |
"WILDFLY_PASSWORD=bitnami", |
| 74 |
"WILDFLY_PUBLIC_CONSOLE=true", |
| 75 |
"WILDFLY_SERVER_AJP_PORT_NUMBER=8009", |
| 76 |
"WILDFLY_SERVER_HTTP_PORT_NUMBER=8080", |
| 77 |
"WILDFLY_SERVER_INTERFACE=0.0.0.0", |
| 78 |
"WILDFLY_USERNAME=user", |
| 79 |
"WILDFLY_WILDFLY_HOME=/home/wildfly", |
| 80 |
"WILDFLY_WILDFLY_OPTS=-Dwildfly.as.deployment.ondemand=false" |
| 81 |
], |
| 82 |
"ExposedPorts": { |
| 83 |
"8080/tcp": {}, |
| 84 |
"9990/tcp": {} |
| 85 |
}, |
| 86 |
"Labels": { |
| 87 |
"maintainer": "Bitnami <[email protected]>" |
| 88 |
} |
| 89 |
}, |
| 90 |
"Created": "2018-09-25T04:07:45.934395523Z", |
| 91 |
"Digest": "sha256:5c7d8e2dd66dcf4a152a4032a1d3c5a33458c67e1c1335edd8d18d738892356b", |
| 92 |
"GraphDriver": { |
| 93 |
"Data": { |
| 94 |
"LowerDir": "/var/lib/containers/storage/overlay/a9dbf5616cc16919a8ac0dfc60aff87a72b5be52994c4649fcc91a089a12931\ |
| 95 |
f/diff:/var/lib/containers/storage/overlay/67129bd46022122a7d8b7acb490092af6c7ce244ce4fbd7d9e2d2b7f5979e090/diff:/var/lib/containers/storage/overlay/7c51242c\ |
| 96 |
4c5db5c74afda76d7fdbeab6965d8b21804bb3fc597dee09c770b0ca/diff:/var/lib/containers/storage/overlay/f97315dc58a9c002ba0cabccb9933d4b0d2113733d204188c88d72f75569b57b/diff:/var/lib/containers/storage/overlay/1dbde2dd497ddde2b467727125b900958a051a72561e58d29abe3d660dcaa9a7/diff:/var/lib/containers/storage/overlay/4aad9d80f30c3f0608f58173558b7554d84dee4dc4479672926eca29f75e6e33/diff:/var/lib/containers/storage/overlay/6751fc9b6868254870c062d75a511543fc8cfda2ce6262f4945f107449219632/diff:/var/lib/containers/storage/overlay/a27034d79081347421dd24d7e9e776c18271cd9a6e51053cb39af4d3d9c400e8/diff:/var/lib/containers/storage/overlay/537cf0045ed9cd7989f7944e7393019c81b16c1799a2198d8348cd182665397f/diff:/var/lib/containers/storage/overlay/27578615c5ae352af4e8449862d61aaf5c11b105a7d5905af55bd01b0c656d6e/diff:/var/lib/containers/storage/overlay/566542742840fe3034b3596f7cb9e62a6274c95a69f368f9e713746f8712c0b6/diff", |
| 97 |
"MergedDir": "/var/lib/containers/storage/overlay/72bb96d6\ |
| 98 |
c53ad57a0b1e44cab226a6251598accbead40b23fac89c19ad8c25ca/merged", |
| 99 |
"UpperDir": "/var/lib/containers/storage/overlay/72bb96d6c53ad57a0b1e44cab226a6251598accbead40b23fac89c19ad8c25ca/diff", |
| 100 |
"WorkDir": "/var/lib/containers/storage/overlay/72bb96d6c53ad57a0b1e44cab226a6251598accbead40b23fac89c19ad8c25ca/work" |
| 101 |
}, |
| 102 |
"Name": "overlay" |
| 103 |
}, |
| 104 |
"Id": "bcacbdf7a119c0fa934661ca8af839e625ce6540d9ceb6827cdd389f823d49e0", |
| 105 |
"Labels": { |
| 106 |
"maintainer": "Bitnami <[email protected]>" |
| 107 |
}, |
| 108 |
"ManifestType": "application/vnd.docker.distribution.manifest.v1+prettyjws", |
| 109 |
"Os": "linux", |
| 110 |
"Parent": "", |
| 111 |
"RepoDigests": [ |
| 112 |
"quay.io/bitnami/wildfly@sha256:5c7d8e2dd66dcf4a152a4032a1d3c5a33458c67e1c1335edd8d18d738892356b" |
| 113 |
], |
| 114 |
"RepoTags": [ |
| 115 |
"quay.io/bitnami/wildfly:latest" |
| 116 |
], |
| 117 |
"RootFS": { |
| 118 |
"Layers": [ |
| 119 |
"sha256:75391df2c87e076b0c2f72d20c95c57dc8be7ee684cc07273416cce622b43367", |
| 120 |
"sha256:7dd303f041039bfe8f0833092673ac35f93137d10e0fbc4302021ea65ad57731", |
| 121 |
"sha256:720d9edf0cd2a9bb56b88b80be9070dbfaad359514c70094c65066963fed485d", |
| 122 |
"sha256:6a567ecbf97725501a634fcb486271999aa4591b633b4ae9932a46b40f5aaf47", |
| 123 |
"sha256:59e9a6db8f178f3da868614564faabb2820cdfb69be32e63a4405d6f7772f68c", |
| 124 |
"sha256:310a82ccb092cd650215ab375da8943d235a263af9a029b8ac26a281446c04db", |
| 125 |
"sha256:36cb91cf4513543a8f0953fed785747ea18b675bc2677f3839889cfca0aac79e" |
| 126 |
], |
| 127 |
"Type": "layers" |
| 128 |
}, |
| 129 |
"Size": 569919342, |
| 130 |
"User": "", |
| 131 |
"Version": "17.06.0-ce", |
| 132 |
"VirtualSize": 569919342 |
| 133 |
} |
| 134 |
] |
| 135 |
""" |
| 136 |
|
| 137 |
EXAMPLES = """ |
| 138 |
# What modules does for example |
| 139 |
- containers.podman.podman_load: |
| 140 |
input: /path/to/tar/file |
| 141 |
""" |
| 142 |
|
| 143 |
import json # noqa: E402 |
| 144 |
from ansible.module_utils.basic import AnsibleModule # noqa: E402 |
| 145 |
|
| 146 |
|
| 147 |
def load(module, executable): |
| 148 |
changed = False |
| 149 |
command = [executable, "load", "--input"] |
| 150 |
command.append(module.params["input"]) |
| 151 |
changed = True |
| 152 |
if module.check_mode: |
| 153 |
return changed, "", "", "" |
| 154 |
rc, out, err = module.run_command(command) |
| 155 |
if rc != 0: |
| 156 |
module.fail_json(msg="Image loading failed: %s" % (err)) |
| 157 |
image_name_line = [i for i in out.splitlines() if "Loaded image" in i][0] |
| 158 |
# For Podman < 4.x |
| 159 |
if "Loaded image(s):" in image_name_line: |
| 160 |
image_name = image_name_line.split("Loaded image(s): ")[1].split(",")[0].strip() |
| 161 |
# For Podman > 4.x |
| 162 |
elif "Loaded image:" in image_name_line: |
| 163 |
image_name = image_name_line.split("Loaded image: ")[1].strip() |
| 164 |
else: |
| 165 |
module.fail_json(msg="Not found images in %s" % image_name_line) |
| 166 |
rc, out2, err2 = module.run_command([executable, "image", "inspect", image_name]) |
| 167 |
if rc != 0: |
| 168 |
module.fail_json(msg="Image %s inspection failed: %s" % (image_name, err2)) |
| 169 |
try: |
| 170 |
info = json.loads(out2)[0] |
| 171 |
except Exception as e: |
| 172 |
module.fail_json(msg="Could not parse JSON from image %s: %s" % (image_name, e)) |
| 173 |
return changed, out, err, info |
| 174 |
|
| 175 |
|
| 176 |
def main(): |
| 177 |
module = AnsibleModule( |
| 178 |
argument_spec=dict( |
| 179 |
input=dict(type="str", required=True, aliases=["path"]), |
| 180 |
executable=dict(type="str", default="podman"), |
| 181 |
), |
| 182 |
supports_check_mode=True, |
| 183 |
) |
| 184 |
|
| 185 |
executable = module.get_bin_path(module.params["executable"], required=True) |
| 186 |
changed, out, err, image_info = load(module, executable) |
| 187 |
|
| 188 |
results = { |
| 189 |
"changed": changed, |
| 190 |
"stdout": out, |
| 191 |
"stderr": err, |
| 192 |
"image": image_info, |
| 193 |
} |
| 194 |
|
| 195 |
module.exit_json(**results) |
| 196 |
|
| 197 |
|
| 198 |
if __name__ == "__main__": |
| 199 |
main() |