Skip to content

Commit

Permalink
mksysb: using CmdRunner (#5484)
Browse files Browse the repository at this point in the history
* mksysb: using CmdRunner

* add changelog fragment

* adjust code when check_mode true

* Update plugins/modules/mksysb.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
russoz and felixfontein authored Nov 7, 2022
1 parent c757e20 commit 8758f6a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5484-mksysb-cmd-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- mksysb - refactored module to use ``CmdRunner`` to execute ``mksysb`` (https://github.com/ansible-collections/community.general/pull/5484).
56 changes: 31 additions & 25 deletions plugins/modules/mksysb.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@

import os

from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper

from ansible_collections.community.general.plugins.module_utils.module_helper import (
CmdModuleHelper, ArgFormat
ArgFormat
)


class MkSysB(CmdModuleHelper):
class MkSysB(ModuleHelper):
module = dict(
argument_spec=dict(
backup_crypt_files=dict(type='bool', default=True),
Expand All @@ -120,37 +123,40 @@ class MkSysB(CmdModuleHelper):
),
supports_check_mode=True,
)
command = ['mksysb', '-X']
command_args_formats = dict(
create_map_files=dict(fmt="-m", style=ArgFormat.BOOLEAN),
use_snapshot=dict(fmt="-T", style=ArgFormat.BOOLEAN),
exclude_files=dict(fmt="-e", style=ArgFormat.BOOLEAN),
exclude_wpar_files=dict(fmt="-G", style=ArgFormat.BOOLEAN),
new_image_data=dict(fmt="-i", style=ArgFormat.BOOLEAN),
software_packing=dict(fmt="-p", style=ArgFormat.BOOLEAN_NOT),
extended_attrs=dict(fmt="-a", style=ArgFormat.BOOLEAN),
backup_crypt_files=dict(fmt="-Z", style=ArgFormat.BOOLEAN_NOT),
backup_dmapi_fs=dict(fmt="-A", style=ArgFormat.BOOLEAN),
combined_path=dict(fmt=lambda p, n: ["%s/%s" % (p, n)], stars=1)
create_map_files=cmd_runner_fmt.as_bool("-m"),
use_snapshot=cmd_runner_fmt.as_bool("-T"),
exclude_files=cmd_runner_fmt.as_bool("-e"),
exclude_wpar_files=cmd_runner_fmt.as_bool("-G"),
new_image_data=cmd_runner_fmt.as_bool("-i"),
software_packing=cmd_runner_fmt.as_bool_not("-p"),
extended_attrs=cmd_runner_fmt.as_bool("-a"),
backup_crypt_files=cmd_runner_fmt.as_bool_not("-Z"),
backup_dmapi_fs=cmd_runner_fmt.as_bool("-A"),
combined_path=cmd_runner_fmt.as_func(cmd_runner_fmt.unpack_args(lambda p, n: ["%s/%s" % (p, n)])),
)

def __init_module__(self):
if not os.path.isdir(self.vars.storage_path):
self.do_raise("Storage path %s is not valid." % self.vars.storage_path)

def __run__(self):
if not self.module.check_mode:
self.run_command(params=[
'create_map_files', 'use_snapshot', 'exclude_files', 'exclude_wpar_files', 'software_packing',
'extended_attrs', 'backup_crypt_files', 'backup_dmapi_fs', 'new_image_data',
{'combined_path': [self.vars.storage_path, self.vars.name]},
])
self._changed = True

def process_command_output(self, rc, out, err):
if rc != 0:
self.do_raise("mksysb failed.")
self.vars.msg = out
def process(rc, out, err):
if rc != 0:
self.do_raise("mksysb failed.")
self.vars.msg = out

runner = CmdRunner(
self.module,
['mksysb', '-X'],
self.command_args_formats,
)
with runner(['create_map_files', 'use_snapshot', 'exclude_files', 'exclude_wpar_files', 'software_packing',
'extended_attrs', 'backup_crypt_files', 'backup_dmapi_fs', 'new_image_data', 'combined_path'],
output_process=process, check_mode_skip=True) as ctx:
ctx.run(combined_path=[self.vars.storage_path, self.vars.name])

self.changed = True


def main():
Expand Down

0 comments on commit 8758f6a

Please sign in to comment.