Skip to content

Commit

Permalink
ceph_ec_profile: add support more plugins
Browse files Browse the repository at this point in the history
Add support more plugins with their respective args.

Signed-off-by: Seena Fallah <[email protected]>
  • Loading branch information
clwluvw authored and guits committed Mar 16, 2024
1 parent 7b9a459 commit 80666a2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
99 changes: 53 additions & 46 deletions library/ceph_ec_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
RETURN = '''# '''


def get_profile(module, name, cluster='ceph', container_image=None):
def get_profile(name, cluster='ceph', container_image=None):
'''
Get existing profile
'''
Expand All @@ -116,16 +116,14 @@ def get_profile(module, name, cluster='ceph', container_image=None):
return cmd


def create_profile(module, name, k, m, stripe_unit, crush_device_class, cluster='ceph', force=False, container_image=None): # noqa: E501
def create_profile(name, user_profile, force, cluster='ceph', container_image=None): # noqa: E501
'''
Create a profile
'''

args = ['set', name, 'k={}'.format(k), 'm={}'.format(m)]
if stripe_unit:
args.append('stripe_unit={}'.format(stripe_unit))
if crush_device_class:
args.append('crush-device-class={}'.format(crush_device_class))
args = ['set', name]
for key, value in user_profile.items():
args.append('{}={}'.format(key, value))
if force:
args.append('--force')

Expand All @@ -137,7 +135,7 @@ def create_profile(module, name, k, m, stripe_unit, crush_device_class, cluster=
return cmd


def delete_profile(module, name, cluster='ceph', container_image=None):
def delete_profile(name, cluster='ceph', container_image=None):
'''
Delete a profile
'''
Expand All @@ -152,16 +150,41 @@ def delete_profile(module, name, cluster='ceph', container_image=None):
return cmd


def parse_user_profile(module):
profile_keys = ['plugin',
'k', 'm', 'd', 'l', 'c',
'stripe_unit', 'scalar_mds', 'technique',
'crush-root', 'crush-device-class', 'crush-failure-domain']

profile = {}
for key in profile_keys:
ansible_lookup_key = key.replace('-', '_')
value = module.params.get(ansible_lookup_key)
if value:
profile[key] = value

return profile


def run_module():
module_args = dict(
cluster=dict(type='str', required=False, default='ceph'),
name=dict(type='str', required=True),
state=dict(type='str', required=False,
choices=['present', 'absent'], default='present'),
stripe_unit=dict(type='str', required=False),
k=dict(type='str', required=False),
m=dict(type='str', required=False),
crush_device_class=dict(type='str', required=False, default=''),
plugin=dict(type='str', required=False, default='jerasure'),
k=dict(type='int', required=False),
m=dict(type='int', required=False),
d=dict(type='int', required=False),
l=dict(type='int', required=False),
c=dict(type='int', required=False),
scalar_mds=dict(type='str', required=False),
technique=dict(type='str', required=False),
crush_root=dict(type='str', required=False),
crush_failure_domain=dict(type='str', required=False),
crush_device_class=dict(type='str', required=False),
force=dict(type='bool', required=False, default=False),
)

module = AnsibleModule(
Expand All @@ -174,10 +197,8 @@ def run_module():
name = module.params.get('name')
cluster = module.params.get('cluster')
state = module.params.get('state')
stripe_unit = module.params.get('stripe_unit')
k = module.params.get('k')
m = module.params.get('m')
crush_device_class = module.params.get('crush_device_class')
force = module.params.get('force')
user_profile = parse_user_profile(module)

if module.check_mode:
module.exit_json(
Expand All @@ -191,54 +212,40 @@ def run_module():
)

startd = datetime.datetime.now()
diff = dict(before="", after="")
changed = False

# will return either the image name or None
container_image = is_containerized()

if state == "present":
rc, cmd, out, err = exec_command(module, get_profile(module, name, cluster, container_image=container_image)) # noqa: E501
rc, cmd, out, err = exec_command(module, get_profile(name, cluster, container_image=container_image)) # noqa: E501
current_profile = {}
if rc == 0:
# the profile already exists, let's check whether we have to
# update it
current_profile = json.loads(out)
if current_profile['k'] != k or \
current_profile['m'] != m or \
current_profile.get('stripe_unit', stripe_unit) != stripe_unit or \
current_profile.get('crush-device-class', crush_device_class) != crush_device_class: # noqa: E501
rc, cmd, out, err = exec_command(module,
create_profile(module,
name,
k,
m,
stripe_unit,
crush_device_class, # noqa: E501
cluster,
force=True, container_image=container_image)) # noqa: E501
changed = True
else:
# the profile doesn't exist, it has to be created
rc, cmd, out, err = exec_command(module, create_profile(module,
name,
k,
m,
stripe_unit, # noqa: E501
crush_device_class, # noqa: E501
cluster,
container_image=container_image)) # noqa: E501
if rc == 0:
changed = True

changed = current_profile != user_profile
if changed:
diff['before'] = json.dumps(current_profile)
diff['after'] = json.dumps(user_profile)
rc, cmd, out, err = exec_command(module,
create_profile(name,
user_profile,
force,
cluster,
container_image=container_image), # noqa: E501
check_rc=True)

elif state == "absent":
rc, cmd, out, err = exec_command(module, delete_profile(module, name, cluster, container_image=container_image)) # noqa: E501
rc, cmd, out, err = exec_command(module, delete_profile(name, cluster, container_image=container_image)) # noqa: E501
if not err:
out = 'Profile {} removed.'.format(name)
changed = True
else:
rc = 0
out = "Skipping, the profile {} doesn't exist".format(name)

exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501
exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed, diff=diff) # noqa: E501


def main():
Expand Down
4 changes: 2 additions & 2 deletions module_utils/ca_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ def pre_generate_cmd(cmd, container_image=None, interactive=False):
return cmd


def exec_command(module, cmd, stdin=None):
def exec_command(module, cmd, stdin=None, check_rc=False):
'''
Execute command(s)
'''

binary_data = False
if stdin:
binary_data = True
rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data)
rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501

return rc, cmd, out, err

Expand Down

0 comments on commit 80666a2

Please sign in to comment.