1
0
Fork 0
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:
Alexei Znamensky 2021-08-29 07:50:09 +12:00 committed by GitHub
parent 4e2d4e3c68
commit 69641d36e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 25 deletions

View 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).

View file

@ -70,9 +70,7 @@ RETURN = '''
'''
import os
import glob
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_bytes, to_native
module = None
init_script = None
@ -81,15 +79,12 @@ init_script = None
# ===============================
# Check if service is enabled
def is_enabled():
(rc, out, err) = module.run_command("%s enabled" % init_script)
if rc == 0:
return True
return False
rc, dummy, dummy = module.run_command([init_script, 'enabled'])
return rc == 0
# ===========================================
# Main control flow
def main():
global module, init_script
# init
@ -98,22 +93,19 @@ def main():
name=dict(required=True, type='str', aliases=['service']),
state=dict(type='str', choices=['started', 'stopped', 'restarted', 'reloaded']),
enabled=dict(type='bool'),
pattern=dict(type='str', required=False, default=None),
pattern=dict(type='str'),
),
supports_check_mode=True,
required_one_of=[['state', 'enabled']],
required_one_of=[('state', 'enabled')],
)
# initialize
service = module.params['name']
init_script = '/etc/init.d/' + service
rc = 0
out = err = ''
result = {
'name': service,
'changed': False,
}
# check if service exists
if not os.path.exists(init_script):
module.fail_json(msg='service %s does not exist' % service)
@ -129,13 +121,10 @@ def main():
# Change enable/disable if needed
if enabled != module.params['enabled']:
result['changed'] = True
if module.params['enabled']:
action = 'enable'
else:
action = 'disable'
action = 'enable' if module.params['enabled'] else 'disable'
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'
# 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
@ -153,17 +142,13 @@ def main():
psbin = module.get_bin_path('ps', True)
# 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 == 0:
lines = psout.split("\n")
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
running = any((module.params['pattern'] in line and "pattern=" not in line) for line in lines)
else:
(rc, out, err) = module.run_command("%s running" % init_script)
rc, dummy, dummy = module.run_command([init_script, 'running'])
if rc == 0:
running = True
@ -187,7 +172,7 @@ def main():
if action:
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:
module.fail_json(msg="Unable to %s service %s: %s" % (action, service, err))