1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

vdo - refactor (#3191)

* refactor to vdo

* adjusted if condition

* added changelog fragment

* Update plugins/modules/system/vdo.py

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

* adjustements per the PR

* more occurrences of bool compared with yes or no

* Update changelogs/fragments/3191-vdo-refactor.yml

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

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2021-08-16 22:23:06 +12:00 committed by GitHub
parent 432c891487
commit 16945d3847
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 183 deletions

View file

@ -0,0 +1,4 @@
minor_changes:
- vdo - minor refactoring of the code (https://github.com/ansible-collections/community.general/pull/3191).
bugfixes:
- vdo - boolean arguments now compared with proper ``true`` and ``false`` values instead of string representations like ``"yes"`` or ``"no"`` (https://github.com/ansible-collections/community.general/pull/3191).

View file

@ -315,7 +315,7 @@ except ImportError:
# #
# @return vdolist A list of currently created VDO volumes. # @return vdolist A list of currently created VDO volumes.
def inventory_vdos(module, vdocmd): def inventory_vdos(module, vdocmd):
rc, vdostatusout, err = module.run_command("%s status" % (vdocmd)) rc, vdostatusout, err = module.run_command([vdocmd, "status"])
# if rc != 0: # if rc != 0:
# module.fail_json(msg="Inventorying VDOs failed: %s" # module.fail_json(msg="Inventorying VDOs failed: %s"
@ -323,15 +323,13 @@ def inventory_vdos(module, vdocmd):
vdolist = [] vdolist = []
if (rc == 2 and if rc == 2 and re.findall(r"vdoconf\.yml does not exist", err, re.MULTILINE):
re.findall(r"vdoconf.yml does not exist", err, re.MULTILINE)):
# If there is no /etc/vdoconf.yml file, assume there are no # If there is no /etc/vdoconf.yml file, assume there are no
# VDO volumes. Return an empty list of VDO volumes. # VDO volumes. Return an empty list of VDO volumes.
return vdolist return vdolist
if rc != 0: if rc != 0:
module.fail_json(msg="Inventorying VDOs failed: %s" module.fail_json(msg="Inventorying VDOs failed: %s" % vdostatusout, rc=rc, err=err)
% vdostatusout, rc=rc, err=err)
vdostatusyaml = yaml.load(vdostatusout) vdostatusyaml = yaml.load(vdostatusout)
if vdostatusyaml is None: if vdostatusyaml is None:
@ -346,7 +344,7 @@ def inventory_vdos(module, vdocmd):
def list_running_vdos(module, vdocmd): def list_running_vdos(module, vdocmd):
rc, vdolistout, err = module.run_command("%s list" % (vdocmd)) rc, vdolistout, err = module.run_command([vdocmd, "list"])
runningvdolist = filter(None, vdolistout.split('\n')) runningvdolist = filter(None, vdolistout.split('\n'))
return runningvdolist return runningvdolist
@ -360,36 +358,30 @@ def list_running_vdos(module, vdocmd):
# #
# @return vdocmdoptions A string to be used in a 'vdo <action>' command. # @return vdocmdoptions A string to be used in a 'vdo <action>' command.
def start_vdo(module, vdoname, vdocmd): def start_vdo(module, vdoname, vdocmd):
rc, out, err = module.run_command("%s start --name=%s" % (vdocmd, vdoname)) rc, out, err = module.run_command([vdocmd, "start", "--name=%s" % vdoname])
if rc == 0: if rc == 0:
module.log("started VDO volume %s" % vdoname) module.log("started VDO volume %s" % vdoname)
return rc return rc
def stop_vdo(module, vdoname, vdocmd): def stop_vdo(module, vdoname, vdocmd):
rc, out, err = module.run_command("%s stop --name=%s" % (vdocmd, vdoname)) rc, out, err = module.run_command([vdocmd, "stop", "--name=%s" % vdoname])
if rc == 0: if rc == 0:
module.log("stopped VDO volume %s" % vdoname) module.log("stopped VDO volume %s" % vdoname)
return rc return rc
def activate_vdo(module, vdoname, vdocmd): def activate_vdo(module, vdoname, vdocmd):
rc, out, err = module.run_command("%s activate --name=%s" rc, out, err = module.run_command([vdocmd, "activate", "--name=%s" % vdoname])
% (vdocmd, vdoname))
if rc == 0: if rc == 0:
module.log("activated VDO volume %s" % vdoname) module.log("activated VDO volume %s" % vdoname)
return rc return rc
def deactivate_vdo(module, vdoname, vdocmd): def deactivate_vdo(module, vdoname, vdocmd):
rc, out, err = module.run_command("%s deactivate --name=%s" rc, out, err = module.run_command([vdocmd, "deactivate", "--name=%s" % vdoname])
% (vdocmd, vdoname))
if rc == 0: if rc == 0:
module.log("deactivated VDO volume %s" % vdoname) module.log("deactivated VDO volume %s" % vdoname)
return rc return rc
@ -397,32 +389,31 @@ def add_vdooptions(params):
vdocmdoptions = "" vdocmdoptions = ""
options = [] options = []
if ('logicalsize' in params) and (params['logicalsize'] is not None): if params.get('logicalsize') is not None:
options.append("--vdoLogicalSize=" + params['logicalsize']) options.append("--vdoLogicalSize=" + params['logicalsize'])
if (('blockmapcachesize' in params) and if params.get('blockmapcachesize') is not None:
(params['blockmapcachesize'] is not None)):
options.append("--blockMapCacheSize=" + params['blockmapcachesize']) options.append("--blockMapCacheSize=" + params['blockmapcachesize'])
if ('readcache' in params) and (params['readcache'] == 'enabled'): if params.get('readcache') == 'enabled':
options.append("--readCache=enabled") options.append("--readCache=enabled")
if ('readcachesize' in params) and (params['readcachesize'] is not None): if params.get('readcachesize') is not None:
options.append("--readCacheSize=" + params['readcachesize']) options.append("--readCacheSize=" + params['readcachesize'])
if ('slabsize' in params) and (params['slabsize'] is not None): if params.get('slabsize') is not None:
options.append("--vdoSlabSize=" + params['slabsize']) options.append("--vdoSlabSize=" + params['slabsize'])
if ('emulate512' in params) and (params['emulate512']): if params.get('emulate512'):
options.append("--emulate512=enabled") options.append("--emulate512=enabled")
if ('indexmem' in params) and (params['indexmem'] is not None): if params.get('indexmem') is not None:
options.append("--indexMem=" + params['indexmem']) options.append("--indexMem=" + params['indexmem'])
if ('indexmode' in params) and (params['indexmode'] == 'sparse'): if params.get('indexmode') == 'sparse':
options.append("--sparseIndex=enabled") options.append("--sparseIndex=enabled")
if ('force' in params) and (params['force']): if params.get('force'):
options.append("--force") options.append("--force")
# Entering an invalid thread config results in a cryptic # Entering an invalid thread config results in a cryptic
@ -431,23 +422,21 @@ def add_vdooptions(params):
# output a more helpful message, but one would have to log # output a more helpful message, but one would have to log
# onto that system to read the error. For now, heed the thread # onto that system to read the error. For now, heed the thread
# limit warnings in the DOCUMENTATION section above. # limit warnings in the DOCUMENTATION section above.
if ('ackthreads' in params) and (params['ackthreads'] is not None): if params.get('ackthreads') is not None:
options.append("--vdoAckThreads=" + params['ackthreads']) options.append("--vdoAckThreads=" + params['ackthreads'])
if ('biothreads' in params) and (params['biothreads'] is not None): if params.get('biothreads') is not None:
options.append("--vdoBioThreads=" + params['biothreads']) options.append("--vdoBioThreads=" + params['biothreads'])
if ('cputhreads' in params) and (params['cputhreads'] is not None): if params.get('cputhreads') is not None:
options.append("--vdoCpuThreads=" + params['cputhreads']) options.append("--vdoCpuThreads=" + params['cputhreads'])
if ('logicalthreads' in params) and (params['logicalthreads'] is not None): if params.get('logicalthreads') is not None:
options.append("--vdoLogicalThreads=" + params['logicalthreads']) options.append("--vdoLogicalThreads=" + params['logicalthreads'])
if (('physicalthreads' in params) and if params.get('physicalthreads') is not None:
(params['physicalthreads'] is not None)):
options.append("--vdoPhysicalThreads=" + params['physicalthreads']) options.append("--vdoPhysicalThreads=" + params['physicalthreads'])
vdocmdoptions = ' '.join(options)
return vdocmdoptions return vdocmdoptions
@ -531,31 +520,24 @@ def run_module():
# Since this is a creation of a new VDO volume, it will contain all # Since this is a creation of a new VDO volume, it will contain all
# all of the parameters given by the playbook; the rest will # all of the parameters given by the playbook; the rest will
# assume default values. # assume default values.
options = module.params vdocmdoptions = add_vdooptions(module.params)
vdocmdoptions = add_vdooptions(options) rc, out, err = module.run_command(
rc, out, err = module.run_command("%s create --name=%s --device=%s %s" [vdocmd, "create", "--name=%s" % desiredvdo, "--device=%s" % device] + vdocmdoptions)
% (vdocmd, desiredvdo, device,
vdocmdoptions))
if rc == 0: if rc == 0:
result['changed'] = True result['changed'] = True
else: else:
module.fail_json(msg="Creating VDO %s failed." module.fail_json(msg="Creating VDO %s failed." % desiredvdo, rc=rc, err=err)
% desiredvdo, rc=rc, err=err)
if (module.params['compression'] == 'disabled'): if module.params['compression'] == 'disabled':
rc, out, err = module.run_command("%s disableCompression --name=%s" rc, out, err = module.run_command([vdocmd, "disableCompression", "--name=%s" % desiredvdo])
% (vdocmd, desiredvdo))
if ((module.params['deduplication'] is not None) and if module.params['deduplication'] == 'disabled':
module.params['deduplication'] == 'disabled'): rc, out, err = module.run_command([vdocmd, "disableDeduplication", "--name=%s" % desiredvdo])
rc, out, err = module.run_command("%s disableDeduplication "
"--name=%s"
% (vdocmd, desiredvdo))
if module.params['activated'] == 'no': if module.params['activated'] is False:
deactivate_vdo(module, desiredvdo, vdocmd) deactivate_vdo(module, desiredvdo, vdocmd)
if module.params['running'] == 'no': if module.params['running'] is False:
stop_vdo(module, desiredvdo, vdocmd) stop_vdo(module, desiredvdo, vdocmd)
# Print a post-run list of VDO volumes in the result object. # Print a post-run list of VDO volumes in the result object.
@ -564,8 +546,8 @@ def run_module():
module.exit_json(**result) module.exit_json(**result)
# Modify the current parameters of a VDO that exists. # Modify the current parameters of a VDO that exists.
if (desiredvdo in vdolist) and (state == 'present'): if desiredvdo in vdolist and state == 'present':
rc, vdostatusoutput, err = module.run_command("%s status" % (vdocmd)) rc, vdostatusoutput, err = module.run_command([vdocmd, "status"])
vdostatusyaml = yaml.load(vdostatusoutput) vdostatusyaml = yaml.load(vdostatusoutput)
# An empty dictionary to contain dictionaries of VDO statistics # An empty dictionary to contain dictionaries of VDO statistics
@ -630,7 +612,7 @@ def run_module():
diffparams = {} diffparams = {}
# Check for differences between the playbook parameters and the # Check for differences between the playbook parameters and the
# current parameters. This will need a comparison function; # current parameters. This will need a comparison function;
# since AnsibleModule params are all strings, compare them as # since AnsibleModule params are all strings, compare them as
# strings (but if it's None; skip). # strings (but if it's None; skip).
for key in currentparams.keys(): for key in currentparams.keys():
@ -641,10 +623,7 @@ def run_module():
if diffparams: if diffparams:
vdocmdoptions = add_vdooptions(diffparams) vdocmdoptions = add_vdooptions(diffparams)
if vdocmdoptions: if vdocmdoptions:
rc, out, err = module.run_command("%s modify --name=%s %s" rc, out, err = module.run_command([vdocmd, "modify", "--name=%s" % desiredvdo] + vdocmdoptions)
% (vdocmd,
desiredvdo,
vdocmdoptions))
if rc == 0: if rc == 0:
result['changed'] = True result['changed'] = True
else: else:
@ -653,107 +632,36 @@ def run_module():
if 'deduplication' in diffparams.keys(): if 'deduplication' in diffparams.keys():
dedupemod = diffparams['deduplication'] dedupemod = diffparams['deduplication']
if dedupemod == 'disabled': dedupeparam = "disableDeduplication" if dedupemod == 'disabled' else "enableDeduplication"
rc, out, err = module.run_command("%s " rc, out, err = module.run_command([vdocmd, dedupeparam, "--name=%s" % desiredvdo])
"disableDeduplication "
"--name=%s"
% (vdocmd, desiredvdo))
if rc == 0: if rc == 0:
result['changed'] = True result['changed'] = True
else: else:
module.fail_json(msg="Changing deduplication on " module.fail_json(msg="Changing deduplication on VDO volume %s failed." % desiredvdo, rc=rc, err=err)
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
if dedupemod == 'enabled':
rc, out, err = module.run_command("%s "
"enableDeduplication "
"--name=%s"
% (vdocmd, desiredvdo))
if rc == 0:
result['changed'] = True
else:
module.fail_json(msg="Changing deduplication on "
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
if 'compression' in diffparams.keys(): if 'compression' in diffparams.keys():
compressmod = diffparams['compression'] compressmod = diffparams['compression']
if compressmod == 'disabled': compressparam = "disableCompression" if compressmod == 'disabled' else "enableCompression"
rc, out, err = module.run_command("%s disableCompression " rc, out, err = module.run_command([vdocmd, compressparam, "--name=%s" % desiredvdo])
"--name=%s" if rc == 0:
% (vdocmd, desiredvdo)) result['changed'] = True
else:
if rc == 0: module.fail_json(msg="Changing compression on VDO volume %s failed." % desiredvdo, rc=rc, err=err)
result['changed'] = True
else:
module.fail_json(msg="Changing compression on "
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
if compressmod == 'enabled':
rc, out, err = module.run_command("%s enableCompression "
"--name=%s"
% (vdocmd, desiredvdo))
if rc == 0:
result['changed'] = True
else:
module.fail_json(msg="Changing compression on "
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
if 'writepolicy' in diffparams.keys(): if 'writepolicy' in diffparams.keys():
writepolmod = diffparams['writepolicy'] writepolmod = diffparams['writepolicy']
if writepolmod == 'auto': rc, out, err = module.run_command([
rc, out, err = module.run_command("%s " vdocmd,
"changeWritePolicy " "changeWritePolicy",
"--name=%s " "--name=%s" % desiredvdo,
"--writePolicy=%s" "--writePolicy=%s" % writepolmod,
% (vdocmd, ])
desiredvdo,
writepolmod))
if rc == 0: if rc == 0:
result['changed'] = True result['changed'] = True
else: else:
module.fail_json(msg="Changing write policy on " module.fail_json(msg="Changing write policy on VDO volume %s failed." % desiredvdo, rc=rc, err=err)
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
if writepolmod == 'sync':
rc, out, err = module.run_command("%s "
"changeWritePolicy "
"--name=%s "
"--writePolicy=%s"
% (vdocmd,
desiredvdo,
writepolmod))
if rc == 0:
result['changed'] = True
else:
module.fail_json(msg="Changing write policy on "
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
if writepolmod == 'async':
rc, out, err = module.run_command("%s "
"changeWritePolicy "
"--name=%s "
"--writePolicy=%s"
% (vdocmd,
desiredvdo,
writepolmod))
if rc == 0:
result['changed'] = True
else:
module.fail_json(msg="Changing write policy on "
"VDO volume %s failed."
% desiredvdo, rc=rc, err=err)
# Process the size parameters, to determine of a growPhysical or # Process the size parameters, to determine of a growPhysical or
# growLogical operation needs to occur. # growLogical operation needs to occur.
@ -771,19 +679,15 @@ def run_module():
diffsizeparams = {} diffsizeparams = {}
for key in sizeparams.keys(): for key in sizeparams.keys():
if module.params[key] is not None: if module.params[key] is not None and str(sizeparams[key]) != module.params[key]:
if str(sizeparams[key]) != module.params[key]: diffsizeparams[key] = module.params[key]
diffsizeparams[key] = module.params[key]
if module.params['growphysical']: if module.params['growphysical']:
physdevice = module.params['device'] physdevice = module.params['device']
rc, devsectors, err = module.run_command("blockdev --getsz %s" rc, devsectors, err = module.run_command([module.get_bin_path("blockdev"), "--getsz", physdevice])
% (physdevice))
devblocks = (int(devsectors) / 8) devblocks = (int(devsectors) / 8)
dmvdoname = ('/dev/mapper/' + desiredvdo) dmvdoname = ('/dev/mapper/' + desiredvdo)
currentvdostats = (processedvdos[desiredvdo] currentvdostats = processedvdos[desiredvdo]['VDO statistics'][dmvdoname]
['VDO statistics']
[dmvdoname])
currentphysblocks = currentvdostats['physical blocks'] currentphysblocks = currentvdostats['physical blocks']
# Set a growPhysical threshold to grow only when there is # Set a growPhysical threshold to grow only when there is
@ -795,34 +699,25 @@ def run_module():
if currentphysblocks > growthresh: if currentphysblocks > growthresh:
result['changed'] = True result['changed'] = True
rc, out, err = module.run_command("%s growPhysical --name=%s" rc, out, err = module.run_command([vdocmd, "growPhysical", "--name=%s" % desiredvdo])
% (vdocmd, desiredvdo))
if 'logicalsize' in diffsizeparams.keys(): if 'logicalsize' in diffsizeparams.keys():
result['changed'] = True result['changed'] = True
vdocmdoptions = ("--vdoLogicalSize=" + rc, out, err = module.run_command([vdocmd, "growLogical", "--name=%s" % desiredvdo, "--vdoLogicalSize=%s" % diffsizeparams['logicalsize']])
diffsizeparams['logicalsize'])
rc, out, err = module.run_command("%s growLogical --name=%s %s"
% (vdocmd,
desiredvdo,
vdocmdoptions))
vdoactivatestatus = processedvdos[desiredvdo]['Activate'] vdoactivatestatus = processedvdos[desiredvdo]['Activate']
if ((module.params['activated'] == 'no') and if module.params['activated'] is False and vdoactivatestatus == 'enabled':
(vdoactivatestatus == 'enabled')):
deactivate_vdo(module, desiredvdo, vdocmd) deactivate_vdo(module, desiredvdo, vdocmd)
if not result['changed']: if not result['changed']:
result['changed'] = True result['changed'] = True
if ((module.params['activated'] == 'yes') and if module.params['activated'] and vdoactivatestatus == 'disabled':
(vdoactivatestatus == 'disabled')):
activate_vdo(module, desiredvdo, vdocmd) activate_vdo(module, desiredvdo, vdocmd)
if not result['changed']: if not result['changed']:
result['changed'] = True result['changed'] = True
if ((module.params['running'] == 'no') and if module.params['running'] is False and desiredvdo in runningvdolist:
(desiredvdo in runningvdolist)):
stop_vdo(module, desiredvdo, vdocmd) stop_vdo(module, desiredvdo, vdocmd)
if not result['changed']: if not result['changed']:
result['changed'] = True result['changed'] = True
@ -834,10 +729,7 @@ def run_module():
# the activate_vdo() operation succeeded, as 'vdoactivatestatus' # the activate_vdo() operation succeeded, as 'vdoactivatestatus'
# will have the activated status prior to the activate_vdo() # will have the activated status prior to the activate_vdo()
# call. # call.
if (((vdoactivatestatus == 'enabled') or if (vdoactivatestatus == 'enabled' or module.params['activated']) and module.params['running'] and desiredvdo not in runningvdolist:
(module.params['activated'] == 'yes')) and
(module.params['running'] == 'yes') and
(desiredvdo not in runningvdolist)):
start_vdo(module, desiredvdo, vdocmd) start_vdo(module, desiredvdo, vdocmd)
if not result['changed']: if not result['changed']:
result['changed'] = True result['changed'] = True
@ -850,14 +742,12 @@ def run_module():
module.exit_json(**result) module.exit_json(**result)
# Remove a desired VDO that currently exists. # Remove a desired VDO that currently exists.
if (desiredvdo in vdolist) and (state == 'absent'): if desiredvdo in vdolist and state == 'absent':
rc, out, err = module.run_command("%s remove --name=%s" rc, out, err = module.run_command([vdocmd, "remove", "--name=%s" % desiredvdo])
% (vdocmd, desiredvdo))
if rc == 0: if rc == 0:
result['changed'] = True result['changed'] = True
else: else:
module.fail_json(msg="Removing VDO %s failed." module.fail_json(msg="Removing VDO %s failed." % desiredvdo, rc=rc, err=err)
% desiredvdo, rc=rc, err=err)
# Print a post-run list of VDO volumes in the result object. # Print a post-run list of VDO volumes in the result object.
vdolist = inventory_vdos(module, vdocmd) vdolist = inventory_vdos(module, vdocmd)
@ -869,8 +759,7 @@ def run_module():
# not exist. Print a post-run list of VDO volumes in the result # not exist. Print a post-run list of VDO volumes in the result
# object. # object.
vdolist = inventory_vdos(module, vdocmd) vdolist = inventory_vdos(module, vdocmd)
module.log("received request to remove non-existent VDO volume %s" module.log("received request to remove non-existent VDO volume %s" % desiredvdo)
% desiredvdo)
module.exit_json(**result) module.exit_json(**result)