| 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_export |
| 12 |
short_description: Export a podman container |
| 13 |
author: Sagi Shnaidman (@sshnaidm) |
| 14 |
description: |
| 15 |
- podman export exports the filesystem of a container and saves it as a |
| 16 |
tarball on the local machine |
| 17 |
options: |
| 18 |
dest: |
| 19 |
description: |
| 20 |
- Path to export container to. |
| 21 |
type: str |
| 22 |
required: true |
| 23 |
container: |
| 24 |
description: |
| 25 |
- Container to export. |
| 26 |
type: str |
| 27 |
volume: |
| 28 |
description: |
| 29 |
- Volume to export. |
| 30 |
type: str |
| 31 |
force: |
| 32 |
description: |
| 33 |
- Force saving to file even if it exists. |
| 34 |
type: bool |
| 35 |
default: True |
| 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 |
""" |
| 48 |
|
| 49 |
EXAMPLES = """ |
| 50 |
# What modules does for example |
| 51 |
- containers.podman.podman_export: |
| 52 |
dest: /path/to/tar/file |
| 53 |
container: container-name |
| 54 |
- containers.podman.podman_export: |
| 55 |
dest: /path/to/tar/file |
| 56 |
volume: volume-name |
| 57 |
""" |
| 58 |
|
| 59 |
import os # noqa: E402 |
| 60 |
from ansible.module_utils.basic import AnsibleModule # noqa: E402 |
| 61 |
from ..module_utils.podman.common import remove_file_or_dir # noqa: E402 |
| 62 |
|
| 63 |
|
| 64 |
def export(module, executable): |
| 65 |
changed = False |
| 66 |
export_type = "" |
| 67 |
command = [] |
| 68 |
if module.params["container"]: |
| 69 |
export_type = "container" |
| 70 |
command = [executable, "export"] |
| 71 |
else: |
| 72 |
export_type = "volume" |
| 73 |
command = [executable, "volume", "export"] |
| 74 |
|
| 75 |
command += ["-o=%s" % module.params["dest"], module.params[export_type]] |
| 76 |
if module.params["force"]: |
| 77 |
dest = module.params["dest"] |
| 78 |
if os.path.exists(dest): |
| 79 |
changed = True |
| 80 |
if module.check_mode: |
| 81 |
return changed, "", "" |
| 82 |
try: |
| 83 |
remove_file_or_dir(dest) |
| 84 |
except Exception as e: |
| 85 |
module.fail_json(msg="Error deleting %s path: %s" % (dest, e)) |
| 86 |
else: |
| 87 |
changed = not os.path.exists(module.params["dest"]) |
| 88 |
if module.check_mode: |
| 89 |
return changed, "", "" |
| 90 |
rc, out, err = module.run_command(command) |
| 91 |
if rc != 0: |
| 92 |
module.fail_json(msg="Error exporting %s %s: %s" % (export_type, module.params["container"], err)) |
| 93 |
return changed, out, err |
| 94 |
|
| 95 |
|
| 96 |
def main(): |
| 97 |
module = AnsibleModule( |
| 98 |
argument_spec=dict( |
| 99 |
dest=dict(type="str", required=True), |
| 100 |
container=dict(type="str"), |
| 101 |
volume=dict(type="str"), |
| 102 |
force=dict(type="bool", default=True), |
| 103 |
executable=dict(type="str", default="podman"), |
| 104 |
), |
| 105 |
supports_check_mode=True, |
| 106 |
mutually_exclusive=[ |
| 107 |
("container", "volume"), |
| 108 |
], |
| 109 |
required_one_of=[ |
| 110 |
("container", "volume"), |
| 111 |
], |
| 112 |
) |
| 113 |
|
| 114 |
executable = module.get_bin_path(module.params["executable"], required=True) |
| 115 |
changed, out, err = export(module, executable) |
| 116 |
|
| 117 |
results = { |
| 118 |
"changed": changed, |
| 119 |
"stdout": out, |
| 120 |
"stderr": err, |
| 121 |
} |
| 122 |
module.exit_json(**results) |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
main() |