mirror of https://github.com/ceph/ceph-ansible.git
library: make cephadm_adopt module idempotent
Running the cephadm_adopt module on an already adopted daemon will
fail because the cephadm adopt command isn't idempotent.
Closes: https://bugzilla.redhat.com/show_bug.cgi?id=1918424
Signed-off-by: Dimitri Savineau <dsavinea@redhat.com>
(cherry picked from commit ff9d314305
)
pull/6955/head
parent
864acaae10
commit
4ab40842df
|
@ -17,6 +17,7 @@ __metaclass__ = type
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
import datetime
|
||||
import json
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {
|
||||
|
@ -135,6 +136,35 @@ def main():
|
|||
|
||||
startd = datetime.datetime.now()
|
||||
|
||||
cmd = ['cephadm', 'ls', '--no-detail']
|
||||
|
||||
if module.check_mode:
|
||||
exit_module(
|
||||
module=module,
|
||||
out='',
|
||||
rc=0,
|
||||
cmd=cmd,
|
||||
err='',
|
||||
startd=startd,
|
||||
changed=False
|
||||
)
|
||||
else:
|
||||
rc, out, err = module.run_command(cmd)
|
||||
|
||||
if rc == 0:
|
||||
if name in [x["name"] for x in json.loads(out) if x["style"] == "cephadm:v1"]:
|
||||
exit_module(
|
||||
module=module,
|
||||
out='{} is already adopted'.format(name),
|
||||
rc=0,
|
||||
cmd=cmd,
|
||||
err='',
|
||||
startd=startd,
|
||||
changed=False
|
||||
)
|
||||
else:
|
||||
module.fail_json(msg=err, rc=rc)
|
||||
|
||||
cmd = ['cephadm']
|
||||
|
||||
if docker:
|
||||
|
@ -151,27 +181,16 @@ def main():
|
|||
if not firewalld:
|
||||
cmd.append('--skip-firewalld')
|
||||
|
||||
if module.check_mode:
|
||||
exit_module(
|
||||
module=module,
|
||||
out='',
|
||||
rc=0,
|
||||
cmd=cmd,
|
||||
err='',
|
||||
startd=startd,
|
||||
changed=False
|
||||
)
|
||||
else:
|
||||
rc, out, err = module.run_command(cmd)
|
||||
exit_module(
|
||||
module=module,
|
||||
out=out,
|
||||
rc=rc,
|
||||
cmd=cmd,
|
||||
err=err,
|
||||
startd=startd,
|
||||
changed=True
|
||||
)
|
||||
rc, out, err = module.run_command(cmd)
|
||||
exit_module(
|
||||
module=module,
|
||||
out=out,
|
||||
rc=rc,
|
||||
cmd=cmd,
|
||||
err=err,
|
||||
startd=startd,
|
||||
changed=True
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -57,12 +57,12 @@ class TestCephadmAdoptModule(object):
|
|||
|
||||
result = result.value.args[0]
|
||||
assert not result['changed']
|
||||
assert result['cmd'] == ['cephadm', 'adopt', '--cluster', fake_cluster, '--name', fake_name, '--style', 'legacy']
|
||||
assert result['cmd'] == ['cephadm', 'ls', '--no-detail']
|
||||
assert result['rc'] == 0
|
||||
assert not result['stdout']
|
||||
assert not result['stderr']
|
||||
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.fail_json')
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
||||
def test_with_failure(self, m_run_command, m_exit_json):
|
||||
set_module_args({
|
||||
|
@ -78,10 +78,8 @@ class TestCephadmAdoptModule(object):
|
|||
cephadm_adopt.main()
|
||||
|
||||
result = result.value.args[0]
|
||||
assert result['changed']
|
||||
assert result['cmd'] == ['cephadm', 'adopt', '--cluster', fake_cluster, '--name', fake_name, '--style', 'legacy']
|
||||
assert result['rc'] == 1
|
||||
assert result['stderr'] == 'ERROR: cephadm should be run as root'
|
||||
assert result['msg'] == 'ERROR: cephadm should be run as root'
|
||||
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
||||
|
@ -99,7 +97,10 @@ class TestCephadmAdoptModule(object):
|
|||
'firewalld ready'.format(fake_name, fake_name)
|
||||
stderr = ''
|
||||
rc = 0
|
||||
m_run_command.return_value = rc, stdout, stderr
|
||||
m_run_command.side_effect = [
|
||||
(0, '[{{"style":"legacy","name":"{}"}}]'.format(fake_name), ''),
|
||||
(rc, stdout, stderr)
|
||||
]
|
||||
|
||||
with pytest.raises(AnsibleExitJson) as result:
|
||||
cephadm_adopt.main()
|
||||
|
@ -111,6 +112,28 @@ class TestCephadmAdoptModule(object):
|
|||
assert result['stderr'] == stderr
|
||||
assert result['stdout'] == stdout
|
||||
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
||||
def test_already_adopted(self, m_run_command, m_exit_json):
|
||||
set_module_args({
|
||||
'name': fake_name
|
||||
})
|
||||
m_exit_json.side_effect = exit_json
|
||||
stderr = ''
|
||||
stdout = '[{{"style":"cephadm:v1","name":"{}"}}]'.format(fake_name)
|
||||
rc = 0
|
||||
m_run_command.return_value = rc, stdout, stderr
|
||||
|
||||
with pytest.raises(AnsibleExitJson) as result:
|
||||
cephadm_adopt.main()
|
||||
|
||||
result = result.value.args[0]
|
||||
assert not result['changed']
|
||||
assert result['cmd'] == ['cephadm', 'ls', '--no-detail']
|
||||
assert result['rc'] == 0
|
||||
assert result['stderr'] == stderr
|
||||
assert result['stdout'] == '{} is already adopted'.format(fake_name)
|
||||
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
||||
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
||||
def test_with_docker(self, m_run_command, m_exit_json):
|
||||
|
@ -122,7 +145,10 @@ class TestCephadmAdoptModule(object):
|
|||
stdout = ''
|
||||
stderr = ''
|
||||
rc = 0
|
||||
m_run_command.return_value = rc, stdout, stderr
|
||||
m_run_command.side_effect = [
|
||||
(0, '[{{"style":"legacy","name":"{}"}}]'.format(fake_name), ''),
|
||||
(rc, stdout, stderr)
|
||||
]
|
||||
|
||||
with pytest.raises(AnsibleExitJson) as result:
|
||||
cephadm_adopt.main()
|
||||
|
@ -143,7 +169,10 @@ class TestCephadmAdoptModule(object):
|
|||
stdout = ''
|
||||
stderr = ''
|
||||
rc = 0
|
||||
m_run_command.return_value = rc, stdout, stderr
|
||||
m_run_command.side_effect = [
|
||||
(0, '[{{"style":"legacy","name":"{}"}}]'.format(fake_name), ''),
|
||||
(rc, stdout, stderr)
|
||||
]
|
||||
|
||||
with pytest.raises(AnsibleExitJson) as result:
|
||||
cephadm_adopt.main()
|
||||
|
@ -164,7 +193,10 @@ class TestCephadmAdoptModule(object):
|
|||
stdout = ''
|
||||
stderr = ''
|
||||
rc = 0
|
||||
m_run_command.return_value = rc, stdout, stderr
|
||||
m_run_command.side_effect = [
|
||||
(0, '[{{"style":"legacy","name":"{}"}}]'.format(fake_name), ''),
|
||||
(rc, stdout, stderr)
|
||||
]
|
||||
|
||||
with pytest.raises(AnsibleExitJson) as result:
|
||||
cephadm_adopt.main()
|
||||
|
@ -185,7 +217,10 @@ class TestCephadmAdoptModule(object):
|
|||
stdout = ''
|
||||
stderr = ''
|
||||
rc = 0
|
||||
m_run_command.return_value = rc, stdout, stderr
|
||||
m_run_command.side_effect = [
|
||||
(0, '[{{"style":"legacy","name":"{}"}}]'.format(fake_name), ''),
|
||||
(rc, stdout, stderr)
|
||||
]
|
||||
|
||||
with pytest.raises(AnsibleExitJson) as result:
|
||||
cephadm_adopt.main()
|
||||
|
|
Loading…
Reference in New Issue