mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
openwrt_init - improvements (#3284)
* improvements on openwrt_init * added changelog fragment
This commit is contained in:
parent
4e2d4e3c68
commit
69641d36e1
2 changed files with 14 additions and 25 deletions
4
changelogs/fragments/3284-openwrt_init-improvements.yaml
Normal file
4
changelogs/fragments/3284-openwrt_init-improvements.yaml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
minor_changes:
|
||||||
|
- openwrt_init - minor refactoring (https://github.com/ansible-collections/community.general/pull/3284).
|
||||||
|
bugfixes:
|
||||||
|
- openwrt_init - calling ``run_command`` with arguments as ``list`` instead of ``str`` (https://github.com/ansible-collections/community.general/pull/3284).
|
|
@ -70,9 +70,7 @@ RETURN = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import glob
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.common.text.converters import to_bytes, to_native
|
|
||||||
|
|
||||||
module = None
|
module = None
|
||||||
init_script = None
|
init_script = None
|
||||||
|
@ -81,15 +79,12 @@ init_script = None
|
||||||
# ===============================
|
# ===============================
|
||||||
# Check if service is enabled
|
# Check if service is enabled
|
||||||
def is_enabled():
|
def is_enabled():
|
||||||
(rc, out, err) = module.run_command("%s enabled" % init_script)
|
rc, dummy, dummy = module.run_command([init_script, 'enabled'])
|
||||||
if rc == 0:
|
return rc == 0
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# Main control flow
|
# Main control flow
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global module, init_script
|
global module, init_script
|
||||||
# init
|
# init
|
||||||
|
@ -98,22 +93,19 @@ def main():
|
||||||
name=dict(required=True, type='str', aliases=['service']),
|
name=dict(required=True, type='str', aliases=['service']),
|
||||||
state=dict(type='str', choices=['started', 'stopped', 'restarted', 'reloaded']),
|
state=dict(type='str', choices=['started', 'stopped', 'restarted', 'reloaded']),
|
||||||
enabled=dict(type='bool'),
|
enabled=dict(type='bool'),
|
||||||
pattern=dict(type='str', required=False, default=None),
|
pattern=dict(type='str'),
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
required_one_of=[['state', 'enabled']],
|
required_one_of=[('state', 'enabled')],
|
||||||
)
|
)
|
||||||
|
|
||||||
# initialize
|
# initialize
|
||||||
service = module.params['name']
|
service = module.params['name']
|
||||||
init_script = '/etc/init.d/' + service
|
init_script = '/etc/init.d/' + service
|
||||||
rc = 0
|
|
||||||
out = err = ''
|
|
||||||
result = {
|
result = {
|
||||||
'name': service,
|
'name': service,
|
||||||
'changed': False,
|
'changed': False,
|
||||||
}
|
}
|
||||||
|
|
||||||
# check if service exists
|
# check if service exists
|
||||||
if not os.path.exists(init_script):
|
if not os.path.exists(init_script):
|
||||||
module.fail_json(msg='service %s does not exist' % service)
|
module.fail_json(msg='service %s does not exist' % service)
|
||||||
|
@ -129,13 +121,10 @@ def main():
|
||||||
# Change enable/disable if needed
|
# Change enable/disable if needed
|
||||||
if enabled != module.params['enabled']:
|
if enabled != module.params['enabled']:
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
if module.params['enabled']:
|
action = 'enable' if module.params['enabled'] else 'disable'
|
||||||
action = 'enable'
|
|
||||||
else:
|
|
||||||
action = 'disable'
|
|
||||||
|
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
(rc, out, err) = module.run_command("%s %s" % (init_script, action))
|
rc, dummy, err = module.run_command([init_script, action])
|
||||||
# openwrt init scripts can return a non-zero exit code on a successful 'enable'
|
# openwrt init scripts can return a non-zero exit code on a successful 'enable'
|
||||||
# command if the init script doesn't contain a STOP value, so we ignore the exit
|
# command if the init script doesn't contain a STOP value, so we ignore the exit
|
||||||
# code and explicitly check if the service is now in the desired state
|
# code and explicitly check if the service is now in the desired state
|
||||||
|
@ -153,17 +142,13 @@ def main():
|
||||||
psbin = module.get_bin_path('ps', True)
|
psbin = module.get_bin_path('ps', True)
|
||||||
|
|
||||||
# this should be busybox ps, so we only want/need to the 'w' option
|
# this should be busybox ps, so we only want/need to the 'w' option
|
||||||
(rc, psout, pserr) = module.run_command('%s w' % psbin)
|
rc, psout, dummy = module.run_command([psbin, 'w'])
|
||||||
# If rc is 0, set running as appropriate
|
# If rc is 0, set running as appropriate
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
lines = psout.split("\n")
|
lines = psout.split("\n")
|
||||||
for line in lines:
|
running = any((module.params['pattern'] in line and "pattern=" not in line) for line in lines)
|
||||||
if module.params['pattern'] in line and "pattern=" not in line:
|
|
||||||
# so as to not confuse ./hacking/test-module.py
|
|
||||||
running = True
|
|
||||||
break
|
|
||||||
else:
|
else:
|
||||||
(rc, out, err) = module.run_command("%s running" % init_script)
|
rc, dummy, dummy = module.run_command([init_script, 'running'])
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
|
@ -187,7 +172,7 @@ def main():
|
||||||
|
|
||||||
if action:
|
if action:
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
(rc, out, err) = module.run_command("%s %s" % (init_script, action))
|
rc, dummy, err = module.run_command([init_script, action])
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(msg="Unable to %s service %s: %s" % (action, service, err))
|
module.fail_json(msg="Unable to %s service %s: %s" % (action, service, err))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue