| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2021, Alexei Znamensky (@russoz) <[email protected]> |
| 4 |
# Copyright (c) 2017, Kairo Araujo <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
author: Kairo Araujo (@kairoaraujo) |
| 12 |
module: mksysb |
| 13 |
short_description: Generates AIX mksysb rootvg backups |
| 14 |
description: |
| 15 |
- This module manages a basic AIX mksysb (image) of rootvg. |
| 16 |
seealso: |
| 17 |
- name: C(mksysb) command manual page |
| 18 |
description: Manual page for the command. |
| 19 |
link: https://www.ibm.com/docs/en/aix/7.3?topic=m-mksysb-command |
| 20 |
|
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
backup_crypt_files: |
| 30 |
description: |
| 31 |
- Backup encrypted files. |
| 32 |
type: bool |
| 33 |
default: true |
| 34 |
backup_dmapi_fs: |
| 35 |
description: |
| 36 |
- Back up DMAPI filesystem files. |
| 37 |
type: bool |
| 38 |
default: true |
| 39 |
create_map_files: |
| 40 |
description: |
| 41 |
- Creates a new MAP files. |
| 42 |
type: bool |
| 43 |
default: false |
| 44 |
exclude_files: |
| 45 |
description: |
| 46 |
- Excludes files using C(/etc/rootvg.exclude). |
| 47 |
type: bool |
| 48 |
default: false |
| 49 |
exclude_wpar_files: |
| 50 |
description: |
| 51 |
- Excludes WPAR files. |
| 52 |
type: bool |
| 53 |
default: false |
| 54 |
extended_attrs: |
| 55 |
description: |
| 56 |
- Backup extended attributes. |
| 57 |
type: bool |
| 58 |
default: true |
| 59 |
name: |
| 60 |
type: str |
| 61 |
description: |
| 62 |
- Backup name. |
| 63 |
required: true |
| 64 |
new_image_data: |
| 65 |
description: |
| 66 |
- Creates a new file data. |
| 67 |
type: bool |
| 68 |
default: true |
| 69 |
software_packing: |
| 70 |
description: |
| 71 |
- Exclude files from packing option listed in C(/etc/exclude_packing.rootvg). |
| 72 |
type: bool |
| 73 |
default: false |
| 74 |
storage_path: |
| 75 |
type: str |
| 76 |
description: |
| 77 |
- Storage path where the mksysb backup is stored. |
| 78 |
required: true |
| 79 |
use_snapshot: |
| 80 |
description: |
| 81 |
- Creates backup using snapshots. |
| 82 |
type: bool |
| 83 |
default: false |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
- name: Running a backup image mksysb |
| 88 |
community.general.mksysb: |
| 89 |
name: myserver |
| 90 |
storage_path: /repository/images |
| 91 |
exclude_files: true |
| 92 |
exclude_wpar_files: true |
| 93 |
""" |
| 94 |
|
| 95 |
RETURN = r""" |
| 96 |
msg: |
| 97 |
description: Return message regarding the action. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
""" |
| 101 |
|
| 102 |
import os |
| 103 |
|
| 104 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 105 |
from ansible_collections.community.general.plugins.module_utils._module_helper import ModuleHelper |
| 106 |
|
| 107 |
|
| 108 |
class MkSysB(ModuleHelper): |
| 109 |
module = dict( |
| 110 |
argument_spec=dict( |
| 111 |
backup_crypt_files=dict(type="bool", default=True), |
| 112 |
backup_dmapi_fs=dict(type="bool", default=True), |
| 113 |
create_map_files=dict(type="bool", default=False), |
| 114 |
exclude_files=dict(type="bool", default=False), |
| 115 |
exclude_wpar_files=dict(type="bool", default=False), |
| 116 |
extended_attrs=dict(type="bool", default=True), |
| 117 |
name=dict(type="str", required=True), |
| 118 |
new_image_data=dict(type="bool", default=True), |
| 119 |
software_packing=dict(type="bool", default=False), |
| 120 |
storage_path=dict(type="str", required=True), |
| 121 |
use_snapshot=dict(type="bool", default=False), |
| 122 |
), |
| 123 |
supports_check_mode=True, |
| 124 |
) |
| 125 |
command_args_formats = dict( |
| 126 |
create_map_files=cmd_runner_fmt.as_bool("-m"), |
| 127 |
use_snapshot=cmd_runner_fmt.as_bool("-T"), |
| 128 |
exclude_files=cmd_runner_fmt.as_bool("-e"), |
| 129 |
exclude_wpar_files=cmd_runner_fmt.as_bool("-G"), |
| 130 |
new_image_data=cmd_runner_fmt.as_bool("-i"), |
| 131 |
software_packing=cmd_runner_fmt.as_bool_not("-p"), |
| 132 |
extended_attrs=cmd_runner_fmt.as_bool("-a"), |
| 133 |
backup_crypt_files=cmd_runner_fmt.as_bool_not("-Z"), |
| 134 |
backup_dmapi_fs=cmd_runner_fmt.as_bool("-A"), |
| 135 |
combined_path=cmd_runner_fmt.as_func(cmd_runner_fmt.unpack_args(lambda p, n: [f"{p}/{n}"])), |
| 136 |
) |
| 137 |
|
| 138 |
def __init_module__(self): |
| 139 |
if not os.path.isdir(self.vars.storage_path): |
| 140 |
self.do_raise(f"Storage path {self.vars.storage_path} is not valid.") |
| 141 |
|
| 142 |
def __run__(self): |
| 143 |
def process(rc, out, err): |
| 144 |
if rc != 0: |
| 145 |
self.do_raise(f"mksysb failed: {out}") |
| 146 |
|
| 147 |
runner = CmdRunner( |
| 148 |
self.module, |
| 149 |
["mksysb", "-X"], |
| 150 |
self.command_args_formats, |
| 151 |
) |
| 152 |
with runner( |
| 153 |
[ |
| 154 |
"create_map_files", |
| 155 |
"use_snapshot", |
| 156 |
"exclude_files", |
| 157 |
"exclude_wpar_files", |
| 158 |
"software_packing", |
| 159 |
"extended_attrs", |
| 160 |
"backup_crypt_files", |
| 161 |
"backup_dmapi_fs", |
| 162 |
"new_image_data", |
| 163 |
"combined_path", |
| 164 |
], |
| 165 |
output_process=process, |
| 166 |
check_mode_skip=True, |
| 167 |
) as ctx: |
| 168 |
ctx.run(combined_path=[self.vars.storage_path, self.vars.name]) |
| 169 |
if self.verbosity >= 4: |
| 170 |
self.vars.run_info = ctx.run_info |
| 171 |
|
| 172 |
self.changed = True |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
MkSysB.execute() |
| 177 |
|
| 178 |
|
| 179 |
if __name__ == "__main__": |
| 180 |
main() |