pacemaker_cluster: fix run_command() calls (#9471)
* pacemaker_cluster: fix run_command() calls * add changelog frag * remove set_node()pull/9486/head
parent
17d36da150
commit
4a1a8d6b38
|
@ -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).
|
|
@ -71,7 +71,7 @@ _PCS_CLUSTER_DOWN = "Error: cluster is not currently running on this node"
|
|||
|
||||
|
||||
def get_cluster_status(module):
|
||||
cmd = "pcs cluster status"
|
||||
cmd = ["pcs", "cluster", "status"]
|
||||
rc, out, err = module.run_command(cmd)
|
||||
if out in _PCS_CLUSTER_DOWN:
|
||||
return 'offline'
|
||||
|
@ -80,10 +80,8 @@ def get_cluster_status(module):
|
|||
|
||||
|
||||
def get_node_status(module, node='all'):
|
||||
if node == 'all':
|
||||
cmd = "pcs cluster pcsd-status %s" % node
|
||||
else:
|
||||
cmd = "pcs cluster pcsd-status"
|
||||
node_l = ["all"] if node == "all" else []
|
||||
cmd = ["pcs", "cluster", "pcsd-status"] + node_l
|
||||
rc, out, err = module.run_command(cmd)
|
||||
if rc == 1:
|
||||
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):
|
||||
cmd = "pcs resource cleanup"
|
||||
cmd = ["pcs", "resource", "cleanup"]
|
||||
rc, out, err = module.run_command(cmd)
|
||||
if rc == 1:
|
||||
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):
|
||||
if state == 'online':
|
||||
cmd = "pcs cluster start"
|
||||
cmd = ["pcs", "cluster", "start"]
|
||||
if state == 'offline':
|
||||
cmd = "pcs cluster stop"
|
||||
cmd = ["pcs", "cluster", "stop"]
|
||||
if force:
|
||||
cmd = "%s --force" % cmd
|
||||
cmd = cmd + ["--force"]
|
||||
rc, out, err = module.run_command(cmd)
|
||||
if rc == 1:
|
||||
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))
|
||||
|
||||
|
||||
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():
|
||||
argument_spec = dict(
|
||||
state=dict(type='str', choices=['online', 'offline', 'restart', 'cleanup']),
|
||||
|
@ -198,7 +167,7 @@ def main():
|
|||
cluster_state = get_node_status(module, node)
|
||||
module.exit_json(changed=True, out=cluster_state)
|
||||
|
||||
if state in ['restart']:
|
||||
elif state == 'restart':
|
||||
if module.check_mode:
|
||||
module.exit_json(changed=True)
|
||||
set_cluster(module, 'offline', timeout, force)
|
||||
|
@ -209,17 +178,16 @@ def main():
|
|||
if cluster_state == 'online':
|
||||
module.exit_json(changed=True, out=cluster_state)
|
||||
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:
|
||||
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:
|
||||
module.exit_json(changed=True)
|
||||
clean_cluster(module, timeout)
|
||||
cluster_state = get_cluster_status(module)
|
||||
module.exit_json(changed=True,
|
||||
out=cluster_state)
|
||||
module.exit_json(changed=True, out=cluster_state)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue