mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
lxc_container: use LVM runners from _lvm module utils (#11920)
* lxc_container: use LVM runners from _lvm module utils Replace direct run_command calls for lvs, vgdisplay, lvdisplay, lvcreate, and lvremove with the shared CmdRunner-based runners from module_utils/_lvm.py. Switches from human-readable text parsing to machine-readable --noheadings/--nosuffix/--separator output. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * lxc_container: add changelog fragment for PR 11920 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * generate run_info information for commands --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
minor_changes:
|
||||||
|
- lxc_container - use shared LVM runners from ``_lvm`` module utils instead of direct ``run_command`` calls for LVM2 commands (https://github.com/ansible-collections/community.general/pull/11920).
|
||||||
@@ -179,7 +179,7 @@ def pvmove_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
|||||||
def vgs_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
def vgs_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
||||||
"""
|
"""
|
||||||
Runner for C(vgs). Used by: community.general.lvol, community.general.lvg,
|
Runner for C(vgs). Used by: community.general.lvol, community.general.lvg,
|
||||||
community.general.lvg_rename.
|
community.general.lvg_rename, community.general.lxc_container.
|
||||||
|
|
||||||
Suggested arg_formats keys: noheadings nosuffix readonly units separator fields select vg
|
Suggested arg_formats keys: noheadings nosuffix readonly units separator fields select vg
|
||||||
"""
|
"""
|
||||||
@@ -341,7 +341,7 @@ def vgrename_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
|||||||
|
|
||||||
def lvs_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
def lvs_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
||||||
"""
|
"""
|
||||||
Runner for C(lvs). Used by: community.general.lvol.
|
Runner for C(lvs). Used by: community.general.lvol, community.general.lxc_container.
|
||||||
|
|
||||||
Suggested arg_formats keys: all noheadings nosuffix units separator fields select vg
|
Suggested arg_formats keys: all noheadings nosuffix units separator fields select vg
|
||||||
|
|
||||||
|
|||||||
@@ -422,6 +422,12 @@ from ansible.module_utils.basic import AnsibleModule
|
|||||||
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
||||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE
|
from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE
|
||||||
|
|
||||||
|
from ansible_collections.community.general.plugins.module_utils._lvm import (
|
||||||
|
lvcreate_runner,
|
||||||
|
lvremove_runner,
|
||||||
|
lvs_runner,
|
||||||
|
vgs_runner,
|
||||||
|
)
|
||||||
from ansible_collections.community.general.plugins.module_utils._lxc import create_script
|
from ansible_collections.community.general.plugins.module_utils._lxc import create_script
|
||||||
|
|
||||||
# LXC_COMPRESSION_MAP is a map of available compression types when creating
|
# LXC_COMPRESSION_MAP is a map of available compression types when creating
|
||||||
@@ -519,6 +525,7 @@ class LxcContainerManagement:
|
|||||||
self.container = self.get_container_bind()
|
self.container = self.get_container_bind()
|
||||||
self.archive_info = None
|
self.archive_info = None
|
||||||
self.clone_info = None
|
self.clone_info = None
|
||||||
|
self.run_info = []
|
||||||
|
|
||||||
def get_container_bind(self):
|
def get_container_bind(self):
|
||||||
return lxc.Container(name=self.container_name)
|
return lxc.Container(name=self.container_name)
|
||||||
@@ -1045,13 +1052,13 @@ class LxcContainerManagement:
|
|||||||
"""Return a list of all lv in a current vg."""
|
"""Return a list of all lv in a current vg."""
|
||||||
|
|
||||||
vg = self._get_lxc_vg()
|
vg = self._get_lxc_vg()
|
||||||
build_command = [self.module.get_bin_path("lvs", True)]
|
with lvs_runner(self.module)("noheadings separator fields vg") as ctx:
|
||||||
rc, stdout, err = self.module.run_command(build_command)
|
rc, stdout, err = ctx.run(separator=";", fields="lv_name", vg=[vg])
|
||||||
|
if self.module._verbosity >= 4:
|
||||||
|
self.run_info.append(ctx.run_info)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
self.failure(err=err, rc=rc, msg="Failed to get list of LVs", command=" ".join(build_command))
|
self.failure(err=err, rc=rc, msg="Failed to get list of LVs")
|
||||||
|
return [line.strip() for line in stdout.splitlines() if line.strip()]
|
||||||
all_lvms = [i.split() for i in stdout.splitlines()][1:]
|
|
||||||
return [lv_entry[0] for lv_entry in all_lvms if lv_entry[1] == vg]
|
|
||||||
|
|
||||||
def _get_vg_free_pe(self, vg_name):
|
def _get_vg_free_pe(self, vg_name):
|
||||||
"""Return the available size of a given VG.
|
"""Return the available size of a given VG.
|
||||||
@@ -1062,15 +1069,13 @@ class LxcContainerManagement:
|
|||||||
:type: ``tuple``
|
:type: ``tuple``
|
||||||
"""
|
"""
|
||||||
|
|
||||||
build_command = ["vgdisplay", vg_name, "--units", "g"]
|
with vgs_runner(self.module)("noheadings nosuffix units separator fields vg") as ctx:
|
||||||
rc, stdout, err = self.module.run_command(build_command)
|
rc, stdout, err = ctx.run(units="g", separator=";", fields="vg_free", vg=[vg_name])
|
||||||
|
if self.module._verbosity >= 4:
|
||||||
|
self.run_info.append(ctx.run_info)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
self.failure(err=err, rc=rc, msg=f"failed to read vg {vg_name}", command=" ".join(build_command))
|
self.failure(err=err, rc=rc, msg=f"failed to read vg {vg_name}")
|
||||||
|
return float(stdout.strip()), "g"
|
||||||
vg_info = [i.strip() for i in stdout.splitlines()][1:]
|
|
||||||
free_pe = [i for i in vg_info if i.startswith("Free")]
|
|
||||||
_free_pe = free_pe[0].split()
|
|
||||||
return float(_free_pe[-2]), _free_pe[-1]
|
|
||||||
|
|
||||||
def _get_lv_size(self, lv_name):
|
def _get_lv_size(self, lv_name):
|
||||||
"""Return the available size of a given LV.
|
"""Return the available size of a given LV.
|
||||||
@@ -1083,15 +1088,13 @@ class LxcContainerManagement:
|
|||||||
|
|
||||||
vg = self._get_lxc_vg()
|
vg = self._get_lxc_vg()
|
||||||
lv = os.path.join(vg, lv_name)
|
lv = os.path.join(vg, lv_name)
|
||||||
build_command = ["lvdisplay", lv, "--units", "g"]
|
with lvs_runner(self.module)("noheadings nosuffix units separator fields vg") as ctx:
|
||||||
rc, stdout, err = self.module.run_command(build_command)
|
rc, stdout, err = ctx.run(units="g", separator=";", fields="lv_size", vg=[lv])
|
||||||
|
if self.module._verbosity >= 4:
|
||||||
|
self.run_info.append(ctx.run_info)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
self.failure(err=err, rc=rc, msg=f"failed to read lv {lv}", command=" ".join(build_command))
|
self.failure(err=err, rc=rc, msg=f"failed to read lv {lv}")
|
||||||
|
return self._roundup(float(stdout.strip())), "g"
|
||||||
lv_info = [i.strip() for i in stdout.splitlines()][1:]
|
|
||||||
_free_pe = [i for i in lv_info if i.startswith("LV Size")]
|
|
||||||
free_pe = _free_pe[0].split()
|
|
||||||
return self._roundup(float(free_pe[-2])), free_pe[-1]
|
|
||||||
|
|
||||||
def _lvm_snapshot_create(self, source_lv, snapshot_name, snapshot_size_gb=5):
|
def _lvm_snapshot_create(self, source_lv, snapshot_name, snapshot_size_gb=5):
|
||||||
"""Create an LVM snapshot.
|
"""Create an LVM snapshot.
|
||||||
@@ -1113,16 +1116,15 @@ class LxcContainerManagement:
|
|||||||
)
|
)
|
||||||
self.failure(error="Not enough space to create snapshot", rc=2, msg=message)
|
self.failure(error="Not enough space to create snapshot", rc=2, msg=message)
|
||||||
|
|
||||||
# Create LVM Snapshot
|
with lvcreate_runner(self.module)("size_L is_snapshot lv vg") as ctx:
|
||||||
build_command = [
|
rc, dummy, err = ctx.run(
|
||||||
self.module.get_bin_path("lvcreate", True),
|
size_L=f"{snapshot_size_gb}g",
|
||||||
"-n",
|
is_snapshot=True,
|
||||||
snapshot_name,
|
lv=[snapshot_name],
|
||||||
"-s",
|
vg=[os.path.join(vg, source_lv)],
|
||||||
os.path.join(vg, source_lv),
|
)
|
||||||
f"-L{snapshot_size_gb}g",
|
if self.module._verbosity >= 4:
|
||||||
]
|
self.run_info.append(ctx.run_info)
|
||||||
rc, stdout, err = self.module.run_command(build_command)
|
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
self.failure(err=err, rc=rc, msg=f"Failed to Create LVM snapshot {vg}/{source_lv} --> {snapshot_name}")
|
self.failure(err=err, rc=rc, msg=f"Failed to Create LVM snapshot {vg}/{source_lv} --> {snapshot_name}")
|
||||||
|
|
||||||
@@ -1190,14 +1192,12 @@ class LxcContainerManagement:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
vg = self._get_lxc_vg()
|
vg = self._get_lxc_vg()
|
||||||
build_command = [
|
with lvremove_runner(self.module)("force lv") as ctx:
|
||||||
self.module.get_bin_path("lvremove", True),
|
rc, dummy, err = ctx.run(force=True, lv=[f"{vg}/{lv_name}"])
|
||||||
"-f",
|
if self.module._verbosity >= 4:
|
||||||
f"{vg}/{lv_name}",
|
self.run_info.append(ctx.run_info)
|
||||||
]
|
|
||||||
rc, stdout, err = self.module.run_command(build_command)
|
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
self.failure(err=err, rc=rc, msg=f"Failed to remove LVM LV {vg}/{lv_name}", command=" ".join(build_command))
|
self.failure(err=err, rc=rc, msg=f"Failed to remove LVM LV {vg}/{lv_name}")
|
||||||
|
|
||||||
def _rsync_data(self, container_path, temp_dir):
|
def _rsync_data(self, container_path, temp_dir):
|
||||||
"""Sync the container directory to the temp directory.
|
"""Sync the container directory to the temp directory.
|
||||||
@@ -1375,6 +1375,8 @@ class LxcContainerManagement:
|
|||||||
:param rc: ``int`` Return code while executing an Ansible command.
|
:param rc: ``int`` Return code while executing an Ansible command.
|
||||||
:param msg: ``str`` Message to report.
|
:param msg: ``str`` Message to report.
|
||||||
"""
|
"""
|
||||||
|
if self.run_info:
|
||||||
|
kwargs["run_info"] = self.run_info
|
||||||
|
|
||||||
self.module.fail_json(**kwargs)
|
self.module.fail_json(**kwargs)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user