mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-14 16:05:04 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -224,63 +224,59 @@ class PersistentMemory:
|
|||
def __init__(self):
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
appdirect=dict(type='int'),
|
||||
appdirect_interleaved=dict(type='bool', default=True),
|
||||
memorymode=dict(type='int'),
|
||||
reserved=dict(type='int'),
|
||||
appdirect=dict(type="int"),
|
||||
appdirect_interleaved=dict(type="bool", default=True),
|
||||
memorymode=dict(type="int"),
|
||||
reserved=dict(type="int"),
|
||||
socket=dict(
|
||||
type='list', elements='dict',
|
||||
type="list",
|
||||
elements="dict",
|
||||
options=dict(
|
||||
id=dict(required=True, type='int'),
|
||||
appdirect=dict(required=True, type='int'),
|
||||
appdirect_interleaved=dict(type='bool', default=True),
|
||||
memorymode=dict(required=True, type='int'),
|
||||
reserved=dict(type='int'),
|
||||
id=dict(required=True, type="int"),
|
||||
appdirect=dict(required=True, type="int"),
|
||||
appdirect_interleaved=dict(type="bool", default=True),
|
||||
memorymode=dict(required=True, type="int"),
|
||||
reserved=dict(type="int"),
|
||||
),
|
||||
),
|
||||
namespace=dict(
|
||||
type='list', elements='dict',
|
||||
type="list",
|
||||
elements="dict",
|
||||
options=dict(
|
||||
mode=dict(required=True, type='str', choices=['raw', 'sector', 'fsdax', 'devdax']),
|
||||
type=dict(type='str', choices=['pmem', 'blk']),
|
||||
size=dict(type='str'),
|
||||
mode=dict(required=True, type="str", choices=["raw", "sector", "fsdax", "devdax"]),
|
||||
type=dict(type="str", choices=["pmem", "blk"]),
|
||||
size=dict(type="str"),
|
||||
),
|
||||
),
|
||||
namespace_append=dict(type='bool', default=False),
|
||||
),
|
||||
required_together=(
|
||||
['appdirect', 'memorymode'],
|
||||
),
|
||||
required_one_of=(
|
||||
['appdirect', 'memorymode', 'socket', 'namespace'],
|
||||
namespace_append=dict(type="bool", default=False),
|
||||
),
|
||||
required_together=(["appdirect", "memorymode"],),
|
||||
required_one_of=(["appdirect", "memorymode", "socket", "namespace"],),
|
||||
mutually_exclusive=(
|
||||
['appdirect', 'socket'],
|
||||
['memorymode', 'socket'],
|
||||
['appdirect', 'namespace'],
|
||||
['memorymode', 'namespace'],
|
||||
['socket', 'namespace'],
|
||||
['appdirect', 'namespace_append'],
|
||||
['memorymode', 'namespace_append'],
|
||||
['socket', 'namespace_append'],
|
||||
["appdirect", "socket"],
|
||||
["memorymode", "socket"],
|
||||
["appdirect", "namespace"],
|
||||
["memorymode", "namespace"],
|
||||
["socket", "namespace"],
|
||||
["appdirect", "namespace_append"],
|
||||
["memorymode", "namespace_append"],
|
||||
["socket", "namespace_append"],
|
||||
),
|
||||
)
|
||||
|
||||
if not HAS_XMLTODICT_LIBRARY:
|
||||
module.fail_json(
|
||||
msg=missing_required_lib('xmltodict'),
|
||||
exception=XMLTODICT_LIBRARY_IMPORT_ERROR)
|
||||
module.fail_json(msg=missing_required_lib("xmltodict"), exception=XMLTODICT_LIBRARY_IMPORT_ERROR)
|
||||
|
||||
self.ipmctl_exec = module.get_bin_path('ipmctl', True)
|
||||
self.ndctl_exec = module.get_bin_path('ndctl', True)
|
||||
self.ipmctl_exec = module.get_bin_path("ipmctl", True)
|
||||
self.ndctl_exec = module.get_bin_path("ndctl", True)
|
||||
|
||||
self.appdirect = module.params['appdirect']
|
||||
self.interleaved = module.params['appdirect_interleaved']
|
||||
self.memmode = module.params['memorymode']
|
||||
self.reserved = module.params['reserved']
|
||||
self.socket = module.params['socket']
|
||||
self.namespace = module.params['namespace']
|
||||
self.namespace_append = module.params['namespace_append']
|
||||
self.appdirect = module.params["appdirect"]
|
||||
self.interleaved = module.params["appdirect_interleaved"]
|
||||
self.memmode = module.params["memorymode"]
|
||||
self.reserved = module.params["reserved"]
|
||||
self.socket = module.params["socket"]
|
||||
self.namespace = module.params["namespace"]
|
||||
self.namespace_append = module.params["namespace_append"]
|
||||
|
||||
self.module = module
|
||||
self.changed = False
|
||||
|
|
@ -290,77 +286,75 @@ class PersistentMemory:
|
|||
# in case command[] has number
|
||||
cmd = [str(part) for part in command]
|
||||
|
||||
self.module.log(msg=f'pmem_run_command: execute: {cmd}')
|
||||
self.module.log(msg=f"pmem_run_command: execute: {cmd}")
|
||||
|
||||
rc, out, err = self.module.run_command(cmd)
|
||||
|
||||
self.module.log(msg=f'pmem_run_command: result: {out}')
|
||||
self.module.log(msg=f"pmem_run_command: result: {out}")
|
||||
|
||||
if returnCheck and rc != 0:
|
||||
self.module.fail_json(msg=f'Error while running: {cmd}', rc=rc, out=out, err=err)
|
||||
self.module.fail_json(msg=f"Error while running: {cmd}", rc=rc, out=out, err=err)
|
||||
|
||||
return out
|
||||
|
||||
def pmem_run_ipmctl(self, command, returnCheck=True):
|
||||
|
||||
command = [self.ipmctl_exec] + command
|
||||
|
||||
return self.pmem_run_command(command, returnCheck)
|
||||
|
||||
def pmem_run_ndctl(self, command, returnCheck=True):
|
||||
|
||||
command = [self.ndctl_exec] + command
|
||||
|
||||
return self.pmem_run_command(command, returnCheck)
|
||||
|
||||
def pmem_is_dcpmm_installed(self):
|
||||
# To check this system has dcpmm
|
||||
command = ['show', '-system', '-capabilities']
|
||||
command = ["show", "-system", "-capabilities"]
|
||||
return self.pmem_run_ipmctl(command)
|
||||
|
||||
def pmem_get_region_align_size(self, region):
|
||||
aligns = []
|
||||
for rg in region:
|
||||
if rg['align'] not in aligns:
|
||||
aligns.append(rg['align'])
|
||||
if rg["align"] not in aligns:
|
||||
aligns.append(rg["align"])
|
||||
|
||||
return aligns
|
||||
|
||||
def pmem_get_available_region_size(self, region):
|
||||
available_size = []
|
||||
for rg in region:
|
||||
available_size.append(rg['available_size'])
|
||||
available_size.append(rg["available_size"])
|
||||
|
||||
return available_size
|
||||
|
||||
def pmem_get_available_region_type(self, region):
|
||||
types = []
|
||||
for rg in region:
|
||||
if rg['type'] not in types:
|
||||
types.append(rg['type'])
|
||||
if rg["type"] not in types:
|
||||
types.append(rg["type"])
|
||||
|
||||
return types
|
||||
|
||||
def pmem_argument_check(self):
|
||||
def namespace_check(self):
|
||||
command = ['list', '-R']
|
||||
command = ["list", "-R"]
|
||||
out = self.pmem_run_ndctl(command)
|
||||
if not out:
|
||||
return 'Available region(s) is not in this system.'
|
||||
return "Available region(s) is not in this system."
|
||||
region = json.loads(out)
|
||||
|
||||
aligns = self.pmem_get_region_align_size(region)
|
||||
if len(aligns) != 1:
|
||||
return 'Not supported the regions whose alignment size is different.'
|
||||
return "Not supported the regions whose alignment size is different."
|
||||
|
||||
available_size = self.pmem_get_available_region_size(region)
|
||||
types = self.pmem_get_available_region_type(region)
|
||||
for ns in self.namespace:
|
||||
if ns['size']:
|
||||
if ns["size"]:
|
||||
try:
|
||||
size_byte = human_to_bytes(ns['size'])
|
||||
size_byte = human_to_bytes(ns["size"])
|
||||
except ValueError:
|
||||
return 'The format of size: NNN TB|GB|MB|KB|T|G|M|K|B'
|
||||
return "The format of size: NNN TB|GB|MB|KB|T|G|M|K|B"
|
||||
|
||||
if size_byte % aligns[0] != 0:
|
||||
return f"size: {ns['size']} should be align with {aligns[0]}"
|
||||
|
|
@ -375,41 +369,41 @@ class PersistentMemory:
|
|||
if is_space_enough is False:
|
||||
return f"There is not available region for size: {ns['size']}"
|
||||
|
||||
ns['size_byte'] = size_byte
|
||||
ns["size_byte"] = size_byte
|
||||
|
||||
elif len(self.namespace) != 1:
|
||||
return 'size option is required to configure multiple namespaces'
|
||||
return "size option is required to configure multiple namespaces"
|
||||
|
||||
if ns['type'] not in types:
|
||||
if ns["type"] not in types:
|
||||
return f"type {ns['type']} is not supported in this system. Supported type: {types}"
|
||||
|
||||
return None
|
||||
|
||||
def percent_check(self, appdirect, memmode, reserved=None):
|
||||
if appdirect is None or (appdirect < 0 or appdirect > 100):
|
||||
return 'appdirect percent should be from 0 to 100.'
|
||||
return "appdirect percent should be from 0 to 100."
|
||||
if memmode is None or (memmode < 0 or memmode > 100):
|
||||
return 'memorymode percent should be from 0 to 100.'
|
||||
return "memorymode percent should be from 0 to 100."
|
||||
|
||||
if reserved is None:
|
||||
if appdirect + memmode > 100:
|
||||
return 'Total percent should be less equal 100.'
|
||||
return "Total percent should be less equal 100."
|
||||
else:
|
||||
if reserved < 0 or reserved > 100:
|
||||
return 'reserved percent should be from 0 to 100.'
|
||||
return "reserved percent should be from 0 to 100."
|
||||
if appdirect + memmode + reserved != 100:
|
||||
return 'Total percent should be 100.'
|
||||
return "Total percent should be 100."
|
||||
|
||||
def socket_id_check(self):
|
||||
command = ['show', '-o', 'nvmxml', '-socket']
|
||||
command = ["show", "-o", "nvmxml", "-socket"]
|
||||
out = self.pmem_run_ipmctl(command)
|
||||
sockets_dict = xmltodict.parse(out, dict_constructor=dict)['SocketList']['Socket']
|
||||
sockets_dict = xmltodict.parse(out, dict_constructor=dict)["SocketList"]["Socket"]
|
||||
socket_ids = []
|
||||
for sl in sockets_dict:
|
||||
socket_ids.append(int(sl['SocketID'], 16))
|
||||
socket_ids.append(int(sl["SocketID"], 16))
|
||||
|
||||
for skt in self.socket:
|
||||
if skt['id'] not in socket_ids:
|
||||
if skt["id"] not in socket_ids:
|
||||
return f"Invalid socket number: {skt['id']}"
|
||||
|
||||
return None
|
||||
|
|
@ -424,15 +418,14 @@ class PersistentMemory:
|
|||
return ret
|
||||
|
||||
for skt in self.socket:
|
||||
ret = percent_check(
|
||||
self, skt['appdirect'], skt['memorymode'], skt['reserved'])
|
||||
ret = percent_check(self, skt["appdirect"], skt["memorymode"], skt["reserved"])
|
||||
if ret is not None:
|
||||
return ret
|
||||
|
||||
return None
|
||||
|
||||
def pmem_remove_namespaces(self):
|
||||
command = ['list', '-N']
|
||||
command = ["list", "-N"]
|
||||
out = self.pmem_run_ndctl(command)
|
||||
|
||||
# There's nothing namespaces in this system. Nothing to do.
|
||||
|
|
@ -443,17 +436,17 @@ class PersistentMemory:
|
|||
|
||||
# Disable and destroy all namespaces
|
||||
for ns in namespaces:
|
||||
command = ['disable-namespace', ns['dev']]
|
||||
command = ["disable-namespace", ns["dev"]]
|
||||
self.pmem_run_ndctl(command)
|
||||
|
||||
command = ['destroy-namespace', ns['dev']]
|
||||
command = ["destroy-namespace", ns["dev"]]
|
||||
self.pmem_run_ndctl(command)
|
||||
|
||||
return
|
||||
|
||||
def pmem_delete_goal(self):
|
||||
# delete the goal request
|
||||
command = ['delete', '-goal']
|
||||
command = ["delete", "-goal"]
|
||||
self.pmem_run_ipmctl(command)
|
||||
|
||||
def pmem_init_env(self):
|
||||
|
|
@ -463,16 +456,16 @@ class PersistentMemory:
|
|||
self.pmem_delete_goal()
|
||||
|
||||
def pmem_get_capacity(self, skt=None):
|
||||
command = ['show', '-d', 'Capacity', '-u', 'B', '-o', 'nvmxml', '-dimm']
|
||||
command = ["show", "-d", "Capacity", "-u", "B", "-o", "nvmxml", "-dimm"]
|
||||
if skt:
|
||||
command += ['-socket', skt['id']]
|
||||
command += ["-socket", skt["id"]]
|
||||
out = self.pmem_run_ipmctl(command)
|
||||
|
||||
dimm_list = xmltodict.parse(out, dict_constructor=dict)['DimmList']['Dimm']
|
||||
dimm_list = xmltodict.parse(out, dict_constructor=dict)["DimmList"]["Dimm"]
|
||||
capacity = 0
|
||||
for entry in dimm_list:
|
||||
for key, v in entry.items():
|
||||
if key == 'Capacity':
|
||||
if key == "Capacity":
|
||||
capacity += int(v.split()[0])
|
||||
|
||||
return capacity
|
||||
|
|
@ -482,11 +475,11 @@ class PersistentMemory:
|
|||
ipmctl_opts = []
|
||||
|
||||
if skt:
|
||||
appdirect = skt['appdirect']
|
||||
memmode = skt['memorymode']
|
||||
reserved = skt['reserved']
|
||||
socket_id = skt['id']
|
||||
ipmctl_opts += ['-socket', socket_id]
|
||||
appdirect = skt["appdirect"]
|
||||
memmode = skt["memorymode"]
|
||||
reserved = skt["reserved"]
|
||||
socket_id = skt["id"]
|
||||
ipmctl_opts += ["-socket", socket_id]
|
||||
else:
|
||||
appdirect = self.appdirect
|
||||
memmode = self.memmode
|
||||
|
|
@ -494,61 +487,59 @@ class PersistentMemory:
|
|||
|
||||
if reserved is None:
|
||||
res = 100 - memmode - appdirect
|
||||
ipmctl_opts += [f'memorymode={memmode}', f'reserved={res}']
|
||||
ipmctl_opts += [f"memorymode={memmode}", f"reserved={res}"]
|
||||
else:
|
||||
ipmctl_opts += [f'memorymode={memmode}', f'reserved={reserved}']
|
||||
ipmctl_opts += [f"memorymode={memmode}", f"reserved={reserved}"]
|
||||
|
||||
if self.interleaved:
|
||||
ipmctl_opts += ['PersistentMemoryType=AppDirect']
|
||||
ipmctl_opts += ["PersistentMemoryType=AppDirect"]
|
||||
else:
|
||||
ipmctl_opts += ['PersistentMemoryType=AppDirectNotInterleaved']
|
||||
ipmctl_opts += ["PersistentMemoryType=AppDirectNotInterleaved"]
|
||||
|
||||
return ipmctl_opts
|
||||
|
||||
def is_allocation_good(self, ipmctl_out, command):
|
||||
warning = re.compile('WARNING')
|
||||
error = re.compile('.*Error.*')
|
||||
ignore_error = re.compile(
|
||||
'Do you want to continue? [y/n] Error: Invalid data input.')
|
||||
warning = re.compile("WARNING")
|
||||
error = re.compile(".*Error.*")
|
||||
ignore_error = re.compile("Do you want to continue? [y/n] Error: Invalid data input.")
|
||||
|
||||
errmsg = ''
|
||||
errmsg = ""
|
||||
rc = True
|
||||
for line in ipmctl_out.splitlines():
|
||||
if warning.match(line):
|
||||
errmsg = f'{line} (command: {command})'
|
||||
errmsg = f"{line} (command: {command})"
|
||||
rc = False
|
||||
break
|
||||
elif error.match(line):
|
||||
if not ignore_error:
|
||||
errmsg = f'{line} (command: {command})'
|
||||
errmsg = f"{line} (command: {command})"
|
||||
rc = False
|
||||
break
|
||||
|
||||
return rc, errmsg
|
||||
|
||||
def get_allocation_result(self, goal, skt=None):
|
||||
ret = {'appdirect': 0, 'memorymode': 0}
|
||||
ret = {"appdirect": 0, "memorymode": 0}
|
||||
|
||||
if skt:
|
||||
ret['socket'] = skt['id']
|
||||
ret["socket"] = skt["id"]
|
||||
|
||||
out = xmltodict.parse(goal, dict_constructor=dict)['ConfigGoalList']['ConfigGoal']
|
||||
out = xmltodict.parse(goal, dict_constructor=dict)["ConfigGoalList"]["ConfigGoal"]
|
||||
for entry in out:
|
||||
|
||||
# Probably it is a bug of ipmctl to show the socket goal
|
||||
# which isn't specified by the -socket option.
|
||||
# Anyway, filter the noise out here:
|
||||
if skt and skt['id'] != int(entry['SocketID'], 16):
|
||||
if skt and skt["id"] != int(entry["SocketID"], 16):
|
||||
continue
|
||||
|
||||
for key, v in entry.items():
|
||||
if key == 'MemorySize':
|
||||
ret['memorymode'] += int(v.split()[0])
|
||||
elif key == 'AppDirect1Size' or key == 'AapDirect2Size':
|
||||
ret['appdirect'] += int(v.split()[0])
|
||||
if key == "MemorySize":
|
||||
ret["memorymode"] += int(v.split()[0])
|
||||
elif key == "AppDirect1Size" or key == "AapDirect2Size":
|
||||
ret["appdirect"] += int(v.split()[0])
|
||||
|
||||
capacity = self.pmem_get_capacity(skt)
|
||||
ret['reserved'] = capacity - ret['appdirect'] - ret['memorymode']
|
||||
ret["reserved"] = capacity - ret["appdirect"] - ret["memorymode"]
|
||||
|
||||
return ret
|
||||
|
||||
|
|
@ -557,26 +548,26 @@ class PersistentMemory:
|
|||
ipmctl_opts = build_ipmctl_creation_opts(self, skt)
|
||||
|
||||
# First, do dry run ipmctl create command to check the error and warning.
|
||||
command = ['create', '-goal'] + ipmctl_opts
|
||||
command = ["create", "-goal"] + ipmctl_opts
|
||||
out = self.pmem_run_ipmctl(command, returnCheck=False)
|
||||
rc, errmsg = is_allocation_good(self, out, command)
|
||||
if rc is False:
|
||||
return reboot_required, {}, errmsg
|
||||
|
||||
# Run actual creation here
|
||||
command = ['create', '-u', 'B', '-o', 'nvmxml', '-force', '-goal'] + ipmctl_opts
|
||||
command = ["create", "-u", "B", "-o", "nvmxml", "-force", "-goal"] + ipmctl_opts
|
||||
goal = self.pmem_run_ipmctl(command)
|
||||
ret = get_allocation_result(self, goal, skt)
|
||||
reboot_required = True
|
||||
|
||||
return reboot_required, ret, ''
|
||||
return reboot_required, ret, ""
|
||||
|
||||
def pmem_config_namespaces(self, namespace):
|
||||
command = ['create-namespace', '-m', namespace['mode']]
|
||||
if namespace['type']:
|
||||
command += ['-t', namespace['type']]
|
||||
if 'size_byte' in namespace:
|
||||
command += ['-s', namespace['size_byte']]
|
||||
command = ["create-namespace", "-m", namespace["mode"]]
|
||||
if namespace["type"]:
|
||||
command += ["-t", namespace["type"]]
|
||||
if "size_byte" in namespace:
|
||||
command += ["-s", namespace["size_byte"]]
|
||||
|
||||
self.pmem_run_ndctl(command)
|
||||
|
||||
|
|
@ -584,7 +575,6 @@ class PersistentMemory:
|
|||
|
||||
|
||||
def main():
|
||||
|
||||
pmem = PersistentMemory()
|
||||
|
||||
pmem.pmem_is_dcpmm_installed()
|
||||
|
|
@ -600,7 +590,7 @@ def main():
|
|||
for ns in pmem.namespace:
|
||||
pmem.pmem_config_namespaces(ns)
|
||||
|
||||
command = ['list', '-N']
|
||||
command = ["list", "-N"]
|
||||
out = pmem.pmem_run_ndctl(command)
|
||||
all_ns = json.loads(out)
|
||||
|
||||
|
|
@ -623,12 +613,8 @@ def main():
|
|||
|
||||
pmem.result.append(skt_ret)
|
||||
|
||||
pmem.module.exit_json(
|
||||
changed=pmem.changed,
|
||||
reboot_required=reboot_required,
|
||||
result=pmem.result
|
||||
)
|
||||
pmem.module.exit_json(changed=pmem.changed, reboot_required=reboot_required, result=pmem.result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue