Skip to content

Commit

Permalink
ceph_osd_flag: support setting noout flag at osd or bucket level
Browse files Browse the repository at this point in the history
Currently `ceph_osd_flag` module only supports setting `noout` flag at
the cluster level.
Ceph supports setting this flag at the osd or the bucket level so let's
implement this support in `ceph_osd_flag` module.

Signed-off-by: Guillaume Abrioux <gabrioux@redhat.com>
  • Loading branch information
guits committed Dec 16, 2020
1 parent 827b233 commit 3cbddd0
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
58 changes: 55 additions & 3 deletions library/ceph_osd_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@
- name of the ceph OSD flag.
required: true
choices: ['noup', 'nodown', 'noout', 'nobackfill', 'norebalance', 'norecover', 'noscrub', 'nodeep-scrub']
level:
description:
- This is applicable only when 'name' is 'noout'.
This flag can be applied at several levels:
1/ at the whole cluster level
2/ at the bucket level
3/ at the osd.X level
required: false
choices: ['osd', 'bucket', 'cluster']
default: 'cluster'
osd:
description:
- pass the osd when 'level' is 'osd'
required: false
bucket:
description:
- pass the bucket name when 'level' is 'bucket'
required: false
cluster:
description:
- The ceph cluster name.
Expand Down Expand Up @@ -70,6 +88,19 @@
loop:
- 'noup'
- 'norebalance'
- name: set noup flag on osd.123
ceph_osd_flag:
name: noup
level: osd
osd: osd.123
- name: unset noup flag on bucket 'host-456'
ceph_osd_flag:
state: absent
name: noup
level: bucket
bucket: host-456
'''

RETURN = '''# '''
Expand All @@ -79,24 +110,45 @@ def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True, choices=['noup', 'nodown', 'noout', 'nobackfill', 'norebalance', 'norecover', 'noscrub', 'nodeep-scrub']),
level=dict(type='str', required=False, default='cluster', choices=['cluster', 'bucket', 'osd']),
osd=dict(type='str', required=False),
bucket=dict(type='str', required=False),
cluster=dict(type='str', required=False, default='ceph'),
state=dict(type='str', required=False, default='present', choices=['present', 'absent']),
),
supports_check_mode=True,
)

required_if=[
['level', 'osd', ['osd']],
['level', 'bucket', ['bucket']]
]

name = module.params.get('name')
level = module.params.get('level')
osd = module.params.get('osd')
bucket = module.params.get('bucket')
cluster = module.params.get('cluster')
state = module.params.get('state')

startd = datetime.datetime.now()

container_image = is_containerized()

if state == 'present':
cmd = generate_ceph_cmd(['osd', 'set'], [name], cluster=cluster, container_image=container_image)
osd_sub_cmd = ['osd']
if name == 'noout' and level in ['osd', 'bucket']:
if level == 'osd':
action = 'add-noout' if state == 'present' else 'rm-noout'
name = osd
if level == 'bucket':
action = 'set-group' if state == 'present' else 'unset-group'
name = bucket
osd_sub_cmd.extend([action])

else:
cmd = generate_ceph_cmd(['osd', 'unset'], [name], cluster=cluster, container_image=container_image)
osd_sub_cmd.extend(['set']) if state == 'present' else osd_sub_cmd.extend(['unset'])

cmd = generate_ceph_cmd(osd_sub_cmd, [name], cluster=cluster, container_image=container_image)

if module.check_mode:
exit_module(
Expand Down
76 changes: 76 additions & 0 deletions tests/library/test_ceph_osd_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,79 @@ def test_with_container(self, m_run_command, m_exit_json):
assert result['rc'] == rc
assert result['stderr'] == stderr
assert result['stdout'] == stdout

@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
@pytest.mark.parametrize('state', ['present', 'absent'])
def test_flag_noout_osd_level(self, m_run_command, m_exit_json, state):
ca_test_common.set_module_args({
'name': 'noout',
'state': state,
'level': 'osd',
'osd': 'osd.123'
})
m_exit_json.side_effect = ca_test_common.exit_json
stdout = ''
stderr = ''
rc = 0
m_run_command.return_value = rc, stdout, stderr

with pytest.raises(ca_test_common.AnsibleExitJson) as result:
ceph_osd_flag.main()

result = result.value.args[0]

sub_cmd = 'add-noout' if state == 'present' else 'rm-noout'

assert result['cmd'] == ['ceph', '-n', 'client.admin', '-k', '/etc/ceph/ceph.client.admin.keyring', '--cluster', 'ceph', 'osd', 'add-noout' if state == 'present' else 'rm-noout', 'osd.123']
assert result['rc'] == rc
assert result['stderr'] == ''
assert result['stdout'] == ''

@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
@pytest.mark.parametrize('state', ['present', 'absent'])
def test_flag_noout_bucket_level(self, m_run_command, m_exit_json, state):
ca_test_common.set_module_args({
'name': 'noout',
'state': state,
'level': 'bucket',
'bucket': 'my_osd_host_123'
})
m_exit_json.side_effect = ca_test_common.exit_json
stdout = ''
stderr = ''
rc = 0
m_run_command.return_value = rc, stdout, stderr

with pytest.raises(ca_test_common.AnsibleExitJson) as result:
ceph_osd_flag.main()

result = result.value.args[0]
assert result['cmd'] == ['ceph', '-n', 'client.admin', '-k', '/etc/ceph/ceph.client.admin.keyring', '--cluster', 'ceph', 'osd', 'set-group' if state == 'present' else 'unset-group', 'my_osd_host_123']
assert result['rc'] == rc
assert result['stderr'] == ''
assert result['stdout'] == ''

@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
@pytest.mark.parametrize('state', ['present', 'absent'])
def test_flag_noout_cluster_level(self, m_run_command, m_exit_json, state):
ca_test_common.set_module_args({
'name': 'noout',
'state': state
})
m_exit_json.side_effect = ca_test_common.exit_json
stdout = ''
stderr = ''
rc = 0
m_run_command.return_value = rc, stdout, stderr

with pytest.raises(ca_test_common.AnsibleExitJson) as result:
ceph_osd_flag.main()

result = result.value.args[0]
assert result['cmd'] == ['ceph', '-n', 'client.admin', '-k', '/etc/ceph/ceph.client.admin.keyring', '--cluster', 'ceph', 'osd', 'set' if state == 'present' else 'unset', 'noout']
assert result['rc'] == rc
assert result['stderr'] == ''
assert result['stdout'] == ''

0 comments on commit 3cbddd0

Please sign in to comment.