Convert some run_command() string args to lists (#8264)

* Convert some run_command() string args to lists.

* Change run_command with pipe and shell to Python code.

* Add changelog.

* Simplify syntax.

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

---------

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
Felix Fontein 2024-04-29 22:57:08 +02:00 committed by GitHub
commit 70adba8991
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 144 additions and 144 deletions

View file

@ -236,6 +236,7 @@ EXAMPLES = '''
'''
import re
import shlex
from ansible.module_utils.basic import AnsibleModule
@ -281,7 +282,7 @@ def parse_vgs(data):
def get_lvm_version(module):
ver_cmd = module.get_bin_path("lvm", required=True)
rc, out, err = module.run_command("%s version" % (ver_cmd))
rc, out, err = module.run_command([ver_cmd, "version"])
if rc != 0:
return None
m = re.search(r"LVM version:\s+(\d+)\.(\d+)\.(\d+).*(\d{4}-\d{2}-\d{2})", out)
@ -320,14 +321,14 @@ def main():
module.fail_json(msg="Failed to get LVM version number")
version_yesopt = mkversion(2, 2, 99) # First LVM with the "--yes" option
if version_found >= version_yesopt:
yesopt = "--yes"
yesopt = ["--yes"]
else:
yesopt = ""
yesopt = []
vg = module.params['vg']
lv = module.params['lv']
size = module.params['size']
opts = module.params['opts']
opts = shlex.split(module.params['opts'] or '')
state = module.params['state']
force = module.boolean(module.params['force'])
shrink = module.boolean(module.params['shrink'])
@ -338,21 +339,13 @@ def main():
size_unit = 'm'
size_operator = None
snapshot = module.params['snapshot']
pvs = module.params['pvs']
if pvs is None:
pvs = ""
else:
pvs = " ".join(pvs)
if opts is None:
opts = ""
pvs = module.params['pvs'] or []
# Add --test option when running in check-mode
if module.check_mode:
test_opt = ' --test'
test_opt = ['--test']
else:
test_opt = ''
test_opt = []
if size:
# LVEXTEND(8)/LVREDUCE(8) -l, -L options: Check for relative value for resizing
@ -400,7 +393,7 @@ def main():
# Get information on volume group requested
vgs_cmd = module.get_bin_path("vgs", required=True)
rc, current_vgs, err = module.run_command(
"%s --noheadings --nosuffix -o vg_name,size,free,vg_extent_size --units %s --separator ';' %s" % (vgs_cmd, unit.lower(), vg))
[vgs_cmd, "--noheadings", "--nosuffix", "-o", "vg_name,size,free,vg_extent_size", "--units", unit.lower(), "--separator", ";", vg])
if rc != 0:
if state == 'absent':
@ -414,7 +407,7 @@ def main():
# Get information on logical volume requested
lvs_cmd = module.get_bin_path("lvs", required=True)
rc, current_lvs, err = module.run_command(
"%s -a --noheadings --nosuffix -o lv_name,size,lv_attr --units %s --separator ';' %s" % (lvs_cmd, unit.lower(), vg))
[lvs_cmd, "-a", "--noheadings", "--nosuffix", "-o", "lv_name,size,lv_attr", "--units", unit.lower(), "--separator", ";", vg])
if rc != 0:
if state == 'absent':
@ -474,20 +467,23 @@ def main():
# create LV
lvcreate_cmd = module.get_bin_path("lvcreate", required=True)
cmd = [lvcreate_cmd] + test_opt + yesopt
if snapshot is not None:
if size:
cmd = "%s %s %s -%s %s%s -s -n %s %s %s/%s" % (lvcreate_cmd, test_opt, yesopt, size_opt, size, size_unit, snapshot, opts, vg, lv)
else:
cmd = "%s %s %s -s -n %s %s %s/%s" % (lvcreate_cmd, test_opt, yesopt, snapshot, opts, vg, lv)
elif thinpool and lv:
if size_opt == 'l':
module.fail_json(changed=False, msg="Thin volume sizing with percentage not supported.")
size_opt = 'V'
cmd = "%s %s %s -n %s -%s %s%s %s -T %s/%s" % (lvcreate_cmd, test_opt, yesopt, lv, size_opt, size, size_unit, opts, vg, thinpool)
elif thinpool and not lv:
cmd = "%s %s %s -%s %s%s %s -T %s/%s" % (lvcreate_cmd, test_opt, yesopt, size_opt, size, size_unit, opts, vg, thinpool)
cmd += ["-%s" % size_opt, "%s%s" % (size, size_unit)]
cmd += ["-s", "-n", snapshot] + opts + ["%s/%s" % (vg, lv)]
elif thinpool:
if lv:
if size_opt == 'l':
module.fail_json(changed=False, msg="Thin volume sizing with percentage not supported.")
size_opt = 'V'
cmd += ["-n", lv]
cmd += ["-%s" % size_opt, "%s%s" % (size, size_unit)]
cmd += opts + ["-T", "%s/%s" % (vg, thinpool)]
else:
cmd = "%s %s %s -n %s -%s %s%s %s %s %s" % (lvcreate_cmd, test_opt, yesopt, lv, size_opt, size, size_unit, opts, vg, pvs)
cmd += ["-n", lv]
cmd += ["-%s" % size_opt, "%s%s" % (size, size_unit)]
cmd += opts + [vg] + pvs
rc, dummy, err = module.run_command(cmd)
if rc == 0:
changed = True
@ -499,7 +495,7 @@ def main():
if not force:
module.fail_json(msg="Sorry, no removal of logical volume %s without force=true." % (this_lv['name']))
lvremove_cmd = module.get_bin_path("lvremove", required=True)
rc, dummy, err = module.run_command("%s %s --force %s/%s" % (lvremove_cmd, test_opt, vg, this_lv['name']))
rc, dummy, err = module.run_command([lvremove_cmd] + test_opt + ["--force", "%s/%s" % (vg, this_lv['name'])])
if rc == 0:
module.exit_json(changed=True)
else:
@ -527,7 +523,7 @@ def main():
if this_lv['size'] < size_requested:
if (size_free > 0) and (size_free >= (size_requested - this_lv['size'])):
tool = module.get_bin_path("lvextend", required=True)
tool = [module.get_bin_path("lvextend", required=True)]
else:
module.fail_json(
msg="Logical Volume %s could not be extended. Not enough free space left (%s%s required / %s%s available)" %
@ -539,16 +535,17 @@ def main():
elif not force:
module.fail_json(msg="Sorry, no shrinking of %s without force=true" % (this_lv['name']))
else:
tool = module.get_bin_path("lvreduce", required=True)
tool = '%s %s' % (tool, '--force')
tool = [module.get_bin_path("lvreduce", required=True), '--force']
if tool:
if resizefs:
tool = '%s %s' % (tool, '--resizefs')
tool += ['--resizefs']
cmd = tool + test_opt
if size_operator:
cmd = "%s %s -%s %s%s%s %s/%s %s" % (tool, test_opt, size_opt, size_operator, size, size_unit, vg, this_lv['name'], pvs)
cmd += ["-%s" % size_opt, "%s%s%s" % (size_operator, size, size_unit)]
else:
cmd = "%s %s -%s %s%s %s/%s %s" % (tool, test_opt, size_opt, size, size_unit, vg, this_lv['name'], pvs)
cmd += ["-%s" % size_opt, "%s%s" % (size, size_unit)]
cmd += ["%s/%s" % (vg, this_lv['name'])] + pvs
rc, out, err = module.run_command(cmd)
if "Reached maximum COW size" in out:
module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err, out=out)
@ -566,23 +563,24 @@ def main():
# resize LV based on absolute values
tool = None
if float(size) > this_lv['size'] or size_operator == '+':
tool = module.get_bin_path("lvextend", required=True)
tool = [module.get_bin_path("lvextend", required=True)]
elif shrink and float(size) < this_lv['size'] or size_operator == '-':
if float(size) == 0:
module.fail_json(msg="Sorry, no shrinking of %s to 0 permitted." % (this_lv['name']))
if not force:
module.fail_json(msg="Sorry, no shrinking of %s without force=true." % (this_lv['name']))
else:
tool = module.get_bin_path("lvreduce", required=True)
tool = '%s %s' % (tool, '--force')
tool = [module.get_bin_path("lvreduce", required=True), '--force']
if tool:
if resizefs:
tool = '%s %s' % (tool, '--resizefs')
tool += ['--resizefs']
cmd = tool + test_opt
if size_operator:
cmd = "%s %s -%s %s%s%s %s/%s %s" % (tool, test_opt, size_opt, size_operator, size, size_unit, vg, this_lv['name'], pvs)
cmd += ["-%s" % size_opt, "%s%s%s" % (size_operator, size, size_unit)]
else:
cmd = "%s %s -%s %s%s %s/%s %s" % (tool, test_opt, size_opt, size, size_unit, vg, this_lv['name'], pvs)
cmd += ["-%s" % size_opt, "%s%s" % (size, size_unit)]
cmd += ["%s/%s" % (vg, this_lv['name'])] + pvs
rc, out, err = module.run_command(cmd)
if "Reached maximum COW size" in out:
module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err, out=out)
@ -598,14 +596,14 @@ def main():
if this_lv is not None:
if active:
lvchange_cmd = module.get_bin_path("lvchange", required=True)
rc, dummy, err = module.run_command("%s -ay %s/%s" % (lvchange_cmd, vg, this_lv['name']))
rc, dummy, err = module.run_command([lvchange_cmd, "-ay", "%s/%s" % (vg, this_lv['name'])])
if rc == 0:
module.exit_json(changed=((not this_lv['active']) or changed), vg=vg, lv=this_lv['name'], size=this_lv['size'])
else:
module.fail_json(msg="Failed to activate logical volume %s" % (lv), rc=rc, err=err)
else:
lvchange_cmd = module.get_bin_path("lvchange", required=True)
rc, dummy, err = module.run_command("%s -an %s/%s" % (lvchange_cmd, vg, this_lv['name']))
rc, dummy, err = module.run_command([lvchange_cmd, "-an", "%s/%s" % (vg, this_lv['name'])])
if rc == 0:
module.exit_json(changed=(this_lv['active'] or changed), vg=vg, lv=this_lv['name'], size=this_lv['size'])
else: