Skip to content

Commit

Permalink
Fix bugs and add better rety handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KAMI911 committed Dec 5, 2023
1 parent d984096 commit 8c2660b
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions library/jbosscli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
#
# (c) 2017, Antonio Insusti <[email protected]>
#
Expand Down Expand Up @@ -45,74 +45,81 @@
user:
required: false
description:
- Jboss management user
- JBoss management user
password:
required: false
description:
- Jboss management user password
- JBoss management user password
server:
required: false
default: localhost:9990
description:
- JBoss server or domain controller, with management port
timeout:
required: false
default: 20
description:
- Try process jcli script on JBoss server with this timeout
retry:
required: false
default: 20
description:
- Try process jcli script on JBoss server by retry count every timeout interval
verbose:
required: false
default: false
description:
- Show the JBoss Cli output, commonly in DMR
notes:
- "jboss-cli.sh need to be running on client host, and $JAVA_HOME/bin is needed in Client $PATH"
- "jboss-cli.sh need to be running on client host, and $JAVA_HOME/bin is needed in client $PATH"
- ""
"""

EXAMPLES = """
# chage scan-interval value, on user wildfly installation
- jboss:
# Change scan-interval value, on user wildfly installation
- jbosscli:
command: /subsystem=deployment-scanner/scanner=default:write-attribute(name=scan-interval,value=6000)
cli_path: /home/user/wildfly-10.1.0.Final/bin
# change ExampleDS datasource user-name, on 192.168.20.55:9990 default installation
- jboss:
# Change ExampleDS datasource user-name, on 192.168.20.55:9990 default installation
- jbosscli:
command: /subsystem=datasources/data-source=ExampleDS:write-attribute(name=user-name,value=other)
server: 192.168.20.55:9990
# Undeploy the hello world application on wildfly server
- jboss:
- jbosscli:
command: undeploy hello.war
server: "{{ ansible_hostname}}:9990"
"""

RETURN = '''
---
changed:
description: if jboss cli command change something
description: if JBoss Cli command change something
returned: true
type: string
sample: 'changed: true'
command:
description: Jboss Cli command
description: JBoss Cli command
returned: JBoss Cli command
type: string
sample: 'command: "/subsystem=deployment-scanner/scanner=default:write-attribute(name=scan-interval,value=1000)"'
stdout:
description: Jboss Cli command output
description: JBoss Cli command output
returned: If verbose JBoss Cli command output, else success or failed
type: string
sample: '{"outcome" => "success"}'
Error:
description: Jboss Cli command error
description: JBoss Cli command error
returned: return error if falied
type: string
sample: '{"outcome" => "success"}'
'''

import os
import shutil
import time
import re
import grp
import platform
import time
from ansible.module_utils.basic import AnsibleModule


Expand All @@ -126,7 +133,10 @@ def main():
cli_path=dict(default='/usr/share/wildfly/bin'),
server=dict(default='localhost:9990'),
verbose=dict(default="False"),
timeout=dict(default=20),
retry=dict(default=20),
),
mutually_exclusive=[['command', 'src']],
)

changed = False
Expand All @@ -138,20 +148,19 @@ def main():
cli_path = module.params['cli_path']
server = module.params['server']
verbose = module.params['verbose']
jsout = None

if command and src:
module.fail_json(msg="Argument 'src' and 'command' are mutually exclusive")

timeout = module.params['timeout']
retry = module.params['retry']
current_retry = int(retry)
if user and not password:
module.fail_json(msg="Argument 'user' need 'password' ")
module.fail_json(msg="Argument 'user' needs 'password' ")

if not os.access(cli_path + "/jboss-cli.sh", os.X_OK):
module.fail_json(msg="jboss-cli.sh in not found on cli_path ")

cmd = [cli_path + "/jboss-cli.sh"]
cmd.append('-c')
cmd.append("--controller=" + str(server))
cmd.append('--echo-command')
cmd.append('--controller=' + str(server))

if user:
cmd.append('--user=' + str(user))
Expand All @@ -160,15 +169,23 @@ def main():
if src:
cmd.append('--file=' + str(src))
else:
cmd.append('--commands={}'.format(str(command)))
cmd.append('%s' % str(command))

rc = None
out = ''
err = ''
result = {}
result['command'] = command

(rc, out, err) = module.run_command(cmd, encoding='utf-8')
current_retry = int(retry)
while current_retry > 0:
(rc, out, err) = module.run_command(cmd, encoding='utf-8')
print (f'{rc}, {out}, {err}')
if rc != 0:
current_retry -= 1
time.sleep(timeout)
else:
current_retry = 0

if rc is None:
result['changed'] = False
Expand All @@ -184,16 +201,16 @@ def main():

if out and not src:
if not out.find("outcome") < 0:
if out.find('success'):
if out.find('success') != -1:
result['changed'] = True
if verbose == "True":
if verbose:
result['stdout'] = out
else:
result['stdout'] = 'success'
else:
result['changed'] = False
result['Error'] = re.findall("failure-description.+", out)
if verbose == "True":
if verbose:
result['stdout'] = out
else:
result['stdout'] = re.findall("outcome.+", out)
Expand All @@ -212,7 +229,6 @@ def main():
module.fail_json(name='jbosscli', msg=out)
if err:
result['Error'] = err

module.exit_json(**result)

if __name__ == '__main__':
Expand Down

0 comments on commit 8c2660b

Please sign in to comment.