pacemaker_cluster: fix run_command() calls (#9471)

* pacemaker_cluster: fix run_command() calls

* add changelog frag

* remove set_node()
pull/9486/head
Alexei Znamensky 2024-12-30 23:49:29 +13:00 committed by GitHub
parent 17d36da150
commit 4a1a8d6b38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 44 deletions

View File

@ -0,0 +1,3 @@
minor_changes:
- pacemaker_cluster - using safer mechanism to run external command (https://github.com/ansible-collections/community.general/pull/9471).
- pacemaker_cluster - remove unused code (https://github.com/ansible-collections/community.general/pull/9471).

View File

@ -71,7 +71,7 @@ _PCS_CLUSTER_DOWN = "Error: cluster is not currently running on this node"
def get_cluster_status(module): def get_cluster_status(module):
cmd = "pcs cluster status" cmd = ["pcs", "cluster", "status"]
rc, out, err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if out in _PCS_CLUSTER_DOWN: if out in _PCS_CLUSTER_DOWN:
return 'offline' return 'offline'
@ -80,10 +80,8 @@ def get_cluster_status(module):
def get_node_status(module, node='all'): def get_node_status(module, node='all'):
if node == 'all': node_l = ["all"] if node == "all" else []
cmd = "pcs cluster pcsd-status %s" % node cmd = ["pcs", "cluster", "pcsd-status"] + node_l
else:
cmd = "pcs cluster pcsd-status"
rc, out, err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if rc == 1: if rc == 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err)) module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
@ -94,7 +92,7 @@ def get_node_status(module, node='all'):
def clean_cluster(module, timeout): def clean_cluster(module, timeout):
cmd = "pcs resource cleanup" cmd = ["pcs", "resource", "cleanup"]
rc, out, err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if rc == 1: if rc == 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err)) module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
@ -102,11 +100,11 @@ def clean_cluster(module, timeout):
def set_cluster(module, state, timeout, force): def set_cluster(module, state, timeout, force):
if state == 'online': if state == 'online':
cmd = "pcs cluster start" cmd = ["pcs", "cluster", "start"]
if state == 'offline': if state == 'offline':
cmd = "pcs cluster stop" cmd = ["pcs", "cluster", "stop"]
if force: if force:
cmd = "%s --force" % cmd cmd = cmd + ["--force"]
rc, out, err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if rc == 1: if rc == 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err)) module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
@ -122,35 +120,6 @@ def set_cluster(module, state, timeout, force):
module.fail_json(msg="Failed to set the state `%s` on the cluster\n" % (state)) module.fail_json(msg="Failed to set the state `%s` on the cluster\n" % (state))
def set_node(module, state, timeout, force, node='all'):
# map states
if state == 'online':
cmd = "pcs cluster start"
if state == 'offline':
cmd = "pcs cluster stop"
if force:
cmd = "%s --force" % cmd
nodes_state = get_node_status(module, node)
for node in nodes_state:
if node[1].strip().lower() != state:
cmd = "%s %s" % (cmd, node[0].strip())
rc, out, err = module.run_command(cmd)
if rc == 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
t = time.time()
ready = False
while time.time() < t + timeout:
nodes_state = get_node_status(module)
for node in nodes_state:
if node[1].strip().lower() == state:
ready = True
break
if not ready:
module.fail_json(msg="Failed to set the state `%s` on the cluster\n" % (state))
def main(): def main():
argument_spec = dict( argument_spec = dict(
state=dict(type='str', choices=['online', 'offline', 'restart', 'cleanup']), state=dict(type='str', choices=['online', 'offline', 'restart', 'cleanup']),
@ -198,7 +167,7 @@ def main():
cluster_state = get_node_status(module, node) cluster_state = get_node_status(module, node)
module.exit_json(changed=True, out=cluster_state) module.exit_json(changed=True, out=cluster_state)
if state in ['restart']: elif state == 'restart':
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) module.exit_json(changed=True)
set_cluster(module, 'offline', timeout, force) set_cluster(module, 'offline', timeout, force)
@ -209,17 +178,16 @@ def main():
if cluster_state == 'online': if cluster_state == 'online':
module.exit_json(changed=True, out=cluster_state) module.exit_json(changed=True, out=cluster_state)
else: else:
module.fail_json(msg="Failed during the restart of the cluster, the cluster can't be started") module.fail_json(msg="Failed during the restart of the cluster, the cluster cannot be started")
else: else:
module.fail_json(msg="Failed during the restart of the cluster, the cluster can't be stopped") module.fail_json(msg="Failed during the restart of the cluster, the cluster cannot be stopped")
if state in ['cleanup']: elif state == 'cleanup':
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) module.exit_json(changed=True)
clean_cluster(module, timeout) clean_cluster(module, timeout)
cluster_state = get_cluster_status(module) cluster_state = get_cluster_status(module)
module.exit_json(changed=True, module.exit_json(changed=True, out=cluster_state)
out=cluster_state)
if __name__ == '__main__': if __name__ == '__main__':